|
from fastapi import FastAPI, Query |
|
from pydantic import BaseModel |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline |
|
|
|
app = FastAPI() |
|
|
|
|
|
model_name = "gyesibiney/Sentiment-review-analysis-roberta-3" |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
sentiment = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) |
|
|
|
|
|
|
|
sentiment_label_mapping = { |
|
"LABEL_1": "positive", |
|
"LABEL_0": "negative",} |
|
|
|
|
|
class SentimentRequest(BaseModel): |
|
text: str |
|
|
|
|
|
class SentimentResponse(BaseModel): |
|
sentiment: str |
|
score: float |
|
|
|
|
|
@app.get("/sentiment/") |
|
async def analyze_sentiment(text: str = Query(..., description="Input text for sentiment analysis")): |
|
result = sentiment(text) |
|
sentiment_label = result[0]["label"] |
|
sentiment_score = result[0]["score"] |
|
|
|
sentiment_value = sentiment_label_mapping.get(sentiment_label, -1) |
|
|
|
return SentimentResponse(sentiment=sentiment_value, score=sentiment_score) |
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run(app, host="0.0.0.0", port=8000) |
|
|
|
|