Spaces:
Runtime error
Runtime error
Commit
·
a0110cc
1
Parent(s):
57f4714
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import librosa
|
| 3 |
-
import librosa.display
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def create_spectrogram_and_get_info(audio_file):
|
| 7 |
-
# Read the audio data from the file
|
| 8 |
-
audio_data, sample_rate = librosa.load(audio_file)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
plt.
|
| 18 |
-
|
| 19 |
-
plt.colorbar(format='%+2.0f dB')
|
| 20 |
-
plt.title('Mel spectrogram')
|
| 21 |
-
plt.tight_layout()
|
| 22 |
|
| 23 |
# Save the spectrogram to a PNG file
|
| 24 |
plt.savefig('spectrogram.png')
|
|
@@ -41,3 +40,8 @@ def create_spectrogram_and_get_info(audio_file):
|
|
| 41 |
|
| 42 |
# Return the PNG file of the spectrogram and the info table
|
| 43 |
return info_table, 'spectrogram.png'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
import soundfile as sf
|
| 6 |
|
| 7 |
def create_spectrogram_and_get_info(audio_file):
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Clear figure in case it has data in it
|
| 10 |
+
plt.clf()
|
| 11 |
+
|
| 12 |
+
# Read the audio data from the file
|
| 13 |
+
audio_data, sample_rate = sf.read(audio_file)
|
| 14 |
|
| 15 |
+
# Flatten the audio data if it's not mono
|
| 16 |
+
audio_data = audio_data.flatten() if len(audio_data.shape) > 1 else audio_data
|
| 17 |
|
| 18 |
+
# Create the spectrogram
|
| 19 |
+
plt.specgram(audio_data, Fs=sample_rate / 1, NFFT=4096, sides='onesided',
|
| 20 |
+
cmap="Reds_r", scale_by_freq=True, scale='dB', mode='magnitude')
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Save the spectrogram to a PNG file
|
| 23 |
plt.savefig('spectrogram.png')
|
|
|
|
| 40 |
|
| 41 |
# Return the PNG file of the spectrogram and the info table
|
| 42 |
return info_table, 'spectrogram.png'
|
| 43 |
+
|
| 44 |
+
# Create the Gradio interface
|
| 45 |
+
iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.Audio(type="filepath"), outputs=["markdown", "image"])
|
| 46 |
+
iface.launch()
|
| 47 |
+
|