Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,60 +2,85 @@
|
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
# 2. Load the
|
6 |
-
# We are using a model fine-tuned to recognize multiple emotions.
|
7 |
-
# This is more nuanced than a simple positive/negative classifier.
|
8 |
emotion_classifier = pipeline(
|
9 |
"text-classification",
|
10 |
-
model="
|
11 |
-
top_k=None
|
12 |
)
|
13 |
|
14 |
-
# 3. Define the function
|
15 |
-
def
|
16 |
"""
|
17 |
This function takes text, passes it to the AI model,
|
18 |
-
and then formats the results for
|
19 |
"""
|
20 |
# Get the raw predictions from the model
|
21 |
-
predictions = emotion_classifier(text_input)
|
22 |
|
23 |
-
#
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
|
|
36 |
|
37 |
-
# 4. Create the Gradio web interface
|
38 |
app_interface = gr.Interface(
|
39 |
-
fn=
|
40 |
inputs=gr.Textbox(
|
41 |
lines=8,
|
42 |
label="Social Media Post",
|
43 |
-
placeholder="Type or paste a social media post here
|
44 |
-
),
|
45 |
-
outputs=gr.Label(
|
46 |
-
num_top_classes=5,
|
47 |
-
label="Key Emotional Indicators"
|
48 |
),
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
description="""
|
51 |
-
|
52 |
-
This
|
53 |
If you are struggling, please seek help from a qualified professional.
|
54 |
""",
|
55 |
examples=[
|
56 |
["I'm so behind on all my assignments and the exams are next week. I don't know how I'm going to manage all this pressure."],
|
57 |
["Feeling completely isolated and lonely on campus. It seems like everyone else has their friend group figured out."],
|
58 |
-
["
|
59 |
]
|
60 |
)
|
61 |
|
|
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# 2. Load the NEW, more specialized AI model
|
|
|
|
|
6 |
emotion_classifier = pipeline(
|
7 |
"text-classification",
|
8 |
+
model="mental/mental-roberta-base", # <-- UPGRADED MODEL
|
9 |
+
top_k=None
|
10 |
)
|
11 |
|
12 |
+
# 3. Define the function to process the input and return outputs
|
13 |
+
def analyze_text(text_input):
|
14 |
"""
|
15 |
This function takes text, passes it to the AI model,
|
16 |
+
and then formats the results for a better user experience.
|
17 |
"""
|
18 |
# Get the raw predictions from the model
|
19 |
+
predictions = emotion_classifier(text_input)[0]
|
20 |
|
21 |
+
# Create a dictionary to hold the scores for key indicators
|
22 |
+
key_indicators = {
|
23 |
+
'sadness': 0, 'anger': 0, 'fear': 0, 'joy': 0
|
24 |
+
}
|
25 |
+
for emotion in predictions:
|
26 |
+
if emotion['label'] in key_indicators:
|
27 |
+
key_indicators[emotion['label']] = round(emotion['score'], 3)
|
28 |
+
|
29 |
+
# --- NEW: Interpretation Logic ---
|
30 |
+
# Find the dominant emotion among our key indicators
|
31 |
+
if not key_indicators:
|
32 |
+
dominant_emotion = "neutral"
|
33 |
+
else:
|
34 |
+
dominant_emotion = max(key_indicators, key=key_indicators.get)
|
35 |
|
36 |
+
interpretation_text = ""
|
37 |
+
resource_links = ""
|
38 |
|
39 |
+
if key_indicators.get(dominant_emotion, 0) > 0.4: # Set a threshold
|
40 |
+
if dominant_emotion in ['sadness', 'fear', 'anger']:
|
41 |
+
interpretation_text = (
|
42 |
+
f"The analysis shows a strong presence of **{dominant_emotion}**. "
|
43 |
+
"These feelings can be challenging. Remember, it's okay to seek support."
|
44 |
+
)
|
45 |
+
# --- NEW: Provide helpful resources ---
|
46 |
+
resource_links = """
|
47 |
+
**Please consider reaching out to a professional:**
|
48 |
+
- **Vandrevala Foundation:** [vandrev ফাউন্ডেশন](https://www.vandrevalafoundation.com/) (India)
|
49 |
+
- **NIMHANS Centre for Well-Being:** [NIMHANS](http://www.nimhans.ac.in/well-being-centre/) (Bengaluru)
|
50 |
+
- **Find a Helpline:** [findahelpline.com](https://findahelpline.com/) (Global)
|
51 |
+
"""
|
52 |
+
elif dominant_emotion == 'joy':
|
53 |
+
interpretation_text = (
|
54 |
+
f"The text shows strong indicators of **joy**. It's wonderful to see such positive expression."
|
55 |
+
)
|
56 |
|
57 |
+
# Return the scores, the interpretation, and the resources
|
58 |
+
return key_indicators, interpretation_text, resource_links
|
59 |
|
60 |
+
# 4. Create the Gradio web interface with new components
|
61 |
app_interface = gr.Interface(
|
62 |
+
fn=analyze_text,
|
63 |
inputs=gr.Textbox(
|
64 |
lines=8,
|
65 |
label="Social Media Post",
|
66 |
+
placeholder="Type or paste a social media post here..."
|
|
|
|
|
|
|
|
|
67 |
),
|
68 |
+
# --- NEW: Multiple output components for a richer display ---
|
69 |
+
outputs=[
|
70 |
+
gr.Label(label="Key Emotional Indicators"),
|
71 |
+
gr.Markdown(label="Interpretation"),
|
72 |
+
gr.Markdown(label="Resources")
|
73 |
+
],
|
74 |
+
title="Enhanced Student Wellness Analyzer 🧠✨",
|
75 |
description="""
|
76 |
+
This upgraded tool uses a specialized AI to analyze text for emotional indicators.
|
77 |
+
**Disclaimer:** This is **not a medical diagnostic tool**. It is an AI demonstration.
|
78 |
If you are struggling, please seek help from a qualified professional.
|
79 |
""",
|
80 |
examples=[
|
81 |
["I'm so behind on all my assignments and the exams are next week. I don't know how I'm going to manage all this pressure."],
|
82 |
["Feeling completely isolated and lonely on campus. It seems like everyone else has their friend group figured out."],
|
83 |
+
["Finished my project and I'm so proud of how it turned out! The hard work really paid off."]
|
84 |
]
|
85 |
)
|
86 |
|