File size: 3,715 Bytes
b1bca3c
d54d1ef
 
 
2c8fd9e
6afd016
d54d1ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1bca3c
d54d1ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import streamlit as st
from transformers import pipeline
import random
import re
from textblob import TextBlob

# 1) Load a pretrained emotion classifier
@st.cache(allow_output_mutation=True)
def load_emotion_pipeline():
    return pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")

clf = load_emotion_pipeline()

# 2) Your response logic (from your script)
responses = {
    "sadness": [
        "It’s okay to feel down sometimes. I’m here to support you.",
        "I'm really sorry you're going through this. Want to talk more about it?",
        "You're not alone — I’m here for you."
    ],
    "anger": [
        "That must have been frustrating. Want to vent about it?",
        "It's okay to feel this way. I'm listening.",
        "Would it help to talk through it?"
    ],
    "love": [
        "That’s beautiful to hear! What made you feel that way?",
        "It’s amazing to experience moments like that.",
        "Sounds like something truly meaningful."
    ],
    "joy": [  # note: pipeline uses 'joy' instead of 'happiness'
        "That's awesome! What’s bringing you joy today?",
        "I love hearing good news. 😊",
        "Yay! Want to share more about it?"
    ],
    "surprise": [
        "Oh! That’s surprising. Tell me more!",
        "Wow, that’s unexpected!",
        "Sounds like something caught you off guard."
    ],
    "fear": [
        "I hear your concern. 😟",
        "It’s okay to feel anxious sometimes.",
        "I’m here with you."
    ],
    "disgust": [
        "That sounds upsetting. I'm here to listen.",
        "I understand that’s hard to hear.",
        "Let’s talk through what’s bothering you."
    ],
    "neutral": [
        "Got it. I’m here if you want to dive deeper.",
        "Thanks for sharing that. Tell me more if you’d like.",
        "I’m listening. How else can I support you?"
    ]
}

help_keywords   = ["suggest","help","calm","exercise","relax","any tips","can you"]
bye_inputs      = ["bye","goodbye","exit","quit"]
thank_you_inputs= ["thank","thanks","thank you"]
awaiting_tip    = False
relaxation_resources = {
    "exercise": "Try the 5-4-3-2-1 grounding method:\n- 5 things you see\n- 4 you can touch\n- 3 you hear\n- 2 you smell\n- 1 you taste",
    "video":      "Here’s a short calming video: https://youtu.be/O-6f5wQXSu8"
}

# 3) Streamlit UI
st.title("🌿 EmotiBot")
user_input = st.text_input("You:")

if st.button("Send") and user_input:
    # Optional: spelling correction
    user = str(TextBlob(user_input).correct())

    # Exit
    if any(b in user.lower() for b in bye_inputs):
        st.write("EmotiBot 🌿: Take care! I’m here whenever you want to talk.")
    # Thank you
    elif any(t in user.lower() for t in thank_you_inputs):
        st.write("EmotiBot 🌿: You're most welcome! 💙")
    # Help keywords → ask tip
    elif any(k in user.lower() for k in help_keywords):
        awaiting_tip = True
        st.write("EmotiBot 🌿: Would you prefer a short calming video or a quick breathing exercise?")
    # Tip follow-up
    elif awaiting_tip:
        choice = "video" if "video" in user.lower() else "exercise"
        response = relaxation_resources.get(choice, relaxation_resources["exercise"])
        st.write(f"EmotiBot 🌿: {response}")
        awaiting_tip = False
    else:
        # 4) Emotion inference
        pred = clf(user)[0]["label"].lower()
        # Map pipeline labels to your keys
        pred = "happiness" if pred == "joy" else pred
        st.write(f"**Detected emotion:** {pred}")
        reply = random.choice(responses.get(pred, responses["neutral"]))
        st.write(f"EmotiBot 🌿: {reply}")