Skip to content

Commit

Permalink
Fix Loading SSR'd apps via gr.load (#9827)
Browse files Browse the repository at this point in the history
* add code

* add changeset

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
freddyaboulton and gradio-pr-bot authored Oct 30, 2024
1 parent 458a38c commit 7ed8d02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
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

0 comments on commit 7ed8d02

Please sign in to comment.