Hjgugugjhuhjggg
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import torch
|
4 |
+
from audiocraft.models import MusicGen
|
5 |
+
from audiocraft.data.audio import audio_write
|
6 |
+
import spaces
|
7 |
+
|
8 |
+
model = MusicGen.get_pretrained("small")
|
9 |
+
|
10 |
+
@spaces.GPU()
|
11 |
+
def infer(duration, descriptions):
|
12 |
+
if len(descriptions) > 8192:
|
13 |
+
return "Error: La descripción no puede exceder los 8192 caracteres."
|
14 |
+
|
15 |
+
seed = random.randint(0, 10000)
|
16 |
+
torch.manual_seed(seed)
|
17 |
+
|
18 |
+
model.set_generation_params(duration=duration, temperature=0.5)
|
19 |
+
wav = model.generate(descriptions.split(", "))
|
20 |
+
output_files = []
|
21 |
+
|
22 |
+
for idx, one_wav in enumerate(wav):
|
23 |
+
file_name = f'output_{idx}.wav'
|
24 |
+
audio_write(file_name, one_wav.cpu(), model.sample_rate, strategy="loudness")
|
25 |
+
output_files.append(file_name)
|
26 |
+
|
27 |
+
return output_files[0]
|
28 |
+
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("# Generador de Música con MusicGen")
|
31 |
+
duration_input = gr.Slider(minimum=1, maximum=600, label="Duración de la canción (segundos)", value=8)
|
32 |
+
description_input = gr.Textbox(placeholder="Ejemplo: happy rock, energetic EDM", label="Descripción de la música")
|
33 |
+
generate_button = gr.Button("Generar Música")
|
34 |
+
output_audio = gr.Audio(label="Escuchar Música", type="filepath")
|
35 |
+
|
36 |
+
generate_button.click(
|
37 |
+
fn=infer,
|
38 |
+
inputs=[duration_input, description_input],
|
39 |
+
outputs=output_audio
|
40 |
+
)
|
41 |
+
|
42 |
+
demo.launch()
|