import os from fastapi import FastAPI, HTTPException from pydantic import BaseModel from huggingface_hub import InferenceClient MODEL_ID = os.getenv( "MODEL_ID", "distilbert/distilbert-base-uncased-finetuned-sst-2-english", ) client = InferenceClient( provider="hf-inference", api_key=os.environ["HF_TOKEN"], ) app = FastAPI() class Payload(BaseModel): text: str @app.post("/analyze") def analyze(p: Payload): if not p.text.strip(): raise HTTPException(status_code=400, detail="Empty text") result = client.text_classification( p.text, model=MODEL_ID, ) return {"model": MODEL_ID, "result": result}