Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,7 @@ from pydub import AudioSegment
|
|
21 |
from pydub.silence import split_on_silence
|
22 |
import os
|
23 |
|
24 |
-
def remove_silence(file_path,output_path):
|
25 |
# Extract file name and format from the provided path
|
26 |
file_name = os.path.basename(file_path)
|
27 |
audio_format = "wav"
|
@@ -31,7 +31,7 @@ def remove_silence(file_path,output_path):
|
|
31 |
audio_chunks = split_on_silence(sound,
|
32 |
min_silence_len=100,
|
33 |
silence_thresh=-45,
|
34 |
-
keep_silence=
|
35 |
|
36 |
# Putting the file back together
|
37 |
combined = AudioSegment.empty()
|
@@ -43,7 +43,7 @@ def remove_silence(file_path,output_path):
|
|
43 |
|
44 |
return output_path
|
45 |
|
46 |
-
def process_file(upload_audio_path):
|
47 |
base_path = os.path.dirname(upload_audio_path)
|
48 |
base_file_name = os.path.basename(upload_audio_path)
|
49 |
file_name_without_extension, file_extension = os.path.splitext(base_file_name)
|
@@ -59,7 +59,7 @@ def process_file(upload_audio_path):
|
|
59 |
else:
|
60 |
raise ValueError("Unsupported file format. Please upload an MP3 or WAV file.")
|
61 |
output_path=os.path.join(base_path, f"{file_name_without_extension}_{random_uuid}.wav")
|
62 |
-
remove_silence(save_path,output_path)
|
63 |
return output_path
|
64 |
|
65 |
def store_path_in_json(path, json_file_path="stored_paths.json"):
|
@@ -155,9 +155,10 @@ import os
|
|
155 |
|
156 |
|
157 |
|
158 |
-
def process_audio(audio_file):
|
|
|
159 |
# Process the uploaded audio file
|
160 |
-
output_audio_file= process_file(audio_file)
|
161 |
|
162 |
# Store the processed file path in a JSON file
|
163 |
store_path_in_json(output_audio_file)
|
@@ -172,7 +173,8 @@ def process_audio(audio_file):
|
|
172 |
|
173 |
|
174 |
demo = gr.Interface(process_audio,
|
175 |
-
[gr.Audio(label="Upload Audio",type="filepath",sources=['upload', 'microphone'])
|
|
|
176 |
[gr.Audio(label="Play Audio"),gr.File(label="Download Audio File"),gr.Textbox(label="Duration")],
|
177 |
examples=['./audio/audio.wav'],
|
178 |
cache_examples=True)
|
|
|
21 |
from pydub.silence import split_on_silence
|
22 |
import os
|
23 |
|
24 |
+
def remove_silence(file_path,output_path,minimum_silence=50):
|
25 |
# Extract file name and format from the provided path
|
26 |
file_name = os.path.basename(file_path)
|
27 |
audio_format = "wav"
|
|
|
31 |
audio_chunks = split_on_silence(sound,
|
32 |
min_silence_len=100,
|
33 |
silence_thresh=-45,
|
34 |
+
keep_silence=minimum_silence)
|
35 |
|
36 |
# Putting the file back together
|
37 |
combined = AudioSegment.empty()
|
|
|
43 |
|
44 |
return output_path
|
45 |
|
46 |
+
def process_file(upload_audio_path,silence=50):
|
47 |
base_path = os.path.dirname(upload_audio_path)
|
48 |
base_file_name = os.path.basename(upload_audio_path)
|
49 |
file_name_without_extension, file_extension = os.path.splitext(base_file_name)
|
|
|
59 |
else:
|
60 |
raise ValueError("Unsupported file format. Please upload an MP3 or WAV file.")
|
61 |
output_path=os.path.join(base_path, f"{file_name_without_extension}_{random_uuid}.wav")
|
62 |
+
remove_silence(save_path,output_path,minimum_silence=silence)
|
63 |
return output_path
|
64 |
|
65 |
def store_path_in_json(path, json_file_path="stored_paths.json"):
|
|
|
155 |
|
156 |
|
157 |
|
158 |
+
def process_audio(audio_file,keep_silence):
|
159 |
+
keep_silence=int(seconds * 1000)
|
160 |
# Process the uploaded audio file
|
161 |
+
output_audio_file= process_file(audio_file,silence=keep_silence)
|
162 |
|
163 |
# Store the processed file path in a JSON file
|
164 |
store_path_in_json(output_audio_file)
|
|
|
173 |
|
174 |
|
175 |
demo = gr.Interface(process_audio,
|
176 |
+
[gr.Audio(label="Upload Audio",type="filepath",sources=['upload', 'microphone']),
|
177 |
+
gr.Number(label="Keep Silence Upto (In seconds)",value=0.05)],
|
178 |
[gr.Audio(label="Play Audio"),gr.File(label="Download Audio File"),gr.Textbox(label="Duration")],
|
179 |
examples=['./audio/audio.wav'],
|
180 |
cache_examples=True)
|