exo / API_flask_server.py
Edmilson Alexandre
yeah again 4
a94e576
raw
history blame
1.25 kB
## An python API for our Model
## Design by edander32 (Edmilson Alexandre) and jjambo(Joaquim Jambo)
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import pandas as pd
import joblib
from huggingface_hub import hf_hub_download
# Inicializa o app
app = FastAPI(title="Exoplanet Classifier API")
# Baixa e carrega o modelo
model_path = hf_hub_download(
repo_id="Edalexan/ia-pkl", # substitua pelo seu repo/ID
filename="exoplanet_model.pkl"
)
model = joblib.load(model_path)
# Define o modelo de entrada
class InputData(BaseModel):
# Substitua pelos nomes reais das features que seu modelo espera
feature1: float
feature2: float
feature3: float # e assim por diante
@app.get("/")
def home():
return {"message": "API do classificador de exoplanetas online."}
@app.post("/predict")
def predict(input_data: InputData):
try:
# Converte para DataFrame
df = pd.DataFrame([input_data.dict()])
pred = model.predict(df)[0]
prob = model.predict_proba(df)[0][1]
return {"prediction": int(pred), "probability": float(prob)}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))