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

Fixes URL resolution on Windows #3108

Merged
merged 6 commits into from Feb 2, 2023
Merged

Fixes URL resolution on Windows #3108

merged 6 commits into from Feb 2, 2023

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Feb 1, 2023

Previously, we would run utils.abspath() on a path before we knew whether it was a URL or a string path. On Windows, this would lead to an error if the path provided was a URL. This fixes that and makes the code a little clearer to read.

Test code (run this on a Windows machine):

import gradio as gr
 
with gr.Blocks() as demo:
    gr.Gallery(["https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80"])

demo.launch()

@gradio-pr-bot
Copy link
Contributor

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-3108-all-demos

@freddyaboulton
Copy link
Collaborator

@abidlabs Is that test code supposed to fail on Windows with the latest gradio version? The gallery shows up correctly for me on windows.

This is what I did. Lemme know if I messed something up cause I'm not used to windows development.

Install gradio from Pypi in a virtualenv on Windows

image

Run the test case

image

Gallery shows up for me 🤔

image

The proposed change looks good to me though and the deployed demos are working fine - just want to make sure I'm testing properly

@abidlabs
Copy link
Member Author

abidlabs commented Feb 2, 2023

Very strange, it isn't working for me on Windows @freddyaboulton. Can you try this simpler repro:

from pathlib import Path

url = "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80"

Path(url).resolve()

@freddyaboulton
Copy link
Collaborator

Yea calling .resolve() on a url returns a faulty path on windows but also ubuntu!

Windows

image

Ubuntu

image

It's strange it's broken for you since I just looked at the file route code again and we call validate_url on path not abspath so it should correctly detect it's a url no?

        @app.head("/file={path:path}", dependencies=[Depends(login_check)])
        @app.get("/file={path:path}", dependencies=[Depends(login_check)])
        async def file(path: str, request: fastapi.Request):
            abs_path = str(utils.abspath(path))
            blocks = app.get_blocks()
            if utils.validate_url(path):
                return RedirectResponse(url=path, status_code=status.HTTP_302_FOUND)

I wonder if something else is at play 🤔

@abidlabs
Copy link
Member Author

abidlabs commented Feb 2, 2023

Ahh it looks like it's a Python 3.9 vs. 3.10 thing, specifically on Windows 🤯

When I run the same code I asked you to run on Python 3.9, I get this error raised:

image

Apparently, pathlib fixed it at some point in Python 3.10: python/cpython@4696f12

@abidlabs
Copy link
Member Author

abidlabs commented Feb 2, 2023

You're right, we do call validate_url, but the problem is that we call it after we call utils.abspath(path).

This PR changes the order so that we only call abspath after we've confirmed that it is not a URL. Let me know if that makes sense

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.

Thanks for the fix + debugging with me @abidlabs !

@abidlabs
Copy link
Member Author

abidlabs commented Feb 2, 2023

Thank you @freddyaboulton!

@abidlabs abidlabs merged commit 431a987 into main Feb 2, 2023
@abidlabs abidlabs deleted the windows-urls branch February 2, 2023 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants