File size: 2,099 Bytes
e53f453
c8d06c3
e53f453
 
c8d06c3
8fa41ac
 
 
 
 
 
 
 
 
 
5551363
8fa41ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e53f453
 
 
 
c8d06c3
 
e53f453
 
 
 
 
 
 
 
 
 
 
c8d06c3
 
8fa41ac
e53f453
5551363
 
e53f453
c8d06c3
 
 
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
import os
import gradio as gr
from transformers import BarkModel, AutoProcessor
import torch

# función para el título animado 
js = """
function createGradioAnimation() {
    var container = document.createElement('div');
    container.id = 'gradio-animation';
    container.style.fontSize = '2em';
    container.style.fontWeight = 'bold';
    container.style.textAlign = 'center';
    container.style.marginBottom = '20px';

    var text = 'Audio Generation!';
    for (var i = 0; i < text.length; i++) {
        (function(i){
            setTimeout(function(){
                var letter = document.createElement('span');
                letter.style.opacity = '0';
                letter.style.transition = 'opacity 0.5s';
                letter.innerText = text[i];

                container.appendChild(letter);

                setTimeout(function() {
                    letter.style.opacity = '1';
                }, 50);
            }, i * 250);
        })(i);
    }

    var gradioContainer = document.querySelector('.gradio-container');
    gradioContainer.insertBefore(container, gradioContainer.firstChild);

    return 'Animation created';
}
"""
# Desactivar paralelismo en tokenizers para evitar advertencias
os.environ["TOKENIZERS_PARALLELISM"] = "false"

device = "cuda" if torch.cuda.is_available() else "cpu"

def say_something(text):
    processor = AutoProcessor.from_pretrained("suno/bark-small")

    model = BarkModel.from_pretrained("suno/bark-small").to(device)

    inputs = processor(text)
    audio_array = model.generate(**inputs)
    audio_array = audio_array.cpu().numpy().squeeze()

    # Devuelve el audio como un numpy array junto con el sample rate
    sample_rate = model.generation_config.sample_rate
    return (sample_rate, audio_array)
    

with gr.Blocks(js=js) as demo:
    textBox = gr.Textbox(label="Text", value="Hello! [laugths]. This is a test!")
    button = gr.Button("Generate Audio", variant="primary")
    audio_output = gr.Audio(label="Generated Audio")
    button.click(say_something, inputs=textBox, outputs=audio_output)


demo.launch()