Update pages/ai_buddy.py
Browse files- pages/ai_buddy.py +92 -24
pages/ai_buddy.py
CHANGED
@@ -9,7 +9,12 @@ from datetime import datetime
|
|
9 |
import plotly.express as px
|
10 |
import json
|
11 |
import time
|
|
|
12 |
from playsound import playsound
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Load environment variables
|
15 |
load_dotenv()
|
@@ -53,51 +58,114 @@ def get_ai_response(user_input, buddy_config, therapy_technique=None):
|
|
53 |
response = chat.invoke(messages).content
|
54 |
return response
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
def show_meditation_timer():
|
57 |
st.subheader("🧘♀️ Enhanced Meditation Timer")
|
|
|
|
|
|
|
58 |
col1, col2 = st.columns(2)
|
59 |
|
60 |
with col1:
|
61 |
duration = st.slider("Select duration (minutes)", 1, 60, 5)
|
62 |
-
|
63 |
-
background_sound = st.selectbox("Background Sound", background_options)
|
64 |
|
65 |
with col2:
|
66 |
interval_options = ["None", "Every 5 minutes", "Every 10 minutes"]
|
67 |
interval_reminder = st.selectbox("Interval Reminders", interval_options)
|
68 |
-
|
69 |
-
end_sound = st.selectbox("End of Session Sound", end_sound_options)
|
70 |
|
71 |
if st.button("Start Meditation", key="start_meditation"):
|
72 |
progress_bar = st.progress(0)
|
73 |
status_text = st.empty()
|
74 |
|
75 |
-
#
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
|
83 |
-
|
84 |
-
if interval_reminder != "None":
|
85 |
-
interval = 5 if interval_reminder == "Every 5 minutes" else 10
|
86 |
-
if i > 0 and i % (interval * 60) == 0:
|
87 |
-
st.toast(f"{interval} minutes passed", icon="⏰")
|
88 |
-
|
89 |
-
time.sleep(1)
|
90 |
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
st.success("Meditation complete!")
|
95 |
st.balloons()
|
96 |
|
97 |
-
# Play end of session sound
|
98 |
-
end_sound_file = f"sounds/{end_sound.lower().replace(' ', '_')}.mp3"
|
99 |
-
playsound(end_sound_file)
|
100 |
-
|
101 |
if 'achievements' not in st.session_state:
|
102 |
st.session_state.achievements = set()
|
103 |
st.session_state.achievements.add("Zen Master")
|
|
|
9 |
import plotly.express as px
|
10 |
import json
|
11 |
import time
|
12 |
+
import threading
|
13 |
from playsound import playsound
|
14 |
+
import pygame
|
15 |
+
|
16 |
+
|
17 |
+
pygame.mixer.init()
|
18 |
|
19 |
# Load environment variables
|
20 |
load_dotenv()
|
|
|
58 |
response = chat.invoke(messages).content
|
59 |
return response
|
60 |
|
61 |
+
def play_sound_loop(sound_file, stop_event):
|
62 |
+
while not stop_event.is_set():
|
63 |
+
playsound(sound_file)
|
64 |
+
|
65 |
+
def play_sound_for_duration(sound_file, duration):
|
66 |
+
start_time = time.time()
|
67 |
+
while time.time() - start_time < duration:
|
68 |
+
playsound(sound_file, block=False)
|
69 |
+
time.sleep(0.1) # Short sleep to prevent excessive CPU usage
|
70 |
+
# Ensure the sound stops after the duration
|
71 |
+
pygame.mixer.quit()
|
72 |
+
|
73 |
+
def get_sound_files(directory):
|
74 |
+
return [f for f in os.listdir(directory) if f.endswith('.mp3')]
|
75 |
+
|
76 |
+
def get_sound_file_path(sound_name, sound_dir):
|
77 |
+
# Convert the sound name to a filename
|
78 |
+
filename = f"{sound_name.lower().replace(' ', '_')}.mp3"
|
79 |
+
return os.path.join(sound_dir, filename)
|
80 |
+
|
81 |
+
SOUND_OPTIONS = [
|
82 |
+
"Gentle Rain", "Ocean Waves", "Forest Ambience", "Soft Wind Chimes",
|
83 |
+
"Tibetan Singing Bowls", "Crackling Fireplace", "Flowing Stream",
|
84 |
+
"Birdsong", "White Noise", "Zen Garden", "Heartbeat", "Deep Space",
|
85 |
+
"Whale Songs", "Bamboo Flute", "Thunderstorm", "Cat Purring",
|
86 |
+
"Campfire", "Windchimes", "Waterfall", "Beach Waves", "Cicadas",
|
87 |
+
"Coffee Shop Ambience", "Grandfather Clock", "Rainstorm on Tent",
|
88 |
+
"Tropical Birds", "Subway Train", "Washing Machine", "Fan White Noise",
|
89 |
+
"Tibetan Bells", "Wind in Trees"
|
90 |
+
]
|
91 |
+
|
92 |
def show_meditation_timer():
|
93 |
st.subheader("🧘♀️ Enhanced Meditation Timer")
|
94 |
+
|
95 |
+
sound_dir = os.path.join(os.path.dirname(__file__), "..", "sounds")
|
96 |
+
|
97 |
col1, col2 = st.columns(2)
|
98 |
|
99 |
with col1:
|
100 |
duration = st.slider("Select duration (minutes)", 1, 60, 5)
|
101 |
+
background_sound = st.selectbox("Background Sound", SOUND_OPTIONS)
|
|
|
102 |
|
103 |
with col2:
|
104 |
interval_options = ["None", "Every 5 minutes", "Every 10 minutes"]
|
105 |
interval_reminder = st.selectbox("Interval Reminders", interval_options)
|
106 |
+
end_sound = st.selectbox("End of Session Sound", SOUND_OPTIONS)
|
|
|
107 |
|
108 |
if st.button("Start Meditation", key="start_meditation"):
|
109 |
progress_bar = st.progress(0)
|
110 |
status_text = st.empty()
|
111 |
|
112 |
+
# Initialize pygame mixer
|
113 |
+
pygame.mixer.init()
|
114 |
+
|
115 |
+
# Load background sound
|
116 |
+
background_sound_file = get_sound_file_path(background_sound, sound_dir)
|
117 |
+
if not os.path.exists(background_sound_file):
|
118 |
+
st.error(f"Background sound file not found: {background_sound_file}")
|
119 |
+
return
|
120 |
+
|
121 |
+
# Load end of session sound
|
122 |
+
end_sound_file = get_sound_file_path(end_sound, sound_dir)
|
123 |
+
if not os.path.exists(end_sound_file):
|
124 |
+
st.error(f"End sound file not found: {end_sound_file}")
|
125 |
+
return
|
126 |
|
127 |
+
# Play background sound on loop
|
128 |
+
pygame.mixer.music.load(background_sound_file)
|
129 |
+
pygame.mixer.music.play(-1) # -1 means loop indefinitely
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
+
start_time = time.time()
|
132 |
+
end_time = start_time + (duration * 60)
|
133 |
+
|
134 |
+
try:
|
135 |
+
while time.time() < end_time:
|
136 |
+
elapsed_time = time.time() - start_time
|
137 |
+
progress = elapsed_time / (duration * 60)
|
138 |
+
progress_bar.progress(progress)
|
139 |
+
|
140 |
+
remaining_time = end_time - time.time()
|
141 |
+
mins, secs = divmod(int(remaining_time), 60)
|
142 |
+
status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
|
143 |
+
|
144 |
+
if interval_reminder != "None":
|
145 |
+
interval = 5 if interval_reminder == "Every 5 minutes" else 10
|
146 |
+
if int(elapsed_time) > 0 and int(elapsed_time) % (interval * 60) == 0:
|
147 |
+
st.toast(f"{interval} minutes passed", icon="⏰")
|
148 |
+
|
149 |
+
# Check if 10 seconds remaining
|
150 |
+
if remaining_time <= 10 and remaining_time > 9:
|
151 |
+
pygame.mixer.music.stop() # Stop background sound
|
152 |
+
pygame.mixer.Sound(end_sound_file).play() # Play end sound
|
153 |
+
|
154 |
+
if remaining_time <= 0:
|
155 |
+
break
|
156 |
+
|
157 |
+
time.sleep(0.1) # Update more frequently for smoother countdown
|
158 |
+
finally:
|
159 |
+
# Stop all sounds
|
160 |
+
pygame.mixer.quit()
|
161 |
+
|
162 |
+
# Ensure the progress bar is full and time remaining shows 00:00
|
163 |
+
progress_bar.progress(1.0)
|
164 |
+
status_text.text("Time remaining: 00:00")
|
165 |
|
166 |
st.success("Meditation complete!")
|
167 |
st.balloons()
|
168 |
|
|
|
|
|
|
|
|
|
169 |
if 'achievements' not in st.session_state:
|
170 |
st.session_state.achievements = set()
|
171 |
st.session_state.achievements.add("Zen Master")
|