ciaochris commited on
Commit
a1f72bb
·
verified ·
1 Parent(s): 6dec25a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -39
app.py CHANGED
@@ -1,30 +1,101 @@
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")
@@ -32,50 +103,91 @@ def create_interface():
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
 
1
  import os
 
2
  import gradio as gr
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
+ from PIL import Image
6
+ import soundfile as sf
7
+ import tempfile
8
+ import time
9
+ from rhythma import RhythmaModulationEngine, RhythmaSymphAICore
10
+
11
+ # Initialize the SymphAI Core
12
+ symphai_core = RhythmaSymphAICore()
13
 
14
+ def analyze_input(input_text=None, audio_input=None):
15
+ """Analyze user input for emotional state and rhythm patterns"""
16
+ if audio_input:
17
+ # If audio transcription service is available, integrate here
18
+ transcription = "Audio transcription would go here"
19
+ input_text = transcription
20
+
21
+ if not input_text or not input_text.strip():
22
+ return {
23
+ "error": "Please enter a description of your emotional state or provide an audio input."
24
+ }
25
+
26
+ # Analyze the input using SymphAI Core
27
+ analysis = symphai_core.analyze_input(input_text)
28
 
29
+ return {
30
+ "input_text": input_text,
31
+ "emotional_state": analysis["emotional_state"],
32
+ "rhythm_pattern": analysis["rhythm_pattern"],
33
+ "transcription": input_text if audio_input else None
34
+ }
35
+
36
+ def generate_modulated_experience(analysis_result, base_freq=None, modulation_type="sine", rhythm_pattern=None, duration=5):
37
+ """Generate a complete modulated experience based on analysis and parameters"""
38
+ if "error" in analysis_result:
39
+ return analysis_result["error"], None, None, None, None
40
+
41
+ # Create a unique timestamp for file naming
42
+ timestamp = int(time.time())
43
+
44
+ # Use emotional state and rhythm pattern from analysis if not manually overridden
45
+ emotional_state = analysis_result["emotional_state"]
46
+ rhythm_pattern = rhythm_pattern or analysis_result["rhythm_pattern"]
47
 
48
+ # Initialize the Rhythma Engine
49
+ engine = RhythmaModulationEngine(
50
+ base_freq=base_freq, # Will be overridden by emotional state if None
51
+ modulation_type=modulation_type,
52
+ rhythm_pattern=rhythm_pattern,
53
+ emotional_state=emotional_state
54
+ )
55
+
56
+ # Generate modulated audio
57
+ temp_dir = tempfile.gettempdir()
58
+ audio_file = os.path.join(temp_dir, f"rhythma_{timestamp}.wav")
59
+ engine.save_audio(duration, audio_file)
60
 
61
+ # Generate waveform visualization
62
  fig = engine.visualize_waveform(duration)
63
 
64
+ # Get simple waveform image
65
+ waveform_image = engine.get_waveform_image()
66
+
67
+ # Get complete analysis text
68
+ analysis_text = engine.get_complete_analysis()
69
+
70
  # Get symbolic interpretation
71
  symbolic = engine.get_symbolic_interpretation()
72
 
73
+ return analysis_text, audio_file, fig, waveform_image, symbolic
74
+
75
+ def rhythma_experience(
76
+ input_text, audio_input,
77
+ override_freq=None,
78
+ override_modulation="sine",
79
+ override_rhythm=None,
80
+ duration=5
81
+ ):
82
+ """Complete Rhythma experience pipeline"""
83
+ # Step 1: Analyze input
84
+ analysis = analyze_input(input_text, audio_input)
85
+
86
+ # Step 2: Generate modulated experience
87
+ analysis_text, audio_file, fig, waveform_image, symbolic = generate_modulated_experience(
88
+ analysis,
89
+ base_freq=override_freq,
90
+ modulation_type=override_modulation,
91
+ rhythm_pattern=override_rhythm,
92
+ duration=duration
93
+ )
94
+
95
+ # Return all outputs
96
+ return analysis_text, audio_file, fig, waveform_image, symbolic
97
 
98
+ # Create the Gradio interface
99
  def create_interface():
100
  with gr.Blocks(title="Rhythma: The Living Modulation Engine") as demo:
101
  gr.Markdown("# Rhythma: The Living Modulation Engine")
 
103
 
104
  with gr.Row():
105
  with gr.Column(scale=1):
106
+ # Input section
107
+ input_text = gr.Textbox(
108
+ label="How are you feeling?",
109
+ placeholder="Describe your emotional state or intention..."
 
 
110
  )
111
+ audio_input = gr.Audio(
112
+ type="filepath",
113
+ label="Or speak about your feelings"
 
114
  )
115
+
116
+ with gr.Accordion("Advanced Settings", open=False):
117
+ override_freq = gr.Slider(
118
+ minimum=20, maximum=1000, value=None,
119
+ label="Override Frequency (Hz, leave at 0 for automatic)"
120
+ )
121
+ override_modulation = gr.Dropdown(
122
+ choices=["sine", "pulse", "chirp"],
123
+ value="sine",
124
+ label="Modulation Type"
125
+ )
126
+ override_rhythm = gr.Dropdown(
127
+ choices=[None, "calm", "active", "focused", "relaxed"],
128
+ value=None,
129
+ label="Override Rhythm Pattern (leave blank for automatic)"
130
+ )
131
+ duration = gr.Slider(
132
+ minimum=1, maximum=30, value=5, step=1,
133
+ label="Duration (seconds)"
134
+ )
135
 
136
  # Generate button
137
+ generate_button = gr.Button("Generate Rhythma Experience", variant="primary")
138
 
139
  with gr.Column(scale=2):
140
  # Outputs
141
+ analysis_output = gr.Textbox(label="Rhythma Analysis", lines=10)
142
+ with gr.Row():
143
+ audio_output = gr.Audio(label="Modulated Audio", type="filepath")
144
+ waveform_simple = gr.Image(label="Basic Waveform")
145
+ waveform_plot = gr.Plot(label="Detailed Waveform Analysis")
146
  symbolic_output = gr.Textbox(label="Symbolic Interpretation")
 
 
147
 
148
  # Button action
149
  generate_button.click(
150
+ rhythma_experience,
151
+ inputs=[
152
+ input_text, audio_input,
153
+ override_freq, override_modulation, override_rhythm,
154
+ duration
155
+ ],
156
+ outputs=[
157
+ analysis_output, audio_output,
158
+ waveform_plot, waveform_simple, symbolic_output
159
+ ]
160
+ )
161
+
162
+ # Examples
163
+ gr.Examples(
164
+ examples=[
165
+ ["I'm feeling anxious about my upcoming presentation.", None],
166
+ ["I feel at peace and grounded today.", None],
167
+ ["I need to focus on my work but keep getting distracted.", None],
168
+ ["I'm feeling overwhelmed with all my responsibilities.", None],
169
+ ["I'm excited about my vacation next week!", None]
170
+ ],
171
+ inputs=[input_text, audio_input]
172
  )
173
 
174
  gr.Markdown("""
175
+ ## About Rhythma: The Living Modulation Engine
176
+
177
+ Rhythma is a dynamic rhythm-based enhancer that creates responsive sound experiences
178
+ based on your emotional state and intentions.
179
+
180
+ ### Key Features:
181
 
182
+ 1. **Input Layer** Senses the rhythm through text descriptions and audio input
183
+ 2. **SymphAI Core** – Interprets your emotional state and maps it to rhythm patterns
184
+ 3. **Modulation Engine** – Creates dynamic audio experiences with frequency modulation
185
+ 4. **Output Layer** – Delivers audio-visual feedback tailored to your state
186
 
187
+ Rhythma uses principles of Floquet modulation and rhythm analysis to generate audio that
188
  resonates with your current state or intention.
189
 
190
+ © 2025 Rhythma Technologies
 
 
 
 
 
191
  """)
192
 
193
  return demo