Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 1. Import necessary libraries
|
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 |
+
|
62 |
+
# 5. Launch the app
|
63 |
+
app_interface.launch()
|