Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,7 @@ sia = SentimentIntensityAnalyzer()
|
|
21 |
# Load English emotion detection pipeline
|
22 |
emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
23 |
|
24 |
-
# Load
|
25 |
multilingual_pipeline = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
26 |
|
27 |
# Sample texts
|
@@ -76,35 +76,46 @@ if text_option == "Choose Your Own Text":
|
|
76 |
if text_option == "Choose Your Own Text" and word_count <= MAX_WORDS:
|
77 |
st.session_state.user_text = text
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Function to generate AI-driven feedback
|
80 |
def generate_feedback(sentiment_score, top_emotion):
|
81 |
-
if sentiment_score['pos'] > 0.6:
|
82 |
return "π Your reading is positive! This text might boost engagement and motivation."
|
83 |
-
elif sentiment_score['neg'] > 0.6:
|
84 |
return "π The text seems to have a strong negative tone. Consider balancing it with positive information."
|
85 |
-
elif top_emotion in ["
|
86 |
-
return "π‘ This text might evoke strong emotions. Reflect on how it makes you feel and why."
|
87 |
-
elif top_emotion in ["joy", "optimism"]:
|
88 |
-
return "π This is an engaging and uplifting text. Keep exploring such content!"
|
89 |
-
else:
|
90 |
return "π€ The text is neutral. You might want to explore different perspectives."
|
|
|
|
|
91 |
|
92 |
# Sentiment Analysis
|
93 |
if st.button("Analyze Engagement"):
|
94 |
if text and (text_option != "Choose Your Own Text" or word_count <= MAX_WORDS):
|
95 |
-
# Sentiment Analysis
|
96 |
sentiment_score = sia.polarity_scores(text)
|
97 |
|
98 |
# Emotion Detection
|
99 |
if selected_language == "English":
|
100 |
emotion_results = emotion_pipeline(text)
|
101 |
top_emotion = max(emotion_results[0], key=lambda x: x['score'])['label']
|
102 |
-
emotion_scores = {e['label']: e['score'] for e in emotion_results[0]}
|
103 |
else:
|
104 |
-
# Multilingual Sentiment Analysis (
|
105 |
multilingual_results = multilingual_pipeline(text)
|
106 |
-
top_emotion = multilingual_results[0]["label"]
|
107 |
-
emotion_scores = {top_emotion: multilingual_results[0]["score"]}
|
108 |
|
109 |
# Display Sentiment
|
110 |
st.subheader("π Sentiment Analysis")
|
@@ -114,20 +125,11 @@ if st.button("Analyze Engagement"):
|
|
114 |
st.subheader("π Emotion Detection")
|
115 |
st.write(f"Top Emotion: **{top_emotion.capitalize()}**")
|
116 |
|
117 |
-
# Display Multiple Emotion Scores
|
118 |
-
st.subheader("π Expanded Emotion Insights")
|
119 |
-
for emotion, score in emotion_scores.items():
|
120 |
-
st.write(f"**{emotion.capitalize()}**: {score:.2f}")
|
121 |
-
|
122 |
# AI-Generated Feedback
|
123 |
st.subheader("π‘ AI-Generated Feedback")
|
124 |
feedback = generate_feedback(sentiment_score, top_emotion)
|
125 |
st.write(feedback)
|
126 |
|
127 |
-
# Visualization
|
128 |
-
fig, ax = plt.subplots()
|
129 |
-
ax.bar(emotion_scores.keys(), emotion_scores.values())
|
130 |
-
st.pyplot(fig)
|
131 |
elif text_option == "Choose Your Own Text" and word_count > MAX_WORDS:
|
132 |
st.warning("β οΈ Please reduce the text length to analyze.")
|
133 |
else:
|
|
|
21 |
# Load English emotion detection pipeline
|
22 |
emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
23 |
|
24 |
+
# Load multilingual sentiment model (returns star ratings 1-5 for non-English texts)
|
25 |
multilingual_pipeline = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
26 |
|
27 |
# Sample texts
|
|
|
76 |
if text_option == "Choose Your Own Text" and word_count <= MAX_WORDS:
|
77 |
st.session_state.user_text = text
|
78 |
|
79 |
+
# Function to convert star ratings (1-5) into emotion categories
|
80 |
+
def convert_star_rating_to_emotion(stars):
|
81 |
+
if stars == "1 star":
|
82 |
+
return "very negative"
|
83 |
+
elif stars == "2 stars":
|
84 |
+
return "negative"
|
85 |
+
elif stars == "3 stars":
|
86 |
+
return "neutral"
|
87 |
+
elif stars == "4 stars":
|
88 |
+
return "positive"
|
89 |
+
elif stars == "5 stars":
|
90 |
+
return "very positive"
|
91 |
+
else:
|
92 |
+
return "unknown"
|
93 |
+
|
94 |
# Function to generate AI-driven feedback
|
95 |
def generate_feedback(sentiment_score, top_emotion):
|
96 |
+
if sentiment_score['pos'] > 0.6 or top_emotion in ["very positive", "positive", "joy", "optimism"]:
|
97 |
return "π Your reading is positive! This text might boost engagement and motivation."
|
98 |
+
elif sentiment_score['neg'] > 0.6 or top_emotion in ["very negative", "negative", "anger", "disgust"]:
|
99 |
return "π The text seems to have a strong negative tone. Consider balancing it with positive information."
|
100 |
+
elif top_emotion in ["neutral"]:
|
|
|
|
|
|
|
|
|
101 |
return "π€ The text is neutral. You might want to explore different perspectives."
|
102 |
+
else:
|
103 |
+
return "βοΈ The analysis is inconclusive. Try refining your text for clearer sentiment."
|
104 |
|
105 |
# Sentiment Analysis
|
106 |
if st.button("Analyze Engagement"):
|
107 |
if text and (text_option != "Choose Your Own Text" or word_count <= MAX_WORDS):
|
108 |
+
# Sentiment Analysis (English)
|
109 |
sentiment_score = sia.polarity_scores(text)
|
110 |
|
111 |
# Emotion Detection
|
112 |
if selected_language == "English":
|
113 |
emotion_results = emotion_pipeline(text)
|
114 |
top_emotion = max(emotion_results[0], key=lambda x: x['score'])['label']
|
|
|
115 |
else:
|
116 |
+
# Multilingual Sentiment Analysis (Converts Star Ratings to Emotion)
|
117 |
multilingual_results = multilingual_pipeline(text)
|
118 |
+
top_emotion = convert_star_rating_to_emotion(multilingual_results[0]["label"])
|
|
|
119 |
|
120 |
# Display Sentiment
|
121 |
st.subheader("π Sentiment Analysis")
|
|
|
125 |
st.subheader("π Emotion Detection")
|
126 |
st.write(f"Top Emotion: **{top_emotion.capitalize()}**")
|
127 |
|
|
|
|
|
|
|
|
|
|
|
128 |
# AI-Generated Feedback
|
129 |
st.subheader("π‘ AI-Generated Feedback")
|
130 |
feedback = generate_feedback(sentiment_score, top_emotion)
|
131 |
st.write(feedback)
|
132 |
|
|
|
|
|
|
|
|
|
133 |
elif text_option == "Choose Your Own Text" and word_count > MAX_WORDS:
|
134 |
st.warning("β οΈ Please reduce the text length to analyze.")
|
135 |
else:
|