File size: 2,531 Bytes
bde4f53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c07f3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)