KavyaBansal commited on
Commit
84e30ed
·
verified ·
1 Parent(s): 18a51bb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import noisereduce as nr
2
+ import scipy.io.wavfile as wavfile
3
+ import numpy as np
4
+ import gradio as gr
5
+ import os
6
+ import tempfile
7
+ import shutil
8
+
9
+ def denoise_audio_file(input_path, output_path):
10
+ rate, data = wavfile.read(input_path)
11
+
12
+ if len(data.shape) > 1:
13
+ reduced_noise = np.zeros_like(data, dtype=np.float32)
14
+ for channel in range(data.shape[1]):
15
+ reduced_noise[:, channel] = nr.reduce_noise(y=data[:, channel], sr=rate)
16
+ else:
17
+ reduced_noise = nr.reduce_noise(y=data, sr=rate)
18
+
19
+ wavfile.write(output_path, rate, reduced_noise.astype(data.dtype))
20
+ return output_path
21
+
22
+ def process_single_file(file):
23
+ if not file.name.endswith('.wav'):
24
+ raise gr.Error("Please upload a WAV file")
25
+
26
+ # Use the original filename for the denoised file, but in a temp dir
27
+ name, ext = os.path.splitext(os.path.basename(file.name))
28
+ base_filename = f"{name}_denoised{ext}"
29
+ temp_dir = tempfile.mkdtemp()
30
+ output_path = os.path.join(temp_dir, base_filename)
31
+ denoise_audio_file(file.name, output_path)
32
+ return output_path
33
+
34
+ def process_batch_files(files):
35
+ output_files = []
36
+ temp_dir = tempfile.mkdtemp()
37
+ for file in files:
38
+ if file.name.endswith('.wav'):
39
+ name, ext = os.path.splitext(os.path.basename(file.name))
40
+ base_filename = f"{name}_denoised{ext}"
41
+ output_path = os.path.join(temp_dir, base_filename)
42
+ denoise_audio_file(file.name, output_path)
43
+ output_files.append(output_path)
44
+ return output_files
45
+
46
+ with gr.Blocks(title="Audio Noise Reducer") as demo:
47
+ gr.Markdown("# 🎧 Audio Noise Reduction")
48
+ gr.Markdown("Upload WAV files to remove background noise using AI-powered processing.")
49
+
50
+ with gr.Tab("Single File Processing"):
51
+ with gr.Row():
52
+ with gr.Column():
53
+ single_file = gr.File(label="Upload WAV File", file_types=[".wav"])
54
+ single_btn = gr.Button("Process File")
55
+ with gr.Column():
56
+ single_output = gr.File(label="Download Denoised File")
57
+ single_status = gr.Textbox(label="Processing Status", interactive=False)
58
+
59
+ single_btn.click(
60
+ fn=process_single_file,
61
+ inputs=single_file,
62
+ outputs=single_output,
63
+ api_name="process_single"
64
+ )
65
+
66
+ with gr.Tab("Batch Processing"):
67
+ with gr.Row():
68
+ with gr.Column():
69
+ batch_files = gr.File(label="Upload WAV Files", file_count="multiple", file_types=[".wav"])
70
+ batch_btn = gr.Button("Process Files")
71
+ with gr.Column():
72
+ batch_output = gr.Files(label="Download Denoised Files")
73
+ batch_status = gr.Textbox(label="Processing Status", interactive=False)
74
+
75
+ batch_btn.click(
76
+ fn=process_batch_files,
77
+ inputs=batch_files,
78
+ outputs=batch_output,
79
+ api_name="process_batch"
80
+ )
81
+
82
+ demo.queue()
83
+
84
+ if __name__ == "__main__":
85
+ demo.launch()