Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -4,36 +4,29 @@ import torchaudio
|
|
4 |
from audiocraft.models import MusicGen
|
5 |
from audiocraft.data.audio import audio_write
|
6 |
|
7 |
-
# Carrega o modelo pré-treinado
|
8 |
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
9 |
-
model.set_generation_params(duration=8) #
|
10 |
|
11 |
-
@spaces.GPU(duration=120) #
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
# Geração incondicional com descrições
|
16 |
-
wav = model.generate(descriptions)
|
17 |
else:
|
18 |
-
|
19 |
-
melody, sr
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
# Salva o arquivo de áudio gerado
|
25 |
-
output_path = 'generated_audio.wav'
|
26 |
-
audio_write(output_path, wav[0].cpu().float(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
27 |
-
|
28 |
-
return output_path
|
29 |
-
|
30 |
-
# Cria a interface de usuário com Gradio
|
31 |
iface = gr.Interface(
|
32 |
-
fn=
|
33 |
inputs=[
|
34 |
-
gr.
|
35 |
-
gr.
|
36 |
-
|
|
|
|
|
|
|
37 |
)
|
38 |
|
39 |
iface.launch()
|
|
|
4 |
from audiocraft.models import MusicGen
|
5 |
from audiocraft.data.audio import audio_write
|
6 |
|
|
|
7 |
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
8 |
+
model.set_generation_params(duration=8) # generate 8 seconds.
|
9 |
|
10 |
+
@spaces.GPU(duration=120) # Specify duration if the function is expected to take more than 60s
|
11 |
+
def generate_music(description, audio_file):
|
12 |
+
if audio_file is None:
|
13 |
+
wav = model.generate([description]) # generates 1 sample based on the provided description
|
|
|
|
|
14 |
else:
|
15 |
+
melody, sr = torchaudio.load(audio_file)
|
16 |
+
wav = model.generate_with_chroma([description], melody[None], sr) # generates using the melody from the given audio and the provided description
|
17 |
+
|
18 |
+
audio_write('output', wav[0].cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
19 |
+
return 'output.wav'
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
iface = gr.Interface(
|
22 |
+
fn=generate_music,
|
23 |
inputs=[
|
24 |
+
gr.Text(label="Description"),
|
25 |
+
gr.Audio(type="filepath", label="Audio File (optional)")
|
26 |
+
],
|
27 |
+
outputs=gr.Audio(type="file"),
|
28 |
+
title="MusicGen",
|
29 |
+
description="Generate music using the MusicGen model. Provide a description and optionally an audio file for melody.",
|
30 |
)
|
31 |
|
32 |
iface.launch()
|