Spaces:
Running
Running
Update app.py
Browse files
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)
|
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 |
-
|
74 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
91 |
-
if
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
else:
|
96 |
-
|
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="
|
109 |
-
description="Detects
|
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()
|