Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,30 +4,7 @@ import os
|
|
| 4 |
import soundfile as sf
|
| 5 |
from pydub import AudioSegment
|
| 6 |
from io import BytesIO
|
| 7 |
-
|
| 8 |
-
def main():
|
| 9 |
-
# Gradio Interface
|
| 10 |
-
with gr.Blocks() as app:
|
| 11 |
-
gr.Markdown(
|
| 12 |
-
"""
|
| 13 |
-
# <div align="center"> diablofx Audio Interval Cutter (BETA) </div>
|
| 14 |
-
Want to [support](https://ko-fi.com/diablofx) me? Or [join AI HUB](https://discord.gg/aihub) for more Help!\n
|
| 15 |
-
"""
|
| 16 |
-
)
|
| 17 |
-
with gr.Row():
|
| 18 |
-
with gr.Column():
|
| 19 |
-
audio_input = gr.Audio(type='filepath')
|
| 20 |
-
create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
|
| 21 |
-
split_audio_butt = gr.Button(value='Split Audio into 5s Clips', variant='success')
|
| 22 |
-
with gr.Column():
|
| 23 |
-
output_markdown = gr.Markdown(value="", visible=True)
|
| 24 |
-
|
| 25 |
-
create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input], outputs=[output_markdown])
|
| 26 |
-
split_audio_butt.click(fn=split_audio_into_clips, inputs=[audio_input])
|
| 27 |
-
|
| 28 |
-
app.queue(max_size=1022).launch()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
def get_audio_file_info(audio_file):
|
| 33 |
# Read the audio data from the file
|
|
@@ -66,28 +43,44 @@ def get_audio_file_info(audio_file):
|
|
| 66 |
|
| 67 |
def split_audio_into_clips(audio_file):
|
| 68 |
# Read the audio data from the file
|
| 69 |
-
audio_data, sample_rate =
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
audio_data = np.mean(audio_data, axis=1)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
for i, clip in enumerate(clips):
|
| 84 |
-
clip_data = clip.raw_data
|
| 85 |
-
clip_io = BytesIO(clip_data)
|
| 86 |
-
clip_name = f"clip_{i+1}.wav"
|
| 87 |
-
sf.write(clip_name, clip_io.read(), sample_rate)
|
| 88 |
-
download_buttons.append((f"Download Clip {i+1}", clip_name))
|
| 89 |
|
| 90 |
-
|
| 91 |
|
| 92 |
# Create the Gradio interface
|
| 93 |
-
main()
|
|
|
|
| 4 |
import soundfile as sf
|
| 5 |
from pydub import AudioSegment
|
| 6 |
from io import BytesIO
|
| 7 |
+
import torchaudio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def get_audio_file_info(audio_file):
|
| 10 |
# Read the audio data from the file
|
|
|
|
| 43 |
|
| 44 |
def split_audio_into_clips(audio_file):
|
| 45 |
# Read the audio data from the file
|
| 46 |
+
audio_data, sample_rate = torchaudio.load(audio_file, normalize=True)
|
| 47 |
|
| 48 |
+
# Calculate the duration of each 5-second segment
|
| 49 |
+
segment_duration = 5 * sample_rate
|
|
|
|
| 50 |
|
| 51 |
+
# Split the audio into segments
|
| 52 |
+
segments = [audio_data[:, i:i+segment_duration] for i in range(0, audio_data.size(1), segment_duration)]
|
| 53 |
|
| 54 |
+
# Save each segment to a separate file
|
| 55 |
+
output_files = []
|
| 56 |
+
for i, segment in enumerate(segments):
|
| 57 |
+
output_file = f"output_segment_{i+1}.wav"
|
| 58 |
+
torchaudio.save(output_file, segment, sample_rate)
|
| 59 |
+
output_files.append(output_file)
|
| 60 |
+
|
| 61 |
+
return output_files
|
| 62 |
+
|
| 63 |
+
def main():
|
| 64 |
+
# Gradio Interface
|
| 65 |
+
with gr.Blocks() as app:
|
| 66 |
+
gr.Markdown(
|
| 67 |
+
"""
|
| 68 |
+
# <div align="center"> diablofx Audio Interval Cutter (BETA) </div>
|
| 69 |
+
Want to [support](https://ko-fi.com/diablofx) me? Or [join AI HUB](https://discord.gg/aihub) for more Help!\n
|
| 70 |
+
"""
|
| 71 |
+
)
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column():
|
| 74 |
+
audio_input = gr.Audio(type='filepath')
|
| 75 |
+
create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
|
| 76 |
+
split_audio_butt = gr.Button(value='Split Audio into 5s Clips', variant='success')
|
| 77 |
+
with gr.Column():
|
| 78 |
+
output_markdown = gr.Markdown(value="", visible=True)
|
| 79 |
|
| 80 |
+
create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input], outputs=[output_markdown])
|
| 81 |
+
split_audio_butt.click(fn=split_audio_into_clips, inputs=[audio_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
app.queue(max_size=1022).launch()
|
| 84 |
|
| 85 |
# Create the Gradio interface
|
| 86 |
+
main()
|