Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,254 +1,86 @@
|
|
| 1 |
-
# Copyright 2024 Christopher Woodyard
|
| 2 |
-
#
|
| 3 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
-
# you may not use this file except in compliance with the License.
|
| 5 |
-
# You may obtain a copy of the License at
|
| 6 |
-
#
|
| 7 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
-
#
|
| 9 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
-
# See the License for the specific language governing permissions and
|
| 13 |
-
# limitations under the License.
|
| 14 |
-
|
| 15 |
import os
|
|
|
|
| 16 |
import gradio as gr
|
| 17 |
-
from groq import Groq
|
| 18 |
import numpy as np
|
| 19 |
-
import soundfile as sf
|
| 20 |
-
from sentence_transformers import SentenceTransformer
|
| 21 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 22 |
import matplotlib.pyplot as plt
|
| 23 |
-
from
|
| 24 |
-
import io
|
| 25 |
-
|
| 26 |
-
# Initialize Groq client
|
| 27 |
-
client = Groq(
|
| 28 |
-
api_key=os.environ.get("GROQ_API_KEY"),
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# Load pre-trained sentence transformer model
|
| 32 |
-
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 33 |
-
|
| 34 |
-
# Vibrational advice dictionary
|
| 35 |
-
vibrational_advice = {
|
| 36 |
-
"anxious": {
|
| 37 |
-
"frequency": 396,
|
| 38 |
-
"name": "Liberating Guilt and Fear",
|
| 39 |
-
"advice": "The 396 Hz frequency is associated with releasing fear and guilt. Try these techniques:\n"
|
| 40 |
-
"1. Practice deep, slow breathing exercises for 5-10 minutes.\n"
|
| 41 |
-
"2. Use positive affirmations like 'I am safe' or 'I release my fears'.\n"
|
| 42 |
-
"3. Ground yourself by walking barefoot on grass or soil.\n"
|
| 43 |
-
"4. Listen to 396 Hz music while meditating or before sleep.\n"
|
| 44 |
-
"5. Try progressive muscle relaxation to release physical tension."
|
| 45 |
-
},
|
| 46 |
-
"stressed": {
|
| 47 |
-
"frequency": 528,
|
| 48 |
-
"name": "Transformation and Miracles",
|
| 49 |
-
"advice": "The 528 Hz frequency is known as the 'miracle tone' for transformation and DNA repair. Consider these practices:\n"
|
| 50 |
-
"1. Start your day with a gratitude journal, listing 3-5 things you're thankful for.\n"
|
| 51 |
-
"2. Practice mindfulness meditation focusing on your breath for 10-15 minutes.\n"
|
| 52 |
-
"3. Take regular breaks during work to stretch and reset your mind.\n"
|
| 53 |
-
"4. Listen to 528 Hz music during your self-care routine or while working.\n"
|
| 54 |
-
"5. Engage in creative activities like drawing, coloring, or crafting to reduce stress."
|
| 55 |
-
},
|
| 56 |
-
"calm": {
|
| 57 |
-
"frequency": 741,
|
| 58 |
-
"name": "Awakening Intuition",
|
| 59 |
-
"advice": "The 741 Hz frequency is associated with awakening intuition and solving problems. Enhance your state with these tips:\n"
|
| 60 |
-
"1. Start a dream journal to tap into your subconscious mind.\n"
|
| 61 |
-
"2. Practice visualization techniques, imagining your goals and aspirations.\n"
|
| 62 |
-
"3. Engage in mindful movement like yoga or tai chi.\n"
|
| 63 |
-
"4. Spend time in nature to maintain your sense of calm and clarity.\n"
|
| 64 |
-
"5. Listen to 741 Hz music during problem-solving or creative sessions."
|
| 65 |
-
},
|
| 66 |
-
"sad": {
|
| 67 |
-
"frequency": 417,
|
| 68 |
-
"name": "Facilitating Change",
|
| 69 |
-
"advice": "The 417 Hz frequency is believed to facilitate change and let go of negative energy. Try these approaches:\n"
|
| 70 |
-
"1. Write a letter expressing your feelings, then safely burn or discard it as a symbolic release.\n"
|
| 71 |
-
"2. Engage in physical exercise to boost endorphins and improve mood.\n"
|
| 72 |
-
"3. Reach out to a friend or loved one for support and connection.\n"
|
| 73 |
-
"4. Practice self-compassion meditation, treating yourself with kindness.\n"
|
| 74 |
-
"5. Listen to 417 Hz music while journaling or during a relaxing bath."
|
| 75 |
-
},
|
| 76 |
-
"angry": {
|
| 77 |
-
"frequency": 852,
|
| 78 |
-
"name": "Returning to Spiritual Order",
|
| 79 |
-
"advice": "The 852 Hz frequency is associated with returning to spiritual order and inner strength. Consider these techniques:\n"
|
| 80 |
-
"1. Practice 'square breathing': inhale for 4 counts, hold for 4, exhale for 4, hold for 4, repeat.\n"
|
| 81 |
-
"2. Use positive self-talk to reframe the situation causing anger.\n"
|
| 82 |
-
"3. Engage in a physical activity like running or punching a pillow to release tension.\n"
|
| 83 |
-
"4. Write down your angry thoughts, then rewrite them from a calm perspective.\n"
|
| 84 |
-
"5. Listen to 852 Hz music during meditation or while practicing forgiveness exercises."
|
| 85 |
-
},
|
| 86 |
-
"fearful": {
|
| 87 |
-
"frequency": 639,
|
| 88 |
-
"name": "Connecting Relationships",
|
| 89 |
-
"advice": "The 639 Hz frequency is linked to connecting relationships and understanding. When feeling fearful, try these:\n"
|
| 90 |
-
"1. Reach out to a trusted friend or family member to share your fears.\n"
|
| 91 |
-
"2. Practice 'loving-kindness' meditation to foster feelings of safety and connection.\n"
|
| 92 |
-
"3. Create a 'fear ladder' and gradually expose yourself to what you fear in small steps.\n"
|
| 93 |
-
"4. Use positive visualization to imagine successfully overcoming your fears.\n"
|
| 94 |
-
"5. Listen to 639 Hz music while working on building your support network."
|
| 95 |
-
},
|
| 96 |
-
"confused": {
|
| 97 |
-
"frequency": 285,
|
| 98 |
-
"name": "Quantum Cognition",
|
| 99 |
-
"advice": "The 285 Hz frequency is believed to influence energy fields and aid in healing. When feeling confused, consider:\n"
|
| 100 |
-
"1. Create a mind map to visually organize your thoughts and ideas.\n"
|
| 101 |
-
"2. Take a short walk to clear your mind and gain new perspectives.\n"
|
| 102 |
-
"3. Practice the 'brain dump' technique: write down all your thoughts without judgment.\n"
|
| 103 |
-
"4. Use the Pomodoro technique to break tasks into manageable chunks.\n"
|
| 104 |
-
"5. Listen to 285 Hz music while studying or problem-solving to enhance focus."
|
| 105 |
-
},
|
| 106 |
-
"happy": {
|
| 107 |
-
"frequency": 432,
|
| 108 |
-
"name": "Harmonizing Vibrations",
|
| 109 |
-
"advice": "The 432 Hz frequency is associated with harmonizing vibrations and promoting wellbeing. Enhance your happy state with these practices:\n"
|
| 110 |
-
"1. Express gratitude by sharing your joy with others or writing in a gratitude journal.\n"
|
| 111 |
-
"2. Engage in activities that bring you joy and fulfillment.\n"
|
| 112 |
-
"3. Practice 'savoring' by fully immersing yourself in positive experiences.\n"
|
| 113 |
-
"4. Share your positive energy through acts of kindness or volunteering.\n"
|
| 114 |
-
"5. Listen to 432 Hz music to maintain and amplify your positive vibrations."
|
| 115 |
-
}
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
# Create embeddings for predefined emotions
|
| 119 |
-
emotion_embeddings = {emotion: model.encode([emotion]) for emotion in vibrational_advice.keys()}
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
for emotion, embedding in emotion_embeddings.items()}
|
| 125 |
-
return max(similarities, key=similarities.get)
|
| 126 |
-
|
| 127 |
-
def generate_tone(frequency, duration=5, sample_rate=44100):
|
| 128 |
-
t = np.linspace(0, duration, int(sample_rate * duration), False)
|
| 129 |
-
tone = np.sin(2 * np.pi * frequency * t)
|
| 130 |
-
return (tone * 32767).astype(np.int16)
|
| 131 |
-
|
| 132 |
-
def save_tone(frequency):
|
| 133 |
-
tone = generate_tone(frequency)
|
| 134 |
-
filename = f"/tmp/tone_{frequency}Hz.wav"
|
| 135 |
-
sf.write(filename, tone, 44100)
|
| 136 |
-
return filename
|
| 137 |
-
|
| 138 |
-
def generate_waveform(frequency):
|
| 139 |
-
duration = 0.1 # Short duration for visualization
|
| 140 |
-
sample_rate = 44100
|
| 141 |
-
t = np.linspace(0, duration, int(sample_rate * duration), False)
|
| 142 |
-
tone = np.sin(2 * np.pi * frequency * t)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
plt.title(f"Waveform of {frequency} Hz Tone")
|
| 147 |
-
plt.xlabel("Time (s)")
|
| 148 |
-
plt.ylabel("Amplitude")
|
| 149 |
-
plt.ylim(-1.1, 1.1)
|
| 150 |
-
plt.grid(True)
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
plt.close()
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
def transcribe_audio(audio_path):
|
| 160 |
-
try:
|
| 161 |
-
# Open and read the audio file
|
| 162 |
-
with open(audio_path, "rb") as audio_file:
|
| 163 |
-
audio_data = audio_file.read()
|
| 164 |
-
|
| 165 |
-
# Transcribe the audio using Distil-Whisper
|
| 166 |
-
transcription = client.audio.transcriptions.create(
|
| 167 |
-
file=(os.path.basename(audio_path), audio_data),
|
| 168 |
-
model="distil-whisper-large-v3-en",
|
| 169 |
-
response_format="verbose_json",
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
return transcription.text
|
| 173 |
-
except Exception as e:
|
| 174 |
-
return f"Error in transcription: {str(e)}"
|
| 175 |
-
|
| 176 |
-
def vibrational_wellness(input_text, audio_input):
|
| 177 |
-
if audio_input:
|
| 178 |
-
input_text = transcribe_audio(audio_input)
|
| 179 |
-
|
| 180 |
-
if not input_text.strip():
|
| 181 |
-
return "Please enter a description of your emotional state or provide an audio input.", None, None, None
|
| 182 |
-
|
| 183 |
-
prompt = f"""Analyze the following text and determine the primary emotion expressed.
|
| 184 |
-
Choose the most appropriate emotion from this list: anxious, stressed, calm, sad, angry, fearful, confused, happy.
|
| 185 |
-
Provide your answer as a single word that best describes the emotional state.
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
],
|
| 198 |
-
model="llama-3.3-70b-versatile",
|
| 199 |
-
max_tokens=10, # Increased to allow for longer emotion words
|
| 200 |
-
)
|
| 201 |
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
else:
|
| 207 |
-
closest_emotion = detected_emotion
|
| 208 |
-
|
| 209 |
-
result = vibrational_advice[closest_emotion]
|
| 210 |
|
| 211 |
-
|
| 212 |
-
audio_file = save_tone(frequency)
|
| 213 |
-
waveform_image = generate_waveform(frequency)
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
f"Matched Vibrational State: {closest_emotion.capitalize()}\n"
|
| 218 |
-
f"Suggested Frequency: {frequency} Hz - {result['name']}\n\n"
|
| 219 |
-
f"Advice:\n{result['advice']}\n\n"
|
| 220 |
-
f"Listen to the {frequency} Hz tone below and observe its waveform:"
|
| 221 |
-
)
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
gr.Audio(label="Resonant Tone"),
|
| 238 |
-
gr.Image(label="Waveform Visualization"),
|
| 239 |
-
gr.Textbox(label="Transcribed Audio (if provided)")
|
| 240 |
-
],
|
| 241 |
-
title="Vers3Dynamics Vibrational Wellness🌅",
|
| 242 |
-
description="Describe how you feel through text or voice, and receive personalized cymatic frequency advice based on your resonance.",
|
| 243 |
-
examples=[
|
| 244 |
-
["I feel at peace after eating some pancakes.", None],
|
| 245 |
-
["I'm feeling anxious about my upcoming presentation.", None],
|
| 246 |
-
["I'm excited about my vacation next week!", None],
|
| 247 |
-
["I'm frustrated with my current work situation.", None],
|
| 248 |
-
["I'm feeling overwhelmed with all my responsibilities.", None]
|
| 249 |
-
],
|
| 250 |
-
theme=gr.themes.Soft()
|
| 251 |
-
)
|
| 252 |
-
|
| 253 |
-
# Launch the app
|
| 254 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import soundfile as sf
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
+
from rhythma import RhythmaModulationEngine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Function to apply modulation and visualize waveform
|
| 9 |
+
def generate_modulated_audio(base_freq, modulation_type, rhythm_pattern, duration=5):
|
| 10 |
+
engine = RhythmaModulationEngine(base_freq, modulation_type, rhythm_pattern)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Generate modulated audio
|
| 13 |
+
modulated_audio = engine.generate_modulated_wave(duration)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Save and return path for Gradio interface
|
| 16 |
+
file_path = "modulated_audio.wav"
|
| 17 |
+
sf.write(file_path, modulated_audio, engine.sample_rate)
|
|
|
|
| 18 |
|
| 19 |
+
# Visualize waveform
|
| 20 |
+
fig = engine.visualize_waveform(duration)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Get symbolic interpretation
|
| 23 |
+
symbolic = engine.get_symbolic_interpretation()
|
| 24 |
|
| 25 |
+
return file_path, fig, symbolic
|
| 26 |
+
|
| 27 |
+
# Gradio interface setup
|
| 28 |
+
def create_interface():
|
| 29 |
+
with gr.Blocks(title="Rhythma: The Living Modulation Engine") as demo:
|
| 30 |
+
gr.Markdown("# Rhythma: The Living Modulation Engine")
|
| 31 |
+
gr.Markdown("### Dynamic rhythm-based sound modulation for wellbeing")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column(scale=1):
|
| 35 |
+
# Controls for frequency, modulation type, and rhythm
|
| 36 |
+
base_frequency = gr.Slider(minimum=20, maximum=1000, value=440, step=1, label="Base Frequency (Hz)")
|
| 37 |
+
modulation_type = gr.Dropdown(
|
| 38 |
+
choices=["sine", "pulse", "chirp"],
|
| 39 |
+
value="sine",
|
| 40 |
+
label="Modulation Type"
|
| 41 |
+
)
|
| 42 |
+
rhythm_pattern = gr.Dropdown(
|
| 43 |
+
choices=["calm", "active", "focused", "relaxed"],
|
| 44 |
+
value="calm",
|
| 45 |
+
label="Rhythm Pattern"
|
| 46 |
+
)
|
| 47 |
+
duration = gr.Slider(minimum=1, maximum=30, value=5, step=1, label="Duration (seconds)")
|
| 48 |
+
|
| 49 |
+
# Generate button
|
| 50 |
+
generate_button = gr.Button("Generate Modulated Audio", variant="primary")
|
| 51 |
+
|
| 52 |
+
with gr.Column(scale=2):
|
| 53 |
+
# Outputs
|
| 54 |
+
symbolic_output = gr.Textbox(label="Symbolic Interpretation")
|
| 55 |
+
modulated_audio_output = gr.Audio(label="Modulated Audio", type="filepath")
|
| 56 |
+
waveform_plot = gr.Plot(label="Waveform Visualization")
|
| 57 |
+
|
| 58 |
+
# Button action
|
| 59 |
+
generate_button.click(
|
| 60 |
+
generate_modulated_audio,
|
| 61 |
+
inputs=[base_frequency, modulation_type, rhythm_pattern, duration],
|
| 62 |
+
outputs=[modulated_audio_output, waveform_plot, symbolic_output]
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
gr.Markdown("""
|
| 66 |
+
## About Rhythma
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
Rhythma is a dynamic rhythm-based enhancer that creates responsive sound experiences.
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
It uses principles of Floquet modulation and rhythm analysis to generate audio that
|
| 71 |
+
resonates with your current state or intention.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
**How to use:**
|
| 74 |
+
1. Select a base frequency
|
| 75 |
+
2. Choose a modulation type
|
| 76 |
+
3. Select a rhythm pattern that matches your intention
|
| 77 |
+
4. Set the duration
|
| 78 |
+
5. Generate and listen to the modulated audio
|
| 79 |
+
""")
|
| 80 |
+
|
| 81 |
+
return demo
|
| 82 |
+
|
| 83 |
+
# Run Gradio interface
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo = create_interface()
|
| 86 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|