Spaces:
Sleeping
Sleeping
| # app_nlp.py | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel, Field | |
| from typing import Dict | |
| from transformers import pipeline | |
| APP_VERSION = "1.0.0" | |
| app = FastAPI( | |
| title="Class 8 - NLP Model Serving (Sentiment)", | |
| version=APP_VERSION, | |
| description="Serve a small pretrained NLP model via FastAPI." | |
| ) | |
| # A small, commonly used sentiment model: | |
| # (It will download on first run.) | |
| SENTIMENT_MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english" | |
| # Load once at startup (good pattern) | |
| nlp = pipeline("sentiment-analysis", model=SENTIMENT_MODEL_NAME) | |
| class TextRequest(BaseModel): | |
| text: str = Field(..., min_length=1, max_length=1000, description="Input text to analyze") | |
| class NLPResponse(BaseModel): | |
| ok: bool | |
| model_version: str | |
| model_name: str | |
| result: Dict[str, float | str] | |
| def health(): | |
| return {"status": "ok", "model_loaded": True, "model_name": SENTIMENT_MODEL_NAME} | |
| def predict(payload: TextRequest): | |
| try: | |
| out = nlp(payload.text)[0] # {'label': 'POSITIVE', 'score': ...} | |
| return NLPResponse( | |
| ok=True, | |
| model_version=APP_VERSION, | |
| model_name=SENTIMENT_MODEL_NAME, | |
| result={"label": out["label"], "score": float(out["score"])} | |
| ) | |
| except Exception: | |
| raise HTTPException(status_code=500, detail="NLP inference failed") | |