Skip to content

Load example from external Spaces#2594

Merged
abidlabs merged 21 commits into
mainfrom
actually-load-examples
Nov 3, 2022
Merged

Load example from external Spaces#2594
abidlabs merged 21 commits into
mainfrom
actually-load-examples

Conversation

@abidlabs
Copy link
Copy Markdown
Member

@abidlabs abidlabs commented Nov 2, 2022

gr.Blocks.load() or gr.Interface.load() now correctly loads examples from Spaces. You can test by running this:

import gradio as gr

gr.Interface.load("spaces/sayakpaul/raindrop-deraining-maxim").launch()

or a more complex example:

import gradio as gr

gr.Interface.load("spaces/gradio/test-loading-examples").launch()

Loading more than 1 Space in one Blocks:

import gradio as gr

with gr.Blocks() as demo:
    with gr.Tab("Demo 1"):
        gr.Interface.load("spaces/sayakpaul/raindrop-deraining-maxim")
    with gr.Tab("Demo 2"):
        gr.Interface.load("spaces/gradio/test-loading-examples")

demo.launch()

It is unfortunately not going to be possible to override examples when you load a gradio Interface or Blocks running 3.x. It is not possible to override any of the Interface properties (e.g. title) because we do not capture any reference to these Interface-specific properties. We instead load a generic Blocks that "happens" to look like an Interface. It might be possible to parse Interface-specific properties but it will be quite complex and I do not think the added complexity is worth it.

Fixes: #2554

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 2, 2022

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

@abidlabs abidlabs changed the title Load examples from external Spaces Load example images from external Spaces Nov 2, 2022
Comment thread gradio/blocks.py Outdated
block_config["props"].pop("type", None)
block_config["props"].pop("name", None)
style = block_config["props"].pop("style", None)
if block_config["props"].get("root_url") is None:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If the Space is already loading from another Space, it should use the root_url of the parent Space. This allows loading a chain of Spaces

Comment thread gradio/blocks.py

with Blocks(theme=config["theme"], css=config["theme"]) as blocks:
# ID 0 should be the root Blocks component
original_mapping[0] = Context.root_block or blocks
Copy link
Copy Markdown
Member Author

@abidlabs abidlabs Nov 3, 2022

Choose a reason for hiding this comment

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

Fixing a different bug that was preventing gr.Interface.load() from working if a dataset had caching enabled

@abidlabs abidlabs marked this pull request as ready for review November 3, 2022 00:34
@abidlabs abidlabs changed the title Load example images from external Spaces Load example from external Spaces Nov 3, 2022
Copy link
Copy Markdown
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 @abidlabs ! Looks good.

Noticed some weird behavior when trying to load a private space. Posting it here so you can review.

Comment thread gradio/blocks.py
Comment thread gradio/external.py
else:
fns.append(None)
return gradio.Blocks.from_config(config, fns)
return gradio.Blocks.from_config(config, fns, iframe_url)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When loading a private space, the inputs are shown correctly but the outputs are.

image

Testing with this private space: https://huggingface.co/spaces/gradio/test-loading-examples-private

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The boat image shows up on the left hand side but not in the dataset component.

image

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Curious what behavior you see, I ran it again on the private space and now everything but the model3d shows up 🤔

image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Lol that's interesting. I get the same behavior as you the 2nd time -- everything but Model3D shows up. Nice catch, let me investigate

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This seems like a weird CORS issue and tbh I'm not sure what to do here. I think since this is a relatively niche use case (loading private Spaces, with (only?) the Model3D component), we can save for a future PR. I know @aliabid94 will be looking to CORS soon as well so flagging for your attention @aliabid94

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sounds good with me. What makes you think it's a CORS issue?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Will also not display the dataframe properly in the dataset if it's a private space:

image

Works fine for public spaces though. Confused as to why it would not impact images,audio, etc. Agree we can leave it for another issue but let's file it so we don't forget!

Comment thread gradio/blocks.py Outdated
for target in targets:
event_method = getattr(original_mapping[target], trigger)
event_method(fn=fn, **dependency)
original_mapping[target].set_event_trigger(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How come we need to make this change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because we define a "fake_event" that doesn't actually exist as an event method.

Also the less that we use methods like getattr() the better imo!

@abidlabs
Copy link
Copy Markdown
Member Author

abidlabs commented Nov 3, 2022

See my comments above @freddyaboulton, thanks for the review!

Copy link
Copy Markdown
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 @abidlabs ! Awesome PR

@abidlabs abidlabs merged commit 43f5cf5 into main Nov 3, 2022
@abidlabs abidlabs deleted the actually-load-examples branch November 3, 2022 19:35
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.

Load examples when Interface/Block is loaded unless examples are passed

2 participants