mirror of
https://github.com/gradio-app/gradio.git
synced 2024-12-15 02:11:15 +08:00
ed5178acd3
* Revert "remove chatbot_streaming from guide (#4729)"
This reverts commit 21c0198da1
.
* bring back streaming demo
29 lines
745 B
Python
29 lines
745 B
Python
import gradio as gr
|
|
import random
|
|
import time
|
|
|
|
with gr.Blocks() as demo:
|
|
chatbot = gr.Chatbot()
|
|
msg = gr.Textbox()
|
|
clear = gr.Button("Clear")
|
|
|
|
def user(user_message, history):
|
|
return "", history + [[user_message, None]]
|
|
|
|
def bot(history):
|
|
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
|
history[-1][1] = ""
|
|
for character in bot_message:
|
|
history[-1][1] += 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)
|
|
|
|
demo.queue()
|
|
if __name__ == "__main__":
|
|
demo.launch()
|