Spaces:
Runtime error
Runtime error
alexander-lazarin
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pydub import AudioSegment
|
3 |
+
import os
|
4 |
+
|
5 |
+
def cut_audio(audio, start_time, end_time):
|
6 |
+
# Convert start and end times to milliseconds
|
7 |
+
start_time_ms = start_time * 1000
|
8 |
+
end_time_ms = end_time * 1000
|
9 |
+
|
10 |
+
# Load the audio file using pydub
|
11 |
+
audio_segment = AudioSegment.from_file(audio.name)
|
12 |
+
|
13 |
+
# Cut the audio segment
|
14 |
+
cut_segment = audio_segment[start_time_ms:end_time_ms]
|
15 |
+
|
16 |
+
# Determine the output format based on the original file extension
|
17 |
+
file_ext = os.path.splitext(audio.name)[-1].lower()
|
18 |
+
|
19 |
+
# Create a temporary file to save the cut audio
|
20 |
+
output_file = f"cut_audio{file_ext}"
|
21 |
+
cut_segment.export(output_file, format=file_ext[1:])
|
22 |
+
|
23 |
+
return output_file
|
24 |
+
|
25 |
+
# Create Gradio interface
|
26 |
+
interface = gr.Interface(
|
27 |
+
fn=cut_audio,
|
28 |
+
inputs=[
|
29 |
+
gr.Audio(source="upload", type="file"), # Audio input
|
30 |
+
gr.Number(label="Start Time (seconds)"), # Numeric input for start time
|
31 |
+
gr.Number(label="End Time (seconds)") # Numeric input for end time
|
32 |
+
],
|
33 |
+
outputs=gr.File(label="Cut Audio"), # Output the cut audio file
|
34 |
+
title="Audio Cutter",
|
35 |
+
description="Upload an audio file and specify the start and end times to cut the audio. The output will be the cut audio in the same format as the original."
|
36 |
+
)
|
37 |
+
|
38 |
+
# Launch the interface
|
39 |
+
interface.launch()
|