gyesibiney commited on
Commit
475ca58
·
1 Parent(s): 7111d0e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +45 -2
main.py CHANGED
@@ -1,2 +1,45 @@
1
- from fastapi import FastAPI, HTTPException, Query
2
- import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #from fastapi import FastAPI, HTTPException, Query
2
+ #import pandas as pd
3
+ from fastapi import FastAPI
4
+ from pydantic import BaseModel
5
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
6
+
7
+ app = FastAPI()
8
+
9
+ # Load the pre-trained model and tokenizer
10
+ model_name = "gyesibiney/covid-tweet-sentimental-Analysis-roberta"
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+
14
+ # Create a sentiment analysis pipeline
15
+ sentiment = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
16
+
17
+ # Define a request body model
18
+ class SentimentRequest(BaseModel):
19
+ text: str
20
+
21
+ # Define a response model
22
+ class SentimentResponse(BaseModel):
23
+ sentiment: str
24
+ score: float
25
+
26
+ # Create an endpoint for sentiment analysis
27
+ @app.post("/sentiment/")
28
+ async def analyze_sentiment(request: SentimentRequest):
29
+ input_text = request.text
30
+ result = sentiment(input_text)
31
+ sentiment_label = result[0]["label"]
32
+ sentiment_score = result[0]["score"]
33
+
34
+ if sentiment_label == "LABEL_1":
35
+ sentiment_label = "positive"
36
+ elif sentiment_label == "LABEL_0":
37
+ sentiment_label = "neutral"
38
+ else:
39
+ sentiment_label = "negative"
40
+
41
+ return SentimentResponse(sentiment=sentiment_label.capitalize(), score=sentiment_score)
42
+
43
+ if __name__ == "__main__":
44
+ import uvicorn
45
+ uvicorn.run(app, host="0.0.0.0", port=8000)