Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,4 @@
|
|
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 |
|
@@ -10,52 +8,28 @@ def main():
|
|
10 |
gr.Markdown(
|
11 |
"""
|
12 |
# <div align="center"> diablofx Audio Interval Cutter (BETA) </div>
|
13 |
-
Want to [support](https://ko-fi.com/diablofx) me?
|
14 |
"""
|
15 |
)
|
16 |
-
|
17 |
with gr.Row():
|
18 |
with gr.Column():
|
19 |
audio_input = gr.Audio(type='filepath')
|
20 |
-
|
21 |
with gr.Column():
|
22 |
output_markdown = gr.Markdown(value="", visible=True)
|
23 |
-
image_output = gr.Image(type='filepath', interactive=False)
|
24 |
|
25 |
-
|
26 |
|
27 |
app.queue(max_size=1022).launch(share=True)
|
28 |
|
29 |
-
def
|
30 |
-
# Clear figure in case it has data in it
|
31 |
-
plt.clf()
|
32 |
-
|
33 |
-
# Read the audio data from the file
|
34 |
-
audio_data, sample_rate = sf.read(audio_file)
|
35 |
-
|
36 |
-
# Convert to mono if it's not mono
|
37 |
-
if len(audio_data.shape) > 1:
|
38 |
-
audio_data = np.mean(audio_data, axis=1)
|
39 |
-
|
40 |
-
# Create the spectrogram
|
41 |
-
plt.specgram(audio_data, Fs=sample_rate / 1, NFFT=4096, sides='onesided',
|
42 |
-
cmap="Reds_r", scale_by_freq=True, scale='dB', mode='magnitude', window=np.hanning(4096))
|
43 |
-
|
44 |
-
# Save the spectrogram to a PNG file
|
45 |
-
plt.savefig('spectrogram.png')
|
46 |
-
|
47 |
# Get the audio file info
|
48 |
audio_info = sf.info(audio_file)
|
49 |
|
50 |
bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
|
51 |
|
52 |
-
# Convert duration to minutes
|
53 |
minutes, seconds = divmod(audio_info.duration, 60)
|
54 |
-
seconds, milliseconds = divmod(seconds, 1)
|
55 |
-
milliseconds *= 1000 # convert from seconds to milliseconds
|
56 |
-
|
57 |
-
# Convert bitrate to mb/s
|
58 |
-
bitrate = audio_info.samplerate * audio_info.channels * bit_depth / 8 / 1024 / 1024
|
59 |
|
60 |
# Calculate speed in kbps
|
61 |
speed_in_kbps = audio_info.samplerate * bit_depth / 1000
|
@@ -66,16 +40,13 @@ def create_spectrogram_and_get_info(audio_file):
|
|
66 |
| Information | Value |
|
67 |
| :---: | :---: |
|
68 |
| File Name | {os.path.basename(audio_file)} |
|
69 |
-
| Duration | {int(minutes)} minutes - {int(seconds)} seconds
|
70 |
| Bitrate | {speed_in_kbps} kbp/s |
|
71 |
-
| Audio Channels | {audio_info.channels} |
|
72 |
| Samples per second | {audio_info.samplerate} Hz |
|
73 |
-
| Bit per second | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s |
|
74 |
|
75 |
"""
|
76 |
|
77 |
-
|
78 |
-
return info_table, 'spectrogram.png'
|
79 |
|
80 |
# Create the Gradio interface
|
81 |
main()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import os
|
3 |
import soundfile as sf
|
4 |
|
|
|
8 |
gr.Markdown(
|
9 |
"""
|
10 |
# <div align="center"> diablofx Audio Interval Cutter (BETA) </div>
|
11 |
+
Want to [support](https://ko-fi.com/diablofx) me? Or [join AI HUB](https://discord.gg/aihub) for more Help!\n
|
12 |
"""
|
13 |
)
|
|
|
14 |
with gr.Row():
|
15 |
with gr.Column():
|
16 |
audio_input = gr.Audio(type='filepath')
|
17 |
+
create_info_butt = gr.Button(value='Get Audio Info', variant='primary')
|
18 |
with gr.Column():
|
19 |
output_markdown = gr.Markdown(value="", visible=True)
|
|
|
20 |
|
21 |
+
create_info_butt.click(fn=get_audio_info, inputs=[audio_input], outputs=[output_markdown])
|
22 |
|
23 |
app.queue(max_size=1022).launch(share=True)
|
24 |
|
25 |
+
def get_audio_info(audio_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Get the audio file info
|
27 |
audio_info = sf.info(audio_file)
|
28 |
|
29 |
bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
|
30 |
|
31 |
+
# Convert duration to minutes and seconds
|
32 |
minutes, seconds = divmod(audio_info.duration, 60)
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# Calculate speed in kbps
|
35 |
speed_in_kbps = audio_info.samplerate * bit_depth / 1000
|
|
|
40 |
| Information | Value |
|
41 |
| :---: | :---: |
|
42 |
| File Name | {os.path.basename(audio_file)} |
|
43 |
+
| Duration | {int(minutes)} minutes - {int(seconds)} seconds |
|
44 |
| Bitrate | {speed_in_kbps} kbp/s |
|
|
|
45 |
| Samples per second | {audio_info.samplerate} Hz |
|
|
|
46 |
|
47 |
"""
|
48 |
|
49 |
+
return info_table
|
|
|
50 |
|
51 |
# Create the Gradio interface
|
52 |
main()
|