Spaces:
Running
on
Zero
Running
on
Zero
artificialguybr
commited on
Commit
•
8b4b52c
1
Parent(s):
b087ac0
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,47 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import torchaudio
|
3 |
from audiocraft.models import MusicGen
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|