Hjgugugjhuhjggg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,57 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import random
|
3 |
import torch
|
4 |
-
|
5 |
-
from
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
4 |
+
from TTS.api import TTS
|
5 |
+
import librosa
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
9 |
+
model_musicgen = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
10 |
+
model_tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to("cpu")
|
11 |
+
|
12 |
+
def generate_music(text_prompt, audio_prompt=None, tts_text=None):
|
13 |
+
inputs = processor(text=text_prompt, padding=True, return_tensors="pt")
|
14 |
+
max_duration_sec = 600
|
15 |
+
min_duration_sec = 30
|
16 |
+
max_new_tokens = int(max_duration_sec * model_musicgen.config.audio_length // model_musicgen.config.generation_length)
|
17 |
+
min_new_tokens = int(min_duration_sec * model_musicgen.config.audio_length // model_musicgen.config.generation_length)
|
18 |
+
audio_values = model_musicgen.generate(**inputs.to("cpu"), max_new_tokens=max_new_tokens, min_new_tokens=min_new_tokens)
|
19 |
+
music = audio_values.cpu().numpy()[0]
|
20 |
+
|
21 |
+
if audio_prompt is not None and tts_text is not None:
|
22 |
+
cloned_audio = model_tts.tts_with_vc(tts_text, speaker_wav=audio_prompt, language="es")
|
23 |
+
|
24 |
+
if model_tts.synthesizer.output_sample_rate != 44100:
|
25 |
+
cloned_audio, _ = librosa.resample(cloned_audio, model_tts.synthesizer.output_sample_rate, 44100)
|
26 |
+
|
27 |
+
try:
|
28 |
+
music_np = librosa.util.buf_to_float(music, n_bytes=2)
|
29 |
+
|
30 |
+
if len(music_np) > len(cloned_audio):
|
31 |
+
padding = np.zeros(len(music_np) - len(cloned_audio))
|
32 |
+
cloned_audio = np.concatenate([cloned_audio, padding])
|
33 |
+
elif len(cloned_audio) > len(music_np):
|
34 |
+
padding = np.zeros(len(cloned_audio) - len(music_np))
|
35 |
+
music_np = np.concatenate([music_np, padding])
|
36 |
+
|
37 |
+
combined_audio = music_np + cloned_audio
|
38 |
+
return (44100, combined_audio)
|
39 |
+
except ImportError:
|
40 |
+
print("Error: Se requiere librosa para combinar audio. Instale con 'pip install librosa'")
|
41 |
+
return (44100, music)
|
42 |
+
|
43 |
+
return (44100, music)
|
44 |
+
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=generate_music,
|
47 |
+
inputs=[
|
48 |
+
gr.Textbox(label="Descripción de la música"),
|
49 |
+
gr.Audio(source="microphone", type="filepath", label="Audio de voz (Opcional)", optional=True),
|
50 |
+
gr.Textbox(label="Texto para clonar la voz (Opcional)", optional=True),
|
51 |
+
],
|
52 |
+
outputs=gr.Audio(label="Música generada", type="numpy"),
|
53 |
+
title="Generador de Música con MusicGen y XTTS",
|
54 |
+
description="Introduce una descripción de la música que deseas generar y opcionalmente un audio de voz para clonar con texto.",
|
55 |
+
)
|
56 |
+
|
57 |
+
iface.launch()
|