artificialguybr commited on
Commit
1381c8e
1 Parent(s): 4830873

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -26
app.py CHANGED
@@ -1,17 +1,13 @@
1
  import gradio as gr
2
- import torchaudio
3
  from audiocraft.models import MusicGen
4
- from audiocraft.data.audio import audio_write
5
  import spaces
6
  import logging
7
- import os
8
- import uuid
9
- import tempfile
10
 
11
  # Configura o logging
12
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13
 
14
- @spaces.GPU(duration=120)
15
  def generate_music(description, melody_audio):
16
  logging.info("Iniciando a geração de música.")
17
 
@@ -33,39 +29,25 @@ def generate_music(description, melody_audio):
33
  else:
34
  logging.info("Gerando música de forma incondicional.")
35
  wav = model.generate_unconditional(1)
36
-
37
- # Gera nome de arquivo único
38
- filename = f'{str(uuid.uuid4())}.wav'
39
-
40
- # Salva na pasta temporária
41
- output_path = tempfile.mktemp(suffix='.wav')
42
- logging.info(f"Salvando a música gerada em: {output_path}")
43
-
44
- audio_write(output_path, wav[0].cpu(), model.sample_rate,
45
- strategy="loudness", loudness_compressor=True)
46
-
47
- # Verifica se o arquivo existe
48
- if not os.access(output_path, os.R_OK):
49
- raise ValueError(f'Failed to save audio to {output_path}')
50
 
51
  logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
52
- logging.info("Música gerada e salva com sucesso.")
 
 
53
 
54
- return output_path
55
-
56
  # Define a interface Gradio
57
  description = gr.Textbox(label="Description", placeholder="acoustic, guitar, melody, trap, d minor, 90 bpm")
58
  melody_audio = gr.Audio(label="Melody Audio (optional)", type="filepath")
59
- output_path = gr.Audio(label="Generated Music", type="filepath")
60
 
61
  gr.Interface(
62
  fn=generate_music,
63
  inputs=[description, melody_audio],
64
- outputs=output_path,
65
  title="MusicGen Demo",
66
  description="Generate music using the MusicGen model.",
67
  examples=[
68
  ["trap, synthesizer, songstarters, dark, G# minor, 140 bpm", "./assets/kalhonaho.mp3"],
69
  ["upbeat, electronic, synth, dance, 120 bpm", None]
70
  ]
71
- ).launch()
 
1
  import gradio as gr
2
+ import torchaudio
3
  from audiocraft.models import MusicGen
 
4
  import spaces
5
  import logging
 
 
 
6
 
7
  # Configura o logging
8
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
 
10
+ @spaces.GPU(duration=120)
11
  def generate_music(description, melody_audio):
12
  logging.info("Iniciando a geração de música.")
13
 
 
29
  else:
30
  logging.info("Gerando música de forma incondicional.")
31
  wav = model.generate_unconditional(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
34
+ logging.info("Música gerada com sucesso.")
35
+
36
+ return wav[0] # Retorna o tensor de áudio diretamente
37
 
 
 
38
  # Define a interface Gradio
39
  description = gr.Textbox(label="Description", placeholder="acoustic, guitar, melody, trap, d minor, 90 bpm")
40
  melody_audio = gr.Audio(label="Melody Audio (optional)", type="filepath")
41
+ output_audio = gr.Audio(label="Generated Music", type="numpy") # Especifica o tipo como "numpy"
42
 
43
  gr.Interface(
44
  fn=generate_music,
45
  inputs=[description, melody_audio],
46
+ outputs=output_audio,
47
  title="MusicGen Demo",
48
  description="Generate music using the MusicGen model.",
49
  examples=[
50
  ["trap, synthesizer, songstarters, dark, G# minor, 140 bpm", "./assets/kalhonaho.mp3"],
51
  ["upbeat, electronic, synth, dance, 120 bpm", None]
52
  ]
53
+ ).launch()