brijeshpandya's picture
inference API
89c88e1
raw
history blame
1.03 kB
# inference/app.py
from fastapi import FastAPI
from pydantic import BaseModel
from pathlib import Path
from transformers import pipeline
app = FastAPI(title="Incident ML Inference API")
LOCAL_MODEL = Path(__file__).resolve().parents[1] / "models" / "incident_classifier"
# Category classifier (your fine-tuned model if available)
if LOCAL_MODEL.exists():
incident_classifier = pipeline("text-classification", model="brijeshpandya/incident-classifier")
else:
incident_classifier = pipeline("text-classification", model="cardiffnlp/twitter-xlm-roberta-base")
# Sentiment (keep public model for now)
sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")
class AnalyzeIn(BaseModel):
text: str
@app.get("/health")
def health(): return {"ok": True, "using_local_model": LOCAL_MODEL.exists()}
@app.post("/analyze")
def analyze(data: AnalyzeIn):
return {
"category": incident_classifier(data.text),
"sentiment": sentiment_analyzer(data.text)
}