NaimaAqeel commited on
Commit
0563dcb
·
verified ·
1 Parent(s): 89c4f2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -20
app.py CHANGED
@@ -61,7 +61,7 @@ def create_visualization(emotions, toxicity):
61
 
62
  def analyze_text(text):
63
  # Detect all emotions
64
- emo_res = emotion_pipeline(text, top_k=None) # <-- get all emotions
65
  emotions = {e["label"].lower(): e["score"] for e in emo_res if e["score"] > EMO_THRESHOLD}
66
 
67
  # Detect toxicity
@@ -70,32 +70,55 @@ def analyze_text(text):
70
 
71
  # Prepare detailed per-emotion output
72
  details = ""
73
- total_score = 0
74
- total_weight = 0
75
-
 
76
  for i, (emo, score) in enumerate(emotions.items(), 1):
77
  polarity = EMO_SENTIMENT_MAPPING.get(emo, 0)
78
- total_score += polarity * score
79
- total_weight += score
80
-
81
  underlying = ", ".join(UNDERLYING_EMOTIONS.get(emo, ["Unknown"]))
82
  recommendation = RECOMMENDATIONS.get(emo, "Take care of yourself.")
83
- emo_label = "Positive" if polarity > 0 else "Negative" if polarity < 0 else "Neutral"
 
 
 
 
 
 
 
 
 
84
 
85
  details += f"Emotion {i}: {emo.capitalize()} ({emo_label}, Confidence: {score:.2f})\n"
86
  details += f"Underlying emotions: {underlying}\n"
87
  details += f"Recommendation: {recommendation}\n\n"
88
 
89
- # Final sentiment conclusion
90
- overall_score = total_score / total_weight if total_weight > 0 else 0
91
- if overall_score > 0.2:
92
- final_sentiment = "Positive"
93
- elif overall_score < -0.2:
94
- final_sentiment = "Negative"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  else:
96
- final_sentiment = "Neutral"
97
-
98
- details += f"Conclusion: Overall Sentiment based on all emotions: {final_sentiment}\n"
99
 
100
  visualization = create_visualization(emotions, toxicity)
101
  return details, visualization
@@ -105,8 +128,8 @@ iface = gr.Interface(
105
  fn=analyze_text,
106
  inputs=gr.Textbox(label="Enter text here", placeholder="Type a sentence..."),
107
  outputs=[gr.Textbox(label="Emotion Details & Recommendations"), gr.HTML(label="Emotion & Toxicity Chart")],
108
- title="Multi-Emotion Sentiment Analyzer",
109
- description="Detects multiple emotions, lists underlying emotions, gives recommendations, and calculates final overall sentiment."
110
  )
111
 
112
- iface.launch()
 
61
 
62
  def analyze_text(text):
63
  # Detect all emotions
64
+ emo_res = emotion_pipeline(text, top_k=None)
65
  emotions = {e["label"].lower(): e["score"] for e in emo_res if e["score"] > EMO_THRESHOLD}
66
 
67
  # Detect toxicity
 
70
 
71
  # Prepare detailed per-emotion output
72
  details = ""
73
+ positive_count = 0
74
+ negative_count = 0
75
+ neutral_count = 0
76
+
77
  for i, (emo, score) in enumerate(emotions.items(), 1):
78
  polarity = EMO_SENTIMENT_MAPPING.get(emo, 0)
 
 
 
79
  underlying = ", ".join(UNDERLYING_EMOTIONS.get(emo, ["Unknown"]))
80
  recommendation = RECOMMENDATIONS.get(emo, "Take care of yourself.")
81
+
82
+ if polarity > 0:
83
+ emo_label = "Positive"
84
+ positive_count += 1
85
+ elif polarity < 0:
86
+ emo_label = "Negative"
87
+ negative_count += 1
88
+ else:
89
+ emo_label = "Neutral"
90
+ neutral_count += 1
91
 
92
  details += f"Emotion {i}: {emo.capitalize()} ({emo_label}, Confidence: {score:.2f})\n"
93
  details += f"Underlying emotions: {underlying}\n"
94
  details += f"Recommendation: {recommendation}\n\n"
95
 
96
+ # Final sentiment conclusion based on counts
97
+ total_emotions = len(emotions)
98
+ if total_emotions > 0:
99
+ positive_percent = positive_count / total_emotions
100
+ negative_percent = negative_count / total_emotions
101
+
102
+ if positive_percent > 0.7:
103
+ final_sentiment = "Strongly Positive"
104
+ elif positive_percent > 0.5:
105
+ final_sentiment = "Positive"
106
+ elif negative_percent > 0.7:
107
+ final_sentiment = "Strongly Negative"
108
+ elif negative_percent > 0.5:
109
+ final_sentiment = "Negative"
110
+ else:
111
+ if positive_count > negative_count:
112
+ final_sentiment = "Slightly Positive"
113
+ elif negative_count > positive_count:
114
+ final_sentiment = "Slightly Negative"
115
+ else:
116
+ final_sentiment = "Neutral"
117
+
118
+ details += f"\nConclusion: The text contains {positive_count} positive, {negative_count} negative and {neutral_count} neutral emotions.\n"
119
+ details += f"Final Sentiment: {final_sentiment} (based on underlying emotions analysis)"
120
  else:
121
+ details += "\nConclusion: No significant emotions detected."
 
 
122
 
123
  visualization = create_visualization(emotions, toxicity)
124
  return details, visualization
 
128
  fn=analyze_text,
129
  inputs=gr.Textbox(label="Enter text here", placeholder="Type a sentence..."),
130
  outputs=[gr.Textbox(label="Emotion Details & Recommendations"), gr.HTML(label="Emotion & Toxicity Chart")],
131
+ title="Advanced Emotion Sentiment Analyzer",
132
+ description="Detects and analyzes each emotion separately with underlying emotions, recommendations, and comprehensive sentiment conclusion."
133
  )
134
 
135
+ iface.launch()