gradio/demo/chatbot_demo/run.py

27 lines
731 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 []
2022-07-25 09:18:42 +08:00
message = message.lower()
if message.startswith("how many"):
response = random.randint(1, 10)
2022-07-25 09:18:42 +08:00
elif message.startswith("how"):
2021-09-21 09:35:18 +08:00
response = random.choice(["Great", "Good", "Okay", "Bad"])
2022-07-25 09:18:42 +08:00
elif message.startswith("where"):
2021-09-21 09:35:18 +08:00
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()