import gradio as gr from transformers import pipeline from PIL import Image import os # Sentiment analysis pipeline (small model) emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1) # Only two emotions and 4 apps each app_suggestions = { "sadness": [ ("Messenger", "icons/Messenger.png"), ("Spotify", "icons/Spotify.png"), ("Reddit", "icons/tiktok.png"), ("Headspace", "icons/Youtube.png"), ], "joy": [ ("Instagram", "icons/Blackboard.png"), ("YouTube", "icons/Outlook.png"), ("TikTok", "icons/UC.png"), ("Snapchat", "icons/Word.png"), ] } def analyze_day(text): result = emotion_pipeline(text)[0][0] emotion = result['label'].lower() if emotion not in app_suggestions: return f"Detected Emotion: **{emotion.capitalize()}**\n\nNo suggestions available.", [] suggestions = app_suggestions[emotion] images = [] for name, path in suggestions: try: img = Image.open(path).resize((80, 80)) images.append(gr.update(value=img, visible=True)) except Exception as e: images.append(gr.update(visible=False)) # Pad with None if fewer than 4 while len(images) < 4: images.append(gr.update(visible=False)) app_output = f"Detected Emotion: **{emotion.capitalize()}**\n\nSuggested Apps:" return (app_output, *images) with gr.Blocks() as demo: gr.Markdown("## 😊😢 How Do you feel today?") gr.Markdown( "This app uses emotion detection to understand whether you're feeling **happy** or **sad**.\n\n" "If you're **happy**, it recommends **productivity apps**.\n" "If you're **sad**, it recommends **entertainment apps** to lift your mood. 😊" ) user_input = gr.Textbox(lines=3, placeholder="Type something like 'I feel so happy today!'", label="Your day") output_text = gr.Markdown() with gr.Row() as output_gallery: img1 = gr.Image(label="", width=80, height=80, visible=False) img2 = gr.Image(label="", width=80, height=80, visible=False) img3 = gr.Image(label="", width=80, height=80, visible=False) img4 = gr.Image(label="", width=80, height=80, visible=False) submit_btn = gr.Button("Analyze & Suggest Apps") submit_btn.click(fn=analyze_day, inputs=user_input, outputs=[output_text, img1, img2, img3, img4]) demo.launch(share=True)