gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
30.69k stars 2.28k forks source link

Handle special arguments when extracting parameter names for view API page #8400

Closed abidlabs closed 4 weeks ago

abidlabs commented 1 month ago

Closes: https://github.com/gradio-app/gradio/issues/8399

Test with:

import gradio as gr
import time

def echo(message, request: gr.Request, num):
    return message

demo = gr.Interface(
    echo,
    ["textbox", "slider"],
    ["textbox"],
)

demo.launch()

For the case of gr.ChatInterface, we can't get the original parameter names because we construct a wrapper function over the user's defined function, so in such cases, the parameter names are generated automatically:

image
gradio-pr-bot commented 1 month ago

🪼 branch checks and previews

• Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
:unicorn: Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/e686632a341e5789eb5cf482e2199c7a29dd8f2b/gradio-4.32.2-py3-none-any.whl

Install Gradio Python Client from this PR

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

Install Gradio JS Client from this PR

npm install https://gradio-builds.s3.amazonaws.com/e686632a341e5789eb5cf482e2199c7a29dd8f2b/gradio-client-0.20.1.tgz
gradio-pr-bot commented 1 month ago

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch

With the following changelog entry.

Handle special arguments when extracting parameter names for view API page

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](https://github.com/gradio-app/gradio/edit/fix-args/.changeset/fair-items-sort.md).
freddyaboulton commented 1 month ago

Can we use something like functools.wraps in the chat_interface case to preserve the original signature?

Also, I was wondering why we need to construct the special args within the chat_interface function. Shouldn't that be handled by the regular request life-cycle in Blocks.

abidlabs commented 1 month ago

Can we use something like functools.wraps in the chat_interface case to preserve the original signature?

I think so yes I'll take a look

Also, I was wondering why we need to construct the special args within the chat_interface function. Shouldn't that be handled by the regular request life-cycle in Blocks.

Sorry what are you referring to by this?

freddyaboulton commented 1 month ago

Sorry what are you referring to by this?

If we can use something like functools.wraps to preserve the original signature, then I don't think we need to call special_args within submit_fn because gr.Blocks will know to pass the request as an input if it sees the type hint.

abidlabs commented 4 weeks ago

Thanks @freddyaboulton for the suggestion. Refactored to use functools.wraps, so now we handle gr.ChatInterface with additional parameters correctly as well. For the example in the original issue, we now have:

image
abidlabs commented 4 weeks ago

Indeed. Thanks @freddyaboulton for the reviews and suggestions.