M4xjunior commited on
Commit
94b63de
·
verified ·
1 Parent(s): 2de0cdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -1,20 +1,20 @@
 
1
  try:
2
- import spaces # Presumindo que spaces represente o ambiente do Hugging Face Spaces.
3
  USING_SPACES = True
4
  except ImportError:
5
  USING_SPACES = False
6
 
7
- import argparse
8
- from functools import partial
9
  import gradio as gr
10
  import torch
11
  import torchaudio
 
12
  from resemble_enhance.enhancer.inference import denoise, enhance
13
 
 
 
 
14
  def gpu_decorator(func):
15
- """
16
- Decorador para determinar o dispositivo (CPU ou GPU) baseado no ambiente.
17
- """
18
  def wrapper(*args, **kwargs):
19
  device = "cpu"
20
  if not USING_SPACES and torch.cuda.is_available():
@@ -23,11 +23,9 @@ def gpu_decorator(func):
23
  return func(*args, **kwargs)
24
  return wrapper
25
 
 
26
  @gpu_decorator
27
  def _fn(path, solver, nfe, tau, denoising, unlimited, device="cpu"):
28
- """
29
- Função para processar o áudio com as opções especificadas.
30
- """
31
  if path is None:
32
  return "Please upload an audio file.", None
33
 
@@ -50,11 +48,12 @@ def _fn(path, solver, nfe, tau, denoising, unlimited, device="cpu"):
50
 
51
  return (new_sr, wav1), (new_sr, wav2)
52
 
53
- def create_app(args):
 
54
  """
55
- Cria a interface do Gradio com as configurações especificadas.
56
  """
57
- inputs: list = [
58
  gr.Audio(type="filepath", label="Input Audio"),
59
  gr.Dropdown(
60
  choices=["Midpoint", "RK4", "Euler"],
@@ -79,40 +78,42 @@ def create_app(args):
79
  value=False,
80
  label="Denoise Before Enhancement (tick if your audio contains heavy background noise)",
81
  ),
 
 
 
 
82
  ]
83
 
84
- outputs: list = [
85
  gr.Audio(label="Output Denoised Audio"),
86
  gr.Audio(label="Output Enhanced Audio"),
87
  ]
88
 
 
89
  interface = gr.Interface(
90
- fn=partial(_fn, unlimited=args.unlimited),
91
- title="Resemble Enhance",
92
- description="AI-driven audio enhancement for your audio files, powered by Resemble AI.",
93
  inputs=inputs,
94
  outputs=outputs,
95
- live=False, # Evitar execução em tempo real.
96
- queue=True, # Adiciona suporte a fila.
97
  )
98
- return interface
99
 
100
- def main(port=None, host="0.0.0.0", share=False, api=False):
 
 
 
101
  """
102
- Função principal para inicializar o aplicativo no ambiente adequado.
103
  """
104
- parser = argparse.ArgumentParser()
105
- parser.add_argument("--unlimited", action="store_true", help="Permitir áudios maiores que 60 segundos.")
106
- args = parser.parse_args()
107
-
108
  global app
109
- app = create_app(args)
110
  print("Starting app...")
111
- app.queue(api_open=api).launch(server_name=host, server_port=port, share=share, show_api=api)
 
 
112
 
113
  if __name__ == "__main__":
114
  if not USING_SPACES:
115
  main()
116
  else:
117
- app = create_app(argparse.Namespace(unlimited=True))
118
- app.queue().launch()
 
1
+
2
  try:
3
+ import spaces # Para ambientes específicos como Hugging Face Spaces
4
  USING_SPACES = True
5
  except ImportError:
6
  USING_SPACES = False
7
 
 
 
8
  import gradio as gr
9
  import torch
10
  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():
 
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
 
 
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(
59
  choices=["Midpoint", "RK4", "Euler"],
 
78
  value=False,
79
  label="Denoise Before Enhancement (tick if your audio contains heavy background noise)",
80
  ),
81
+ gr.Checkbox(
82
+ value=False,
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,
95
+ title="Audio Enhancement",
96
+ description="AI-driven audio enhancement powered by Resemble AI.",
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()