Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py — HuggingFace Space (FastAPI)
|
| 2 |
+
# Serves our fine-tuned sentiment model as a REST API
|
| 3 |
+
# Deploy this to: huggingface.co/spaces/YOUR_USERNAME/creatorpulse-api
|
| 4 |
+
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
from typing import List
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
app = FastAPI(title="CreatorPulse Sentiment API")
|
| 12 |
+
|
| 13 |
+
# ── Load model once on startup ─────────────────────────────────────────────────
|
| 14 |
+
HF_USERNAME = os.getenv("HF_USERNAME", "ningaraddi")
|
| 15 |
+
MODEL_REPO = f"{HF_USERNAME}/creatorpulse-sentiment"
|
| 16 |
+
|
| 17 |
+
print(f"Loading model from {MODEL_REPO}...")
|
| 18 |
+
|
| 19 |
+
classifier = pipeline(
|
| 20 |
+
"sentiment-analysis",
|
| 21 |
+
model=MODEL_REPO,
|
| 22 |
+
tokenizer=MODEL_REPO,
|
| 23 |
+
truncation=True,
|
| 24 |
+
max_length=128,
|
| 25 |
+
device=-1 # CPU on free tier — fast enough for our use case
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
print("✅ Model loaded and ready.")
|
| 29 |
+
|
| 30 |
+
# ── Request / Response schemas ──────────────────────────────────────────────────
|
| 31 |
+
class ClassifyRequest(BaseModel):
|
| 32 |
+
texts: List[str] # up to 100 comments at once
|
| 33 |
+
|
| 34 |
+
class PredictionResult(BaseModel):
|
| 35 |
+
text: str
|
| 36 |
+
label: str # POSITIVE | NEUTRAL | NEGATIVE
|
| 37 |
+
confidence: float
|
| 38 |
+
|
| 39 |
+
class ClassifyResponse(BaseModel):
|
| 40 |
+
predictions: List[PredictionResult]
|
| 41 |
+
|
| 42 |
+
# ── Health check ────────────────────────────────────────────────────────────────
|
| 43 |
+
@app.get("/")
|
| 44 |
+
def health():
|
| 45 |
+
return {"status": "ok", "model": MODEL_REPO}
|
| 46 |
+
|
| 47 |
+
# ── Main classify endpoint ──────────────────────────────────────────────────────
|
| 48 |
+
@app.post("/classify", response_model=ClassifyResponse)
|
| 49 |
+
def classify(request: ClassifyRequest):
|
| 50 |
+
# Limit to 100 texts per call to avoid timeout
|
| 51 |
+
texts = request.texts[:100]
|
| 52 |
+
|
| 53 |
+
results = classifier(texts, batch_size=16)
|
| 54 |
+
|
| 55 |
+
predictions = [
|
| 56 |
+
PredictionResult(
|
| 57 |
+
text=text,
|
| 58 |
+
label=result["label"], # POSITIVE / NEUTRAL / NEGATIVE
|
| 59 |
+
confidence=round(result["score"], 4)
|
| 60 |
+
)
|
| 61 |
+
for text, result in zip(texts, results)
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
return ClassifyResponse(predictions=predictions)
|