NeuralFalcon commited on
Commit
b8720d8
·
verified ·
1 Parent(s): bbc7566

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import os
4
+ import uuid
5
+ from pydub import AudioSegment
6
+ from pydub.silence import split_on_silence
7
+ import re
8
+
9
+ def clean_file_name(file_path):
10
+ # Get the base file name and extension
11
+ file_name = os.path.basename(file_path)
12
+ file_name, file_extension = os.path.splitext(file_name)
13
+
14
+ # Replace non-alphanumeric characters with an underscore
15
+ cleaned = re.sub(r'[^a-zA-Z\d]+', '_', file_name)
16
+
17
+ # Remove any multiple underscores
18
+ clean_file_name = re.sub(r'_+', '_', cleaned).strip('_')
19
+
20
+ # Generate a random UUID for uniqueness
21
+ random_uuid = uuid.uuid4().hex[:6]
22
+
23
+ # Combine cleaned file name with the original extension
24
+ clean_file_path = os.path.join(os.path.dirname(file_path), clean_file_name + f"_{random_uuid}" + file_extension)
25
+
26
+ return clean_file_path
27
+
28
+
29
+
30
+ def remove_silence(file_path, minimum_silence=50):
31
+ sound = AudioSegment.from_file(file_path) # auto-detects format
32
+ audio_chunks = split_on_silence(sound,
33
+ min_silence_len=100,
34
+ silence_thresh=-45,
35
+ keep_silence=minimum_silence)
36
+ combined = AudioSegment.empty()
37
+ for chunk in audio_chunks:
38
+ combined += chunk
39
+ output_path=clean_file_name(file_path)
40
+ combined.export(output_path) # format inferred from output file extension
41
+ return output_path
42
+
43
+
44
+
45
+ def calculate_duration(file_path):
46
+ audio = AudioSegment.from_file(file_path)
47
+ duration_seconds = len(audio) / 1000.0 # pydub uses milliseconds
48
+ return duration_seconds
49
+
50
+
51
+ def process_audio(audio_file, seconds=0.05):
52
+ keep_silence = int(seconds * 1000)
53
+ output_audio_file = remove_silence(audio_file, minimum_silence=keep_silence)
54
+ before = calculate_duration(audio_file)
55
+ after = calculate_duration(output_audio_file)
56
+ text = f"Old Duration: {before:.3f} seconds \nNew Duration: {after:.3f} seconds"
57
+ return output_audio_file, output_audio_file, text
58
+
59
+ def ui():
60
+ demo = gr.Interface(
61
+ fn=process_audio,
62
+ inputs=[
63
+ gr.Audio(label="Upload Audio", type="filepath", sources=['upload', 'microphone']),
64
+ gr.Number(label="Keep Silence Upto (In seconds)", value=0.05)
65
+ ],
66
+ outputs=[
67
+ gr.Audio(label="Play Audio"),
68
+ gr.File(label="Download Audio File"),
69
+ gr.Textbox(label="Duration")
70
+ ],
71
+ title="Remove Silence From Audio",
72
+ description="Upload an MP3 or WAV file, and it will remove silent parts from it.",
73
+ #theme='NoCrypt/miku'
74
+ )
75
+ return demo
76
+
77
+ import click
78
+ @click.command()
79
+ @click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
80
+ @click.option("--share", is_flag=True, default=False, help="Enable sharing of the interface.")
81
+ def main(debug, share):
82
+ demo=ui()
83
+ demo.queue().launch(debug=debug, share=share)
84
+ if __name__ == "__main__":
85
+ main()