Edmilson Alexandre commited on
Commit
d63f9a0
·
1 Parent(s): b27d595
Files changed (1) hide show
  1. API_fastapi_server.py +17 -30
API_fastapi_server.py CHANGED
@@ -1,42 +1,29 @@
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
 
 
1
  ## An python API for our Model
2
  ## Design by edander32 (Edmilson Alexandre) and jjambo(Joaquim Jambo)
3
 
4
+ import gradio as gr
 
5
  import pandas as pd
6
  import joblib
7
  from huggingface_hub import hf_hub_download
8
 
9
+ # Carrega modelo
10
+ model_path = hf_hub_download(repo_id="Edalexan/ia-pkl", filename="exoplanet_model.pkl")
11
+ model = joblib.load(model_path)
12
 
13
+ def predict(feature1, feature2, feature3):
14
+ df = pd.DataFrame([[feature1, feature2, feature3]], columns=["feature1", "feature2", "feature3"])
15
+ pred = model.predict(df)[0]
16
+ prob = model.predict_proba(df)[0][1]
17
+ return {"prediction": int(pred), "probability": float(prob)}
18
+
19
+ # Interface mínima só para expor a API
20
+ iface = gr.Interface(
21
+ fn=predict,
22
+ inputs=["number", "number", "number"],
23
+ outputs="json"
24
  )
 
25
 
26
+ iface.launch(server_name="0.0.0.0", server_port=7860)
27
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29