M4xjunior commited on
Commit
65afc75
·
verified ·
1 Parent(s): 94b63de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -37
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  try:
3
  import spaces # Para ambientes específicos como Hugging Face Spaces
4
  USING_SPACES = True
@@ -11,48 +10,33 @@ import torchaudio
11
  from functools import partial
12
  from resemble_enhance.enhancer.inference import denoise, enhance
13
 
14
-
15
-
16
-
17
  def gpu_decorator(func):
18
- def wrapper(*args, **kwargs):
19
- device = "cpu"
20
- if not USING_SPACES and torch.cuda.is_available():
21
- device = "cuda"
22
- kwargs['device'] = device
23
- return func(*args, **kwargs)
24
- return wrapper
25
-
26
 
27
  @gpu_decorator
28
- def _fn(path, solver, nfe, tau, denoising, unlimited, device="cpu"):
29
  if path is None:
30
  return "Please upload an audio file.", None
31
-
32
  info = torchaudio.info(path)
33
  if not unlimited and (info.num_frames / info.sample_rate > 60):
34
  return "Only audio files shorter than 60 seconds are supported.", None
35
-
36
  solver = solver.lower()
37
  nfe = int(nfe)
38
  lambd = 0.9 if denoising else 0.1
39
-
40
  dwav, sr = torchaudio.load(path)
41
  dwav = dwav.mean(dim=0)
42
-
43
- wav1, new_sr = denoise(dwav, sr, device)
44
- wav2, new_sr = enhance(dwav, sr, device, nfe=nfe, solver=solver, lambd=lambd, tau=tau)
45
-
46
  wav1 = wav1.cpu().numpy()
47
  wav2 = wav2.cpu().numpy()
48
-
49
  return (new_sr, wav1), (new_sr, wav2)
50
 
51
 
52
- def create_app():
53
- """
54
- Cria a interface do Gradio.
55
- """
56
  inputs = [
57
  gr.Audio(type="filepath", label="Input Audio"),
58
  gr.Dropdown(
@@ -83,12 +67,10 @@ def create_app():
83
  label="Allow Unlimited Audio Length (supports files longer than 60 seconds)",
84
  ),
85
  ]
86
-
87
  outputs = [
88
  gr.Audio(label="Output Denoised Audio"),
89
  gr.Audio(label="Output Enhanced Audio"),
90
  ]
91
-
92
  # Interface Gradio
93
  interface = gr.Interface(
94
  fn=_fn,
@@ -97,23 +79,17 @@ def create_app():
97
  inputs=inputs,
98
  outputs=outputs,
99
  )
100
-
101
- return interface.queue() # Ativa filas para processamento
102
 
103
 
104
- def main(port=7860, host="0.0.0.0", share=False, api=False):
105
- """
106
- Inicializa a aplicação dependendo do ambiente.
107
- """
108
  global app
109
  print("Starting app...")
110
- app = create_app()
111
- app.launch(server_name=host, server_port=port, share=share, show_api=api)
112
 
113
 
114
  if __name__ == "__main__":
115
  if not USING_SPACES:
116
  main()
117
  else:
118
- app = create_app()
119
- app.launch()
 
 
1
  try:
2
  import spaces # Para ambientes específicos como Hugging Face Spaces
3
  USING_SPACES = True
 
10
  from functools import partial
11
  from resemble_enhance.enhancer.inference import denoise, enhance
12
 
 
 
 
13
  def gpu_decorator(func):
14
+ if USING_SPACES:
15
+ return spaces.GPU(func)
16
+ else:
17
+ return func
 
 
 
 
18
 
19
  @gpu_decorator
20
+ def _fn(path, solver, nfe, tau, denoising, unlimited):
21
  if path is None:
22
  return "Please upload an audio file.", None
 
23
  info = torchaudio.info(path)
24
  if not unlimited and (info.num_frames / info.sample_rate > 60):
25
  return "Only audio files shorter than 60 seconds are supported.", None
 
26
  solver = solver.lower()
27
  nfe = int(nfe)
28
  lambd = 0.9 if denoising else 0.1
 
29
  dwav, sr = torchaudio.load(path)
30
  dwav = dwav.mean(dim=0)
31
+ wav1, new_sr = denoise(dwav, sr) # Remove o argumento device
32
+ wav2, new_sr = enhance(dwav, sr, nfe=nfe, solver=solver, lambd=lambd, tau=tau) # Remove o argumento device
 
 
33
  wav1 = wav1.cpu().numpy()
34
  wav2 = wav2.cpu().numpy()
 
35
  return (new_sr, wav1), (new_sr, wav2)
36
 
37
 
38
+
39
+ with gr.Blocks() as app:
 
 
40
  inputs = [
41
  gr.Audio(type="filepath", label="Input Audio"),
42
  gr.Dropdown(
 
67
  label="Allow Unlimited Audio Length (supports files longer than 60 seconds)",
68
  ),
69
  ]
 
70
  outputs = [
71
  gr.Audio(label="Output Denoised Audio"),
72
  gr.Audio(label="Output Enhanced Audio"),
73
  ]
 
74
  # Interface Gradio
75
  interface = gr.Interface(
76
  fn=_fn,
 
79
  inputs=inputs,
80
  outputs=outputs,
81
  )
82
+ app = interface.queue()
 
83
 
84
 
85
+ def main():
 
 
 
86
  global app
87
  print("Starting app...")
88
+ app.launch(share=True)
 
89
 
90
 
91
  if __name__ == "__main__":
92
  if not USING_SPACES:
93
  main()
94
  else:
95
+ app.launch(share=True)