File size: 1,013 Bytes
0e3b935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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=str(LOCAL_MODEL))
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)
    }