From b483b6d455e0aba6d70b113c4b7e503294a69bb3 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Thu, 11 Aug 2022 01:17:51 -0700 Subject: [PATCH 1/3] change --- ui/packages/app/src/api.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ui/packages/app/src/api.ts b/ui/packages/app/src/api.ts index a6e2e8226cd7..4600facf783e 100644 --- a/ui/packages/app/src/api.ts +++ b/ui/packages/app/src/api.ts @@ -31,10 +31,13 @@ interface Payload { declare let BUILD_MODE: string; declare let BACKEND_URL: string; -const WS_ENDPOINT = +const WS_PROTOCOL = location.protocol === "https:" ? "wss:" : "ws:"; +const WS_HOST = BUILD_MODE === "dev" || location.origin === "http://localhost:3000" - ? `ws://${BACKEND_URL.replace("http://", "")}queue/join` - : `ws://${location.host}/queue/join`; + ? BACKEND_URL.replace("http://", "").slice(0,-1) + : location.host; +const WS_PATH = `${location.pathname === "/" ? "" : location.pathname}/queue/join` +const WS_ENDPOINT = `${WS_PROTOCOL}//${WS_HOST}${WS_PATH}`; async function post_data< Return extends Record = Record From c2adc4bcae1fc71732d4bfe15c7173d41ac2e659 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Thu, 11 Aug 2022 01:20:09 -0700 Subject: [PATCH 2/3] format --- ui/packages/app/src/api.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ui/packages/app/src/api.ts b/ui/packages/app/src/api.ts index 4600facf783e..b45281dfd3f9 100644 --- a/ui/packages/app/src/api.ts +++ b/ui/packages/app/src/api.ts @@ -34,9 +34,11 @@ declare let BACKEND_URL: string; const WS_PROTOCOL = location.protocol === "https:" ? "wss:" : "ws:"; const WS_HOST = BUILD_MODE === "dev" || location.origin === "http://localhost:3000" - ? BACKEND_URL.replace("http://", "").slice(0,-1) + ? BACKEND_URL.replace("http://", "").slice(0, -1) : location.host; -const WS_PATH = `${location.pathname === "/" ? "" : location.pathname}/queue/join` +const WS_PATH = `${ + location.pathname === "/" ? "" : location.pathname +}/queue/join`; const WS_ENDPOINT = `${WS_PROTOCOL}//${WS_HOST}${WS_PATH}`; async function post_data< From e3ca40b3420eebb94b09378f8716d91023d58452 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Thu, 11 Aug 2022 12:12:20 -0700 Subject: [PATCH 3/3] changes --- gradio/blocks.py | 1 + gradio/version.txt | 2 +- ui/packages/app/src/api.ts | 25 ++++++++++++++----------- ui/packages/app/src/main.ts | 3 ++- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/gradio/blocks.py b/gradio/blocks.py index bf5094a44c51..bf8ffa18392e 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -666,6 +666,7 @@ def get_config_file(self): "theme": self.theme, "css": self.css, "title": self.title or "Gradio", + "is_space": self.is_space, "enable_queue": getattr( self, "enable_queue", False ), # attribute set at launch diff --git a/gradio/version.txt b/gradio/version.txt index 0a2ea3051140..5ee8af93b293 100644 --- a/gradio/version.txt +++ b/gradio/version.txt @@ -1 +1 @@ -3.1.4b3 +3.1.4b4 diff --git a/ui/packages/app/src/api.ts b/ui/packages/app/src/api.ts index b45281dfd3f9..b032dff8247c 100644 --- a/ui/packages/app/src/api.ts +++ b/ui/packages/app/src/api.ts @@ -31,16 +31,6 @@ interface Payload { declare let BUILD_MODE: string; declare let BACKEND_URL: string; -const WS_PROTOCOL = location.protocol === "https:" ? "wss:" : "ws:"; -const WS_HOST = - BUILD_MODE === "dev" || location.origin === "http://localhost:3000" - ? BACKEND_URL.replace("http://", "").slice(0, -1) - : location.host; -const WS_PATH = `${ - location.pathname === "/" ? "" : location.pathname -}/queue/join`; -const WS_ENDPOINT = `${WS_PROTOCOL}//${WS_HOST}${WS_PATH}`; - async function post_data< Return extends Record = Record >(url: string, body: unknown): Promise { @@ -71,7 +61,7 @@ type Output = { const ws_map = new Map(); export const fn = - (session_hash: string, api_endpoint: string) => + (session_hash: string, api_endpoint: string, is_space: boolean) => async ({ action, payload, @@ -112,6 +102,19 @@ export const fn = ws_map.get(fn).connection.send(JSON.stringify(data)); } + var ws_protocol = location.protocol === "https:" ? "wss:" : "ws:"; + if (is_space) { + var ws_path = location.pathname.slice(6, -2); // remove 'embed' from start, '+' from end + var ws_host = "spaces.huggingface.tech"; + } else { + var ws_path = location.pathname === "/" ? "" : location.pathname; + var ws_host = + BUILD_MODE === "dev" || location.origin === "http://localhost:3000" + ? BACKEND_URL.replace("http://", "").slice(0, -1) + : location.host; + } + const WS_ENDPOINT = `${ws_protocol}//${ws_host}${ws_path}/queue/join`; + const websocket_data = { connection: new WebSocket(WS_ENDPOINT), hash: Math.random().toString(36).substring(2) diff --git a/ui/packages/app/src/main.ts b/ui/packages/app/src/main.ts index 210995a4465a..9b4c632398d5 100644 --- a/ui/packages/app/src/main.ts +++ b/ui/packages/app/src/main.ts @@ -33,6 +33,7 @@ interface Config { theme: string; title: string; version: string; + is_space: boolean; // allow_flagging: string; // allow_interpretation: boolean; // article: string; @@ -182,7 +183,7 @@ function mount_app( }); } else { let session_hash = Math.random().toString(36).substring(2); - config.fn = fn(session_hash, config.root + "api/"); + config.fn = fn(session_hash, config.root + "api/", config.is_space); new Blocks({ target: wrapper,