jhauret commited on
Commit
f8ceff7
·
1 Parent(s): 2dc1426
Files changed (1) hide show
  1. app.py +18 -27
app.py CHANGED
@@ -1,18 +1,15 @@
1
  import gradio as gr
2
  import numpy as np
3
- import plotly
4
  import plotly.graph_objects as go
5
- from plotly import tools
6
  import scipy.signal as ssig
7
  import librosa
8
-
9
 
10
  def plot_stft(audio_file):
11
-
12
  # Load audio file
13
  audio, sampling_rate = librosa.load(audio_file)
14
 
15
- # Compute stft
16
  freq, frames, stft = ssig.stft(audio,
17
  sampling_rate,
18
  window='hann',
@@ -22,42 +19,36 @@ def plot_stft(audio_file):
22
  return_onesided=True,
23
  boundary='zeros',
24
  padded=True,
25
- axis=- 1)
26
-
27
 
 
28
  spectrogram = go.Heatmap(z=librosa.amplitude_to_db(np.abs(stft), ref=np.max),
29
- x=frames,
30
- y=freq,
31
- colorscale='Viridis')
32
-
33
-
34
 
 
35
  fig = go.Figure(spectrogram)
36
 
37
-
38
  fig.update_layout(
39
- #width=300,
40
- #height=500,
41
  font=dict(family='Latin Modern Roman', size=18),
42
- xaxis=dict(
43
- title='Time (seconds)',
44
- titlefont=dict(family='Latin Modern Roman',
45
- size=18)),
46
- yaxis=dict(
47
- title='Frequency (Hz)',
48
- titlefont=dict(family='Latin Modern Roman',
49
- size=18)),
50
  margin=dict(l=0, r=0, t=0, b=0),
51
  paper_bgcolor='rgba(0,0,0,0)',
52
- plot_bgcolor='rgba(0,0,0,0)')
 
53
 
54
  fig.update_traces(colorbar_thickness=8, selector=dict(type='heatmap'))
55
- fig.update_traces(showscale=True, showlegend=False, visible = True)
56
  fig.update_xaxes(visible=True, showgrid=False)
57
  fig.update_yaxes(visible=True, showgrid=False)
58
- plotly.offline.plot(fig, filename='stft.html', config={'displaylogo': False, 'modeBarButtonsToRemove': ['toImage','zoomIn', 'zoomOut','resetScale']})
59
 
60
- html_code = "<iframe data-src='stft.html' </iframe>"
 
61
 
62
  return html_code
63
 
 
1
  import gradio as gr
2
  import numpy as np
 
3
  import plotly.graph_objects as go
 
4
  import scipy.signal as ssig
5
  import librosa
6
+ import plotly.io as pio
7
 
8
  def plot_stft(audio_file):
 
9
  # Load audio file
10
  audio, sampling_rate = librosa.load(audio_file)
11
 
12
+ # Compute STFT
13
  freq, frames, stft = ssig.stft(audio,
14
  sampling_rate,
15
  window='hann',
 
19
  return_onesided=True,
20
  boundary='zeros',
21
  padded=True,
22
+ axis=-1)
 
23
 
24
+ # Create spectrogram heatmap
25
  spectrogram = go.Heatmap(z=librosa.amplitude_to_db(np.abs(stft), ref=np.max),
26
+ x=frames,
27
+ y=freq,
28
+ colorscale='Viridis')
 
 
29
 
30
+ # Create Plotly figure
31
  fig = go.Figure(spectrogram)
32
 
33
+ # Customize layout
34
  fig.update_layout(
 
 
35
  font=dict(family='Latin Modern Roman', size=18),
36
+ xaxis=dict(title='Time (seconds)',
37
+ titlefont=dict(family='Latin Modern Roman', size=18)),
38
+ yaxis=dict(title='Frequency (Hz)',
39
+ titlefont=dict(family='Latin Modern Roman', size=18)),
 
 
 
 
40
  margin=dict(l=0, r=0, t=0, b=0),
41
  paper_bgcolor='rgba(0,0,0,0)',
42
+ plot_bgcolor='rgba(0,0,0,0)'
43
+ )
44
 
45
  fig.update_traces(colorbar_thickness=8, selector=dict(type='heatmap'))
46
+ fig.update_traces(showscale=True, showlegend=False, visible=True)
47
  fig.update_xaxes(visible=True, showgrid=False)
48
  fig.update_yaxes(visible=True, showgrid=False)
 
49
 
50
+ # Convert the figure to an HTML string
51
+ html_code = pio.to_html(fig, full_html=False, config={'displaylogo': False, 'modeBarButtonsToRemove': ['toImage', 'zoomIn', 'zoomOut', 'resetScale']})
52
 
53
  return html_code
54