Spaces:
No application file
No application file
File size: 2,825 Bytes
586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa 113c6bb 586a7fa | 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | # app.py — HuggingFace Space (FastAPI)
# FREE CPU tier hosting for CreatorPulse sentiment model
# Space URL: https://ningaraddi-creatorpulse-api.hf.space
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List
from transformers import pipeline
import os
app = FastAPI(title="CreatorPulse Sentiment API")
# ── CORS — allow calls from any origin (our React app) ─────────────────────────
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["POST", "GET"],
allow_headers=["*"],
)
# ── Load model once on startup ─────────────────────────────────────────────────
MODEL_REPO = "ningaraddi/creatorpulse-sentiment"
print(f"Loading model: {MODEL_REPO}")
classifier = pipeline(
"text-classification",
model=MODEL_REPO,
tokenizer=MODEL_REPO,
truncation=True,
max_length=128,
device=-1, # CPU — free tier
top_k=None, # Return all labels with scores
)
print("Model loaded!")
# ── Schemas ────────────────────────────────────────────────────────────────────
class ClassifyRequest(BaseModel):
inputs: List[str]
class Prediction(BaseModel):
label: str
confidence: float
text: str
class ClassifyResponse(BaseModel):
predictions: List[Prediction]
# ── Health check ───────────────────────────────────────────────────────────────
@app.get("/")
def health():
return {"status": "ok", "model": MODEL_REPO}
# ── Classify endpoint ──────────────────────────────────────────────────────────
@app.post("/classify", response_model=ClassifyResponse)
def classify(request: ClassifyRequest):
texts = request.inputs[:100] # Max 100 per call
results = classifier(texts, batch_size=8)
predictions = []
for text, label_list in zip(texts, results):
top = max(label_list, key=lambda x: x["score"])
# Normalize label names
label_map = {"LABEL_0": "NEGATIVE", "LABEL_1": "NEUTRAL", "LABEL_2": "POSITIVE"}
label = label_map.get(top["label"], top["label"])
predictions.append(Prediction(
text=text,
label=label,
confidence=round(top["score"], 4),
))
return ClassifyResponse(predictions=predictions)
|