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

Support additional_outputs in gr.ChatInterface #10071

Merged
merged 33 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4d1a468
artifacts
abidlabs Nov 28, 2024
fffe8bb
add changeset
gradio-pr-bot Nov 28, 2024
b4e3868
chatinterface
abidlabs Nov 28, 2024
afcf979
Merge branch 'additional-outputs' of github.com:gradio-app/gradio int…
abidlabs Nov 28, 2024
c34f43e
guide fixes
abidlabs Nov 28, 2024
ab6e6b9
example
abidlabs Nov 28, 2024
4d7b588
zerogpu
abidlabs Nov 29, 2024
64f7d0d
Revert "zerogpu"
abidlabs Nov 29, 2024
c09a446
changes
abidlabs Nov 29, 2024
db63a22
submit fn
abidlabs Nov 29, 2024
a5bcbba
chat interface
abidlabs Nov 29, 2024
6865a97
Merge branch 'main' into additional-outputs
abidlabs Nov 29, 2024
31cac4c
changes
abidlabs Nov 29, 2024
c43a4c5
Merge branch 'additional-outputs' of github.com:gradio-app/gradio int…
abidlabs Nov 29, 2024
5d448ba
add changeset
gradio-pr-bot Nov 29, 2024
1eb09df
notebook
abidlabs Nov 29, 2024
4b21b70
Merge branch 'additional-outputs' of github.com:gradio-app/gradio int…
abidlabs Nov 29, 2024
7d4f45a
lint
abidlabs Nov 29, 2024
ffd25ca
type
abidlabs Nov 29, 2024
67ad26b
Merge branch 'main' into additional-outputs
abidlabs Dec 1, 2024
eacc2fd
changes
abidlabs Dec 2, 2024
29b8d55
regex
abidlabs Dec 2, 2024
eb3ddd3
add changeset
gradio-pr-bot Dec 2, 2024
89e7ac4
fixes
abidlabs Dec 2, 2024
d6ced3e
Merge branch 'additional-outputs' of github.com:gradio-app/gradio int…
abidlabs Dec 2, 2024
8f81da7
line
abidlabs Dec 2, 2024
0faef73
fixes
abidlabs Dec 2, 2024
df18abd
formatting
abidlabs Dec 2, 2024
0b0433f
change
abidlabs Dec 2, 2024
dfc7bcb
change
abidlabs Dec 2, 2024
93a0638
add guard
abidlabs Dec 2, 2024
fe603f9
link to playground demo
aliabd Dec 2, 2024
591dcd0
add changeset
gradio-pr-bot Dec 2, 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/lazy-symbols-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gradio": minor
"website": minor
---

feat:Support `additional_outputs` in `gr.ChatInterface`
1 change: 1 addition & 0 deletions demo/chatinterface_artifacts/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: chatinterface_artifacts"]}, {"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", "python_code = \"\"\"\n", "def fib(n):\n", " if n <= 0:\n", " return 0\n", " elif n == 1:\n", " return 1\n", " else:\n", " return fib(n-1) + fib(n-2)\n", "\"\"\"\n", "\n", "js_code = \"\"\"\n", "function fib(n) {\n", " if (n <= 0) return 0;\n", " if (n === 1) return 1;\n", " return fib(n - 1) + fib(n - 2);\n", "}\n", "\"\"\"\n", "\n", "def chat(message, history):\n", " if \"python\" in message.lower():\n", " return \"Type Python or JavaScript to see the code.\", gr.Code(language=\"python\", value=python_code)\n", " elif \"javascript\" in message.lower():\n", " return \"Type Python or JavaScript to see the code.\", gr.Code(language=\"javascript\", value=js_code)\n", " else:\n", " return \"Please ask about Python or JavaScript.\", None\n", "\n", "with gr.Blocks() as demo:\n", " code = gr.Code(render=False)\n", " with gr.Row():\n", " with gr.Column():\n", " gr.Markdown(\"<center><h1>Write Python or JavaScript</h1></center>\")\n", " gr.ChatInterface(\n", " chat,\n", " examples=[\"Python\", \"JavaScript\"],\n", " additional_outputs=[code],\n", " type=\"messages\"\n", " )\n", " with gr.Column():\n", " gr.Markdown(\"<center><h1>Code Artifacts</h1></center>\")\n", " code.render()\n", "\n", "demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
44 changes: 44 additions & 0 deletions demo/chatinterface_artifacts/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import gradio as gr

python_code = """
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
"""

js_code = """
function fib(n) {
if (n <= 0) return 0;
if (n === 1) return 1;
return fib(n - 1) + fib(n - 2);
}
"""

def chat(message, history):
if "python" in message.lower():
return "Type Python or JavaScript to see the code.", gr.Code(language="python", value=python_code)
elif "javascript" in message.lower():
return "Type Python or JavaScript to see the code.", gr.Code(language="javascript", value=js_code)
else:
return "Please ask about Python or JavaScript.", None

with gr.Blocks() as demo:
code = gr.Code(render=False)
with gr.Row():
with gr.Column():
gr.Markdown("<center><h1>Write Python or JavaScript</h1></center>")
gr.ChatInterface(
chat,
examples=["Python", "JavaScript"],
additional_outputs=[code],
type="messages"
)
with gr.Column():
gr.Markdown("<center><h1>Code Artifacts</h1></center>")
code.render()

demo.launch()
250 changes: 139 additions & 111 deletions gradio/chat_interface.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion guides/05_chatbots/01_creating-a-chatbot-fast.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# How to Create a Chatbot with Gradio

Tags: NLP, LLM, CHATBOT
Tags: LLM, CHATBOT, NLP

## Introduction

Expand Down
8 changes: 5 additions & 3 deletions guides/05_chatbots/02_chatinterface-examples.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Using Popular LLM libraries and APIs

Tags: LLM, CHATBOT, API

In this Guide, we go through several examples of how to use `gr.ChatInterface` with popular LLM libraries and API providers.

We will cover the following libraries and API providers:
Expand Down Expand Up @@ -37,7 +39,7 @@ Tip: For quick prototyping, the <a href='https://github.com/gradio-app/openai-g

## Hugging Face `transformers`

Of course, in many cases you want to run a chatbot locally. Here's the equivalent example using Together's RedPajama model, from Hugging Face (this requires you to have a GPU with CUDA).
Of course, in many cases you want to run a chatbot locally. Here's the equivalent example using the SmolLM2-135M-Instruct model using the Hugging Face `transformers` library.

$code_llm_hf_transformers

Expand All @@ -47,15 +49,15 @@ The SambaNova Cloud API provides access to full-precision open-source models, su

$code_llm_sambanova

Tip: For quick prototyping, the <a href='https://github.com/gradio-app/sambanova-gradio'>sambanova-gradio library</a> makes it even easier to build chatbots on top of OpenAI models.
Tip: For quick prototyping, the <a href='https://github.com/gradio-app/sambanova-gradio'>sambanova-gradio library</a> makes it even easier to build chatbots on top of SambaNova models.

## Hyperbolic

The Hyperbolic AI API provides access to many open-source models, such as the Llama family. Here's an example of how to build a Gradio app around the SambaNova API

$code_llm_hyperbolic

Tip: For quick prototyping, the <a href='https://github.com/HyperbolicLabs/hyperbolic-gradio'>hyperbolic-gradio library</a> makes it even easier to build chatbots on top of OpenAI models.
Tip: For quick prototyping, the <a href='https://github.com/HyperbolicLabs/hyperbolic-gradio'>hyperbolic-gradio library</a> makes it even easier to build chatbots on top of Hyperbolic models.


## Anthropic's Claude
Expand Down
26 changes: 26 additions & 0 deletions js/_website/generate_jsons/src/docs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import os
import re
import requests
import base64
import urllib.parse



from gradio_client.documentation import document_cls, generate_documentation
Expand Down Expand Up @@ -97,11 +100,34 @@ def add_guides():
add_guides()


def generate_playground_link(demo_name):
playground_url = "https://gradio.app/playground?demo=Blank"
with open(os.path.join(DEMOS_DIR, demo_name, "run.py")) as f:
demo_code = f.read()
encoded_code = base64.b64encode(demo_code.encode('utf-8')).decode('utf-8')
encoded_code_url = urllib.parse.quote(encoded_code, safe='')
playground_url += "&code=" + encoded_code_url
if "requirements.txt" in os.listdir(os.path.join(DEMOS_DIR, demo_name)):
with open(os.path.join(DEMOS_DIR, demo_name, "requirements.txt")) as f:
requirements = f.read()
if requirements:
encoded_reqs = base64.b64encode(requirements.encode('utf-8')).decode('utf-8')
encoded_reqs_url = urllib.parse.quote(encoded_reqs, safe='')
playground_url += "&reqs=" + encoded_reqs_url
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool that it handles reqs as well!

return f"<a href='{playground_url}' target='_blank'>demo/{demo_name}</a>"


def escape_parameters(parameters):
new_parameters = []
for param in parameters:
param = param.copy() # Manipulating the list item directly causes issues, so copy it first
param["doc"] = html.escape(param["doc"]) if param["doc"] else param["doc"]
if param["doc"] and "$demo/" in param["doc"]:
param["doc"] = re.sub(
r"\$demo/(\w+)",
lambda m: generate_playground_link(m.group(1)),
param["doc"],
)
new_parameters.append(param)
assert len(new_parameters) == len(parameters)
return new_parameters
Expand Down
Loading