-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (46 loc) · 1.56 KB
/
app.py
File metadata and controls
57 lines (46 loc) · 1.56 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import logging
from flask import Flask, jsonify
from nltk.sentiment import SentimentIntensityAnalyzer # type: ignore
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)
sia = SentimentIntensityAnalyzer()
app = Flask(__name__)
@app.route("/")
def home():
try:
return jsonify(
message="Hey welcome I am running. \
Use /analyze/text use %20 for blank spaces."
)
except Exception as e:
logger.error(f"Error in the Sentiment analysis home route: {str(e)}")
return jsonify(error="Internal Server Error"), 500
@app.route("/analyze/<input_txt>")
def analyze_sentiment(input_txt):
try:
scores = sia.polarity_scores(input_txt)
print(scores)
pos = float(scores["pos"])
neg = float(scores["neg"])
neu = float(scores["neu"])
res = "positive"
print("pos neg neu", pos, neg, neu)
if neg > pos and neg > neu:
res = "negative"
elif neu > neg and neu > pos:
res = "neutral"
res = jsonify({"scores": scores, "sentiment": res})
print(res)
return res
except Exception as e:
logger.error(
f"Error in Sentiment analysis \
/analyze/<input_txt> route: {str(e)}"
)
return jsonify(error="Internal Server Error"), 500
# Global Exception Handler if any missed
def handle_exceptions(e):
logger.error(f"Unhandled Exception: {str(e)}")
return jsonify(error="Something went wrong"), 500
if __name__ == "__main__":
app.run(debug=False)