Spaces:
Sleeping
Sleeping
from fastapi import FastAPI | |
from pydantic import BaseModel | |
from transformers import pipeline | |
app = FastAPI() | |
# Initialize the sentiment analysis pipeline | |
sentiment_pipeline = pipeline("sentiment-analysis") | |
class SentimentRequest(BaseModel): | |
text: str | |
class SentimentResponse(BaseModel): | |
label: str | |
score: float | |
async def analyze_sentiment(request: SentimentRequest): | |
text = request.text | |
results = sentiment_pipeline(text) | |
best_result = max(results, key=lambda x: x["score"]) | |
return SentimentResponse(label=best_result["label"], score=best_result["score"]) | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=8000) | |