Update app.py
Browse files
app.py
CHANGED
|
@@ -2,85 +2,60 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# 2. Load the
|
|
|
|
|
|
|
| 6 |
emotion_classifier = pipeline(
|
| 7 |
"text-classification",
|
| 8 |
-
model="
|
| 9 |
-
top_k=None
|
| 10 |
)
|
| 11 |
|
| 12 |
-
# 3. Define the function
|
| 13 |
-
def
|
| 14 |
"""
|
| 15 |
This function takes text, passes it to the AI model,
|
| 16 |
-
and then formats the results for
|
| 17 |
"""
|
| 18 |
# Get the raw predictions from the model
|
| 19 |
-
predictions = emotion_classifier(text_input)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 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 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
|
| 58 |
-
return key_indicators, interpretation_text, resource_links
|
| 59 |
|
| 60 |
-
# 4. Create the Gradio web interface
|
| 61 |
app_interface = gr.Interface(
|
| 62 |
-
fn=
|
| 63 |
inputs=gr.Textbox(
|
| 64 |
lines=8,
|
| 65 |
label="Social Media Post",
|
| 66 |
-
placeholder="Type or paste a social media post here..."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
),
|
| 68 |
-
|
| 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
|
| 77 |
-
|
| 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 |
-
["
|
| 84 |
]
|
| 85 |
)
|
| 86 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# 2. Load the pre-trained AI model
|
| 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="SamLowe/roberta-base-go_emotions",
|
| 11 |
+
top_k=None # This ensures we see the scores for all emotions
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# 3. Define the function that will process the input and return the output
|
| 15 |
+
def predict_emotions(text_input):
|
| 16 |
"""
|
| 17 |
This function takes text, passes it to the AI model,
|
| 18 |
+
and then formats the results for display.
|
| 19 |
"""
|
| 20 |
# Get the raw predictions from the model
|
| 21 |
+
predictions = emotion_classifier(text_input)
|
| 22 |
|
| 23 |
+
# The model returns a list of lists. We only need the first element.
|
| 24 |
+
emotions = predictions[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# We will focus on key indicators for stress and depression
|
| 27 |
+
key_indicators = ['sadness', 'fear', 'anger', 'disappointment', 'nervousness']
|
| 28 |
|
| 29 |
+
# Create a dictionary to hold the scores for our key indicators
|
| 30 |
+
formatted_results = {}
|
| 31 |
+
for emotion in emotions:
|
| 32 |
+
if emotion['label'] in key_indicators:
|
| 33 |
+
formatted_results[emotion['label']] = round(emotion['score'], 3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
return formatted_results
|
|
|
|
| 36 |
|
| 37 |
+
# 4. Create the Gradio web interface
|
| 38 |
app_interface = gr.Interface(
|
| 39 |
+
fn=predict_emotions,
|
| 40 |
inputs=gr.Textbox(
|
| 41 |
lines=8,
|
| 42 |
label="Social Media Post",
|
| 43 |
+
placeholder="Type or paste a social media post here to analyze its emotional content..."
|
| 44 |
+
),
|
| 45 |
+
outputs=gr.Label(
|
| 46 |
+
num_top_classes=5,
|
| 47 |
+
label="Key Emotional Indicators"
|
| 48 |
),
|
| 49 |
+
title="Student Wellness Analyzer 🧠",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
description="""
|
| 51 |
+
**Disclaimer:** This is an AI demo and **not a medical diagnostic tool**.
|
| 52 |
+
This model analyzes text for emotional indicators often associated with stress and depression.
|
| 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 |
+
["I failed a midterm I studied really hard for. I just feel like a total disappointment and can't get motivated anymore."]
|
| 59 |
]
|
| 60 |
)
|
| 61 |
|