Johan713 commited on
Commit
a8e3b21
Β·
verified Β·
1 Parent(s): 8830460

Update pages/ai_buddy.py

Browse files
Files changed (1) hide show
  1. pages/ai_buddy.py +659 -660
pages/ai_buddy.py CHANGED
@@ -1,661 +1,660 @@
1
- import streamlit as st
2
- import random
3
- from langchain.chat_models import ChatOpenAI
4
- from langchain.schema import HumanMessage, SystemMessage
5
- import os
6
- from dotenv import load_dotenv
7
- import pandas as pd
8
- from datetime import datetime
9
- import plotly.express as px
10
- import json
11
- import tempfile
12
- import time
13
- import numpy as np
14
- import threading
15
- from playsound import playsound
16
- import pygame
17
- import sounddevice as sd
18
- from scipy.io import wavfile
19
-
20
- pygame.mixer.init()
21
-
22
- # Load environment variables
23
- load_dotenv()
24
-
25
- AI71_BASE_URL = "https://api.ai71.ai/v1/"
26
- AI71_API_KEY = os.getenv('AI71_API_KEY')
27
-
28
- # Initialize the Falcon model
29
- chat = ChatOpenAI(
30
- model="tiiuae/falcon-180B-chat",
31
- api_key=AI71_API_KEY,
32
- base_url=AI71_BASE_URL,
33
- streaming=True,
34
- )
35
-
36
- # Expanded Therapy techniques
37
- THERAPY_TECHNIQUES = {
38
- "CBT": "Use Cognitive Behavioral Therapy techniques to help the user identify and change negative thought patterns.",
39
- "Mindfulness": "Guide the user through mindfulness exercises to promote present-moment awareness and reduce stress.",
40
- "Solution-Focused": "Focus on the user's strengths and resources to help them find solutions to their problems.",
41
- "Emotion-Focused": "Help the user identify, experience, and regulate their emotions more effectively.",
42
- "Psychodynamic": "Explore the user's past experiences and unconscious patterns to gain insight into current issues.",
43
- "ACT": "Use Acceptance and Commitment Therapy to help the user accept their thoughts and feelings while committing to positive changes.",
44
- "DBT": "Apply Dialectical Behavior Therapy techniques to help the user manage intense emotions and improve relationships.",
45
- "Gestalt": "Use Gestalt therapy techniques to focus on the present moment and increase self-awareness.",
46
- "Existential": "Explore existential themes such as meaning, freedom, and responsibility to help the user find purpose.",
47
- "Narrative": "Use storytelling and narrative techniques to help the user reframe their life experiences and create new meaning.",
48
- }
49
-
50
- def get_ai_response(user_input, buddy_config, therapy_technique=None):
51
- system_message = f"You are {buddy_config['name']}, an AI companion with the following personality: {buddy_config['personality']}. "
52
- system_message += f"Additional details about you: {buddy_config['details']}. "
53
-
54
- if therapy_technique:
55
- system_message += f"In this conversation, {THERAPY_TECHNIQUES[therapy_technique]}"
56
-
57
- messages = [
58
- SystemMessage(content=system_message),
59
- HumanMessage(content=user_input)
60
- ]
61
- response = chat.invoke(messages).content
62
- return response
63
-
64
- def play_sound_loop(sound_file, stop_event):
65
- while not stop_event.is_set():
66
- playsound(sound_file)
67
-
68
- def play_sound_for_duration(sound_file, duration):
69
- start_time = time.time()
70
- while time.time() - start_time < duration:
71
- playsound(sound_file, block=False)
72
- time.sleep(0.1) # Short sleep to prevent excessive CPU usage
73
- # Ensure the sound stops after the duration
74
- pygame.mixer.quit()
75
-
76
- def get_sound_files(directory):
77
- return [f for f in os.listdir(directory) if f.endswith('.mp3')]
78
-
79
- def get_sound_file_path(sound_name, sound_dir):
80
- # Convert the sound name to a filename
81
- filename = f"{sound_name.lower().replace(' ', '_')}.mp3"
82
- return os.path.join(sound_dir, filename)
83
-
84
- SOUND_OPTIONS = [
85
- "Gentle Rain", "Ocean Waves", "Forest Ambience", "Soft Wind Chimes",
86
- "Tibetan Singing Bowls", "Humming Song", "Crackling Fireplace",
87
- "Birdsong", "White Noise", "Zen River", "Heartbeat", "Deep Space",
88
- "Whale Songs", "Bamboo Flute", "Thunderstorm", "Cat Purring",
89
- "Campfire", "Windchimes", "Waterfall", "Beach Waves", "Cicadas",
90
- "Coffee Shop Ambience", "Grandfather Clock", "Rainstorm on Tent",
91
- "Tropical Birds", "Subway Train", "Washing Machine", "Fan White Noise",
92
- "Tibetan Bells", "Wind in Trees", "Meditation Bowl", "Meditation Bowl2", "Birds Singing Rainy Day"
93
- ]
94
-
95
- def show_meditation_timer():
96
- st.subheader("πŸ§˜β€β™€οΈ Enhanced Meditation Timer")
97
-
98
- sound_dir = os.path.join(os.path.dirname(__file__), "..", "sounds")
99
-
100
- col1, col2 = st.columns(2)
101
-
102
- with col1:
103
- duration = st.slider("Select duration (minutes)", 1, 60, 5)
104
- background_sound = st.selectbox("Background Sound", SOUND_OPTIONS)
105
-
106
- with col2:
107
- interval_options = ["None", "Every 5 minutes", "Every 10 minutes"]
108
- interval_reminder = st.selectbox("Interval Reminders", interval_options)
109
- end_sound = st.selectbox("End of Session Sound", SOUND_OPTIONS)
110
-
111
- if st.button("Start Meditation", key="start_meditation"):
112
- progress_bar = st.progress(0)
113
- status_text = st.empty()
114
-
115
- # Initialize pygame mixer
116
- pygame.mixer.init()
117
-
118
- # Load background sound
119
- background_sound_file = get_sound_file_path(background_sound, sound_dir)
120
- if not os.path.exists(background_sound_file):
121
- st.error(f"Background sound file not found: {background_sound_file}")
122
- return
123
-
124
- # Load end of session sound
125
- end_sound_file = get_sound_file_path(end_sound, sound_dir)
126
- if not os.path.exists(end_sound_file):
127
- st.error(f"End sound file not found: {end_sound_file}")
128
- return
129
-
130
- # Play background sound on loop
131
- pygame.mixer.music.load(background_sound_file)
132
- pygame.mixer.music.play(-1) # -1 means loop indefinitely
133
-
134
- start_time = time.time()
135
- end_time = start_time + (duration * 60)
136
-
137
- try:
138
- while time.time() < end_time:
139
- elapsed_time = time.time() - start_time
140
- progress = elapsed_time / (duration * 60)
141
- progress_bar.progress(progress)
142
-
143
- remaining_time = end_time - time.time()
144
- mins, secs = divmod(int(remaining_time), 60)
145
- status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
146
-
147
- if interval_reminder != "None":
148
- interval = 5 if interval_reminder == "Every 5 minutes" else 10
149
- if int(elapsed_time) > 0 and int(elapsed_time) % (interval * 60) == 0:
150
- st.toast(f"{interval} minutes passed", icon="⏰")
151
-
152
- # Check if 10 seconds remaining
153
- if remaining_time <= 10 and remaining_time > 9:
154
- pygame.mixer.music.stop() # Stop background sound
155
- pygame.mixer.Sound(end_sound_file).play() # Play end sound
156
-
157
- if remaining_time <= 0:
158
- break
159
-
160
- time.sleep(0.1) # Update more frequently for smoother countdown
161
- finally:
162
- # Stop all sounds
163
- pygame.mixer.quit()
164
-
165
- # Ensure the progress bar is full and time remaining shows 00:00
166
- progress_bar.progress(1.0)
167
- status_text.text("Time remaining: 00:00")
168
-
169
- st.success("Meditation complete!")
170
- st.balloons()
171
-
172
- if 'achievements' not in st.session_state:
173
- st.session_state.achievements = set()
174
- st.session_state.achievements.add("Zen Master")
175
- st.success("Achievement Unlocked: Zen Master πŸ§˜β€β™€οΈ")
176
-
177
- def show_personalized_recommendations():
178
- st.subheader("🎯 Personalized Recommendations")
179
-
180
- recommendation_categories = [
181
- "Mental Health",
182
- "Physical Health",
183
- "Personal Development",
184
- "Relationships",
185
- "Career",
186
- "Hobbies",
187
- ]
188
-
189
- selected_category = st.selectbox("Choose a category", recommendation_categories)
190
-
191
- recommendations = {
192
- "Mental Health": [
193
- "Practice daily gratitude journaling",
194
- "Try a guided meditation for stress relief",
195
- "Explore cognitive behavioral therapy techniques",
196
- "Start a mood tracking journal",
197
- "Learn about mindfulness practices",
198
- ],
199
- "Physical Health": [
200
- "Start a 30-day yoga challenge",
201
- "Try intermittent fasting",
202
- "Begin a couch to 5K running program",
203
- "Experiment with new healthy recipes",
204
- "Create a sleep hygiene routine",
205
- ],
206
- "Personal Development": [
207
- "Start learning a new language",
208
- "Read personal development books",
209
- "Take an online course in a subject you're interested in",
210
- "Practice public speaking",
211
- "Start a daily writing habit",
212
- ],
213
- "Relationships": [
214
- "Practice active listening techniques",
215
- "Plan regular date nights or friend meetups",
216
- "Learn about love languages",
217
- "Practice expressing gratitude to loved ones",
218
- "Join a local community or interest group",
219
- ],
220
- "Career": [
221
- "Update your resume and LinkedIn profile",
222
- "Network with professionals in your industry",
223
- "Set SMART career goals",
224
- "Learn a new skill relevant to your field",
225
- "Start a side project or freelance work",
226
- ],
227
- "Hobbies": [
228
- "Start a garden or learn about plant care",
229
- "Try a new art form like painting or sculpting",
230
- "Learn to play a musical instrument",
231
- "Start a DIY home improvement project",
232
- "Explore photography or videography",
233
- ],
234
- }
235
-
236
- st.write("Here are some personalized recommendations for you:")
237
- for recommendation in recommendations[selected_category]:
238
- st.markdown(f"- {recommendation}")
239
-
240
- if st.button("Get More Recommendations"):
241
- st.write("More tailored recommendations:")
242
- additional_recs = random.sample(recommendations[selected_category], 3)
243
- for rec in additional_recs:
244
- st.markdown(f"- {rec}")
245
-
246
- def generate_binaural_beat(freq1, freq2, duration_seconds, sample_rate=44100):
247
- t = np.linspace(0, duration_seconds, int(sample_rate * duration_seconds), False)
248
- left_channel = np.sin(2 * np.pi * freq1 * t)
249
- right_channel = np.sin(2 * np.pi * freq2 * t)
250
- stereo_audio = np.vstack((left_channel, right_channel)).T
251
- return (stereo_audio * 32767).astype(np.int16)
252
-
253
- def get_binary_file_downloader_html(bin_file, file_label='File'):
254
- b64 = base64.b64encode(bin_file).decode()
255
- return f'<a href="data:application/octet-stream;base64,{b64}" download="{file_label}.wav" class="download-link">Download {file_label}</a>'
256
-
257
- def show_binaural_beats():
258
- st.subheader("🎡 Binaural Beats Generator")
259
-
260
- st.markdown("""
261
- <style>
262
- .stButton>button {
263
- background-color: #4CAF50;
264
- color: white;
265
- font-weight: bold;
266
- }
267
- .download-link {
268
- background-color: #008CBA;
269
- color: white;
270
- padding: 10px 15px;
271
- text-align: center;
272
- text-decoration: none;
273
- display: inline-block;
274
- font-size: 16px;
275
- margin: 4px 2px;
276
- cursor: pointer;
277
- border-radius: 4px;
278
- }
279
- .stop-button {
280
- background-color: #f44336;
281
- color: white;
282
- font-weight: bold;
283
- }
284
- </style>
285
- """, unsafe_allow_html=True)
286
-
287
- st.write("Binaural beats are created when two slightly different frequencies are played in each ear, potentially influencing brainwave activity.")
288
-
289
- preset_beats = {
290
- "Deep Relaxation (Delta)": {"base": 100, "beat": 2},
291
- "Meditation (Theta)": {"base": 150, "beat": 6},
292
- "Relaxation (Alpha)": {"base": 200, "beat": 10},
293
- "Light Focus (Low Beta)": {"base": 250, "beat": 14},
294
- "High Focus (Mid Beta)": {"base": 300, "beat": 20},
295
- "Alertness (High Beta)": {"base": 350, "beat": 30},
296
- "Gamma Consciousness": {"base": 400, "beat": 40},
297
- "Lucid Dreaming": {"base": 180, "beat": 3},
298
- "Memory Enhancement": {"base": 270, "beat": 12},
299
- "Creativity Boost": {"base": 220, "beat": 8},
300
- "Pain Relief": {"base": 130, "beat": 4},
301
- "Mood Elevation": {"base": 315, "beat": 18}
302
- }
303
-
304
- col1, col2 = st.columns(2)
305
-
306
- with col1:
307
- beat_type = st.selectbox("Choose a preset or custom:", ["Custom"] + list(preset_beats.keys()))
308
-
309
- with col2:
310
- duration = st.slider("Duration (minutes):", 1, 60, 15)
311
-
312
- if beat_type == "Custom":
313
- col3, col4 = st.columns(2)
314
- with col3:
315
- base_freq = st.slider("Base Frequency (Hz):", 100, 500, 200)
316
- with col4:
317
- beat_freq = st.slider("Desired Beat Frequency (Hz):", 1, 40, 10)
318
- else:
319
- base_freq = preset_beats[beat_type]["base"]
320
- beat_freq = preset_beats[beat_type]["beat"]
321
- st.info(f"Base Frequency: {base_freq} Hz, Beat Frequency: {beat_freq} Hz")
322
-
323
- if 'audio_playing' not in st.session_state:
324
- st.session_state.audio_playing = False
325
-
326
- if 'start_time' not in st.session_state:
327
- st.session_state.start_time = None
328
-
329
- if 'end_time' not in st.session_state:
330
- st.session_state.end_time = None
331
-
332
- # Create persistent placeholders for UI elements
333
- progress_bar = st.empty()
334
- status_text = st.empty()
335
- stop_button = st.empty()
336
-
337
- generate_button = st.button("Generate and Play Binaural Beat")
338
-
339
- if generate_button:
340
- try:
341
- # Stop any currently playing audio
342
- if st.session_state.audio_playing:
343
- pygame.mixer.music.stop()
344
- st.session_state.audio_playing = False
345
-
346
- audio_data = generate_binaural_beat(base_freq, base_freq + beat_freq, duration * 60)
347
-
348
- # Save the generated audio to a temporary file
349
- with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file:
350
- temp_filename = temp_file.name
351
- wavfile.write(temp_filename, 44100, audio_data)
352
-
353
- # Initialize pygame mixer
354
- pygame.mixer.init(frequency=44100, size=-16, channels=2)
355
-
356
- # Load and play the audio
357
- pygame.mixer.music.load(temp_filename)
358
- pygame.mixer.music.play()
359
- st.session_state.audio_playing = True
360
-
361
- st.session_state.start_time = time.time()
362
- st.session_state.end_time = st.session_state.start_time + (duration * 60)
363
-
364
- except Exception as e:
365
- st.error(f"An error occurred: {str(e)}")
366
- st.info("Please try again or contact support if the issue persists.")
367
-
368
- if st.session_state.audio_playing:
369
- stop_button_active = stop_button.button("Stop Binaural Beat", key="stop_binaural", type="primary")
370
- current_time = time.time()
371
-
372
- if stop_button_active:
373
- pygame.mixer.music.stop()
374
- st.session_state.audio_playing = False
375
- st.session_state.start_time = None
376
- st.session_state.end_time = None
377
-
378
- elif current_time < st.session_state.end_time:
379
- elapsed_time = current_time - st.session_state.start_time
380
- progress = elapsed_time / (st.session_state.end_time - st.session_state.start_time)
381
- progress_bar.progress(progress)
382
-
383
- remaining_time = st.session_state.end_time - current_time
384
- mins, secs = divmod(int(remaining_time), 60)
385
- status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
386
- else:
387
- pygame.mixer.music.stop()
388
- st.session_state.audio_playing = False
389
- st.session_state.start_time = None
390
- st.session_state.end_time = None
391
- progress_bar.empty()
392
- status_text.text("Binaural beat session complete!")
393
-
394
- # Offer download of the generated audio
395
- if not st.session_state.audio_playing and 'audio_data' in locals():
396
- with io.BytesIO() as buffer:
397
- wavfile.write(buffer, 44100, audio_data)
398
- st.markdown(get_binary_file_downloader_html(buffer.getvalue(), f"binaural_beat_{base_freq}_{beat_freq}Hz"), unsafe_allow_html=True)
399
-
400
- # Ensure the app updates every second
401
- if st.session_state.audio_playing:
402
- time.sleep(1)
403
- st.experimental_rerun()
404
-
405
- def main():
406
- st.set_page_config(page_title="S.H.E.R.L.O.C.K. AI Buddy", page_icon="πŸ•΅οΈ", layout="wide")
407
-
408
- # Custom CSS for improved styling
409
- st.markdown("""
410
- <style>
411
- .stApp {
412
- background-color: #f0f2f6;
413
- }
414
- .stButton>button {
415
- background-color: #4CAF50;
416
- color: white;
417
- font-weight: bold;
418
- }
419
- .stTextInput>div>div>input {
420
- background-color: #ffffff;
421
- }
422
- </style>
423
- """, unsafe_allow_html=True)
424
-
425
- st.title("πŸ•΅οΈ S.H.E.R.L.O.C.K. AI Buddy")
426
- st.markdown("Your personalized AI companion for conversation, therapy, and personal growth.")
427
-
428
- # Initialize session state
429
- if 'buddy_name' not in st.session_state:
430
- st.session_state.buddy_name = "Sherlock"
431
- if 'buddy_personality' not in st.session_state:
432
- st.session_state.buddy_personality = "Friendly, empathetic, and insightful"
433
- if 'buddy_details' not in st.session_state:
434
- st.session_state.buddy_details = "Knowledgeable about various therapy techniques and always ready to listen"
435
- if 'messages' not in st.session_state:
436
- st.session_state.messages = []
437
-
438
- # Sidebar for AI Buddy configuration and additional features
439
- with st.sidebar:
440
- st.header("πŸ€– Configure Your AI Buddy")
441
- st.session_state.buddy_name = st.text_input("Name your AI Buddy", value=st.session_state.buddy_name)
442
- st.session_state.buddy_personality = st.text_area("Describe your buddy's personality", value=st.session_state.buddy_personality)
443
- st.session_state.buddy_details = st.text_area("Additional details about your buddy", value=st.session_state.buddy_details)
444
-
445
- st.header("🧘 Therapy Session")
446
- therapy_mode = st.checkbox("Enable Therapy Mode")
447
- if therapy_mode:
448
- therapy_technique = st.selectbox("Select Therapy Technique", list(THERAPY_TECHNIQUES.keys()))
449
- else:
450
- therapy_technique = None
451
-
452
- st.markdown("---")
453
-
454
- st.subheader("πŸ“‹ Todo List")
455
- if 'todos' not in st.session_state:
456
- st.session_state.todos = []
457
-
458
- new_todo = st.text_input("Add a new todo:")
459
- if st.button("Add Todo", key="add_todo"):
460
- if new_todo:
461
- st.session_state.todos.append({"task": new_todo, "completed": False})
462
- st.success("Todo added successfully!")
463
- else:
464
- st.warning("Please enter a todo item.")
465
-
466
- for i, todo in enumerate(st.session_state.todos):
467
- col1, col2, col3 = st.columns([0.05, 0.8, 0.15])
468
- with col1:
469
- todo['completed'] = st.checkbox("", todo['completed'], key=f"todo_{i}")
470
- with col2:
471
- st.write(todo['task'], key=f"todo_text_{i}")
472
- with col3:
473
- if st.button("πŸ—‘οΈ", key=f"delete_{i}", help="Delete todo"):
474
- st.session_state.todos.pop(i)
475
- st.experimental_rerun()
476
-
477
- st.subheader("⏱️ Pomodoro Timer")
478
- pomodoro_duration = st.slider("Pomodoro Duration (minutes)", 1, 60, 25)
479
- if st.button("Start Pomodoro"):
480
- progress_bar = st.progress(0)
481
- for i in range(pomodoro_duration * 60):
482
- time.sleep(1)
483
- progress_bar.progress((i + 1) / (pomodoro_duration * 60))
484
- st.success("Pomodoro completed!")
485
- if 'achievements' not in st.session_state:
486
- st.session_state.achievements = set()
487
- st.session_state.achievements.add("Consistent Learner")
488
-
489
- st.markdown("---")
490
- st.markdown("Powered by Falcon-180B and Streamlit")
491
-
492
- st.markdown("---")
493
- st.header("πŸ“” Daily Journal")
494
- journal_entry = st.text_area("Write your thoughts for today")
495
- if st.button("Save Journal Entry"):
496
- if 'journal_entries' not in st.session_state:
497
- st.session_state.journal_entries = []
498
- st.session_state.journal_entries.append({
499
- 'date': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
500
- 'entry': journal_entry
501
- })
502
- st.success("Journal entry saved!")
503
- st.toast("Journal entry saved successfully!", icon="βœ…")
504
-
505
- if 'journal_entries' in st.session_state and st.session_state.journal_entries:
506
- st.subheader("Previous Entries")
507
- for entry in st.session_state.journal_entries[-5:]: # Show last 5 entries
508
- st.text(entry['date'])
509
- st.write(entry['entry'])
510
- st.markdown("---")
511
-
512
- # Main content area
513
- tab1, tab2 = st.tabs(["Chat", "Tools"])
514
-
515
- with tab1:
516
- # Chat interface
517
- st.header("πŸ—¨οΈ Chat with Your AI Buddy")
518
-
519
- # Display chat history
520
- chat_container = st.container()
521
- with chat_container:
522
- for message in st.session_state.messages:
523
- with st.chat_message(message["role"]):
524
- st.markdown(message["content"])
525
-
526
- # User input
527
- prompt = st.chat_input("What's on your mind?")
528
-
529
- # Clear chat history button
530
- if st.button("Clear Chat History"):
531
- st.session_state.messages = []
532
- st.experimental_rerun()
533
-
534
- if prompt:
535
- st.session_state.messages.append({"role": "user", "content": prompt})
536
- with st.chat_message("user"):
537
- st.markdown(prompt)
538
-
539
- buddy_config = {
540
- "name": st.session_state.buddy_name,
541
- "personality": st.session_state.buddy_personality,
542
- "details": st.session_state.buddy_details
543
- }
544
-
545
- with st.chat_message("assistant"):
546
- message_placeholder = st.empty()
547
- full_response = ""
548
- for chunk in chat.stream(get_ai_response(prompt, buddy_config, therapy_technique)):
549
- full_response += chunk.content
550
- message_placeholder.markdown(full_response + "β–Œ")
551
- message_placeholder.markdown(full_response)
552
- st.session_state.messages.append({"role": "assistant", "content": full_response})
553
-
554
- # Force a rerun to update the chat history immediately
555
- st.experimental_rerun()
556
-
557
- with tab2:
558
- tool_choice = st.selectbox("Select a tool", ["Meditation Timer", "Binaural Beats", "Recommendations"])
559
- if tool_choice == "Meditation Timer":
560
- show_meditation_timer()
561
- elif tool_choice == "Recommendations":
562
- show_personalized_recommendations()
563
- elif tool_choice == "Binaural Beats":
564
- show_binaural_beats()
565
-
566
- # Mood tracker
567
- st.sidebar.markdown("---")
568
- st.sidebar.header("😊 Mood Tracker")
569
- mood = st.sidebar.slider("How are you feeling today?", 1, 10, 5)
570
- if st.sidebar.button("Log Mood"):
571
- st.sidebar.success(f"Mood logged: {mood}/10")
572
- st.balloons()
573
-
574
- # Resources and Emergency Contact
575
- st.sidebar.markdown("---")
576
- st.sidebar.header("πŸ†˜ Resources")
577
- st.sidebar.info("If you're in crisis, please reach out for help:")
578
- st.sidebar.markdown("- [Mental Health Resources](https://www.mentalhealth.gov/get-help/immediate-help)")
579
- st.sidebar.markdown("- Emergency Contact: 911 or your local emergency number")
580
-
581
- # Inspiration Quote
582
- st.sidebar.markdown("---")
583
- st.sidebar.header("πŸ’‘ Daily Inspiration")
584
- if st.sidebar.button("Get Inspirational Quote"):
585
- quotes = [
586
- "The only way to do great work is to love what you do. - Steve Jobs",
587
- "Believe you can and you're halfway there. - Theodore Roosevelt",
588
- "The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt",
589
- "Strive not to be a success, but rather to be of value. - Albert Einstein",
590
- "The only limit to our realization of tomorrow will be our doubts of today. - Franklin D. Roosevelt",
591
- "Do not wait to strike till the iron is hot; but make it hot by striking. - William Butler Yeats",
592
- "What lies behind us and what lies before us are tiny matters compared to what lies within us. - Ralph Waldo Emerson",
593
- "Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
594
- "Life is what happens when you're busy making other plans. - John Lennon",
595
- "You miss 100% of the shots you don't take. - Wayne Gretzky",
596
- "The best way to predict the future is to create it. - Peter Drucker",
597
- "It is not the strongest of the species that survive, nor the most intelligent, but the one most responsive to change. - Charles Darwin",
598
- "Whether you think you can or you think you can't, you're right. - Henry Ford",
599
- "The only place where success comes before work is in the dictionary. - Vidal Sassoon",
600
- "Do what you can, with what you have, where you are. - Theodore Roosevelt",
601
- "The purpose of our lives is to be happy. - Dalai Lama",
602
- "Success usually comes to those who are too busy to be looking for it. - Henry David Thoreau",
603
- "Your time is limited, so don't waste it living someone else's life. - Steve Jobs",
604
- "Don't be afraid to give up the good to go for the great. - John D. Rockefeller",
605
- "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
606
- "Success is not the key to happiness. Happiness is the key to success. - Albert Schweitzer",
607
- "It does not matter how slowly you go, as long as you do not stop. - Confucius",
608
- "If you set your goals ridiculously high and it's a failure, you will fail above everyone else's success. - James Cameron",
609
- "Don't watch the clock; do what it does. Keep going. - Sam Levenson",
610
- "Hardships often prepare ordinary people for an extraordinary destiny. - C.S. Lewis",
611
- "Don't count the days, make the days count. - Muhammad Ali",
612
- "The best revenge is massive success. - Frank Sinatra",
613
- "The only impossible journey is the one you never begin. - Tony Robbins",
614
- "Act as if what you do makes a difference. It does. - William James",
615
- "You are never too old to set another goal or to dream a new dream. - C.S. Lewis",
616
- "If you're going through hell, keep going. - Winston Churchill",
617
- "Dream big and dare to fail. - Norman Vaughan",
618
- "In the middle of every difficulty lies opportunity. - Albert Einstein",
619
- "What we achieve inwardly will change outer reality. - Plutarch",
620
- "I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison",
621
- "It always seems impossible until it's done. - Nelson Mandela",
622
- "The future depends on what you do today. - Mahatma Gandhi",
623
- "Don't wait. The time will never be just right. - Napoleon Hill",
624
- "Quality is not an act, it is a habit. - Aristotle",
625
- "Your life does not get better by chance, it gets better by change. - Jim Rohn",
626
- "The only thing standing between you and your goal is the story you keep telling yourself as to why you can't achieve it. - Jordan Belfort",
627
- "Challenges are what make life interesting; overcoming them is what makes life meaningful. - Joshua J. Marine",
628
- "Opportunities don't happen, you create them. - Chris Grosser",
629
- "I can't change the direction of the wind, but I can adjust my sails to always reach my destination. - Jimmy Dean",
630
- "Start where you are. Use what you have. Do what you can. - Arthur Ashe",
631
- "The secret of getting ahead is getting started. - Mark Twain",
632
- "You don’t have to be great to start, but you have to start to be great. - Zig Ziglar",
633
- "Keep your eyes on the stars, and your feet on the ground. - Theodore Roosevelt",
634
- "The only way to achieve the impossible is to believe it is possible. - Charles Kingsleigh"
635
- ]
636
-
637
- random_quote = random.choice(quotes)
638
- st.sidebar.success(random_quote)
639
-
640
- # Chat Export
641
- st.sidebar.markdown("---")
642
- if st.sidebar.button("Export Chat History"):
643
- chat_history = "\n".join([f"{msg['role']}: {msg['content']}" for msg in st.session_state.messages])
644
- st.sidebar.download_button(
645
- label="Download Chat History",
646
- data=chat_history,
647
- file_name="ai_buddy_chat_history.txt",
648
- mime="text/plain"
649
- )
650
-
651
- st.sidebar.success("Chat history ready for download!")
652
-
653
- # Display achievements
654
- if 'achievements' in st.session_state and st.session_state.achievements:
655
- st.sidebar.markdown("---")
656
- st.sidebar.header("πŸ† Achievements")
657
- for achievement in st.session_state.achievements:
658
- st.sidebar.success(f"Unlocked: {achievement}")
659
-
660
- if __name__ == "__main__":
661
  main()
 
1
+ import streamlit as st
2
+ import random
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.schema import HumanMessage, SystemMessage
5
+ import os
6
+ from dotenv import load_dotenv
7
+ import pandas as pd
8
+ from datetime import datetime
9
+ import plotly.express as px
10
+ import json
11
+ import tempfile
12
+ import time
13
+ import numpy as np
14
+ import threading
15
+ from playsound import playsound
16
+ import pygame
17
+ from scipy.io import wavfile
18
+
19
+ pygame.mixer.init()
20
+
21
+ # Load environment variables
22
+ load_dotenv()
23
+
24
+ AI71_BASE_URL = "https://api.ai71.ai/v1/"
25
+ AI71_API_KEY = os.getenv('AI71_API_KEY')
26
+
27
+ # Initialize the Falcon model
28
+ chat = ChatOpenAI(
29
+ model="tiiuae/falcon-180B-chat",
30
+ api_key=AI71_API_KEY,
31
+ base_url=AI71_BASE_URL,
32
+ streaming=True,
33
+ )
34
+
35
+ # Expanded Therapy techniques
36
+ THERAPY_TECHNIQUES = {
37
+ "CBT": "Use Cognitive Behavioral Therapy techniques to help the user identify and change negative thought patterns.",
38
+ "Mindfulness": "Guide the user through mindfulness exercises to promote present-moment awareness and reduce stress.",
39
+ "Solution-Focused": "Focus on the user's strengths and resources to help them find solutions to their problems.",
40
+ "Emotion-Focused": "Help the user identify, experience, and regulate their emotions more effectively.",
41
+ "Psychodynamic": "Explore the user's past experiences and unconscious patterns to gain insight into current issues.",
42
+ "ACT": "Use Acceptance and Commitment Therapy to help the user accept their thoughts and feelings while committing to positive changes.",
43
+ "DBT": "Apply Dialectical Behavior Therapy techniques to help the user manage intense emotions and improve relationships.",
44
+ "Gestalt": "Use Gestalt therapy techniques to focus on the present moment and increase self-awareness.",
45
+ "Existential": "Explore existential themes such as meaning, freedom, and responsibility to help the user find purpose.",
46
+ "Narrative": "Use storytelling and narrative techniques to help the user reframe their life experiences and create new meaning.",
47
+ }
48
+
49
+ def get_ai_response(user_input, buddy_config, therapy_technique=None):
50
+ system_message = f"You are {buddy_config['name']}, an AI companion with the following personality: {buddy_config['personality']}. "
51
+ system_message += f"Additional details about you: {buddy_config['details']}. "
52
+
53
+ if therapy_technique:
54
+ system_message += f"In this conversation, {THERAPY_TECHNIQUES[therapy_technique]}"
55
+
56
+ messages = [
57
+ SystemMessage(content=system_message),
58
+ HumanMessage(content=user_input)
59
+ ]
60
+ response = chat.invoke(messages).content
61
+ return response
62
+
63
+ def play_sound_loop(sound_file, stop_event):
64
+ while not stop_event.is_set():
65
+ playsound(sound_file)
66
+
67
+ def play_sound_for_duration(sound_file, duration):
68
+ start_time = time.time()
69
+ while time.time() - start_time < duration:
70
+ playsound(sound_file, block=False)
71
+ time.sleep(0.1) # Short sleep to prevent excessive CPU usage
72
+ # Ensure the sound stops after the duration
73
+ pygame.mixer.quit()
74
+
75
+ def get_sound_files(directory):
76
+ return [f for f in os.listdir(directory) if f.endswith('.mp3')]
77
+
78
+ def get_sound_file_path(sound_name, sound_dir):
79
+ # Convert the sound name to a filename
80
+ filename = f"{sound_name.lower().replace(' ', '_')}.mp3"
81
+ return os.path.join(sound_dir, filename)
82
+
83
+ SOUND_OPTIONS = [
84
+ "Gentle Rain", "Ocean Waves", "Forest Ambience", "Soft Wind Chimes",
85
+ "Tibetan Singing Bowls", "Humming Song", "Crackling Fireplace",
86
+ "Birdsong", "White Noise", "Zen River", "Heartbeat", "Deep Space",
87
+ "Whale Songs", "Bamboo Flute", "Thunderstorm", "Cat Purring",
88
+ "Campfire", "Windchimes", "Waterfall", "Beach Waves", "Cicadas",
89
+ "Coffee Shop Ambience", "Grandfather Clock", "Rainstorm on Tent",
90
+ "Tropical Birds", "Subway Train", "Washing Machine", "Fan White Noise",
91
+ "Tibetan Bells", "Wind in Trees", "Meditation Bowl", "Meditation Bowl2", "Birds Singing Rainy Day"
92
+ ]
93
+
94
+ def show_meditation_timer():
95
+ st.subheader("πŸ§˜β€β™€οΈ Enhanced Meditation Timer")
96
+
97
+ sound_dir = os.path.join(os.path.dirname(__file__), "..", "sounds")
98
+
99
+ col1, col2 = st.columns(2)
100
+
101
+ with col1:
102
+ duration = st.slider("Select duration (minutes)", 1, 60, 5)
103
+ background_sound = st.selectbox("Background Sound", SOUND_OPTIONS)
104
+
105
+ with col2:
106
+ interval_options = ["None", "Every 5 minutes", "Every 10 minutes"]
107
+ interval_reminder = st.selectbox("Interval Reminders", interval_options)
108
+ end_sound = st.selectbox("End of Session Sound", SOUND_OPTIONS)
109
+
110
+ if st.button("Start Meditation", key="start_meditation"):
111
+ progress_bar = st.progress(0)
112
+ status_text = st.empty()
113
+
114
+ # Initialize pygame mixer
115
+ pygame.mixer.init()
116
+
117
+ # Load background sound
118
+ background_sound_file = get_sound_file_path(background_sound, sound_dir)
119
+ if not os.path.exists(background_sound_file):
120
+ st.error(f"Background sound file not found: {background_sound_file}")
121
+ return
122
+
123
+ # Load end of session sound
124
+ end_sound_file = get_sound_file_path(end_sound, sound_dir)
125
+ if not os.path.exists(end_sound_file):
126
+ st.error(f"End sound file not found: {end_sound_file}")
127
+ return
128
+
129
+ # Play background sound on loop
130
+ pygame.mixer.music.load(background_sound_file)
131
+ pygame.mixer.music.play(-1) # -1 means loop indefinitely
132
+
133
+ start_time = time.time()
134
+ end_time = start_time + (duration * 60)
135
+
136
+ try:
137
+ while time.time() < end_time:
138
+ elapsed_time = time.time() - start_time
139
+ progress = elapsed_time / (duration * 60)
140
+ progress_bar.progress(progress)
141
+
142
+ remaining_time = end_time - time.time()
143
+ mins, secs = divmod(int(remaining_time), 60)
144
+ status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
145
+
146
+ if interval_reminder != "None":
147
+ interval = 5 if interval_reminder == "Every 5 minutes" else 10
148
+ if int(elapsed_time) > 0 and int(elapsed_time) % (interval * 60) == 0:
149
+ st.toast(f"{interval} minutes passed", icon="⏰")
150
+
151
+ # Check if 10 seconds remaining
152
+ if remaining_time <= 10 and remaining_time > 9:
153
+ pygame.mixer.music.stop() # Stop background sound
154
+ pygame.mixer.Sound(end_sound_file).play() # Play end sound
155
+
156
+ if remaining_time <= 0:
157
+ break
158
+
159
+ time.sleep(0.1) # Update more frequently for smoother countdown
160
+ finally:
161
+ # Stop all sounds
162
+ pygame.mixer.quit()
163
+
164
+ # Ensure the progress bar is full and time remaining shows 00:00
165
+ progress_bar.progress(1.0)
166
+ status_text.text("Time remaining: 00:00")
167
+
168
+ st.success("Meditation complete!")
169
+ st.balloons()
170
+
171
+ if 'achievements' not in st.session_state:
172
+ st.session_state.achievements = set()
173
+ st.session_state.achievements.add("Zen Master")
174
+ st.success("Achievement Unlocked: Zen Master πŸ§˜β€β™€οΈ")
175
+
176
+ def show_personalized_recommendations():
177
+ st.subheader("🎯 Personalized Recommendations")
178
+
179
+ recommendation_categories = [
180
+ "Mental Health",
181
+ "Physical Health",
182
+ "Personal Development",
183
+ "Relationships",
184
+ "Career",
185
+ "Hobbies",
186
+ ]
187
+
188
+ selected_category = st.selectbox("Choose a category", recommendation_categories)
189
+
190
+ recommendations = {
191
+ "Mental Health": [
192
+ "Practice daily gratitude journaling",
193
+ "Try a guided meditation for stress relief",
194
+ "Explore cognitive behavioral therapy techniques",
195
+ "Start a mood tracking journal",
196
+ "Learn about mindfulness practices",
197
+ ],
198
+ "Physical Health": [
199
+ "Start a 30-day yoga challenge",
200
+ "Try intermittent fasting",
201
+ "Begin a couch to 5K running program",
202
+ "Experiment with new healthy recipes",
203
+ "Create a sleep hygiene routine",
204
+ ],
205
+ "Personal Development": [
206
+ "Start learning a new language",
207
+ "Read personal development books",
208
+ "Take an online course in a subject you're interested in",
209
+ "Practice public speaking",
210
+ "Start a daily writing habit",
211
+ ],
212
+ "Relationships": [
213
+ "Practice active listening techniques",
214
+ "Plan regular date nights or friend meetups",
215
+ "Learn about love languages",
216
+ "Practice expressing gratitude to loved ones",
217
+ "Join a local community or interest group",
218
+ ],
219
+ "Career": [
220
+ "Update your resume and LinkedIn profile",
221
+ "Network with professionals in your industry",
222
+ "Set SMART career goals",
223
+ "Learn a new skill relevant to your field",
224
+ "Start a side project or freelance work",
225
+ ],
226
+ "Hobbies": [
227
+ "Start a garden or learn about plant care",
228
+ "Try a new art form like painting or sculpting",
229
+ "Learn to play a musical instrument",
230
+ "Start a DIY home improvement project",
231
+ "Explore photography or videography",
232
+ ],
233
+ }
234
+
235
+ st.write("Here are some personalized recommendations for you:")
236
+ for recommendation in recommendations[selected_category]:
237
+ st.markdown(f"- {recommendation}")
238
+
239
+ if st.button("Get More Recommendations"):
240
+ st.write("More tailored recommendations:")
241
+ additional_recs = random.sample(recommendations[selected_category], 3)
242
+ for rec in additional_recs:
243
+ st.markdown(f"- {rec}")
244
+
245
+ def generate_binaural_beat(freq1, freq2, duration_seconds, sample_rate=44100):
246
+ t = np.linspace(0, duration_seconds, int(sample_rate * duration_seconds), False)
247
+ left_channel = np.sin(2 * np.pi * freq1 * t)
248
+ right_channel = np.sin(2 * np.pi * freq2 * t)
249
+ stereo_audio = np.vstack((left_channel, right_channel)).T
250
+ return (stereo_audio * 32767).astype(np.int16)
251
+
252
+ def get_binary_file_downloader_html(bin_file, file_label='File'):
253
+ b64 = base64.b64encode(bin_file).decode()
254
+ return f'<a href="data:application/octet-stream;base64,{b64}" download="{file_label}.wav" class="download-link">Download {file_label}</a>'
255
+
256
+ def show_binaural_beats():
257
+ st.subheader("🎡 Binaural Beats Generator")
258
+
259
+ st.markdown("""
260
+ <style>
261
+ .stButton>button {
262
+ background-color: #4CAF50;
263
+ color: white;
264
+ font-weight: bold;
265
+ }
266
+ .download-link {
267
+ background-color: #008CBA;
268
+ color: white;
269
+ padding: 10px 15px;
270
+ text-align: center;
271
+ text-decoration: none;
272
+ display: inline-block;
273
+ font-size: 16px;
274
+ margin: 4px 2px;
275
+ cursor: pointer;
276
+ border-radius: 4px;
277
+ }
278
+ .stop-button {
279
+ background-color: #f44336;
280
+ color: white;
281
+ font-weight: bold;
282
+ }
283
+ </style>
284
+ """, unsafe_allow_html=True)
285
+
286
+ st.write("Binaural beats are created when two slightly different frequencies are played in each ear, potentially influencing brainwave activity.")
287
+
288
+ preset_beats = {
289
+ "Deep Relaxation (Delta)": {"base": 100, "beat": 2},
290
+ "Meditation (Theta)": {"base": 150, "beat": 6},
291
+ "Relaxation (Alpha)": {"base": 200, "beat": 10},
292
+ "Light Focus (Low Beta)": {"base": 250, "beat": 14},
293
+ "High Focus (Mid Beta)": {"base": 300, "beat": 20},
294
+ "Alertness (High Beta)": {"base": 350, "beat": 30},
295
+ "Gamma Consciousness": {"base": 400, "beat": 40},
296
+ "Lucid Dreaming": {"base": 180, "beat": 3},
297
+ "Memory Enhancement": {"base": 270, "beat": 12},
298
+ "Creativity Boost": {"base": 220, "beat": 8},
299
+ "Pain Relief": {"base": 130, "beat": 4},
300
+ "Mood Elevation": {"base": 315, "beat": 18}
301
+ }
302
+
303
+ col1, col2 = st.columns(2)
304
+
305
+ with col1:
306
+ beat_type = st.selectbox("Choose a preset or custom:", ["Custom"] + list(preset_beats.keys()))
307
+
308
+ with col2:
309
+ duration = st.slider("Duration (minutes):", 1, 60, 15)
310
+
311
+ if beat_type == "Custom":
312
+ col3, col4 = st.columns(2)
313
+ with col3:
314
+ base_freq = st.slider("Base Frequency (Hz):", 100, 500, 200)
315
+ with col4:
316
+ beat_freq = st.slider("Desired Beat Frequency (Hz):", 1, 40, 10)
317
+ else:
318
+ base_freq = preset_beats[beat_type]["base"]
319
+ beat_freq = preset_beats[beat_type]["beat"]
320
+ st.info(f"Base Frequency: {base_freq} Hz, Beat Frequency: {beat_freq} Hz")
321
+
322
+ if 'audio_playing' not in st.session_state:
323
+ st.session_state.audio_playing = False
324
+
325
+ if 'start_time' not in st.session_state:
326
+ st.session_state.start_time = None
327
+
328
+ if 'end_time' not in st.session_state:
329
+ st.session_state.end_time = None
330
+
331
+ # Create persistent placeholders for UI elements
332
+ progress_bar = st.empty()
333
+ status_text = st.empty()
334
+ stop_button = st.empty()
335
+
336
+ generate_button = st.button("Generate and Play Binaural Beat")
337
+
338
+ if generate_button:
339
+ try:
340
+ # Stop any currently playing audio
341
+ if st.session_state.audio_playing:
342
+ pygame.mixer.music.stop()
343
+ st.session_state.audio_playing = False
344
+
345
+ audio_data = generate_binaural_beat(base_freq, base_freq + beat_freq, duration * 60)
346
+
347
+ # Save the generated audio to a temporary file
348
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file:
349
+ temp_filename = temp_file.name
350
+ wavfile.write(temp_filename, 44100, audio_data)
351
+
352
+ # Initialize pygame mixer
353
+ pygame.mixer.init(frequency=44100, size=-16, channels=2)
354
+
355
+ # Load and play the audio
356
+ pygame.mixer.music.load(temp_filename)
357
+ pygame.mixer.music.play()
358
+ st.session_state.audio_playing = True
359
+
360
+ st.session_state.start_time = time.time()
361
+ st.session_state.end_time = st.session_state.start_time + (duration * 60)
362
+
363
+ except Exception as e:
364
+ st.error(f"An error occurred: {str(e)}")
365
+ st.info("Please try again or contact support if the issue persists.")
366
+
367
+ if st.session_state.audio_playing:
368
+ stop_button_active = stop_button.button("Stop Binaural Beat", key="stop_binaural", type="primary")
369
+ current_time = time.time()
370
+
371
+ if stop_button_active:
372
+ pygame.mixer.music.stop()
373
+ st.session_state.audio_playing = False
374
+ st.session_state.start_time = None
375
+ st.session_state.end_time = None
376
+
377
+ elif current_time < st.session_state.end_time:
378
+ elapsed_time = current_time - st.session_state.start_time
379
+ progress = elapsed_time / (st.session_state.end_time - st.session_state.start_time)
380
+ progress_bar.progress(progress)
381
+
382
+ remaining_time = st.session_state.end_time - current_time
383
+ mins, secs = divmod(int(remaining_time), 60)
384
+ status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
385
+ else:
386
+ pygame.mixer.music.stop()
387
+ st.session_state.audio_playing = False
388
+ st.session_state.start_time = None
389
+ st.session_state.end_time = None
390
+ progress_bar.empty()
391
+ status_text.text("Binaural beat session complete!")
392
+
393
+ # Offer download of the generated audio
394
+ if not st.session_state.audio_playing and 'audio_data' in locals():
395
+ with io.BytesIO() as buffer:
396
+ wavfile.write(buffer, 44100, audio_data)
397
+ st.markdown(get_binary_file_downloader_html(buffer.getvalue(), f"binaural_beat_{base_freq}_{beat_freq}Hz"), unsafe_allow_html=True)
398
+
399
+ # Ensure the app updates every second
400
+ if st.session_state.audio_playing:
401
+ time.sleep(1)
402
+ st.experimental_rerun()
403
+
404
+ def main():
405
+ st.set_page_config(page_title="S.H.E.R.L.O.C.K. AI Buddy", page_icon="πŸ•΅οΈ", layout="wide")
406
+
407
+ # Custom CSS for improved styling
408
+ st.markdown("""
409
+ <style>
410
+ .stApp {
411
+ background-color: #f0f2f6;
412
+ }
413
+ .stButton>button {
414
+ background-color: #4CAF50;
415
+ color: white;
416
+ font-weight: bold;
417
+ }
418
+ .stTextInput>div>div>input {
419
+ background-color: #ffffff;
420
+ }
421
+ </style>
422
+ """, unsafe_allow_html=True)
423
+
424
+ st.title("πŸ•΅οΈ S.H.E.R.L.O.C.K. AI Buddy")
425
+ st.markdown("Your personalized AI companion for conversation, therapy, and personal growth.")
426
+
427
+ # Initialize session state
428
+ if 'buddy_name' not in st.session_state:
429
+ st.session_state.buddy_name = "Sherlock"
430
+ if 'buddy_personality' not in st.session_state:
431
+ st.session_state.buddy_personality = "Friendly, empathetic, and insightful"
432
+ if 'buddy_details' not in st.session_state:
433
+ st.session_state.buddy_details = "Knowledgeable about various therapy techniques and always ready to listen"
434
+ if 'messages' not in st.session_state:
435
+ st.session_state.messages = []
436
+
437
+ # Sidebar for AI Buddy configuration and additional features
438
+ with st.sidebar:
439
+ st.header("πŸ€– Configure Your AI Buddy")
440
+ st.session_state.buddy_name = st.text_input("Name your AI Buddy", value=st.session_state.buddy_name)
441
+ st.session_state.buddy_personality = st.text_area("Describe your buddy's personality", value=st.session_state.buddy_personality)
442
+ st.session_state.buddy_details = st.text_area("Additional details about your buddy", value=st.session_state.buddy_details)
443
+
444
+ st.header("🧘 Therapy Session")
445
+ therapy_mode = st.checkbox("Enable Therapy Mode")
446
+ if therapy_mode:
447
+ therapy_technique = st.selectbox("Select Therapy Technique", list(THERAPY_TECHNIQUES.keys()))
448
+ else:
449
+ therapy_technique = None
450
+
451
+ st.markdown("---")
452
+
453
+ st.subheader("πŸ“‹ Todo List")
454
+ if 'todos' not in st.session_state:
455
+ st.session_state.todos = []
456
+
457
+ new_todo = st.text_input("Add a new todo:")
458
+ if st.button("Add Todo", key="add_todo"):
459
+ if new_todo:
460
+ st.session_state.todos.append({"task": new_todo, "completed": False})
461
+ st.success("Todo added successfully!")
462
+ else:
463
+ st.warning("Please enter a todo item.")
464
+
465
+ for i, todo in enumerate(st.session_state.todos):
466
+ col1, col2, col3 = st.columns([0.05, 0.8, 0.15])
467
+ with col1:
468
+ todo['completed'] = st.checkbox("", todo['completed'], key=f"todo_{i}")
469
+ with col2:
470
+ st.write(todo['task'], key=f"todo_text_{i}")
471
+ with col3:
472
+ if st.button("πŸ—‘οΈ", key=f"delete_{i}", help="Delete todo"):
473
+ st.session_state.todos.pop(i)
474
+ st.experimental_rerun()
475
+
476
+ st.subheader("⏱️ Pomodoro Timer")
477
+ pomodoro_duration = st.slider("Pomodoro Duration (minutes)", 1, 60, 25)
478
+ if st.button("Start Pomodoro"):
479
+ progress_bar = st.progress(0)
480
+ for i in range(pomodoro_duration * 60):
481
+ time.sleep(1)
482
+ progress_bar.progress((i + 1) / (pomodoro_duration * 60))
483
+ st.success("Pomodoro completed!")
484
+ if 'achievements' not in st.session_state:
485
+ st.session_state.achievements = set()
486
+ st.session_state.achievements.add("Consistent Learner")
487
+
488
+ st.markdown("---")
489
+ st.markdown("Powered by Falcon-180B and Streamlit")
490
+
491
+ st.markdown("---")
492
+ st.header("πŸ“” Daily Journal")
493
+ journal_entry = st.text_area("Write your thoughts for today")
494
+ if st.button("Save Journal Entry"):
495
+ if 'journal_entries' not in st.session_state:
496
+ st.session_state.journal_entries = []
497
+ st.session_state.journal_entries.append({
498
+ 'date': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
499
+ 'entry': journal_entry
500
+ })
501
+ st.success("Journal entry saved!")
502
+ st.toast("Journal entry saved successfully!", icon="βœ…")
503
+
504
+ if 'journal_entries' in st.session_state and st.session_state.journal_entries:
505
+ st.subheader("Previous Entries")
506
+ for entry in st.session_state.journal_entries[-5:]: # Show last 5 entries
507
+ st.text(entry['date'])
508
+ st.write(entry['entry'])
509
+ st.markdown("---")
510
+
511
+ # Main content area
512
+ tab1, tab2 = st.tabs(["Chat", "Tools"])
513
+
514
+ with tab1:
515
+ # Chat interface
516
+ st.header("πŸ—¨οΈ Chat with Your AI Buddy")
517
+
518
+ # Display chat history
519
+ chat_container = st.container()
520
+ with chat_container:
521
+ for message in st.session_state.messages:
522
+ with st.chat_message(message["role"]):
523
+ st.markdown(message["content"])
524
+
525
+ # User input
526
+ prompt = st.chat_input("What's on your mind?")
527
+
528
+ # Clear chat history button
529
+ if st.button("Clear Chat History"):
530
+ st.session_state.messages = []
531
+ st.experimental_rerun()
532
+
533
+ if prompt:
534
+ st.session_state.messages.append({"role": "user", "content": prompt})
535
+ with st.chat_message("user"):
536
+ st.markdown(prompt)
537
+
538
+ buddy_config = {
539
+ "name": st.session_state.buddy_name,
540
+ "personality": st.session_state.buddy_personality,
541
+ "details": st.session_state.buddy_details
542
+ }
543
+
544
+ with st.chat_message("assistant"):
545
+ message_placeholder = st.empty()
546
+ full_response = ""
547
+ for chunk in chat.stream(get_ai_response(prompt, buddy_config, therapy_technique)):
548
+ full_response += chunk.content
549
+ message_placeholder.markdown(full_response + "β–Œ")
550
+ message_placeholder.markdown(full_response)
551
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
552
+
553
+ # Force a rerun to update the chat history immediately
554
+ st.experimental_rerun()
555
+
556
+ with tab2:
557
+ tool_choice = st.selectbox("Select a tool", ["Meditation Timer", "Binaural Beats", "Recommendations"])
558
+ if tool_choice == "Meditation Timer":
559
+ show_meditation_timer()
560
+ elif tool_choice == "Recommendations":
561
+ show_personalized_recommendations()
562
+ elif tool_choice == "Binaural Beats":
563
+ show_binaural_beats()
564
+
565
+ # Mood tracker
566
+ st.sidebar.markdown("---")
567
+ st.sidebar.header("😊 Mood Tracker")
568
+ mood = st.sidebar.slider("How are you feeling today?", 1, 10, 5)
569
+ if st.sidebar.button("Log Mood"):
570
+ st.sidebar.success(f"Mood logged: {mood}/10")
571
+ st.balloons()
572
+
573
+ # Resources and Emergency Contact
574
+ st.sidebar.markdown("---")
575
+ st.sidebar.header("πŸ†˜ Resources")
576
+ st.sidebar.info("If you're in crisis, please reach out for help:")
577
+ st.sidebar.markdown("- [Mental Health Resources](https://www.mentalhealth.gov/get-help/immediate-help)")
578
+ st.sidebar.markdown("- Emergency Contact: 911 or your local emergency number")
579
+
580
+ # Inspiration Quote
581
+ st.sidebar.markdown("---")
582
+ st.sidebar.header("πŸ’‘ Daily Inspiration")
583
+ if st.sidebar.button("Get Inspirational Quote"):
584
+ quotes = [
585
+ "The only way to do great work is to love what you do. - Steve Jobs",
586
+ "Believe you can and you're halfway there. - Theodore Roosevelt",
587
+ "The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt",
588
+ "Strive not to be a success, but rather to be of value. - Albert Einstein",
589
+ "The only limit to our realization of tomorrow will be our doubts of today. - Franklin D. Roosevelt",
590
+ "Do not wait to strike till the iron is hot; but make it hot by striking. - William Butler Yeats",
591
+ "What lies behind us and what lies before us are tiny matters compared to what lies within us. - Ralph Waldo Emerson",
592
+ "Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
593
+ "Life is what happens when you're busy making other plans. - John Lennon",
594
+ "You miss 100% of the shots you don't take. - Wayne Gretzky",
595
+ "The best way to predict the future is to create it. - Peter Drucker",
596
+ "It is not the strongest of the species that survive, nor the most intelligent, but the one most responsive to change. - Charles Darwin",
597
+ "Whether you think you can or you think you can't, you're right. - Henry Ford",
598
+ "The only place where success comes before work is in the dictionary. - Vidal Sassoon",
599
+ "Do what you can, with what you have, where you are. - Theodore Roosevelt",
600
+ "The purpose of our lives is to be happy. - Dalai Lama",
601
+ "Success usually comes to those who are too busy to be looking for it. - Henry David Thoreau",
602
+ "Your time is limited, so don't waste it living someone else's life. - Steve Jobs",
603
+ "Don't be afraid to give up the good to go for the great. - John D. Rockefeller",
604
+ "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
605
+ "Success is not the key to happiness. Happiness is the key to success. - Albert Schweitzer",
606
+ "It does not matter how slowly you go, as long as you do not stop. - Confucius",
607
+ "If you set your goals ridiculously high and it's a failure, you will fail above everyone else's success. - James Cameron",
608
+ "Don't watch the clock; do what it does. Keep going. - Sam Levenson",
609
+ "Hardships often prepare ordinary people for an extraordinary destiny. - C.S. Lewis",
610
+ "Don't count the days, make the days count. - Muhammad Ali",
611
+ "The best revenge is massive success. - Frank Sinatra",
612
+ "The only impossible journey is the one you never begin. - Tony Robbins",
613
+ "Act as if what you do makes a difference. It does. - William James",
614
+ "You are never too old to set another goal or to dream a new dream. - C.S. Lewis",
615
+ "If you're going through hell, keep going. - Winston Churchill",
616
+ "Dream big and dare to fail. - Norman Vaughan",
617
+ "In the middle of every difficulty lies opportunity. - Albert Einstein",
618
+ "What we achieve inwardly will change outer reality. - Plutarch",
619
+ "I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison",
620
+ "It always seems impossible until it's done. - Nelson Mandela",
621
+ "The future depends on what you do today. - Mahatma Gandhi",
622
+ "Don't wait. The time will never be just right. - Napoleon Hill",
623
+ "Quality is not an act, it is a habit. - Aristotle",
624
+ "Your life does not get better by chance, it gets better by change. - Jim Rohn",
625
+ "The only thing standing between you and your goal is the story you keep telling yourself as to why you can't achieve it. - Jordan Belfort",
626
+ "Challenges are what make life interesting; overcoming them is what makes life meaningful. - Joshua J. Marine",
627
+ "Opportunities don't happen, you create them. - Chris Grosser",
628
+ "I can't change the direction of the wind, but I can adjust my sails to always reach my destination. - Jimmy Dean",
629
+ "Start where you are. Use what you have. Do what you can. - Arthur Ashe",
630
+ "The secret of getting ahead is getting started. - Mark Twain",
631
+ "You don’t have to be great to start, but you have to start to be great. - Zig Ziglar",
632
+ "Keep your eyes on the stars, and your feet on the ground. - Theodore Roosevelt",
633
+ "The only way to achieve the impossible is to believe it is possible. - Charles Kingsleigh"
634
+ ]
635
+
636
+ random_quote = random.choice(quotes)
637
+ st.sidebar.success(random_quote)
638
+
639
+ # Chat Export
640
+ st.sidebar.markdown("---")
641
+ if st.sidebar.button("Export Chat History"):
642
+ chat_history = "\n".join([f"{msg['role']}: {msg['content']}" for msg in st.session_state.messages])
643
+ st.sidebar.download_button(
644
+ label="Download Chat History",
645
+ data=chat_history,
646
+ file_name="ai_buddy_chat_history.txt",
647
+ mime="text/plain"
648
+ )
649
+
650
+ st.sidebar.success("Chat history ready for download!")
651
+
652
+ # Display achievements
653
+ if 'achievements' in st.session_state and st.session_state.achievements:
654
+ st.sidebar.markdown("---")
655
+ st.sidebar.header("πŸ† Achievements")
656
+ for achievement in st.session_state.achievements:
657
+ st.sidebar.success(f"Unlocked: {achievement}")
658
+
659
+ if __name__ == "__main__":
 
660
  main()