Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,88 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
)
|
|
|
9 |
|
10 |
-
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
return send_file(os.path.join("/path/to/your/app", filename), as_attachment=True)
|
21 |
|
22 |
if __name__ == "__main__":
|
23 |
-
|
|
|
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 |
+
from pydub import AudioSegment
|
7 |
|
8 |
+
def main():
|
9 |
+
with gr.Blocks() as app:
|
10 |
+
gr.Markdown(
|
11 |
+
"""
|
12 |
+
# <div align="center"> diablofx Audio Interval Cutter (BETA) </div>
|
13 |
+
Want to [support](https://ko-fi.com/diablofx) me? [Ko-Fi]\n
|
14 |
|
15 |
+
Need help with AI? [Join AI Hub!](https://discord.gg/aihub)
|
16 |
+
"""
|
17 |
+
)
|
18 |
|
19 |
+
with gr.Row():
|
20 |
+
with gr.Column():
|
21 |
+
audio_input = gr.File(label="Upload an audio file")
|
22 |
+
interval_input = gr.Textbox(default="5000", label="Interval Length (ms)")
|
23 |
+
process_button = gr.Button(value='Process Audio and Get Info', variant='primary')
|
24 |
+
with gr.Column():
|
25 |
+
output_html = gr.HTML("Processed audio information will appear here")
|
26 |
|
27 |
+
process_button.click(fn=process_audio_and_display_info, inputs=[audio_input, interval_input], outputs=output_html)
|
28 |
+
|
29 |
+
app.queue(max_size=1022).launch(share=True)
|
30 |
+
|
31 |
+
def cut_audio(input_path, output_path, interval):
|
32 |
+
audio = AudioSegment.from_file(input_path)
|
33 |
+
duration = len(audio)
|
34 |
+
|
35 |
+
cuts = [audio[i:i + interval] for i in range(0, duration, interval)]
|
36 |
+
|
37 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
38 |
+
for i, cut in enumerate(cuts):
|
39 |
+
cut.export(os.path.join(temp_dir, f'cut_{i}.wav'), format="wav")
|
40 |
+
|
41 |
+
zip_file_path = os.path.join(output_path, "cuts.zip")
|
42 |
+
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
|
43 |
+
for root, _, files in os.walk(temp_dir):
|
44 |
+
for file in files:
|
45 |
+
zipf.write(os.path.join(root, file), os.path.basename(file))
|
46 |
+
|
47 |
+
return zip_file_path
|
48 |
+
|
49 |
+
def process_audio_and_display_info(audio_file, interval):
|
50 |
+
# Get the audio file info
|
51 |
+
audio_info = sf.info(audio_file)
|
52 |
+
|
53 |
+
bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
|
54 |
+
|
55 |
+
# Convert duration to minutes, seconds, and milliseconds
|
56 |
+
minutes, seconds = divmod(audio_info.duration, 60)
|
57 |
+
seconds, milliseconds = divmod(seconds, 1)
|
58 |
+
milliseconds *= 1000 # convert from seconds to milliseconds
|
59 |
+
|
60 |
+
# Convert bitrate to mb/s
|
61 |
+
bitrate = audio_info.samplerate * audio_info.channels * bit_depth / 8 / 1024 / 1024
|
62 |
+
|
63 |
+
# Calculate speed in kbps
|
64 |
+
speed_in_kbps = audio_info.samplerate * bit_depth / 1000
|
65 |
+
|
66 |
+
# Create a table with the audio file info
|
67 |
+
info_table = f"""
|
68 |
+
| Information | Value |
|
69 |
+
| :---: | :---: |
|
70 |
+
| File Name | {os.path.basename(audio_file)} |
|
71 |
+
| Duration | {int(minutes)} minutes - {int(seconds)} seconds - {int(milliseconds)} milliseconds |
|
72 |
+
| Bitrate | {speed_in_kbps} kbp/s |
|
73 |
+
| Audio Channels | {audio_info.channels} |
|
74 |
+
| Samples per second | {audio_info.samplerate} Hz |
|
75 |
+
| Bit per second | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s |
|
76 |
+
"""
|
77 |
+
|
78 |
+
# Process the audio file and get the path to the zip file
|
79 |
+
zip_file_path = cut_audio(audio_file, tempfile.mkdtemp(), int(interval))
|
80 |
|
81 |
+
# Create a download link
|
82 |
+
download_link = f'<a href="/download/{os.path.basename(zip_file_path)}" download>Click here to download processed audio</a>'
|
83 |
|
84 |
+
# Return the information table and the download link
|
85 |
+
return info_table, download_link
|
|
|
86 |
|
87 |
if __name__ == "__main__":
|
88 |
+
main()
|