hadhari / app.py
moabos
setup fastapi route with model preprocessing and prediction
37c48d4
raw
history blame contribute delete
674 Bytes
import joblib
from fastapi import FastAPI
from pydantic import BaseModel
from preprocessor import clean_text
app = FastAPI()
model = joblib.load("tfidf_logreg_98acc.joblib")
class Data(BaseModel):
text: str
@app.get("/")
def read_root() -> dict:
return {"message": "Welcome to the Hadhari API!", "usage": "POST to /predict with {'text': 'your input'}"}
@app.post("/predict")
def predict(data: Data) -> dict:
text = clean_text(data.text)
prediction = int(model.predict([text])[0])
probabilities = model.predict_proba([text])[0]
confidence = float(probabilities[prediction])
return {"prediction": prediction, "confidence": confidence}