jhauret commited on
Commit
cdc9708
·
1 Parent(s): cdb9e30
Files changed (2) hide show
  1. app.py +24 -14
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,32 +1,42 @@
1
  import gradio as gr
2
  import numpy as np
3
- import matplotlib.pyplot as plt
4
  import librosa
5
- import librosa.display
6
 
7
  def plot_stft(audio_file):
8
- # Load audio file
9
  y, sr = librosa.load(audio_file)
10
 
11
- # Compute the Short-Time Fourier Transform (STFT)
12
  D = librosa.stft(y)
13
  S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
14
 
15
- # Plot the STFT
16
- plt.figure(figsize=(10, 6))
17
- librosa.display.specshow(S_db, sr=sr, x_axis='time', y_axis='log')
18
- plt.colorbar(format='%+2.0f dB')
19
- plt.title('STFT (Short-Time Fourier Transform)')
20
 
21
- # Save the plot as an image
22
- plt.savefig('stft_plot.png')
23
- plt.close()
 
 
 
 
24
 
25
- return 'stft_plot.png'
 
 
 
 
 
 
 
 
 
26
 
27
  # Gradio interface
28
  demo = gr.Interface(fn=plot_stft,
29
  inputs=gr.Audio(type="filepath"),
30
- outputs="image")
31
 
32
  demo.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ import plotly.graph_objects as go
4
  import librosa
 
5
 
6
  def plot_stft(audio_file):
7
+ # Load the audio file
8
  y, sr = librosa.load(audio_file)
9
 
10
+ # Compute the STFT
11
  D = librosa.stft(y)
12
  S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
13
 
14
+ # Generate time and frequency axes
15
+ times = librosa.times_like(S_db)
16
+ freqs = librosa.fft_frequencies(sr=sr)
 
 
17
 
18
+ # Create Plotly figure
19
+ fig = go.Figure(data=go.Heatmap(
20
+ z=S_db,
21
+ x=times,
22
+ y=freqs,
23
+ colorscale='Viridis'
24
+ ))
25
 
26
+ # Update layout for better visualization
27
+ fig.update_layout(
28
+ title="STFT (Short-Time Fourier Transform)",
29
+ xaxis_title="Time (s)",
30
+ yaxis_title="Frequency (Hz)",
31
+ yaxis_type="log"
32
+ )
33
+
34
+ # Return the HTML representation of the plot
35
+ return fig.to_html()
36
 
37
  # Gradio interface
38
  demo = gr.Interface(fn=plot_stft,
39
  inputs=gr.Audio(type="filepath"),
40
+ outputs="html")
41
 
42
  demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- matplotlib
2
  librosa
 
1
+ plotly
2
  librosa