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

JSON type fix in Client and and typing fix for /chat endpoint in gr.ChatInterface #10193

Merged
merged 12 commits into from
Dec 13, 2024

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Dec 13, 2024

While working on the API for gr.ChatInterface, I noticed that the typing for the gr.JSON component was incorrect. I'm not sure why make a special case for the gr.JSON component in the first place (@freddyaboulton do you remember?) but at least we should fix the typing here.

Also replaces the gr.Textbox in gr.ChatInterface with gr.JSON so that it correctly states that dictionaries can be returned.

For example, the following chat interface is now correctly typed:

import gradio as gr

def fake(message, history):
    if message.strip():
        return {
            "role": "assistant", 
            "content": {
                "path": "https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav"
                }
            }
    else:
        return "Please provide the name of an artist"

_, url, _ = gr.ChatInterface(
    fake,
    type="messages",
    textbox=gr.Textbox(placeholder="Which artist's music do you want to listen to?", scale=7),
    chatbot=gr.Chatbot(placeholder="Play music by any artist!"),
).launch()

from gradio_client import Client

client = Client(url)
client.view_api()

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Dec 13, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/c654ade0861f827272e7672c7ff241f7af0f188a/gradio-5.8.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@c654ade0861f827272e7672c7ff241f7af0f188a#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/c654ade0861f827272e7672c7ff241f7af0f188a/gradio-client-1.8.0.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/c654ade0861f827272e7672c7ff241f7af0f188a/dist/lite.js""></script>

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Dec 13, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
gradio_client patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

JSON type fix in Client and and typing fix for /chat endpoint in gr.ChatInterface

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs added the v: patch A change that requires a patch release label Dec 13, 2024
@abidlabs abidlabs changed the title JSON type fix in Client JSON type fix in Client and and typing fix for /chat endpoint in gr.ChatInterface Dec 13, 2024
@abidlabs
Copy link
Member Author

(For more context, once we the API set up and typed correctly, I'll start working on the guides on how to deploy a Discord bot, Slack bot, etc. directly from a ChatInterface).

@@ -926,7 +926,7 @@ def _json_schema_to_python_type(schema: Any, defs) -> str:
type_ = get_type(schema)
if type_ == {}:
if "json" in schema.get("description", {}):
return "Dict[Any, Any]"
return "str | list | dict"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we could collapse both cases into str | bool | int | float | list | dict since that covers the spectrum of possible json payloads

Copy link
Member Author

Choose a reason for hiding this comment

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

updated

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

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

Nice!

@abidlabs
Copy link
Member Author

Thanks @freddyaboulton!

@abidlabs abidlabs enabled auto-merge (squash) December 13, 2024 01:46
@abidlabs abidlabs merged commit 424365b into main Dec 13, 2024
21 checks passed
@abidlabs abidlabs deleted the json-type-fix branch December 13, 2024 01:58
@dawoodkhan82
Copy link
Collaborator

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v: patch A change that requires a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants