Durganihantri commited on
Commit
57a3c17
Β·
verified Β·
1 Parent(s): 5ee2f6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -22
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 a **fully compatible** multilingual sentiment analysis model
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 ["anger", "disgust"]:
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 (Now Fully Supported)
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: