from fastapi import FastAPI from pydantic import BaseModel from transformers import pipeline app = FastAPI() # Load sentiment analysis model sentiment_pipeline = pipeline("sentiment-analysis") class SentimentRequest(BaseModel): text: str class SentimentResponse(BaseModel): label: str score: float @app.get("/") def home(): return {"message": "Sentiment Analysis API is running!"} @app.post("/predict/", response_model=SentimentResponse) def predict(request: SentimentRequest): result = sentiment_pipeline(request.text) return SentimentResponse(label=result[0]['label'], score=result[0]['score'])