gradio/demo/chatbot_demo/run.py

29 lines
704 B
Python
Raw Normal View History

2021-09-21 09:35:18 +08:00
import random
import gradio as gr
2022-01-14 04:22:38 +08:00
def chat(message, history):
history = history or []
2021-09-21 09:35:18 +08:00
if message.startswith("How many"):
response = random.randint(1, 10)
2021-09-21 09:35:18 +08:00
elif message.startswith("How"):
response = random.choice(["Great", "Good", "Okay", "Bad"])
elif message.startswith("Where"):
response = random.choice(["Here", "There", "Somewhere"])
else:
response = "I don't know"
history.append((message, response))
2022-02-22 13:30:22 +08:00
return history, history
2021-09-21 09:35:18 +08:00
chatbot = gr.Chatbot().style(color_map=("green", "pink"))
2022-03-28 23:11:14 +08:00
demo = gr.Interface(
chat,
["text", "state"],
[chatbot, "state"],
allow_flagging="never",
)
2021-09-21 09:35:18 +08:00
if __name__ == "__main__":
2022-03-28 23:11:14 +08:00
demo.launch()