anshuman7898's picture
Create app.py
f43f6ee verified
raw
history blame
2.43 kB
# 1. Import necessary libraries
import gradio as gr
from transformers import pipeline
# 2. Load the pre-trained AI model
# We are using a model fine-tuned to recognize multiple emotions.
# This is more nuanced than a simple positive/negative classifier.
emotion_classifier = pipeline(
"text-classification",
model="SamLowe/roberta-base-go_emotions",
top_k=None # This ensures we see the scores for all emotions
)
# 3. Define the function that will process the input and return the output
def predict_emotions(text_input):
"""
This function takes text, passes it to the AI model,
and then formats the results for display.
"""
# Get the raw predictions from the model
predictions = emotion_classifier(text_input)
# The model returns a list of lists. We only need the first element.
emotions = predictions[0]
# We will focus on key indicators for stress and depression
key_indicators = ['sadness', 'fear', 'anger', 'disappointment', 'nervousness']
# Create a dictionary to hold the scores for our key indicators
formatted_results = {}
for emotion in emotions:
if emotion['label'] in key_indicators:
formatted_results[emotion['label']] = round(emotion['score'], 3)
return formatted_results
# 4. Create the Gradio web interface
app_interface = gr.Interface(
fn=predict_emotions,
inputs=gr.Textbox(
lines=8,
label="Social Media Post",
placeholder="Type or paste a social media post here to analyze its emotional content..."
),
outputs=gr.Label(
num_top_classes=5,
label="Key Emotional Indicators"
),
title="Student Wellness Analyzer 🧠",
description="""
**Disclaimer:** This is an AI demo and **not a medical diagnostic tool**.
This model analyzes text for emotional indicators often associated with stress and depression.
If you are struggling, please seek help from a qualified professional.
""",
examples=[
["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."],
["Feeling completely isolated and lonely on campus. It seems like everyone else has their friend group figured out."],
["I failed a midterm I studied really hard for. I just feel like a total disappointment and can't get motivated anymore."]
]
)
# 5. Launch the app
app_interface.launch()