Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chatbot Examples #8966

Merged
merged 59 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
d5ecc28
examples
dawoodkhan82 Aug 1, 2024
fffad3a
examples
dawoodkhan82 Aug 6, 2024
929cd98
first pass
dawoodkhan82 Aug 7, 2024
78c195f
remove comments
dawoodkhan82 Aug 7, 2024
838526e
remove comments
dawoodkhan82 Aug 7, 2024
3df4de1
add changeset
gradio-pr-bot Aug 7, 2024
38b01e1
merge
dawoodkhan82 Aug 15, 2024
8ffeb4e
Fix chatinterface e2e test (#9104)
freddyaboulton Aug 14, 2024
b355d8e
fix version + pkg name (#9110)
pngwn Aug 14, 2024
bdfb120
fix dev (#9115)
pngwn Aug 14, 2024
7525bf8
Be able to set optimizeDeps options in gradio.config.js (#9091)
freddyaboulton Aug 14, 2024
79b7986
Better text styling on docs (#9108)
aliabd Aug 14, 2024
9a986c7
setup npm-previews of all packages (#9118)
pngwn Aug 15, 2024
4350687
Fix chatinterface multimodal bug (#9119)
freddyaboulton Aug 15, 2024
9e7b91a
Merge branch '5.0-dev' into chatbot-examples
dawoodkhan82 Aug 18, 2024
e95269e
chatbot examples
dawoodkhan82 Aug 19, 2024
3e38810
examples changes
dawoodkhan82 Aug 22, 2024
5f55c39
chatinterface
dawoodkhan82 Aug 27, 2024
65c54ce
chatinterface
dawoodkhan82 Aug 28, 2024
102f023
Merge branch '5.0-dev' into chatbot-examples
dawoodkhan82 Aug 28, 2024
9ed65fb
pr fixes
dawoodkhan82 Aug 30, 2024
8a48fcf
merge
dawoodkhan82 Aug 30, 2024
6b78118
remove html demo change
dawoodkhan82 Aug 30, 2024
078b896
suggestion width
dawoodkhan82 Aug 30, 2024
645d7da
Merge branch '5.0-dev' into chatbot-examples
abidlabs Aug 31, 2024
e1b475e
type fixes
dawoodkhan82 Aug 31, 2024
05537ec
merge
dawoodkhan82 Aug 31, 2024
88cd30c
format
dawoodkhan82 Aug 31, 2024
6e4b8b2
comment our examples test
dawoodkhan82 Aug 31, 2024
c7dce96
remove cache
dawoodkhan82 Aug 31, 2024
17a50c9
comment example caching test
dawoodkhan82 Sep 1, 2024
e3bb57e
bug fix
dawoodkhan82 Sep 5, 2024
8b2e574
Merge branch '5.0-dev' into chatbot-examples
dawoodkhan82 Sep 5, 2024
08d6784
bug fix
dawoodkhan82 Sep 5, 2024
9ea0f9e
format
dawoodkhan82 Sep 5, 2024
5b7d0fb
Merge branch '5.0-dev' into chatbot-examples
abidlabs Sep 11, 2024
895f843
type fix
dawoodkhan82 Sep 11, 2024
152f630
Proposal: remove `gr.make_waveform` and remove `matplotlib` as a depe…
abidlabs Sep 11, 2024
ee6f8f5
Dont move files to cache automatically in chatbot postprocess (#9303)
freddyaboulton Sep 11, 2024
59b0695
test fix
dawoodkhan82 Sep 12, 2024
18aefcc
format
dawoodkhan82 Sep 12, 2024
bafc39e
changes
Sep 16, 2024
a47072b
Merge branch 'chatbot-examples' of https://github.com/gradio-app/grad…
Sep 16, 2024
c958b21
update guide
dawoodkhan82 Sep 16, 2024
2190363
cache examples
dawoodkhan82 Sep 18, 2024
5a3299a
merge
dawoodkhan82 Sep 18, 2024
57ba214
add changeset
gradio-pr-bot Sep 18, 2024
b5bf42b
format
dawoodkhan82 Sep 18, 2024
a5dcec7
changes
Sep 19, 2024
c1497b9
changes
Sep 19, 2024
7cfdac8
changes
Sep 20, 2024
3b50bfe
changes
Sep 20, 2024
c60095c
changes
Sep 20, 2024
c9e65d9
changes
Sep 20, 2024
f39afd1
changes
Sep 20, 2024
6fddee7
format
abidlabs Sep 20, 2024
c4c5ad4
fixes
abidlabs Sep 20, 2024
182d2bf
Merge branch '5.0-dev' into chatbot-examples
abidlabs Sep 20, 2024
edb40f6
test chat interface fixes
abidlabs Sep 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/dark-moose-shine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/chatbot": minor
"gradio": minor
---

feat:Chatbot Examples
1 change: 0 additions & 1 deletion demo/chatbot_multimodal/requirements.txt

This file was deleted.

2 changes: 1 addition & 1 deletion demo/chatbot_multimodal/run.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_multimodal"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio plotly"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('files')\n", "!wget -q -O files/avatar.png https://github.com/gradio-app/gradio/raw/main/demo/chatbot_multimodal/files/avatar.png\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/chatbot_multimodal/tuples_testcase.py"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import time\n", "\n", "# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.\n", "\n", "def print_like_dislike(x: gr.LikeData):\n", " print(x.index, x.value, x.liked)\n", "\n", "def add_message(history, message):\n", " for x in message[\"files\"]:\n", " history.append({\"role\": \"user\", \"content\": {\"path\": x}})\n", " if message[\"text\"] is not None:\n", " history.append({\"role\": \"user\", \"content\": message[\"text\"]})\n", " return history, gr.MultimodalTextbox(value=None, interactive=False)\n", "\n", "def bot(history: list):\n", " response = \"**That's cool!**\"\n", " history.append({\"role\": \"assistant\", \"content\": \"\"})\n", " for character in response:\n", " history[-1]['content'] += character\n", " time.sleep(0.05)\n", " yield history\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot(\n", " elem_id=\"chatbot\",\n", " bubble_full_width=False,\n", " type=\"messages\"\n", " )\n", "\n", " chat_input = gr.MultimodalTextbox(interactive=True,\n", " file_count=\"multiple\",\n", " placeholder=\"Enter message or upload file...\", show_label=False)\n", "\n", " chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])\n", " bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name=\"bot_response\")\n", " bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])\n", "\n", " chatbot.like(print_like_dislike, None, None)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_multimodal"]}, {"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": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/chatbot_multimodal/tuples_testcase.py"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import time\n", "\n", "# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.\n", "\n", "def print_like_dislike(x: gr.LikeData):\n", " print(x.index, x.value, x.liked)\n", "\n", "def add_message(history, message):\n", " for x in message[\"files\"]:\n", " history.append({\"role\": \"user\", \"content\": {\"path\": x}})\n", " if message[\"text\"] is not None:\n", " history.append({\"role\": \"user\", \"content\": message[\"text\"]})\n", " return history, gr.MultimodalTextbox(value=None, interactive=False)\n", "\n", "def bot(history: list):\n", " response = \"**That's cool!**\"\n", " history.append({\"role\": \"assistant\", \"content\": \"\"})\n", " for character in response:\n", " history[-1]['content'] += character\n", " time.sleep(0.05)\n", " yield history\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot(\n", " elem_id=\"chatbot\",\n", " bubble_full_width=False,\n", " type=\"messages\"\n", " )\n", "\n", " chat_input = gr.MultimodalTextbox(interactive=True,\n", " file_count=\"multiple\",\n", " placeholder=\"Enter message or upload file...\", show_label=False)\n", "\n", " chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])\n", " bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name=\"bot_response\")\n", " bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])\n", "\n", " chatbot.like(print_like_dislike, None, None)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
Binary file added demo/chatbot_suggestions/files/cantina.wav
Binary file not shown.
1 change: 1 addition & 0 deletions demo/chatbot_suggestions/run.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_suggestions"]}, {"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": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('files')\n", "!wget -q -O files/avatar.png https://github.com/gradio-app/gradio/raw/main/demo/chatbot_suggestions/files/avatar.png\n", "!wget -q -O files/cantina.wav https://github.com/gradio-app/gradio/raw/main/demo/chatbot_suggestions/files/cantina.wav"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import os\n", "# Multimodal Chatbot demo that shows support for suggestions (example messages shown within the chatbot).\n", "\n", "def print_like_dislike(x: gr.LikeData):\n", " print(x.index, x.value, x.liked)\n", "\n", "def add_message(history, message):\n", " for x in message[\"files\"]:\n", " history.append(((x,), None))\n", " if message[\"text\"] is not None:\n", " history.append((message[\"text\"], None))\n", " return history, gr.MultimodalTextbox(value=None, interactive=False)\n", "\n", "def append_suggestion_message(x: gr.SelectData, history):\n", " if x.value[\"text\"] is not None:\n", " history.append((x.value[\"text\"], None))\n", " if \"files\" in x.value:\n", " if isinstance(x.value[\"files\"], list):\n", " for file in x.value[\"files\"]:\n", " history.append((file, None))\n", " else:\n", " history.append((x.value[\"files\"], None))\n", " return history\n", "\n", "def respond(history):\n", " history[-1][1] = \"Cool!\"\n", " return history\n", "\n", "\n", "with gr.Blocks(fill_height=True) as demo:\n", " chatbot = gr.Chatbot(\n", " elem_id=\"chatbot\",\n", " bubble_full_width=False,\n", " scale=1,\n", " placeholder='<h1 style=\"font-weight: bold; color: #FF6B6B; text-align: center; font-size: 48px; font-family: Arial, sans-serif;\">Welcome to Gradio!</h1>',\n", " suggestions=[{\"icon\": os.path.join(os.path.abspath(''), \"files/avatar.png\"), \"display_text\": \"Display Text Here!\", \"text\": \"Try this example with this audio.\", \"files\": [os.path.join(os.path.abspath(''), \"files/cantina.wav\")]}, {\"text\": \"Try this example with this image.\", \"files\": [os.path.join(os.path.abspath(''), \"files/avatar.png\")]}, {\"text\": \"This is just text, no files!\"}, {\"text\": \"Try this example with this image.\", \"files\": [os.path.join(os.path.abspath(''), \"files/avatar.png\"), os.path.join(os.path.abspath(''), \"files/avatar.png\")]}, {\"text\": \"Try this example with this Audio.\", \"files\": [os.path.join(os.path.abspath(''), \"files/cantina.wav\")]}]\n", " )\n", "\n", " chat_input = gr.MultimodalTextbox(interactive=True,\n", " file_count=\"multiple\",\n", " placeholder=\"Enter message or upload file...\", show_label=False)\n", "\n", " chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])\n", " bot_msg = chat_msg.then(respond, chatbot, chatbot, api_name=\"bot_response\")\n", " bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])\n", "\n", " chatbot.like(print_like_dislike, None, None)\n", " chatbot.suggestion_select(append_suggestion_message, [chatbot], [chatbot]).then(respond, chatbot, chatbot, api_name=\"respond\")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
52 changes: 52 additions & 0 deletions demo/chatbot_suggestions/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import gradio as gr
import os
# Multimodal Chatbot demo that shows support for suggestions (example messages shown within the chatbot).

def print_like_dislike(x: gr.LikeData):
print(x.index, x.value, x.liked)

def add_message(history, message):
for x in message["files"]:
history.append(((x,), None))
if message["text"] is not None:
history.append((message["text"], None))
return history, gr.MultimodalTextbox(value=None, interactive=False)

def append_suggestion_message(x: gr.SelectData, history):
if x.value["text"] is not None:
history.append((x.value["text"], None))
if "files" in x.value:
if isinstance(x.value["files"], list):
for file in x.value["files"]:
history.append((file, None))
else:
history.append((x.value["files"], None))
return history

def respond(history):
history[-1][1] = "Cool!"
return history


with gr.Blocks(fill_height=True) as demo:
chatbot = gr.Chatbot(
elem_id="chatbot",
bubble_full_width=False,
scale=1,
placeholder='<h1 style="font-weight: bold; color: #FF6B6B; text-align: center; font-size: 48px; font-family: Arial, sans-serif;">Welcome to Gradio!</h1>',
suggestions=[{"icon": os.path.join(os.path.dirname(__file__), "files/avatar.png"), "display_text": "Display Text Here!", "text": "Try this example with this audio.", "files": [os.path.join(os.path.dirname(__file__), "files/cantina.wav")]}, {"text": "Try this example with this image.", "files": [os.path.join(os.path.dirname(__file__), "files/avatar.png")]}, {"text": "This is just text, no files!"}, {"text": "Try this example with this image.", "files": [os.path.join(os.path.dirname(__file__), "files/avatar.png"), os.path.join(os.path.dirname(__file__), "files/avatar.png")]}, {"text": "Try this example with this Audio.", "files": [os.path.join(os.path.dirname(__file__), "files/cantina.wav")]}]
)

chat_input = gr.MultimodalTextbox(interactive=True,
file_count="multiple",
placeholder="Enter message or upload file...", show_label=False)

chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
bot_msg = chat_msg.then(respond, chatbot, chatbot, api_name="bot_response")
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])

chatbot.like(print_like_dislike, None, None)
chatbot.suggestion_select(append_suggestion_message, [chatbot], [chatbot]).then(respond, chatbot, chatbot, api_name="respond")

if __name__ == "__main__":
demo.launch()
Binary file added demo/chatinterface_multimodal/files/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/chatinterface_multimodal/files/cantina.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion demo/chatinterface_multimodal/run.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_multimodal"]}, {"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", "\n", "def echo(message, history):\n", " return message[\"text\"]\n", "\n", "demo = gr.ChatInterface(\n", " fn=echo,\n", " type=\"messages\",\n", " examples=[{\"text\": \"hello\"}, {\"text\": \"hola\"}, {\"text\": \"merhaba\"}],\n", " title=\"Echo Bot\",\n", " multimodal=True,\n", ")\n", "demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_multimodal"]}, {"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": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('files')\n", "!wget -q -O files/avatar.png https://github.com/gradio-app/gradio/raw/main/demo/chatinterface_multimodal/files/avatar.png\n", "!wget -q -O files/cantina.wav https://github.com/gradio-app/gradio/raw/main/demo/chatinterface_multimodal/files/cantina.wav"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "def echo(message, history):\n", " return message[\"text\"]\n", "\n", "demo = gr.ChatInterface(\n", " fn=echo,\n", " type=\"messages\",\n", " examples=[{\"text\": \"hello\"}, {\"text\": \"hola\"}, {\"text\": \"merhaba\"}],\n", " title=\"Echo Bot\",\n", " multimodal=True,\n", ")\n", "demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
Loading
Loading