moodanalyzer / app.py
razanalsulami's picture
Update app.py
903f7b1 verified
raw
history blame
1.48 kB
import gradio as gr
from transformers import pipeline
# Load sentiment analysis pipeline
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
# Function to analyze user's mood based on input
def analyze_mood(user_input):
# Analyze the mood from input text
results = sentiment_analysis(user_input)
mood_summary = {"POSITIVE": 0, "NEGATIVE": 0, "NEUTRAL": 0}
suggestions = []
# Loop through all results and summarize sentiments
for result in results:
label = result["label"]
score = result["score"]
mood_summary[label] += score
# Determine the dominant mood
dominant_mood = max(mood_summary, key=mood_summary.get)
# Provide suggestions based on mood
if dominant_mood == "POSITIVE":
suggestion = "Keep enjoying your day 😊"
elif dominant_mood == "NEGATIVE":
suggestion = "Try playing a game you like or practice some deep breathing exercises. It might help! πŸƒ"
else:
suggestion = "You're doing well! Stay calm 🌸"
# Return mood and suggestion
return f"Your mood seems mostly {dominant_mood.lower()}.", suggestion
inputs = gr.Textbox(label="How are you feeling today?", placeholder="Type your thoughts here...")
outputs = gr.Textbox(label="Mood and Suggestion")
interface = gr.Interface(fn=analyze_mood, inputs=inputs, outputs=outputs, title="Mood Analyzer with Suggestions")
interface.launch()