Update app.py
Browse files
app.py
CHANGED
|
@@ -4,25 +4,17 @@ import torch
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
|
| 6 |
# -------------------------------------------------
|
| 7 |
-
# CONFIG
|
| 8 |
# -------------------------------------------------
|
| 9 |
MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
|
| 10 |
-
HF_TOKEN = True # uses HF_TOKEN env variable
|
| 11 |
|
| 12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
|
| 14 |
# -------------------------------------------------
|
| 15 |
-
# LOAD MODEL (ONCE)
|
| 16 |
# -------------------------------------------------
|
| 17 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 18 |
-
|
| 19 |
-
token=HF_TOKEN
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
| 23 |
-
MODEL_NAME,
|
| 24 |
-
token=HF_TOKEN
|
| 25 |
-
)
|
| 26 |
|
| 27 |
model.to(device)
|
| 28 |
model.eval()
|
|
@@ -30,13 +22,26 @@ model.eval()
|
|
| 30 |
# -------------------------------------------------
|
| 31 |
# FASTAPI APP
|
| 32 |
# -------------------------------------------------
|
| 33 |
-
app = FastAPI(title="
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
class EmotionRequest(BaseModel):
|
| 37 |
text: str
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
@app.post("/emotion")
|
| 41 |
def classify_emotion(payload: EmotionRequest):
|
| 42 |
text = payload.text.strip()
|
|
@@ -62,4 +67,4 @@ def classify_emotion(payload: EmotionRequest):
|
|
| 62 |
return {
|
| 63 |
"emotion": model.config.id2label[pred_id],
|
| 64 |
"confidence": round(probs[0][pred_id].item(), 4),
|
| 65 |
-
}
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
|
| 6 |
# -------------------------------------------------
|
| 7 |
+
# CONFIG
|
| 8 |
# -------------------------------------------------
|
| 9 |
MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
|
|
|
|
| 10 |
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
|
| 13 |
# -------------------------------------------------
|
| 14 |
+
# LOAD MODEL (ONCE AT STARTUP)
|
| 15 |
# -------------------------------------------------
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 17 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
model.to(device)
|
| 20 |
model.eval()
|
|
|
|
| 22 |
# -------------------------------------------------
|
| 23 |
# FASTAPI APP
|
| 24 |
# -------------------------------------------------
|
| 25 |
+
app = FastAPI(title="Emotion Detection API")
|
| 26 |
|
| 27 |
+
# ✅ ROOT ROUTE (CRITICAL FOR HF SPACES)
|
| 28 |
+
@app.get("/")
|
| 29 |
+
def health():
|
| 30 |
+
"""
|
| 31 |
+
Health / wake-up endpoint.
|
| 32 |
+
Hugging Face uses this to wake the Space.
|
| 33 |
+
"""
|
| 34 |
+
return {"status": "ok"}
|
| 35 |
|
| 36 |
+
# -------------------------------------------------
|
| 37 |
+
# REQUEST SCHEMA
|
| 38 |
+
# -------------------------------------------------
|
| 39 |
class EmotionRequest(BaseModel):
|
| 40 |
text: str
|
| 41 |
|
| 42 |
+
# -------------------------------------------------
|
| 43 |
+
# EMOTION ENDPOINT
|
| 44 |
+
# -------------------------------------------------
|
| 45 |
@app.post("/emotion")
|
| 46 |
def classify_emotion(payload: EmotionRequest):
|
| 47 |
text = payload.text.strip()
|
|
|
|
| 67 |
return {
|
| 68 |
"emotion": model.config.id2label[pred_id],
|
| 69 |
"confidence": round(probs[0][pred_id].item(), 4),
|
| 70 |
+
}
|