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

[WIP] Refactor file normalization to be in the backend and remove it from the frontend of each component #7183

Merged
merged 51 commits into from Feb 7, 2024

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Jan 27, 2024

The idea behind this PR is to clean up how we do "file normalization" i.e. how we convert filepaths into URLs like /file=filepath or /proxy=filepath so that the files can be fetched from the frontend of the Gradio app.

A filepath is converted into a URL in the following way:

  • If its a file on the local machine, it is prepended with {root_url}/file=. The root url is the path where the Gradio app is running. Usually, it is just http://localhost:7860. However, if the Gradio app is running within a larger FastAPI app on a subpath, or if it is running behind an nginx server, the root_url might be different, e.g. http://localhost:7860/gradio.

  • Otherwise, if the filepath is on an external Space (in the words, the component has been loaded using gr.load), then the prefix {root_url}/proxy={space_url}/file= is prepended to the filepath, which makes it a URL that can be accessed from the frontend. The local Gradio app serves as a proxy to route the request, as this is needed to access files on private Spaces using the local user's HF token

Previously, this file normalization was done in the frontend, in every component's frontend files, which led to code repetition and error-prone code, and was an annoyance for custom component developers. This PR moves it to the backend and does it in a single place, in a backwards-compatible manner for existing custom components.

Once we have this cleanup in place, it should be easier to tackle issues like: #6744

Todo:

  • handle proxy urls
  • handle root urls
  • fix functional tests
  • fix unit tests
  • handle streaming
  • clean up frontend

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jan 27, 2024

🪼 branch checks and previews

Name Status URL
Spaces building...
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detecting...

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jan 27, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/annotatedimage minor
@gradio/app minor
@gradio/audio minor
@gradio/chatbot minor
@gradio/client minor
@gradio/file minor
@gradio/gallery minor
@gradio/image minor
@gradio/imageeditor minor
@gradio/model3d minor
@gradio/simpleimage minor
@gradio/video minor
gradio minor
gradio_client minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

[WIP] Refactor file normalization to be in the backend and remove it from the frontend of each component

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 self-assigned this Jan 29, 2024
@abidlabs abidlabs changed the title Refactor file normalization to be in the backend and remove it from the frontend of each component [WIP] Refactor file normalization to be in the backend and remove it from the frontend of each component Feb 3, 2024
@abidlabs abidlabs marked this pull request as ready for review February 7, 2024 09:31
@abidlabs abidlabs added the v: patch A change that requires a patch release label Feb 7, 2024
@@ -18,46 +18,18 @@ export function normalise_file(
proxy_url: string | null // root_url: string | null
): FileData[] | FileData | null;

/*
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a need for backwards compatibility? wouldn't previous custom components be version pinned for the client?

Copy link
Member

Choose a reason for hiding this comment

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

They should but we would need to make the client version bump minor, to respect semver.

Copy link
Member

Choose a reason for hiding this comment

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

(Minor because this is a pre-1.0 version, so minor is breaking).

Copy link
Member Author

@abidlabs abidlabs Feb 7, 2024

Choose a reason for hiding this comment

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

Good point @aliabid94 @pngwn I'll just remove this method altogether then and change to minor

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@abidlabs abidlabs added v: minor A change that requires a minor release and removed v: patch A change that requires a patch release labels Feb 7, 2024
@@ -64,8 +62,12 @@
share: ShareData;
}>;

$: url = _value?.url;
$: url, gradio.dispatch("change");
$: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not just listen to the url change as before?

Copy link
Member Author

@abidlabs abidlabs Feb 7, 2024

Choose a reason for hiding this comment

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

Yeah that would be okay too, I ended up removing quite a few things from Image, including this line, and then I copied this snippet over from the Audio component. Either way should be fine

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.

LGTM, nice cleanup

@abidlabs
Copy link
Member Author

abidlabs commented Feb 7, 2024

Thanks for the review @aliabid94! I'm going to merge this in as it'll make it easier to tackle some other issues I'm looking at.

@abidlabs abidlabs merged commit 49d9c48 into main Feb 7, 2024
8 checks passed
@abidlabs abidlabs deleted the refactor-normalise branch February 7, 2024 22:39
@pngwn pngwn mentioned this pull request Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v: minor A change that requires a minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants