SHERLOCK / pages /meditation.py
Johan713's picture
Upload 18 files
600acaa verified
raw
history blame
5.82 kB
import streamlit as st
import time
import random
from pydub import AudioSegment
from pydub.generators import Sine
import io
import base64
# Function to generate binaural beats
def generate_binaural_beat(freq1, freq2, duration_ms):
beat1 = Sine(freq1).to_audio_segment(duration=duration_ms)
beat2 = Sine(freq2).to_audio_segment(duration=duration_ms)
stereo_beat = AudioSegment.from_mono_audiosegments(beat1, beat2)
buffer = io.BytesIO()
stereo_beat.export(buffer, format="wav")
return buffer.getvalue()
# Function to get binary audio data as base64
def get_binary_file_downloader_html(bin_file, file_label='File'):
bin_str = base64.b64encode(bin_file).decode()
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{file_label}.wav">Download {file_label}</a>'
return href
# Streamlit app
def main():
st.set_page_config(page_title="Meditation & Mindfulness Hub", layout="wide")
st.title("🧘 Meditation & Mindfulness Hub")
st.markdown("Improve your mental health and focus with our holistic learning experience.")
# Sidebar for navigation
menu = st.sidebar.selectbox("Choose a Practice", ["Meditation", "Mindfulness Exercises", "Binaural Beats", "Positive Affirmations"])
if menu == "Meditation":
meditation_page()
elif menu == "Mindfulness Exercises":
mindfulness_page()
elif menu == "Binaural Beats":
binaural_beats_page()
elif menu == "Positive Affirmations":
affirmations_page()
def meditation_page():
st.header("Guided Meditation")
meditation_types = {
"Breath Awareness": "Focus on your breath, observing each inhale and exhale...",
"Body Scan": "Starting from your toes, gradually move your attention up through your body...",
"Loving-Kindness": "Direct feelings of love and compassion towards yourself and others...",
"Visualization": "Imagine a peaceful scene, like a serene beach or a tranquil forest..."
}
selected_meditation = st.selectbox("Choose a meditation type:", list(meditation_types.keys()))
duration = st.slider("Meditation duration (minutes):", 5, 30, 10)
if st.button("Start Meditation"):
with st.spinner(f"Meditating for {duration} minutes..."):
st.text(meditation_types[selected_meditation])
meditation_progress = st.progress(0)
for i in range(duration * 60):
time.sleep(1)
meditation_progress.progress((i + 1) / (duration * 60))
st.success("Meditation complete. How do you feel?")
def mindfulness_exercises_page():
st.header("Mindfulness Exercises")
exercises = [
"Take three deep breaths, focusing on the sensation of air entering and leaving your body.",
"Notice five things you can see, four things you can touch, three things you can hear, two things you can smell, and one thing you can taste.",
"Spend one minute focusing solely on the present moment, acknowledging thoughts without judgment.",
"Practice mindful walking by paying attention to each step and the sensations in your feet.",
"Eat a small snack mindfully, noticing the taste, texture, and aroma with each bite."
]
selected_exercise = st.selectbox("Choose a mindfulness exercise:", exercises)
if st.button("Start Exercise"):
with st.spinner("Practicing mindfulness..."):
st.text(selected_exercise)
time.sleep(60) # Give the user a minute to practice
st.success("Exercise complete. Remember to carry this mindfulness with you throughout your day.")
def binaural_beats_page():
st.header("Binaural Beats Generator")
st.write("Binaural beats are created when two slightly different frequencies are played in each ear, potentially influencing brainwave activity.")
base_freq = st.slider("Base Frequency (Hz):", 100, 500, 200)
beat_freq = st.slider("Desired Beat Frequency (Hz):", 1, 40, 10)
duration = st.slider("Duration (seconds):", 30, 300, 60)
if st.button("Generate Binaural Beat"):
with st.spinner("Generating binaural beat..."):
audio_data = generate_binaural_beat(base_freq, base_freq + beat_freq, duration * 1000)
st.audio(audio_data, format='audio/wav')
st.markdown(get_binary_file_downloader_html(audio_data, f"binaural_beat_{base_freq}_{beat_freq}Hz"), unsafe_allow_html=True)
def affirmations_page():
st.header("Positive Affirmations")
predefined_affirmations = [
"I am capable of achieving great things.",
"Every day, I'm getting stronger and more confident.",
"I trust in my abilities and embrace new challenges.",
"I radiate positivity and attract positive experiences.",
"I am worthy of love, respect, and success."
]
affirmation_type = st.radio("Choose affirmation type:", ["Predefined", "Custom"])
if affirmation_type == "Predefined":
affirmation = st.selectbox("Select an affirmation:", predefined_affirmations)
else:
affirmation = st.text_input("Enter your custom affirmation:")
repetitions = st.slider("Number of repetitions:", 1, 50, 10)
interval = st.slider("Interval between repetitions (seconds):", 1, 10, 3)
if st.button("Start Affirmations"):
with st.spinner(f"Repeating affirmation {repetitions} times..."):
for i in range(repetitions):
st.write(affirmation)
time.sleep(interval)
st.success("Affirmation session complete. Carry this positive energy with you!")
if __name__ == "__main__":
main()