Spaces:
Runtime error
Runtime error
TheStinger
commited on
Commit
•
77121d6
1
Parent(s):
41bada8
Update app.py
Browse files
app.py
CHANGED
@@ -4,17 +4,24 @@ import numpy as np
|
|
4 |
import os
|
5 |
import soundfile as sf
|
6 |
|
7 |
-
def create_spectrogram_and_get_info(
|
8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
plt.specgram(audio_data, Fs=sample_rate)
|
10 |
|
11 |
-
#
|
12 |
plt.savefig('spectrogram.png')
|
13 |
|
14 |
-
#
|
15 |
audio_info = sf.info(audio_file.name)
|
16 |
|
17 |
-
#
|
18 |
info_table = f"""
|
19 |
| Informazione | Valore |
|
20 |
| --- | --- |
|
@@ -25,9 +32,10 @@ def create_spectrogram_and_get_info(audio_data, sample_rate):
|
|
25 |
| Estensione | {os.path.splitext(audio_file.name)[1]} |
|
26 |
"""
|
27 |
|
28 |
-
#
|
29 |
return info_table, 'spectrogram.png'
|
30 |
|
31 |
-
#
|
32 |
-
iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.Audio(), outputs=["markdown", "image"])
|
33 |
iface.launch()
|
|
|
|
4 |
import os
|
5 |
import soundfile as sf
|
6 |
|
7 |
+
def create_spectrogram_and_get_info(audio_file):
|
8 |
+
# Read the audio data from the file
|
9 |
+
audio_data, sample_rate = sf.read(audio_file.name)
|
10 |
+
|
11 |
+
# Flatten the audio data if it's not mono
|
12 |
+
if len(audio_data.shape) > 1:
|
13 |
+
audio_data = audio_data.flatten()
|
14 |
+
|
15 |
+
# Create the spectrogram
|
16 |
plt.specgram(audio_data, Fs=sample_rate)
|
17 |
|
18 |
+
# Save the spectrogram to a PNG file
|
19 |
plt.savefig('spectrogram.png')
|
20 |
|
21 |
+
# Get the audio file info
|
22 |
audio_info = sf.info(audio_file.name)
|
23 |
|
24 |
+
# Create a table with the audio file info
|
25 |
info_table = f"""
|
26 |
| Informazione | Valore |
|
27 |
| --- | --- |
|
|
|
32 |
| Estensione | {os.path.splitext(audio_file.name)[1]} |
|
33 |
"""
|
34 |
|
35 |
+
# Return the PNG file of the spectrogram and the info table
|
36 |
return info_table, 'spectrogram.png'
|
37 |
|
38 |
+
# Create the Gradio interface
|
39 |
+
iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.inputs.Audio(type="file"), outputs=["markdown", "image"])
|
40 |
iface.launch()
|
41 |
+
|