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

Fix Loading SSR'd apps via gr.load #9827

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-wolves-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Fix Loading SSR'd apps via gr.load
25 changes: 16 additions & 9 deletions gradio/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ def from_spaces(
headers = {}
if hf_token not in [False, None]:
headers["Authorization"] = f"Bearer {hf_token}"

iframe_url = (
httpx.get(
f"https://huggingface.co/api/spaces/{space_name}/host", headers=headers
Expand All @@ -440,15 +439,23 @@ def from_spaces(
f"Could not find Space: {space_name}. If it is a private or gated Space, please provide your Hugging Face access token (https://huggingface.co/settings/tokens) as the argument for the `hf_token` parameter."
)

r = httpx.get(iframe_url, headers=headers)
config_request = httpx.get(iframe_url + "/config", headers=headers)
if config_request.status_code == 404:
r = httpx.get(iframe_url, headers=headers)

result = re.search(
r"window.gradio_config = (.*?);[\s]*</script>", r.text
) # some basic regex to extract the config
try:
config = json.loads(result.group(1)) # type: ignore
except AttributeError as ae:
raise ValueError(f"Could not load the Space: {space_name}") from ae
result = re.search(
r"window.gradio_config = (.*?);[\s]*</script>", r.text
) # some basic regex to extract the config
try:
config = json.loads(result.group(1)) # type: ignore
except AttributeError as ae:
raise ValueError(f"Could not load the Space: {space_name}") from ae
elif config_request.status_code == 200:
config = config_request.json()
else:
raise ValueError(
f"Could not load the Space: {space_name} because the config could not be fetched."
)
if "allow_flagging" in config: # Create an Interface for Gradio 2.x Spaces
return from_spaces_interface(
space_name, config, alias, hf_token, iframe_url, **kwargs
Expand Down
Loading