Spaces:
Running
Running
Update chatbot simple sample code for Gradio 6.x
#2
by js7123 - opened
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", "
|
|
|
|
| 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
time.sleep(2)
|
| 15 |
-
return "", chat_history
|
| 16 |
-
|
| 17 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 18 |
|
| 19 |
if __name__ == "__main__":
|
| 20 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|