Spaces:
Sleeping
Sleeping
Handle audio_path being None
Browse files
app.py
CHANGED
@@ -23,42 +23,45 @@ def denoise(model_name: str, audio_path: str) -> str:
|
|
23 |
if torch.cuda.is_available():
|
24 |
model = model.cuda()
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
|
63 |
|
64 |
iface = gr.Interface(
|
|
|
23 |
if torch.cuda.is_available():
|
24 |
model = model.cuda()
|
25 |
|
26 |
+
if audio_path:
|
27 |
+
stream_reader = torchaudio.io.StreamReader(audio_path)
|
28 |
+
stream_reader.add_basic_audio_stream(
|
29 |
+
frames_per_chunk=model.config.max_length,
|
30 |
+
sample_rate=model.config.sample_rate,
|
31 |
+
num_channels=1,
|
32 |
+
)
|
33 |
|
34 |
+
stream_writer = torchaudio.io.StreamWriter("denoised.wav")
|
35 |
+
stream_writer.add_audio_stream(
|
36 |
+
sample_rate=model.config.sample_rate, num_channels=1
|
37 |
+
)
|
38 |
|
39 |
+
chunk_size = model.config.max_length
|
40 |
|
41 |
+
with stream_writer.open():
|
42 |
+
for (audio_chunk,) in tqdm(stream_reader.stream()):
|
43 |
+
if audio_chunk is None:
|
44 |
+
break
|
45 |
|
46 |
+
audio_chunk = audio_chunk.permute(1, 0)
|
47 |
+
original_chunk_size = audio_chunk.size(-1)
|
48 |
|
49 |
+
if audio_chunk.size(-1) < chunk_size:
|
50 |
+
padding = chunk_size - audio_chunk.size(-1)
|
51 |
+
audio_chunk = torch.nn.functional.pad(audio_chunk, (0, padding))
|
52 |
|
53 |
+
if torch.cuda.is_available():
|
54 |
+
audio_chunk = audio_chunk.cuda()
|
55 |
|
56 |
+
with torch.no_grad():
|
57 |
+
denoised_chunk = model(audio_chunk[None]).audio
|
58 |
+
denoised_chunk = denoised_chunk[..., :original_chunk_size]
|
59 |
|
60 |
+
stream_writer.write_audio_chunk(
|
61 |
+
0, denoised_chunk.squeeze(0).permute(1, 0).cpu()
|
62 |
+
)
|
63 |
|
64 |
+
return "denoised.wav"
|
65 |
|
66 |
|
67 |
iface = gr.Interface(
|