Edmilson Alexandre commited on
Commit
a94e576
·
1 Parent(s): 03e46db

yeah again 4

Browse files
Files changed (3) hide show
  1. API_flask_server.py +20 -15
  2. Dockerfile +4 -4
  3. requirements.txt +8 -0
API_flask_server.py CHANGED
@@ -1,37 +1,42 @@
1
  ## An python API for our Model
2
  ## Design by edander32 (Edmilson Alexandre) and jjambo(Joaquim Jambo)
3
 
4
- from flask import Flask, request, jsonify
 
5
  import pandas as pd
6
  import joblib
7
-
8
- app = Flask(__name__)
9
-
10
  from huggingface_hub import hf_hub_download
11
- import joblib
12
 
 
 
 
 
13
  model_path = hf_hub_download(
14
- repo_id="Edalexan/ia-pkl", # substitui pelo teu nome/ID
15
  filename="exoplanet_model.pkl"
16
  )
17
  model = joblib.load(model_path)
18
 
 
 
 
 
 
 
19
 
20
- @app.route("/")
21
  def home():
22
  return {"message": "API do classificador de exoplanetas online."}
23
 
24
- @app.route("/predict", methods=["POST"])
25
- def predict():
26
  try:
27
- data = request.get_json()
28
- df = pd.DataFrame([data])
29
  pred = model.predict(df)[0]
30
  prob = model.predict_proba(df)[0][1]
31
- return jsonify({"prediction": int(pred), "probability": float(prob)})
32
  except Exception as e:
33
- return jsonify({"error": str(e)}), 400
34
 
35
- if __name__ == "__main__":
36
- app.run(host="0.0.0.0", port=7860)
37
 
 
1
  ## An python API for our Model
2
  ## Design by edander32 (Edmilson Alexandre) and jjambo(Joaquim Jambo)
3
 
4
+ from fastapi import FastAPI, HTTPException
5
+ from pydantic import BaseModel
6
  import pandas as pd
7
  import joblib
 
 
 
8
  from huggingface_hub import hf_hub_download
 
9
 
10
+ # Inicializa o app
11
+ app = FastAPI(title="Exoplanet Classifier API")
12
+
13
+ # Baixa e carrega o modelo
14
  model_path = hf_hub_download(
15
+ repo_id="Edalexan/ia-pkl", # substitua pelo seu repo/ID
16
  filename="exoplanet_model.pkl"
17
  )
18
  model = joblib.load(model_path)
19
 
20
+ # Define o modelo de entrada
21
+ class InputData(BaseModel):
22
+ # Substitua pelos nomes reais das features que seu modelo espera
23
+ feature1: float
24
+ feature2: float
25
+ feature3: float # e assim por diante
26
 
27
+ @app.get("/")
28
  def home():
29
  return {"message": "API do classificador de exoplanetas online."}
30
 
31
+ @app.post("/predict")
32
+ def predict(input_data: InputData):
33
  try:
34
+ # Converte para DataFrame
35
+ df = pd.DataFrame([input_data.dict()])
36
  pred = model.predict(df)[0]
37
  prob = model.predict_proba(df)[0][1]
38
+ return {"prediction": int(pred), "probability": float(prob)}
39
  except Exception as e:
40
+ raise HTTPException(status_code=400, detail=str(e))
41
 
 
 
42
 
Dockerfile CHANGED
@@ -1,6 +1,6 @@
1
  FROM python:3.11-slim
2
 
3
- # Instalar dependências de sistema necessárias para LightGBM
4
  RUN apt-get update && apt-get install -y \
5
  build-essential \
6
  libgomp1 \
@@ -9,12 +9,12 @@ RUN apt-get update && apt-get install -y \
9
  WORKDIR /app
10
 
11
  COPY requirements.txt .
12
- COPY API_flask_server.py .
13
- COPY model.py .
14
 
15
  RUN pip install --no-cache-dir -r requirements.txt
16
 
17
  EXPOSE 7860
18
 
19
- CMD ["python", "API_flask_server.py"]
 
20
 
 
1
  FROM python:3.11-slim
2
 
3
+ # Dependências de sistema para LightGBM
4
  RUN apt-get update && apt-get install -y \
5
  build-essential \
6
  libgomp1 \
 
9
  WORKDIR /app
10
 
11
  COPY requirements.txt .
12
+ COPY API_fastapi_server.py .
 
13
 
14
  RUN pip install --no-cache-dir -r requirements.txt
15
 
16
  EXPOSE 7860
17
 
18
+ # Rodando FastAPI com uvicorn
19
+ CMD ["uvicorn", "API_fastapi_server:app", "--host", "0.0.0.0", "--port", "7860"]
20
 
requirements.txt CHANGED
@@ -4,4 +4,12 @@ pandas
4
  numpy
5
  scikit-learn
6
  lightgbm
 
 
 
 
 
 
 
 
7
 
 
4
  numpy
5
  scikit-learn
6
  lightgbm
7
+ fastapi
8
+ uvicorn
9
+ pandas
10
+ scikit-learn
11
+ joblib
12
+ huggingface_hub
13
+ lightgbm
14
+
15