2024-07-10 19:08:06 +08:00
|
|
|
import gradio as gr
|
|
|
|
import random
|
|
|
|
import time
|
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
2024-07-12 19:53:28 +08:00
|
|
|
chatbot = gr.Chatbot(type="messages")
|
2024-07-10 19:08:06 +08:00
|
|
|
msg = gr.Textbox()
|
|
|
|
clear = gr.Button("Clear")
|
|
|
|
|
|
|
|
def user(user_message, history: list):
|
|
|
|
return "", history + [{"role": "user", "content": user_message}]
|
|
|
|
|
|
|
|
def bot(history: list):
|
|
|
|
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
|
|
|
history.append({"role": "assistant", "content": ""})
|
|
|
|
for character in bot_message:
|
|
|
|
history[-1]['content'] += character
|
|
|
|
time.sleep(0.05)
|
|
|
|
yield history
|
|
|
|
|
|
|
|
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
|
|
|
bot, chatbot, chatbot
|
|
|
|
)
|
|
|
|
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
demo.launch()
|