ciaochris commited on
Commit
6e5db15
·
verified ·
1 Parent(s): 7c10d58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -17
app.py CHANGED
@@ -9,7 +9,6 @@ import re
9
  from typing import List, Tuple
10
  import pandas as pd
11
  import io
12
- import base64
13
  import tempfile
14
 
15
  # Load environment variables
@@ -170,10 +169,10 @@ def groq_optimized_frequency_pattern(frequency: float, duration: float, material
170
  # Generate cymatic pattern
171
  cymatic_pattern = generate_cymatic_pattern(waveform)
172
 
173
- # Generate plots and save them as temporary files
174
- waveform_plot = plot_to_base64(create_waveform_plot(waveform, duration))
175
- spectrum_plot = plot_to_base64(create_spectrum_plot(waveform, sample_rate))
176
- cymatic_plot = plot_to_base64(create_cymatic_plot(cymatic_pattern))
177
 
178
  # Generate audio data
179
  audio_data = (waveform * 32767).astype(np.int16)
@@ -184,10 +183,10 @@ def groq_optimized_frequency_pattern(frequency: float, duration: float, material
184
  error_message = f"An error occurred: {str(e)}"
185
  return None, None, error_message, None, None, None
186
 
187
- def plot_to_base64(fig):
188
  with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
189
  fig.savefig(tmp.name, format='png')
190
- return tmp.name
191
 
192
  def create_waveform_plot(waveform, duration):
193
  fig, ax = plt.subplots(figsize=(12, 6))
@@ -195,7 +194,6 @@ def create_waveform_plot(waveform, duration):
195
  ax.set_title('Waveform')
196
  ax.set_xlabel('Time [s]')
197
  ax.set_ylabel('Amplitude')
198
- plt.close(fig)
199
  return fig
200
 
201
  def create_spectrum_plot(waveform, sample_rate):
@@ -205,7 +203,6 @@ def create_spectrum_plot(waveform, sample_rate):
205
  ax.set_ylabel('Frequency [Hz]')
206
  ax.set_xlabel('Time [sec]')
207
  ax.set_title('Frequency Spectrum')
208
- plt.close(fig)
209
  return fig
210
 
211
  def create_cymatic_plot(cymatic_pattern):
@@ -213,16 +210,14 @@ def create_cymatic_plot(cymatic_pattern):
213
  ax.imshow(cymatic_pattern, cmap='viridis')
214
  ax.set_title('Simulated Cymatic Pattern')
215
  ax.axis('off')
216
- plt.close(fig)
217
  return fig
218
 
219
  def export_data(waveform, sample_rate, duration):
220
  time = np.linspace(0, duration, len(waveform))
221
  df = pd.DataFrame({'Time': time, 'Amplitude': waveform})
222
- csv_buffer = io.StringIO()
223
- df.to_csv(csv_buffer, index=False)
224
- csv_buffer.seek(0)
225
- return csv_buffer.getvalue()
226
 
227
  def cymatic_interface(frequency, duration, material_type, length, width, height):
228
  dimensions = (length, width, height)
@@ -233,9 +228,13 @@ def cymatic_interface(frequency, duration, material_type, length, width, height)
233
 
234
  waveform_plot, audio_data, description, spectrum_plot, waveform, cymatic_plot = results
235
 
236
- csv_data = export_data(waveform, 44100, duration)
 
 
237
 
238
- return waveform_plot, (44100, audio_data), description, spectrum_plot, waveform_plot, cymatic_plot, spectrum_plot, csv_data
 
 
239
 
240
  # Gradio UI elements
241
  with gr.Blocks() as iface:
@@ -274,7 +273,7 @@ with gr.Blocks() as iface:
274
  description_output = gr.Textbox(label="Generated Description", lines=10)
275
 
276
  with gr.TabItem("Data Export"):
277
- csv_output = gr.File(label="Download CSV", file_types=[".csv"])
278
 
279
  error_output = gr.Textbox(label="Error Message", visible=False)
280
 
 
9
  from typing import List, Tuple
10
  import pandas as pd
11
  import io
 
12
  import tempfile
13
 
14
  # Load environment variables
 
169
  # Generate cymatic pattern
170
  cymatic_pattern = generate_cymatic_pattern(waveform)
171
 
172
+ # Generate plots
173
+ waveform_plot = create_waveform_plot(waveform, duration)
174
+ spectrum_plot = create_spectrum_plot(waveform, sample_rate)
175
+ cymatic_plot = create_cymatic_plot(cymatic_pattern)
176
 
177
  # Generate audio data
178
  audio_data = (waveform * 32767).astype(np.int16)
 
183
  error_message = f"An error occurred: {str(e)}"
184
  return None, None, error_message, None, None, None
185
 
186
+ def plot_to_file(fig):
187
  with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
188
  fig.savefig(tmp.name, format='png')
189
+ return tmp.name
190
 
191
  def create_waveform_plot(waveform, duration):
192
  fig, ax = plt.subplots(figsize=(12, 6))
 
194
  ax.set_title('Waveform')
195
  ax.set_xlabel('Time [s]')
196
  ax.set_ylabel('Amplitude')
 
197
  return fig
198
 
199
  def create_spectrum_plot(waveform, sample_rate):
 
203
  ax.set_ylabel('Frequency [Hz]')
204
  ax.set_xlabel('Time [sec]')
205
  ax.set_title('Frequency Spectrum')
 
206
  return fig
207
 
208
  def create_cymatic_plot(cymatic_pattern):
 
210
  ax.imshow(cymatic_pattern, cmap='viridis')
211
  ax.set_title('Simulated Cymatic Pattern')
212
  ax.axis('off')
 
213
  return fig
214
 
215
  def export_data(waveform, sample_rate, duration):
216
  time = np.linspace(0, duration, len(waveform))
217
  df = pd.DataFrame({'Time': time, 'Amplitude': waveform})
218
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False, newline='') as tmp:
219
+ df.to_csv(tmp.name, index=False)
220
+ return tmp.name
 
221
 
222
  def cymatic_interface(frequency, duration, material_type, length, width, height):
223
  dimensions = (length, width, height)
 
228
 
229
  waveform_plot, audio_data, description, spectrum_plot, waveform, cymatic_plot = results
230
 
231
+ waveform_plot_file = plot_to_file(waveform_plot)
232
+ spectrum_plot_file = plot_to_file(spectrum_plot)
233
+ cymatic_plot_file = plot_to_file(cymatic_plot)
234
 
235
+ csv_file = export_data(waveform, 44100, duration)
236
+
237
+ return waveform_plot_file, (44100, audio_data), description, spectrum_plot_file, waveform_plot_file, cymatic_plot_file, spectrum_plot_file, csv_file
238
 
239
  # Gradio UI elements
240
  with gr.Blocks() as iface:
 
273
  description_output = gr.Textbox(label="Generated Description", lines=10)
274
 
275
  with gr.TabItem("Data Export"):
276
+ csv_output = gr.File(label="Download CSV")
277
 
278
  error_output = gr.Textbox(label="Error Message", visible=False)
279