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

Improve URL handling in JS Client #8258

Merged
merged 23 commits into from
May 22, 2024
Merged

Improve URL handling in JS Client #8258

merged 23 commits into from
May 22, 2024

Conversation

hannahblair
Copy link
Collaborator

@hannahblair hannahblair commented May 10, 2024

Description

This PR ensures that we correctly handle URLs in various formats. E.g. We weren't handling URLs with paths (to an extent) or URLs with trailing slashes. This also resulted in issues around fetching theme.css, /config and /info in some cases.

The following should now work (providing a gradio space is to be found at these URLs):

To test, run this python app and mount the gradio app on various paths:

import uvicorn
from fastapi import FastAPI

import gradio as gr


def predict(text):
    return text

with gr.Blocks() as block:
    text_in = gr.Textbox()
    text_out = gr.Textbox()
    btn = gr.Button("Predict")
    btn.click(predict, inputs=[text_in], outputs=[text_out], api_name="predict")

block.queue()
app = FastAPI()

gr.mount_gradio_app(app, block, path="")
# gr.mount_gradio_app(app, block, path="/")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)

then in a separate app (or in serve an app as a static file with fastAPI), test you can connect to the app.

const app = await Client.connect("hmb/test-args");
const app = await Client.connect("http://0.0.0.0:7860/app/gradio");
const app = await Client.connect("http://0.0.0.0:7860/");
// etc 

You can also test that it fixes #4463 by testing embedding with the custom path:

import uvicorn
from fastapi import FastAPI

import gradio as gr

app = FastAPI()

@app.get('/app')
def hello():
    return {"message":"hello"}

g_app = gr.Interface(
    lambda x: f"Hello {x}",
    "textbox",
    "textbox",
)

app = gr.mount_gradio_app(app, g_app, path="/app/gradio")

uvicorn.run(app,host="0.0.0.0",port=8080)
<script type="module" src="http://localhost:4321/gradio.js"></script>

<gradio-app src="http://0.0.0.0:8080/app/gradio"></gradio-app>

Closes: #4469
Closes: #4463
Closes: #3472
Closes: #8154
Closes: #8199
Closes: #8073
(probably) Closes: #8129

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented May 10, 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-builds.s3.amazonaws.com/f17855cf0d974ad4076ee8df3e8c3ce60eef0cee/gradio-4.31.5-py3-none-any.whl

Install Gradio Python Client from this PR

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

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented May 10, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/client patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Improve URL handling in JS Client

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.

@hannahblair hannahblair marked this pull request as ready for review May 14, 2024 20:01
Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

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

Code LGTM!

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

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

One small comment, otherwise looks great.

client/js/src/helpers/api_info.ts Outdated Show resolved Hide resolved
Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks @hannahblair!

@hannahblair hannahblair merged commit 1f8e5c4 into main May 22, 2024
7 checks passed
@hannahblair hannahblair deleted the root-url-client branch May 22, 2024 17:47
@pngwn pngwn mentioned this pull request May 22, 2024
@abidlabs
Copy link
Member

Nice!

@autherous
Copy link

Thanks, Have been looking for this update.

Getting this error, when trying to install it
Unable to create package with no name

Is this the right way to install it

$ ./poetry add  https://gradio-builds.s3.amazonaws.com/f17855cf0d974ad4076ee8df3e8c3ce60eef0cee/gradio-4.31.5-py3-none-any.whl

Unable to create package with no name

@hannahblair
Copy link
Collaborator Author

Hi @autherous!

poetry add xyz.whl should be the correct syntax. If you navigate to ./ and try running that command (with just poetry add) does that work? Alternatively is the extra space (there are two spaces after add) causing the issue?

@pngwn
Copy link
Member

pngwn commented May 28, 2024

It could also just be that poetry doesn't like this way of installing packages. It does differ from pop in certain ways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants