-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
35 lines (26 loc) · 974 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
api = FastAPI(title="Fast_Sentiment - Sentimental Analyzer API")
class TextInput(BaseModel):
text: str
@api.get("/")
def home():
return {"Message": "Welcome to Fast_Sentiment!"}
@api.post("/sentiment")
async def get_sentiment(input: TextInput):
analyzer = SentimentIntensityAnalyzer()
result = analyzer.polarity_scores(input.text)
sentiment = None
if result["compound"] >= 0.05:
sentiment = "Positive"
elif result["compound"] <= -0.05:
sentiment = "Negative"
else:
sentiment = "Neutral"
# the composite score is the proportion of text in each category between -1(extreme negative) and 1(extreme positive)
return {"result": f"The sentiment of the text is {sentiment}!"}
# run the API
if __name__ == "__main__":
uvicorn.run(api, host="0.0.0.0", port=8000)