-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
354 lines (300 loc) Β· 13.7 KB
/
app.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import streamlit as st
import time
from datetime import datetime
import cv2
import numpy as np
from deepface import DeepFace
from components.todo import ToDo
from components.qa_generator import QAGenerator
from components.llm import OllamaLLM
from database_mongodb import EmotionDatabase
from logger import logger
import streamlit.components.v1 as components
import base64
def init_session_state():
if 'page' not in st.session_state:
st.session_state.page = 'welcome'
if 'current_emotion' not in st.session_state:
st.session_state.current_emotion = None
if 'last_emotion_check' not in st.session_state:
st.session_state.last_emotion_check = time.time()
if 'emotion_messages' not in st.session_state:
st.session_state.emotion_messages = []
if 'completed_tasks' not in st.session_state:
st.session_state.completed_tasks = set()
if 'qa_generator' not in st.session_state:
st.session_state.qa_generator = QAGenerator()
if 'todo' not in st.session_state:
st.session_state.todo = ToDo()
if 'emotion_db' not in st.session_state:
st.session_state.emotion_db = EmotionDatabase()
if 'llm' not in st.session_state:
st.session_state.llm = OllamaLLM()
if 'camera_on' not in st.session_state:
st.session_state.camera_on = True
def get_emotion_response(emotion: str) -> str:
"""Generate appropriate response based on detected emotion"""
emotion_prompts = {
'happy': "I notice you're in a great mood! Let's make the most of this positive energy. Would you like to tackle something challenging?",
'sad': "I see you might be feeling down. Remember to take breaks when needed. Would you like to try some quick breathing exercises?",
'angry': "I sense some frustration. Let's take a moment to reset. Maybe we could break this task into smaller, more manageable parts?",
'neutral': "Staying focused! Remember to take short breaks every 25 minutes to maintain your concentration.",
'fear': "Feeling anxious? That's normal when studying. Let's break this down into smaller steps to make it less overwhelming.",
'surprise': "Something caught your attention? Let's use that curiosity to dive deeper into the subject!",
'disgust': "Struggling with the material? Let's try approaching it from a different angle."
}
return emotion_prompts.get(emotion, "Keep going! You're making progress!")
def celebrate_completion():
"""Generate celebration message for task completion"""
celebrations = [
"π Fantastic job completing that task! You're making great progress!",
"π Another one down! You're really on a roll today!",
"πͺ Excellent work! Your dedication is paying off!",
"π Task completed! Keep up this amazing momentum!",
"β¨ Well done! You're getting closer to your goals!"
]
return np.random.choice(celebrations)
def capture_and_analyze_emotion():
"""Capture and analyze emotion, return appropriate response"""
current_time = time.time()
if (current_time - st.session_state.last_emotion_check) >= 30:
try:
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cap.release()
if ret:
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = DeepFace.analyze(frame_rgb, actions=['emotion'], enforce_detection=False)
emotion = result[0]['dominant_emotion']
if emotion != st.session_state.current_emotion:
st.session_state.current_emotion = emotion
response = get_emotion_response(emotion)
st.session_state.emotion_messages.append(response)
st.session_state.emotion_db.insert_emotion(emotion)
st.session_state.last_emotion_check = current_time
return emotion, frame_rgb
except Exception as e:
logger.error(f"Error in emotion capture: {e}")
return None, None
def study_session_page():
st.markdown("<h1 class='main-title'>Sidekick</h1>", unsafe_allow_html=True)
tasks_col, main_col, qa_col = st.columns([1,2,1])
with tasks_col:
st.markdown("### π Current Tasks")
current_tasks = st.session_state.todo.display_tasks()
if st.session_state.camera_on:
emotion, frame = capture_and_analyze_emotion()
if emotion and frame is not None:
st.image(frame, channels="RGB", use_container_width=True)
st.write(f"Current mood: {emotion}")
if st.session_state.emotion_messages:
with st.container():
st.markdown("### π Mood Support")
for msg in st.session_state.emotion_messages[-3:]:
st.info(msg)
for task in st.session_state.todo.get_tasks():
if task not in st.session_state.completed_tasks and task in current_tasks:
st.success(celebrate_completion())
st.session_state.completed_tasks.add(task)
with main_col:
st.markdown("### π‘ Study Assistant")
user_message = st.text_input("Ask me anything about your studies...")
if user_message:
with st.spinner("Thinking..."):
emotion_context = f"The student is feeling {st.session_state.current_emotion}. " if st.session_state.current_emotion else ""
response = st.session_state.llm._call(f"{emotion_context}{user_message}")
st.markdown(f"**Assistant:** {response}")
with qa_col:
st.markdown("### π€ Course Assistant")
input_type = st.selectbox("Select input type", ["Text", "PDF", "URL"])
if input_type == "Text":
user_input = st.text_area("Enter study material")
if st.button("Process"):
st.session_state.qa_generator.process_documents("Text", user_input)
elif input_type == "PDF":
uploaded_file = st.file_uploader("Upload PDF", type=['pdf'])
if uploaded_file and st.button("Process"):
st.session_state.qa_generator.process_documents("PDF", uploaded_file)
elif input_type == "URL":
url = st.text_input("Enter URL")
if st.button("Process"):
st.session_state.qa_generator.process_documents("URL", url)
question = st.text_input("Ask a question about your material")
if question and st.button("Get Answer"):
answer = st.session_state.qa_generator.create_response(question)
st.write("Answer:", answer)
def set_page_config():
st.set_page_config(
page_title="Sidekick - Study Vibes, No Jive",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded"
)
st.markdown("""
<style>
.main-title {
text-align: center;
font-size: 3em;
color: #1E88E5;
margin-bottom: 0;
padding-bottom: 0;
}
.tagline {
text-align: center;
font-size: 1.5em;
color: #64B5F6;
margin-top: 0;
padding-top: 0;
}
.stButton>button {
width: 100%;
border-radius: 20px;
height: 3em;
}
.task-container {
background-color: #f0f2f6;
padding: 20px;
border-radius: 10px;
margin: 10px 0;
}
.chat-container {
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
margin: 10px 0;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
</style>
""", unsafe_allow_html=True)
def change_page(new_page):
st.session_state.page = new_page
st.rerun()
def welcome_page():
st.markdown("<h1 class='main-title'>Sidekick</h1>", unsafe_allow_html=True)
st.markdown("<p class='tagline'>Study Vibes, No Jive</p>", unsafe_allow_html=True)
col1, col2, col3 = st.columns([1,2,1])
with col2:
st.markdown("""
<div style='text-align: center; padding: 50px;'>
<h2>π Hello! How are you today?</h2>
</div>
""", unsafe_allow_html=True)
feeling = st.selectbox("I'm feeling...",
["Select your mood", "Energetic π", "Ready to learn π", "Tired π΄", "Anxious π°", "Just okay π"])
if feeling != "Select your mood":
st.markdown("""
<div style='text-align: center; padding: 20px;'>
<h3>Let's make today productive! πͺ</h3>
</div>
""", unsafe_allow_html=True)
if st.button("Start My Study Session"):
change_page('goals')
def goals_page():
st.markdown("<h1 class='main-title'>Sidekick</h1>", unsafe_allow_html=True)
st.markdown("<p class='tagline'>Study Vibes, No Jive</p>", unsafe_allow_html=True)
st.markdown("### π― What do you want to achieve today?")
col1, col2 = st.columns([2,1])
with col1:
new_task = st.text_input("Enter your study goal")
if st.button("Add Goal"):
if new_task:
st.session_state.todo.add_task(new_task)
with col2:
if st.button("Let's Start Studying!", key="start_study"):
change_page('study_prep')
st.session_state.todo.display_tasks()
def generate_study_guide(task):
prompt = f"""Create a comprehensive study guide for: {task}
Include:
1. Key topics to cover
2. Suggested study approach
3. Recommended resources
4. Time management tips
"""
return st.session_state.llm._call(prompt)
def study_prep_page():
st.markdown("<h1 class='main-title'>Sidekick</h1>", unsafe_allow_html=True)
st.markdown("<p class='tagline'>Study Vibes, No Jive</p>", unsafe_allow_html=True)
st.markdown("### π Let's prepare your study session")
tasks = st.session_state.todo.get_tasks()
if tasks:
selected_task = st.selectbox("Which task would you like to focus on?", tasks)
if st.button("Generate Study Guide"):
with st.spinner("Creating your personalized study guide..."):
study_guide = generate_study_guide(selected_task)
st.markdown("### π Your Study Guide")
st.write(study_guide)
if st.button("Begin Study Session"):
st.session_state.page = 'study_session'
st.rerun()
else:
st.warning("Please add some study goals first!")
if st.button("Back to Goals"):
st.session_state.page = 'goals'
st.rerun()
def capture_emotion():
try:
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cap.release()
if ret:
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = DeepFace.analyze(frame_rgb, actions=['emotion'], enforce_detection=False)
emotion = result[0]['dominant_emotion']
st.session_state.emotion_db.insert_emotion(emotion)
return emotion, frame_rgb
except Exception as e:
logger.error(f"Error in emotion capture: {e}")
return None, None
def study_session_page():
st.markdown("<h1 class='main-title'>Sidekick</h1>", unsafe_allow_html=True)
tasks_col, main_col, qa_col = st.columns([1,2,1])
with tasks_col:
st.markdown("### π Tasks")
st.session_state.todo.display_tasks()
if st.button("Toggle Camera"):
st.session_state.camera_on = not st.session_state.camera_on
if st.session_state.camera_on:
emotion, frame = capture_emotion()
if emotion and frame is not None:
st.image(frame, channels="RGB", use_container_width=True)
st.write(f"Current emotion: {emotion}")
with main_col:
st.markdown("### π Study Chat")
user_message = st.text_input("Ask me anything about your studies...")
if user_message:
with st.spinner("Thinking..."):
response = st.session_state.llm._call(user_message)
st.markdown(f"**Assistant:** {response}")
with qa_col:
st.markdown("### π€ Course Assistant")
input_type = st.selectbox("Select input type", ["Text", "PDF", "URL"])
if input_type == "Text":
user_input = st.text_area("Enter study material")
if st.button("Process"):
st.session_state.qa_generator.process_documents("Text", user_input)
elif input_type == "PDF":
uploaded_file = st.file_uploader("Upload PDF", type=['pdf'])
if uploaded_file and st.button("Process"):
st.session_state.qa_generator.process_documents("PDF", uploaded_file)
elif input_type == "URL":
url = st.text_input("Enter URL")
if st.button("Process"):
st.session_state.qa_generator.process_documents("URL", url)
question = st.text_input("Ask a question about your material")
if question and st.button("Get Answer"):
answer = st.session_state.qa_generator.create_response(question)
st.write("Answer:", answer)
def main():
init_session_state()
set_page_config()
if st.session_state.page == 'welcome':
welcome_page()
elif st.session_state.page == 'goals':
goals_page()
elif st.session_state.page == 'study_prep':
study_prep_page()
elif st.session_state.page == 'study_session':
study_session_page()
if __name__ == "__main__":
main()