Shokoufehhh commited on
Commit
88efd7e
·
verified ·
1 Parent(s): cc80b04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import torch
2
  import torchaudio
3
- from sgmse.model import ScoreModel
4
  import gradio as gr
 
5
  from sgmse.util.other import pad_spec
6
  import time # Import the time module
 
7
 
8
- # Define parameters based on the argparse configuration in enhancement.py
9
  args = {
10
  "test_dir": "./test_data", # example directory, adjust as needed
11
  "enhanced_dir": "./enhanced_data", # example directory, adjust as needed
@@ -24,7 +25,7 @@ def enhance_speech(audio_file):
24
  start_time = time.time() # Start the timer
25
 
26
  # Load and process the audio file
27
- y, sr = torchaudio.load(audio_file)
28
  print(f"Loaded audio in {time.time() - start_time:.2f}s")
29
  T_orig = y.size(1)
30
 
@@ -51,19 +52,21 @@ def enhance_speech(audio_file):
51
  # Renormalize
52
  x_hat = x_hat * norm_factor
53
 
54
- # Save the enhanced audio
55
- output_file = 'enhanced_output.wav'
56
  torchaudio.save(output_file, x_hat.cpu(), sr)
57
 
58
  print(f"Processed audio in {time.time() - start_time:.2f}s")
 
 
59
  return output_file
60
 
61
  # Gradio interface setup
62
- inputs = gr.Audio(label="Input Audio", type="filepath")
63
- outputs = gr.Audio(label="Output Audio", type="filepath")
64
  title = "Speech Enhancement using SGMSE"
65
  description = "This Gradio demo uses the SGMSE model for speech enhancement. Upload your audio file to enhance it."
66
  article = "<p style='text-align: center'><a href='https://huggingface.co/SP-UHH/speech-enhancement-sgmse' target='_blank'>Model Card</a></p>"
67
 
68
- # Launch without share=True (as it's not supported on Hugging Face Spaces)
69
  gr.Interface(fn=enhance_speech, inputs=inputs, outputs=outputs, title=title, description=description, article=article).launch()
 
1
  import torch
2
  import torchaudio
 
3
  import gradio as gr
4
+ from sgmse.model import ScoreModel
5
  from sgmse.util.other import pad_spec
6
  import time # Import the time module
7
+ import os
8
 
9
+ # Define parameters based on the configuration in enhancement.py
10
  args = {
11
  "test_dir": "./test_data", # example directory, adjust as needed
12
  "enhanced_dir": "./enhanced_data", # example directory, adjust as needed
 
25
  start_time = time.time() # Start the timer
26
 
27
  # Load and process the audio file
28
+ y, sr = torchaudio.load(audio_file.name) # Gradio passes the file as a file-like object
29
  print(f"Loaded audio in {time.time() - start_time:.2f}s")
30
  T_orig = y.size(1)
31
 
 
52
  # Renormalize
53
  x_hat = x_hat * norm_factor
54
 
55
+ # Save the enhanced audio to a temporary file for Gradio output
56
+ output_file = "enhanced_output.wav"
57
  torchaudio.save(output_file, x_hat.cpu(), sr)
58
 
59
  print(f"Processed audio in {time.time() - start_time:.2f}s")
60
+
61
+ # Return the path to the enhanced file for Gradio to handle
62
  return output_file
63
 
64
  # Gradio interface setup
65
+ inputs = gr.Audio(label="Input Audio", type="file") # Adjusted for file input
66
+ outputs = gr.Audio(label="Enhanced Audio", type="file") # Output as file
67
  title = "Speech Enhancement using SGMSE"
68
  description = "This Gradio demo uses the SGMSE model for speech enhancement. Upload your audio file to enhance it."
69
  article = "<p style='text-align: center'><a href='https://huggingface.co/SP-UHH/speech-enhancement-sgmse' target='_blank'>Model Card</a></p>"
70
 
71
+ # Launch the Gradio interface
72
  gr.Interface(fn=enhance_speech, inputs=inputs, outputs=outputs, title=title, description=description, article=article).launch()