File size: 3,932 Bytes
fd7a397
 
 
6134fc9
 
465ee60
6134fc9
 
 
fd7a397
 
 
 
 
 
b2d4aa4
fd7a397
b2d4aa4
465ee60
 
6e5a5d5
fd7a397
48443f7
 
 
 
 
 
 
 
 
fd7a397
e446f13
6134fc9
48443f7
6134fc9
 
fd7a397
98c6a7f
48443f7
 
 
a665630
 
17bdca8
98c6a7f
6134fc9
 
 
fd7a397
6134fc9
 
fd7a397
48443f7
 
 
 
 
 
 
 
 
 
 
 
 
fd7a397
 
48443f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd7a397
 
48443f7
 
 
fd7a397
48443f7
60d3210
1
2
3
4
5
6
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from diffusers import AudioLDMPipeline
import torch
import gradio as gr
from transformers import pipeline
#from googletrans import Translator
import os



if torch.cuda.is_available():
    device = "cuda"
    torch_dtype = torch.float16
else:
    device = "cpu"
    torch_dtype = torch.float32
print(device)
repo_id = "cvssp/audioldm-m-full"
pipe = AudioLDMPipeline.from_pretrained(repo_id, torch_dtype=torch_dtype)
pipe = pipe.to(device)
# pipe.unet = torch.compile(pipe.unet)
#pipe.unet = torch.compile(pipe.unet)



import base64

with open("Iso_Logotipo_Ceibal.png", "rb") as image_file:
    encoded_image = base64.b64encode(image_file.read()).decode()


def generate_sound(text,steps,audio_length,negative_prompt): 
    print(text)
    # text=translate_text(text)
    text = translate_text(text)
    negative_prompt = translate_text(negative_prompt)
    #translator = Translator()
    #text=translator.translate(text, src='es',dest="en").text
    print(text)
    waveforms = pipe(text, 
                     num_inference_steps=steps, 
                     audio_length_in_s=audio_length,
                     negative_prompt = negative_prompt).audios
    rate =16000
    return rate, waveforms[0]
    #return gr.make_waveform((rate, waveforms[0]))

es_en_translator = pipeline("translation",model = "Helsinki-NLP/opus-mt-es-en")


def translate_text(text):
    text = es_en_translator(text)[0].get("translation_text")
    return text

with gr.Blocks() as demo:
    gr.Markdown("""
    <center>
    <h1>
    Uso de AI para la generación de sonidos a partir de texto.
    </h1>
    <img src='data:image/jpg;base64,{}' width=200px>
    <h3>
    Con este espacio podrás generar sondios a partir de texto, intentá ser lo más descriptivo/a posible en el texto. Se puede usar directamente o podés cambiar ajustes, que impacto tiene cada uno está detallado en su descripción. Cambiá valores y mirá los resultados!
    </h3>
    <h4>El texto se traduce del español al inglés para alimetnar al modelo, también se puede escribir el texto de entrada en inglés.</h4>
    </center>
    """.format(encoded_image))
    with gr.Row():
        with gr.Column():
            gr.Markdown("Primero debes ingresar el texto para generar el sonido:")
            with gr.Row():
                with gr.Column(scale=4):
                    prompt = gr.Textbox(label="Texo base para generar la imagen") #Give prompt some real estate
                with gr.Column(scale=1, min_width=50):
                    btn = gr.Button("Generar") #Submit button side by side!
            with gr.Row():
                with gr.Accordion("Opciones avanzadas", open=False): #Let's hide the advanced options!
                        negative_prompt = gr.Textbox(label="Texto negativo para la generación", info='Al ingresar texto en este campo el modelo intentará alejarse lo mas posible del mismo, este puede ser "baja calidad"')
                        with gr.Row():
                            with gr.Column():
                                audio_len = gr.Slider(label="Duración del sonido", minimum=1, maximum=30, value=5, step = 1,
                                info="Cuánto mayor sonido, mayor será el tiempo de procesamiento.")
                                steps = gr.Slider(label="Paos de Inferencia", minimum=1, maximum=100, value=20,step =1 ,
                                info="Al aumentar los pasos de inferencia se puede acercar más a la descripción del texto pero con un mayor tiempo de procesamiento.")
                                examples = gr.Examples(inputs=[prompt,negative_prompt],examples=[["Un martillo golpeando madera","low quality"]])

        with gr.Column():
            output = gr.Audio(label="Resultado") #Move the output up too
            
    btn.click(fn=generate_sound, inputs=[prompt,steps,audio_len,negative_prompt], outputs=[output])  #steps,guidance,width,height]

gr.close_all()
demo.launch()