Update chatbot simple sample code for Gradio 6.x

#2
by js7123 - opened
Files changed (2) hide show
  1. run.ipynb +1 -1
  2. run.py +12 -13
run.ipynb CHANGED
@@ -1 +1 @@
1
- {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_simple"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import random\n", "import time\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot()\n", " msg = gr.Textbox()\n", " clear = gr.ClearButton([msg, chatbot])\n", "\n", " def respond(message, chat_history):\n", " bot_message = random.choice([\"How are you?\", \"Today is a great day\", \"I'm very hungry\"])\n", " chat_history.append({\"role\": \"user\", \"content\": message})\n", " chat_history.append({\"role\": \"assistant\", \"content\": bot_message})\n", " time.sleep(2)\n", " return \"\", chat_history\n", "\n", " msg.submit(respond, [msg, chatbot], [msg, chatbot])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_simple"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import random\n", "import time\n", "\n", "def respond(message, chat_history):\n", " time.sleep(2) # simulates a delay\n", " bot_message=random.choice([\"How are you?\", \"Today is a great day\", \"I'm very hungry\"])\n", " return bot_message_text\n", "\n", "if __name__ == \"__main__\":\n", " demo=gr.ChatInterface(\n", "fn=respond,\n", " chatbot=gr.Chatbot(height=400, placeholder=\"Start chatting...\"),\n", " textbox=gr.Textbox(placeholder=\"Type a message...\"),\n", " title=\"Simple Gradio 6.x Chatbot\"\n", " )\n", " demo.launch(debug=True)\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py CHANGED
@@ -2,19 +2,18 @@ import gradio as gr
2
  import random
3
  import time
4
 
5
- with gr.Blocks() as demo:
6
- chatbot = gr.Chatbot()
7
- msg = gr.Textbox()
8
- clear = gr.ClearButton([msg, chatbot])
9
 
10
- def respond(message, chat_history):
11
- bot_message = random.choice(["How are you?", "Today is a great day", "I'm very hungry"])
12
- chat_history.append({"role": "user", "content": message})
13
- chat_history.append({"role": "assistant", "content": bot_message})
14
- time.sleep(2)
15
- return "", chat_history
16
-
17
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
18
 
19
  if __name__ == "__main__":
20
- demo.launch()
 
 
 
 
 
 
 
 
2
  import random
3
  import time
4
 
 
 
 
 
5
 
6
+ def respond(message, chat_history):
7
+ time.sleep(2) # simulate delay
8
+ bot_message_text=random.choice(["How are you?", "Today is a great day", "I'm very hungry"])
9
+ return bot_message_text
 
 
 
 
10
 
11
  if __name__ == "__main__":
12
+ demo = gr.ChatInterface(
13
+ fn=respond,
14
+ chatbot = gr.Chatbot(height=400, placeholder="Start chatting..."),
15
+ textbox=gr.Textbox(placeholder="Type a message..."),
16
+ title="Simple Gradio 6.x Chatbot",
17
+ description="A random response chatbot."
18
+ )
19
+ demo.launch(debug=True)