Skip to content

Commit

Permalink
support gradio apps on spaces served on subpaths (#9814)
Browse files Browse the repository at this point in the history
* support spaces served on subpaths

* add changeset

* add changeset

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
pngwn and gradio-pr-bot authored Oct 23, 2024
1 parent 1163a37 commit 6505d42
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/odd-wolves-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/client": patch
"gradio": patch
---

fix:support gradio apps on spaces served on subpaths
4 changes: 2 additions & 2 deletions client/js/src/helpers/api_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import { determine_protocol } from "./init_helpers";

export const RE_SPACE_NAME = /^[a-zA-Z0-9_\-\.]+\/[a-zA-Z0-9_\-\.]+$/;
export const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}$/;
export const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}.*$/;

export async function process_endpoint(
app_reference: string,
Expand Down Expand Up @@ -58,7 +58,7 @@ export async function process_endpoint(
determine_protocol(_app_reference);

return {
space_id: host.replace(".hf.space", ""),
space_id: host.split("/")[0].replace(".hf.space", ""),
ws_protocol,
http_protocol,
host
Expand Down
7 changes: 0 additions & 7 deletions client/js/src/helpers/init_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,6 @@ export function determine_protocol(endpoint: string): {
if (endpoint.startsWith("http")) {
const { protocol, host, pathname } = new URL(endpoint);

if (host.endsWith("hf.space")) {
return {
ws_protocol: "wss",
host: host,
http_protocol: protocol as "http:" | "https:"
};
}
return {
ws_protocol: protocol === "https:" ? "wss" : "ws",
http_protocol: protocol as "http:" | "https:",
Expand Down
12 changes: 12 additions & 0 deletions client/js/src/test/api_info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,18 @@ describe("process_endpoint", () => {
expect(response.space_id).toBe("hmb-hello-world");
expect(response.host).toBe("hmb-hello-world.hf.space");
});

it("handles huggingface subpath urls", async () => {
const app_reference =
"https://pngwn-pr-demos-test.hf.space/demo/audio_debugger/";
const response = await process_endpoint(app_reference);
expect(response.space_id).toBe("pngwn-pr-demos-test");
expect(response.host).toBe(
"pngwn-pr-demos-test.hf.space/demo/audio_debugger"
);

expect(response.http_protocol).toBe("https:");
});
});

describe("join_urls", () => {
Expand Down

0 comments on commit 6505d42

Please sign in to comment.