Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,72 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
-
import nltk
|
3 |
-
from nltk.sentiment import SentimentIntensityAnalyzer
|
4 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
-
from scipy.special import softmax
|
6 |
-
import pandas as pd
|
7 |
-
|
8 |
-
# Initialize Flask app
|
9 |
-
app = Flask(__name__)
|
10 |
-
|
11 |
-
# Load NLTK's VADER
|
12 |
-
nltk.download('vader_lexicon')
|
13 |
-
sia = SentimentIntensityAnalyzer()
|
14 |
-
|
15 |
-
# Load the transformer model and tokenizer (e.g., RoBERTa)
|
16 |
-
tokenizer = AutoTokenizer.from_pretrained('cardiffnlp/twitter-roberta-base-sentiment')
|
17 |
-
model = AutoModelForSequenceClassification.from_pretrained('cardiffnlp/twitter-roberta-base-sentiment')
|
18 |
-
|
19 |
-
def analyze_sentiment(text):
|
20 |
-
# VADER sentiment analysis
|
21 |
-
vader_result = sia.polarity_scores(text)
|
22 |
-
|
23 |
-
# RoBERTa sentiment analysis
|
24 |
-
encoded_input = tokenizer(text, return_tensors='pt')
|
25 |
-
output = model(**encoded_input)
|
26 |
-
scores = output[0][0].detach().numpy()
|
27 |
-
scores = softmax(scores)
|
28 |
-
roberta_result = {
|
29 |
-
'roberta_neg': scores[0],
|
30 |
-
'roberta_neu': scores[1],
|
31 |
-
'roberta_pos': scores[2]
|
32 |
-
}
|
33 |
-
|
34 |
-
return {**vader_result, **roberta_result}
|
35 |
-
|
36 |
-
def sentiment_to_stars(sentiment_score):
|
37 |
-
thresholds = [0.2, 0.4, 0.6, 0.8]
|
38 |
-
if sentiment_score <= thresholds[0]:
|
39 |
-
return 1
|
40 |
-
elif sentiment_score <= thresholds[1]:
|
41 |
-
return 2
|
42 |
-
elif sentiment_score <= thresholds[2]:
|
43 |
-
return 3
|
44 |
-
elif sentiment_score <= thresholds[3]:
|
45 |
-
return 4
|
46 |
-
else:
|
47 |
-
return 5
|
48 |
-
|
49 |
-
@app.route('/analyze', methods=['POST'])
|
50 |
-
def analyze():
|
51 |
-
data = request.json
|
52 |
-
text = data['text']
|
53 |
-
sentiment_scores = analyze_sentiment(text)
|
54 |
-
star_rating = sentiment_to_stars(sentiment_scores['roberta_pos'])
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import nltk
|
3 |
+
from nltk.sentiment import SentimentIntensityAnalyzer
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
+
from scipy.special import softmax
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
# Initialize Flask app
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
# Load NLTK's VADER
|
12 |
+
nltk.download('vader_lexicon')
|
13 |
+
sia = SentimentIntensityAnalyzer()
|
14 |
+
|
15 |
+
# Load the transformer model and tokenizer (e.g., RoBERTa)
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained('cardiffnlp/twitter-roberta-base-sentiment')
|
17 |
+
model = AutoModelForSequenceClassification.from_pretrained('cardiffnlp/twitter-roberta-base-sentiment')
|
18 |
+
|
19 |
+
def analyze_sentiment(text):
|
20 |
+
# VADER sentiment analysis
|
21 |
+
vader_result = sia.polarity_scores(text)
|
22 |
+
|
23 |
+
# RoBERTa sentiment analysis
|
24 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
25 |
+
output = model(**encoded_input)
|
26 |
+
scores = output[0][0].detach().numpy()
|
27 |
+
scores = softmax(scores)
|
28 |
+
roberta_result = {
|
29 |
+
'roberta_neg': scores[0],
|
30 |
+
'roberta_neu': scores[1],
|
31 |
+
'roberta_pos': scores[2]
|
32 |
+
}
|
33 |
+
|
34 |
+
return {**vader_result, **roberta_result}
|
35 |
+
|
36 |
+
def sentiment_to_stars(sentiment_score):
|
37 |
+
thresholds = [0.2, 0.4, 0.6, 0.8]
|
38 |
+
if sentiment_score <= thresholds[0]:
|
39 |
+
return 1
|
40 |
+
elif sentiment_score <= thresholds[1]:
|
41 |
+
return 2
|
42 |
+
elif sentiment_score <= thresholds[2]:
|
43 |
+
return 3
|
44 |
+
elif sentiment_score <= thresholds[3]:
|
45 |
+
return 4
|
46 |
+
else:
|
47 |
+
return 5
|
48 |
+
|
49 |
+
@app.route('/analyze', methods=['POST'])
|
50 |
+
def analyze():
|
51 |
+
data = request.json
|
52 |
+
text = data['text']
|
53 |
+
sentiment_scores = analyze_sentiment(text)
|
54 |
+
star_rating = sentiment_to_stars(sentiment_scores['roberta_pos'])
|
55 |
+
|
56 |
+
# Log the sentiment scores and star rating
|
57 |
+
app.logger.info("Sentiment scores: %s", sentiment_scores)
|
58 |
+
app.logger.info("Star rating: %s", star_rating)
|
59 |
+
|
60 |
+
response = {
|
61 |
+
'sentiment_scores': sentiment_scores,
|
62 |
+
'star_rating': star_rating
|
63 |
+
}
|
64 |
+
|
65 |
+
# Log the complete response before returning it
|
66 |
+
app.logger.info("Complete response: %s", response)
|
67 |
+
|
68 |
+
return jsonify(response)
|
69 |
+
|
70 |
+
|
71 |
+
if __name__ == '__main__':
|
72 |
+
app.run(host='0.0.0.0', port=5000)
|