Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,104 +1,200 @@
|
|
1 |
-
import os
|
2 |
-
import fitz # PyMuPDF for PDF processing
|
3 |
-
import faiss
|
4 |
-
import numpy as np
|
5 |
import streamlit as st
|
6 |
-
|
7 |
-
|
8 |
-
from
|
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 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
from streamlit.components.v1 import html
|
5 |
+
|
6 |
+
# Set page config with light purple theme
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="Emotion Mirror Chatbot",
|
9 |
+
page_icon="π",
|
10 |
+
layout="centered",
|
11 |
+
initial_sidebar_state="collapsed"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Custom CSS for light purple theme
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
:root {
|
18 |
+
--primary: #b19cd9;
|
19 |
+
--background: #f5f0ff;
|
20 |
+
--secondary-background: #e6e0fa;
|
21 |
+
--text: #4a4a4a;
|
22 |
+
--font: "Arial", sans-serif;
|
23 |
+
}
|
24 |
+
|
25 |
+
body {
|
26 |
+
background-color: var(--background);
|
27 |
+
color: var(--text);
|
28 |
+
font-family: var(--font);
|
29 |
+
}
|
30 |
+
|
31 |
+
.stTextInput>div>div>input {
|
32 |
+
background-color: var(--secondary-background) !important;
|
33 |
+
color: var(--text) !important;
|
34 |
+
}
|
35 |
+
|
36 |
+
.stButton>button {
|
37 |
+
background-color: var(--primary) !important;
|
38 |
+
color: white !important;
|
39 |
+
border: none;
|
40 |
+
border-radius: 8px;
|
41 |
+
padding: 8px 16px;
|
42 |
+
}
|
43 |
+
|
44 |
+
.stMarkdown {
|
45 |
+
font-family: monospace !important;
|
46 |
+
font-size: 16px !important;
|
47 |
+
}
|
48 |
+
|
49 |
+
.chat-message {
|
50 |
+
padding: 12px;
|
51 |
+
border-radius: 12px;
|
52 |
+
margin: 8px 0;
|
53 |
+
max-width: 80%;
|
54 |
+
}
|
55 |
+
|
56 |
+
.user-message {
|
57 |
+
background-color: var(--secondary-background);
|
58 |
+
margin-left: auto;
|
59 |
+
text-align: right;
|
60 |
+
}
|
61 |
+
|
62 |
+
.bot-message {
|
63 |
+
background-color: var(--primary);
|
64 |
+
color: white;
|
65 |
+
margin-right: auto;
|
66 |
+
}
|
67 |
+
|
68 |
+
.face-container {
|
69 |
+
text-align: center;
|
70 |
+
padding: 20px;
|
71 |
+
background: white;
|
72 |
+
border-radius: 16px;
|
73 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
74 |
+
margin: 20px 0;
|
75 |
+
}
|
76 |
+
</style>
|
77 |
+
""", unsafe_allow_html=True)
|
78 |
+
|
79 |
+
# Emotion databases
|
80 |
+
POSITIVE_WORDS = {"happy", "awesome", "great", "joy", "excited", "good", "wonderful", "fantastic", "amazing"}
|
81 |
+
NEGATIVE_WORDS = {"sad", "depressed", "angry", "cry", "lonely", "bad", "terrible", "awful", "miserable"}
|
82 |
+
HELP_RESPONSES = [
|
83 |
+
"Would you like to talk about it? π¬",
|
84 |
+
"I'm here to listen π",
|
85 |
+
"Want some uplifting quotes? π",
|
86 |
+
"Would a virtual hug help? π€",
|
87 |
+
"Let's focus on something positive π"
|
88 |
+
]
|
89 |
+
|
90 |
+
# ASCII Art Library
|
91 |
+
FACES = {
|
92 |
+
"happy": r"""
|
93 |
+
ββββββββββ
|
94 |
+
π AWESOME!
|
95 |
+
ββββββββββ
|
96 |
+
""",
|
97 |
+
"sad": r"""
|
98 |
+
ββββββββββ
|
99 |
+
π’ SAD DAY?
|
100 |
+
ββββββββββ
|
101 |
+
""",
|
102 |
+
"neutral": r"""
|
103 |
+
ββββββββββ
|
104 |
+
π HELLO
|
105 |
+
ββββββββββ
|
106 |
+
""",
|
107 |
+
"love": r"""
|
108 |
+
ββββββββββ
|
109 |
+
π LOVELY!
|
110 |
+
ββββββββββ
|
111 |
+
"""
|
112 |
+
}
|
113 |
+
|
114 |
+
# Confetti effect using JavaScript
|
115 |
+
def confetti_effect():
|
116 |
+
confetti_js = """
|
117 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js"></script>
|
118 |
+
<script>
|
119 |
+
const count = 200;
|
120 |
+
const defaults = {
|
121 |
+
origin: { y: 0.7 }
|
122 |
+
};
|
123 |
+
|
124 |
+
function fire(particleRatio, opts) {
|
125 |
+
confetti(Object.assign({}, defaults, opts, {
|
126 |
+
particleCount: Math.floor(count * particleRatio)
|
127 |
+
}));
|
128 |
+
}
|
129 |
+
|
130 |
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
131 |
+
fire(0.2, { spread: 60 });
|
132 |
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
133 |
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
134 |
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
135 |
+
</script>
|
136 |
+
"""
|
137 |
+
html(confetti_js)
|
138 |
+
|
139 |
+
# Emotion detection function
|
140 |
+
def detect_emotion(text):
|
141 |
+
text = text.lower()
|
142 |
+
if any(word in text for word in POSITIVE_WORDS):
|
143 |
+
return "happy"
|
144 |
+
elif any(word in text for word in NEGATIVE_WORDS):
|
145 |
+
return "sad"
|
146 |
+
elif "love" in text or "heart" in text:
|
147 |
+
return "love"
|
148 |
+
return "neutral"
|
149 |
+
|
150 |
+
# Initialize chat history
|
151 |
+
if "messages" not in st.session_state:
|
152 |
+
st.session_state.messages = []
|
153 |
+
st.session_state.current_emotion = "neutral"
|
154 |
+
|
155 |
+
# Title and description
|
156 |
+
st.title("β¨ Emotion Mirror Chatbot")
|
157 |
+
st.markdown("I'm a reactive AI agent that mirrors your emotions! Try words like *happy*, *sad*, or *awesome*")
|
158 |
+
|
159 |
+
# Display current face
|
160 |
+
with st.container():
|
161 |
+
st.markdown(f"<div class='face-container'>\n{FACES[st.session_state.current_emotion]}\n</div>",
|
162 |
+
unsafe_allow_html=True)
|
163 |
+
|
164 |
+
# Display chat messages
|
165 |
+
for message in st.session_state.messages:
|
166 |
+
with st.chat_message(message["role"]):
|
167 |
+
st.markdown(f"<div class='chat-message {message['role']}-message'>{message['content']}</div>",
|
168 |
+
unsafe_allow_html=True)
|
169 |
+
|
170 |
+
# User input
|
171 |
+
if prompt := st.chat_input("How are you feeling today?"):
|
172 |
+
# Add user message to chat history
|
173 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
174 |
+
|
175 |
+
# Detect emotion
|
176 |
+
emotion = detect_emotion(prompt)
|
177 |
+
st.session_state.current_emotion = emotion
|
178 |
+
|
179 |
+
# Generate bot response
|
180 |
+
if emotion == "happy":
|
181 |
+
response = FACES["happy"] + "\n\nπ That's wonderful to hear!"
|
182 |
+
confetti_effect()
|
183 |
+
elif emotion == "sad":
|
184 |
+
response = FACES["sad"] + "\n\n" + random.choice(HELP_RESPONSES)
|
185 |
+
elif emotion == "love":
|
186 |
+
response = FACES["love"] + "\n\nπ Love is in the air!"
|
187 |
+
else:
|
188 |
+
response = FACES["neutral"] + "\n\nTell me more about your feelings..."
|
189 |
+
|
190 |
+
# Add bot response to chat history
|
191 |
+
st.session_state.messages.append({"role": "bot", "content": response})
|
192 |
+
|
193 |
+
# Rerun to update the display
|
194 |
+
st.experimental_rerun()
|
195 |
+
|
196 |
+
# Add reset button
|
197 |
+
if st.button("Reset Conversation"):
|
198 |
+
st.session_state.messages = []
|
199 |
+
st.session_state.current_emotion = "neutral"
|
200 |
+
st.experimental_rerun()
|