Spaces:
Sleeping
Sleeping
Commit
·
0e3b935
0
Parent(s):
Inference API (FastAPI) – first push
Browse files- Dockerfile +6 -0
- README.md +10 -0
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +31 -0
- requirements.txt +4 -0
- runtime.txt +1 -0
Dockerfile
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.11-slim
|
2 |
+
WORKDIR /app
|
3 |
+
COPY requirements.txt .
|
4 |
+
RUN pip install -r requirements.txt
|
5 |
+
COPY . .
|
6 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "9000"]
|
README.md
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Incident ML Inference API
|
2 |
+
|
3 |
+
This FastAPI app provides endpoints for:
|
4 |
+
- `/health` - service health check
|
5 |
+
- `/analyze` - classify incident category & sentiment
|
6 |
+
|
7 |
+
## Deploy to Hugging Face Spaces
|
8 |
+
- Create new Space (FastAPI SDK)
|
9 |
+
- Push contents of this `inference/` folder to the Space repo
|
10 |
+
- Public URL will serve the endpoints
|
__pycache__/app.cpython-311.pyc
ADDED
Binary file (2.07 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# inference/app.py
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from pathlib import Path
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
app = FastAPI(title="Incident ML Inference API")
|
8 |
+
|
9 |
+
LOCAL_MODEL = Path(__file__).resolve().parents[1] / "models" / "incident_classifier"
|
10 |
+
|
11 |
+
# Category classifier (your fine-tuned model if available)
|
12 |
+
if LOCAL_MODEL.exists():
|
13 |
+
incident_classifier = pipeline("text-classification", model=str(LOCAL_MODEL))
|
14 |
+
else:
|
15 |
+
incident_classifier = pipeline("text-classification", model="cardiffnlp/twitter-xlm-roberta-base")
|
16 |
+
|
17 |
+
# Sentiment (keep public model for now)
|
18 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")
|
19 |
+
|
20 |
+
class AnalyzeIn(BaseModel):
|
21 |
+
text: str
|
22 |
+
|
23 |
+
@app.get("/health")
|
24 |
+
def health(): return {"ok": True, "using_local_model": LOCAL_MODEL.exists()}
|
25 |
+
|
26 |
+
@app.post("/analyze")
|
27 |
+
def analyze(data: AnalyzeIn):
|
28 |
+
return {
|
29 |
+
"category": incident_classifier(data.text),
|
30 |
+
"sentiment": sentiment_analyzer(data.text)
|
31 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
transformers
|
4 |
+
torch
|
runtime.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python-3.11
|