ak0601's picture
Upload 2 files
3af8442
raw
history blame contribute delete
742 Bytes
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
@app.post("/sentiment", response_model=SentimentResponse)
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)