File size: 1,780 Bytes
0c97faa
1e937ce
db2ba20
1e937ce
 
d2e67c6
1e937ce
 
 
 
 
 
0c97faa
 
 
 
ccadd27
db2ba20
d67a1b2
d2e67c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e937ce
 
 
d2e67c6
1e937ce
 
 
 
 
 
d2e67c6
 
1e937ce
 
d2e67c6
0c97faa
 
 
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
import gradio as gr
import torch
from huggingface_hub import InferenceClient
from transformers import BarkModel
from transformers import AutoProcessor


model = BarkModel.from_pretrained("suno/bark-small")
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model = model.to(device)

processor = AutoProcessor.from_pretrained("suno/bark")

"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""

client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")

with gr.Blocks() as demo:
    chatbot = gr.Chatbot(type="messages")
    audio_box = gr.Audio(autoplay=True)
    msg = gr.Textbox(submit_btn=True)
    clear = gr.Button("Clear")

    def user(user_message, history: list):
        return "", history + [{"role": "user", "content": user_message}]

    def bot(history: list):
        history.append({"role": "assistant", "content": ""})
        for message in client.chat_completion(
            history,
            stream=True,
        ):
            token = message.choices[0].delta.content
            history[-1]["content"] += token
            yield history

        return history

    def read(history: list):
        text = history[-1]["content"]
        inputs = processor(text=text, return_tensors="pt").to(device)
        speech = model.generate(**inputs.to(device))
        sampling_rate = model.generation_config.sample_rate
        return tuple((sampling_rate, speech.cpu().numpy().squeeze()))

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    ).then(read, chatbot, audio_box)
    clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    demo.launch()