Update app.py
Browse files
app.py
CHANGED
|
@@ -8,30 +8,22 @@ import uuid
|
|
| 8 |
import torch
|
| 9 |
import re
|
| 10 |
|
|
|
|
| 11 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
'Large': load_model('nateraw/musicgen-songstarter-v0.2'),
|
| 18 |
-
'Small': load_model('facebook/musicgen-small')
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
def get_model(model_choice):
|
| 22 |
-
return models.get(model_choice, models['Large'])
|
| 23 |
-
|
| 24 |
-
@spaces.GPU(duration=0)
|
| 25 |
-
def generate_music(description, melody_audio, duration, model_choice):
|
| 26 |
-
model = get_model(model_choice)
|
| 27 |
description = clean_text(description)
|
| 28 |
-
model.set_generation_params(duration=int(duration * 1000))
|
| 29 |
|
| 30 |
try:
|
| 31 |
with torch.no_grad():
|
| 32 |
if description:
|
| 33 |
description = [description]
|
| 34 |
if melody_audio:
|
|
|
|
| 35 |
melody, sr = torchaudio.load(melody_audio, normalize=True)
|
| 36 |
melody = melody.to(device) if torch.cuda.is_available() else melody
|
| 37 |
wav = model.generate_with_chroma(description, melody[None], sr)
|
|
@@ -56,20 +48,21 @@ def clean_text(text):
|
|
| 56 |
text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
| 57 |
return text
|
| 58 |
|
|
|
|
| 59 |
description = gr.Textbox(label="Description", placeholder="Acoustic, guitar, melody, trap, D minor, 90 bpm")
|
| 60 |
melody_audio = gr.Audio(label="Melody Audio (optional)", type="filepath")
|
| 61 |
duration = gr.Number(label="Duration (seconds)", value=10, precision=0)
|
| 62 |
-
|
| 63 |
-
output_path = gr.Audio(label="Generated Music", type="filepath")
|
| 64 |
|
| 65 |
gr.Interface(
|
| 66 |
fn=generate_music,
|
| 67 |
-
inputs=[description, melody_audio, duration
|
| 68 |
outputs=output_path,
|
| 69 |
-
title="MusicGen Demo",
|
| 70 |
-
description="Generate music using the MusicGen model.
|
| 71 |
examples=[
|
| 72 |
-
["
|
| 73 |
-
["
|
|
|
|
| 74 |
]
|
| 75 |
).launch()
|
|
|
|
| 8 |
import torch
|
| 9 |
import re
|
| 10 |
|
| 11 |
+
# Configuraci贸n del dispositivo
|
| 12 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 13 |
|
| 14 |
+
# Cargar el modelo `musicgen-small` una 煤nica vez
|
| 15 |
+
model = MusicGen.get_pretrained("facebook/musicgen-small")
|
| 16 |
|
| 17 |
+
def generate_music(description, melody_audio, duration):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
description = clean_text(description)
|
| 19 |
+
model.set_generation_params(duration=int(duration * 1000)) # Convertir segundos a milisegundos
|
| 20 |
|
| 21 |
try:
|
| 22 |
with torch.no_grad():
|
| 23 |
if description:
|
| 24 |
description = [description]
|
| 25 |
if melody_audio:
|
| 26 |
+
# Cargar el archivo de audio para remixar
|
| 27 |
melody, sr = torchaudio.load(melody_audio, normalize=True)
|
| 28 |
melody = melody.to(device) if torch.cuda.is_available() else melody
|
| 29 |
wav = model.generate_with_chroma(description, melody[None], sr)
|
|
|
|
| 48 |
text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
| 49 |
return text
|
| 50 |
|
| 51 |
+
# Definir la interfaz de Gradio
|
| 52 |
description = gr.Textbox(label="Description", placeholder="Acoustic, guitar, melody, trap, D minor, 90 bpm")
|
| 53 |
melody_audio = gr.Audio(label="Melody Audio (optional)", type="filepath")
|
| 54 |
duration = gr.Number(label="Duration (seconds)", value=10, precision=0)
|
| 55 |
+
output_path = gr.File(label="Generated Music")
|
|
|
|
| 56 |
|
| 57 |
gr.Interface(
|
| 58 |
fn=generate_music,
|
| 59 |
+
inputs=[description, melody_audio, duration],
|
| 60 |
outputs=output_path,
|
| 61 |
+
title="MusicGen Small Demo",
|
| 62 |
+
description="Generate music using the MusicGen small model. Optionally remix with an audio file. Download the generated audio file.",
|
| 63 |
examples=[
|
| 64 |
+
["happy rock", None, 8],
|
| 65 |
+
["energetic EDM", None, 8],
|
| 66 |
+
["chillwave", "./assets/example_melody.mp3", 10]
|
| 67 |
]
|
| 68 |
).launch()
|