artificialguybr commited on
Commit
8b4b52c
1 Parent(s): b087ac0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -53
app.py CHANGED
@@ -1,56 +1,47 @@
1
  import gradio as gr
 
2
  import torchaudio
3
  from audiocraft.models import MusicGen
4
- import spaces
5
- import logging
6
- import tempfile
7
- import numpy as np
8
-
9
- # Configure logging
10
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
11
-
12
- @spaces.GPU(duration=120)
13
- def generate_music(description, melody_audio):
14
- logging.info("Starting music generation.")
15
-
16
- # Load the pre-trained model
17
- logging.info("Loading pre-trained model.")
18
- model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
19
- model.set_generation_params(duration=8)
20
-
21
- if description:
22
- description = [description]
23
- if melody_audio:
24
- logging.info(f"Loading audio melody from: {melody_audio}")
25
- melody, sr = torchaudio.load(melody_audio)
26
- logging.info("Generating music with description and melody.")
27
- wav = model.generate_with_chroma(description, melody[None], sr)
28
- else:
29
- logging.info("Generating music with description only.")
30
- wav = model.generate(description)
31
- else:
32
- logging.info("Generating music unconditionally.")
33
- wav = model.generate_unconditional(1)
34
-
35
- # Check the shape of the generated audio tensor
36
- logging.info(f"The shape of the generated audio tensor: {wav[0].shape}")
37
-
38
- # Convert the generated audio to the format expected by gr.Audio
39
- audio_data = (model.sample_rate, (wav[0].cpu().numpy() * 32767).astype(np.int16))
40
- logging.info("Music generated successfully.")
41
-
42
- return audio_data
43
-
44
- # Create the Gradio interface
45
- with gr.Blocks() as demo:
46
- with gr.Row():
47
- with gr.Column():
48
- description = gr.Textbox(label="Music Description")
49
- melody_audio = gr.Audio(label="Melody Audio")
50
- with gr.Column():
51
- output_audio = gr.Audio(label="Generated Music", interactive=False)
52
-
53
- generate_btn = gr.Button("Generate Music")
54
- generate_btn.click(generate_music, inputs=[description, melody_audio], outputs=[output_audio])
55
-
56
- demo.launch()
 
1
  import gradio as gr
2
+ import spaces
3
  import torchaudio
4
  from audiocraft.models import MusicGen
5
+ from audiocraft.data.audio import audio_write
6
+
7
+ # Importação necessária para o funcionamento do modelo e manipulação de áudio
8
+ model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
9
+ model.set_generation_params(duration=8) # Duração de 8 segundos para a geração
10
+
11
+ @spaces.GPU(duration=120) # Decorador para habilitar o uso de GPU
12
+ def generate_audio(mode, descriptions=None, melody_path=None):
13
+ if mode == 'unconditional':
14
+ wav = model.generate_unconditional(4)
15
+ elif mode == 'descriptions':
16
+ if descriptions is None:
17
+ descriptions = ['acoustic, guitar, melody, trap, d minor, 90 bpm']
18
+ descriptions = descriptions.split(",") # Converte a string em lista
19
+ wav = model.generate(descriptions * 3) # Gera 3 amostras
20
+ elif mode == 'melody':
21
+ if melody_path is None:
22
+ return "Melody path cannot be empty for melody mode."
23
+ melody, sr = torchaudio.load(melody_path)
24
+ if descriptions is None:
25
+ descriptions = ['acoustic, guitar, melody, trap, d minor, 90 bpm']
26
+ descriptions = descriptions.split(",") # Converte a string em lista
27
+ wav = model.generate_with_chroma(descriptions, melody[None].expand(3, -1, -1), sr)
28
+
29
+ # Salva os arquivos de áudio gerados
30
+ for idx, one_wav in enumerate(wav):
31
+ audio_write(f'output_{idx}.wav', one_wav.cpu(), model.sample_rate,
32
+ strategy="loudness", loudness_compressor=True)
33
+
34
+ # Retorna os caminhos dos arquivos de áudio gerados
35
+ return [f"output_{idx}.wav" for idx in range(len(wav))]
36
+
37
+ # Define a interface de usuário com Gradio
38
+ iface = gr.Interface(
39
+ fn=generate_audio,
40
+ inputs=[
41
+ gr.Dropdown(['unconditional', 'descriptions', 'melody'], label="Generation Mode"),
42
+ gr.Textbox(label="Descriptions (comma-separated, optional)", optional=True),
43
+ gr.File(label="Melody File Path (.mp3, optional)", optional=True, type="filepath")],
44
+ outputs=gr.File(label="Generated Audio", type="file", multiple=True)
45
+ )
46
+
47
+ iface.launch()