ZDingman commited on
Commit
8a8d79c
·
verified ·
1 Parent(s): dde59c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -24
app.py CHANGED
@@ -1,25 +1,79 @@
1
  import gradio as gr
2
- import torch
3
- import torchaudio
4
- from transformers import pipeline
5
-
6
- # Load AI model (DeepFilterNet2 or similar pretrained speech enhancement model)
7
- model_id = "speechbrain/mtl-mimic-voicebank"
8
- enhancer = pipeline("audio-classification", model=model_id)
9
-
10
- def denoise(audio, strength):
11
- # Load audio
12
- waveform, sr = torchaudio.load(audio)
13
- # This is placeholder — replace with actual noise suppression call
14
- # For demo purposes, we return the same file
15
- return (sr, waveform.numpy())
16
-
17
- with gr.Blocks(css="body {background-color: #0b1520; color: white;}") as demo:
18
- gr.Markdown("## 🎙 Zack's Audio Outpost — AI Noise Reducer\nUpload your file and compare Original vs Cleaned")
19
- audio_input = gr.Audio(type="filepath", label="Upload Audio")
20
- strength = gr.Radio(["Light", "Medium", "Strong"], value="Medium", label="Noise Reduction Strength")
21
- output_audio = gr.Audio(label="Processed Audio")
22
- run_btn = gr.Button("Run Noise Reduction")
23
- run_btn.click(fn=denoise, inputs=[audio_input, strength], outputs=output_audio)
24
-
25
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import soundfile as sf
4
+
5
+ # DeepFilterNet2
6
+ from df.enhance import enhance, init_df
7
+
8
+ APP_TITLE = "Zack’s Audio Outpost — Voice Denoiser (DeepFilterNet2)"
9
+ APP_DESC = (
10
+ "Upload a voice clip with traffic/hiss/room noise and compare Original vs Processed. "
11
+ "Choose Light / Medium / Strong (1× / 2× / 3× passes)."
12
+ )
13
+
14
+ # Load DFN2 once (first run can take a few minutes while the Space installs packages)
15
+ MODEL_DF, DF_STATE, _ = init_df()
16
+
17
+ def _ensure_2d(x: np.ndarray) -> np.ndarray:
18
+ """Make shape (samples, channels)."""
19
+ if x.ndim == 1:
20
+ x = x[:, None]
21
+ return x
22
+
23
+ def _run_single_pass(stereo: np.ndarray) -> np.ndarray:
24
+ """Run DFN2 per channel; keep same length/channels."""
25
+ out = np.zeros_like(stereo, dtype=np.float32)
26
+ for ch in range(stereo.shape[1]):
27
+ y = enhance(stereo[:, ch].astype(np.float32),
28
+ DF_STATE, model=MODEL_DF, atten_lim_db=12.0)
29
+ out[:len(y), ch] = y[:stereo.shape[0]]
30
+ return out
31
+
32
+ def process(file_obj, strength):
33
+ if file_obj is None:
34
+ raise gr.Error("Please upload an audio file first.")
35
+
36
+ # Load original audio (mono or stereo)
37
+ audio, sr = sf.read(file_obj.name, always_2d=False)
38
+ x = _ensure_2d(audio.astype(np.float32))
39
+
40
+ # Map UI strength to number of passes
41
+ passes = {"Light": 1, "Medium": 2, "Strong": 3}[strength]
42
+
43
+ y = x.copy()
44
+ for _ in range(passes):
45
+ y = _run_single_pass(y)
46
+
47
+ # Avoid clipping if multi-pass pushed levels
48
+ y = np.clip(y, -1.0, 1.0)
49
+
50
+ # Gradio wants (sr, np.array). If mono, squeeze back to 1D
51
+ return (sr, audio), (sr, y.squeeze())
52
+
53
+ THEME = gr.themes.Soft(primary_hue="cyan", neutral_hue="slate").set(
54
+ body_background_fill="#0b1020",
55
+ body_text_color="#e6ecff",
56
+ block_background_fill="#121830",
57
+ block_border_color="#243154",
58
+ button_primary_background_fill="#3dd6ff",
59
+ button_primary_text_color="#001018",
60
+ input_background_fill="#0e1530",
61
+ input_border_color="#243154",
62
+ )
63
+
64
+ with gr.Blocks(title=APP_TITLE, theme=THEME) as demo:
65
+ gr.Markdown(f"## {APP_TITLE}\n{APP_DESC}")
66
+ with gr.Row():
67
+ file = gr.File(label="Upload audio", file_types=["audio"])
68
+ strength = gr.Radio(["Light","Medium","Strong"], value="Medium",
69
+ label="Noise reduction strength")
70
+ run = gr.Button("Process", variant="primary")
71
+
72
+ with gr.Row():
73
+ a_orig = gr.Audio(label="Original (A)", interactive=False)
74
+ a_proc = gr.Audio(label="Processed (B)", interactive=False)
75
+
76
+ run.click(process, inputs=[file, strength], outputs=[a_orig, a_proc])
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()