id2223-lab2 / app.py
EPark25's picture
Audio init
d2e67c6
raw
history blame
1.61 kB
import gradio as gr
from huggingface_hub import InferenceClient
from transformers import pipeline
from scipy.io.wavfile import write as write_wav
AUDIO_FILE_PATH = "bark_generation.wav"
synthesizer = pipeline("text-to-speech", "suno/bark-small")
"""
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 synthesize_audio(text):
speech = synthesizer(text, forward_params={"do_sample": True})
write_wav(AUDIO_FILE_PATH, rate=speech["sampling_rate"], data=speech["audio"])
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, None
synthesize_audio(history[-1]["content"])
return history, AUDIO_FILE_PATH
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, [chatbot, audio_box]
)
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()