Files changed (1) hide show
  1. app.py +0 -169
app.py DELETED
@@ -1,169 +0,0 @@
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
- # theme = gr.themes.Soft(font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"])
61
- # css = ".gradio-container {max-width: none !important;} .tab-content {padding: 20px;}"
62
- # demo = gr.Interface(
63
- # fn=process_audio,
64
- # inputs=[
65
- # gr.Audio(label="Upload Audio", type="filepath", sources=['upload', 'microphone']),
66
- # gr.Number(label="Keep Silence Upto (In seconds)", value=0.05)
67
- # ],
68
- # outputs=[
69
- # gr.Audio(label="Play Audio"),
70
- # gr.File(label="Download Audio File"),
71
- # gr.Textbox(label="Duration")
72
- # ],
73
- # title="Remove Silence From Audio",
74
- # description="Upload an MP3 or WAV file, and it will remove silent parts from it.",
75
- # theme=theme,
76
- # css=css,
77
- # )
78
- # return demo
79
-
80
-
81
- def ui():
82
- theme = gr.themes.Soft(
83
- font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"]
84
- )
85
-
86
- css = """
87
- .gradio-container {max-width: none !important;}
88
- .tab-content {padding: 20px;}
89
-
90
- /* Primary button - BLUE by default */
91
- button.primary {
92
- background-color: #2563eb !important;
93
- color: white !important;
94
- font-weight: 600;
95
- border: none !important;
96
- border-radius: 10px;
97
- padding: 12px 18px;
98
- font-size: 1.05em;
99
- }
100
-
101
- button.primary:hover {
102
- background-color: #1e40af !important;
103
- }
104
- """
105
-
106
- with gr.Blocks(theme=theme, css=css) as demo:
107
-
108
- # Header
109
- gr.HTML("""
110
- <div style="text-align:center; margin:20px auto; max-width:800px;">
111
- <h1 style="font-size:2.4em; margin-bottom:6px;">
112
- 🔇 Remove Silence From Audio
113
- </h1>
114
-
115
- <p style="font-size:1.05em; color:#555; margin:0 0 10px;">
116
- Upload an MP3 or WAV file, and it will remove silent parts from it.
117
- </p>
118
-
119
- <p style="font-size:0.9em; color:#777;">
120
- Made by
121
- <a href="https://github.com/NeuralFalconYT" target="_blank" style="text-decoration:none;">
122
- NeuralFalconYT
123
- </a>
124
- </p>
125
- </div>
126
- """)
127
-
128
- with gr.Row():
129
- # LEFT: Inputs
130
- with gr.Column(scale=1):
131
- audio_input = gr.Audio(
132
- label="Upload Audio",
133
- type="filepath",
134
- sources=["upload", "microphone"]
135
- )
136
-
137
- silence_threshold = gr.Number(
138
- label="Keep Silence Upto (In seconds)",
139
- value=0.05
140
- )
141
-
142
- submit_btn = gr.Button(
143
- "🔇 Remove Silence",
144
- variant="primary"
145
- )
146
-
147
- # RIGHT: Outputs
148
- with gr.Column(scale=1):
149
- audio_output = gr.Audio(label="Play Audio")
150
- file_output = gr.File(label="Download Audio File")
151
- duration_output = gr.Textbox(label="Duration")
152
-
153
- submit_btn.click(
154
- fn=process_audio, # <-- your function
155
- inputs=[audio_input, silence_threshold],
156
- outputs=[audio_output, file_output, duration_output]
157
- )
158
-
159
- return demo
160
-
161
- import click
162
- @click.command()
163
- @click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
164
- @click.option("--share", is_flag=True, default=False, help="Enable sharing of the interface.")
165
- def main(debug, share):
166
- demo=ui()
167
- demo.queue().launch(debug=debug, share=share)
168
- if __name__ == "__main__":
169
- main()