Shokoufehhh commited on
Commit
4dd8499
·
verified ·
1 Parent(s): c3b258c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -1,14 +1,29 @@
1
  import gradio as gr
2
- from speechbrain.inference import SpectralMaskGAN
 
 
3
 
4
  # Load the MetricGAN model
5
- model = SpectralMaskGAN.from_hparams(source="speechbrain/metricgan-plus-voicebank", savedir="tmpdir_metricgan")
 
 
 
6
 
7
  # Define a function to enhance speech
8
  def enhance_speech(audio):
9
- # Process the uploaded audio file through the model
10
- enhanced_audio = model.enhance_file(audio)
11
- return enhanced_audio
 
 
 
 
 
 
 
 
 
 
12
 
13
  # Set up the Gradio interface
14
  iface = gr.Interface(
 
1
  import gradio as gr
2
+ import torch
3
+ import torchaudio
4
+ from speechbrain.inference.enhancement import SpectralMaskEnhancement
5
 
6
  # Load the MetricGAN model
7
+ enhance_model = SpectralMaskEnhancement.from_hparams(
8
+ source="speechbrain/metricgan-plus-voicebank",
9
+ savedir="tmpdir_metricgan",
10
+ )
11
 
12
  # Define a function to enhance speech
13
  def enhance_speech(audio):
14
+ # Load the audio and add fake batch dimension
15
+ noisy = enhance_model.load_audio(audio).unsqueeze(0)
16
+
17
+ # Add relative length tensor (assuming full length)
18
+ lengths = torch.tensor([1.])
19
+
20
+ # Enhance the audio
21
+ enhanced = enhance_model.enhance_batch(noisy, lengths)
22
+
23
+ # Save enhanced audio to a temporary file
24
+ output_path = "enhanced.wav"
25
+ torchaudio.save(output_path, enhanced.cpu(), 16000)
26
+ return output_path
27
 
28
  # Set up the Gradio interface
29
  iface = gr.Interface(