From 3b600c0d8087718a1991e74a4cbd61e7d0586079 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Tue, 26 Dec 2023 21:22:06 +0000 Subject: [PATCH 1/5] changes --- gradio/components/file_explorer.py | 4 +--- gradio/routes.py | 7 ++++++- test/test_routes.py | 31 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/gradio/components/file_explorer.py b/gradio/components/file_explorer.py index 7ed5909fea938..e331d9cd9a2b3 100644 --- a/gradio/components/file_explorer.py +++ b/gradio/components/file_explorer.py @@ -139,10 +139,8 @@ def postprocess(self, value: str | list[str] | None) -> FileExplorerData | None: return FileExplorerData(root=root) @server - def ls(self, value=None) -> list[dict[str, str]] | None: + def ls(self, _) -> list[dict[str, str]] | None: """ - Parameters: - value: file path as a list of strings for each directory level relative to the root. Returns: tuple of list of files in directory, then list of folders in directory """ diff --git a/gradio/routes.py b/gradio/routes.py index 293dfec69aa0e..4235728399da5 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -699,7 +699,12 @@ def component_server(body: ComponentServerBody): block = state[component_id] else: block = app.get_blocks().blocks[component_id] - fn = getattr(block, body.fn_name) + fn = getattr(block, body.fn_name, None) + if fn is None or not getattr(fn, "_is_server_fn", False): + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Function not found.", + ) return fn(body.data) @app.get( diff --git a/test/test_routes.py b/test/test_routes.py index 08bd596d5e353..4b081677ad0c0 100644 --- a/test/test_routes.py +++ b/test/test_routes.py @@ -831,3 +831,34 @@ def test_show_api_true_when_is_wasm_false(self): assert ( interface.show_api is True ), "show_api should be True when IS_WASM is False" + + +def test_component_server_endpoints(connect): + here = os.path.dirname(os.path.abspath(__file__)) + with gr.Blocks() as demo: + gr.FileExplorer(root=here) + + with closing(demo) as io: + app, _, _ = io.launch(prevent_thread_lock=True) + client = TestClient(app) + success_req = client.post( + "/component_server/", + json={ + "session_hash": "123", + "component_id": 1, + "fn_name": "ls", + "data": None, + }, + ) + assert success_req.status_code == 200 + assert len(success_req.json()) > 0 + fail_req = client.post( + "/component_server/", + json={ + "session_hash": "123", + "component_id": 1, + "fn_name": "preprocess", + "data": None, + }, + ) + assert fail_req.status_code == 404 From ef379b8a309a117cfd703acbdf162f792a586e64 Mon Sep 17 00:00:00 2001 From: gradio-pr-bot Date: Tue, 26 Dec 2023 21:24:12 +0000 Subject: [PATCH 2/5] add changeset --- .changeset/ten-chefs-argue.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/ten-chefs-argue.md diff --git a/.changeset/ten-chefs-argue.md b/.changeset/ten-chefs-argue.md new file mode 100644 index 0000000000000..80f81b86229f4 --- /dev/null +++ b/.changeset/ten-chefs-argue.md @@ -0,0 +1,5 @@ +--- +"gradio": minor +--- + +feat:Component Server fix From dc5f3160d2a57be8d232a1f4db401ccef8f88f3f Mon Sep 17 00:00:00 2001 From: aliabid94 Date: Wed, 27 Dec 2023 12:53:14 -0800 Subject: [PATCH 3/5] Update gradio/routes.py Co-authored-by: Aarni Koskela --- gradio/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradio/routes.py b/gradio/routes.py index 4235728399da5..2df22e74bad75 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -700,7 +700,7 @@ def component_server(body: ComponentServerBody): else: block = app.get_blocks().blocks[component_id] fn = getattr(block, body.fn_name, None) - if fn is None or not getattr(fn, "_is_server_fn", False): + if not getattr(fn, "_is_server_fn", False): raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Function not found.", From b626d6f554071ee98edca7a686bdd51e01cc7127 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Wed, 27 Dec 2023 21:15:51 +0000 Subject: [PATCH 4/5] changes --- gradio/components/file_explorer.py | 2 +- gradio/routes.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gradio/components/file_explorer.py b/gradio/components/file_explorer.py index e331d9cd9a2b3..d7b2fac8a93ea 100644 --- a/gradio/components/file_explorer.py +++ b/gradio/components/file_explorer.py @@ -139,7 +139,7 @@ def postprocess(self, value: str | list[str] | None) -> FileExplorerData | None: return FileExplorerData(root=root) @server - def ls(self, _) -> list[dict[str, str]] | None: + def ls(self, _=None) -> list[dict[str, str]] | None: """ Returns: tuple of list of files in directory, then list of folders in directory diff --git a/gradio/routes.py b/gradio/routes.py index 2df22e74bad75..4235728399da5 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -700,7 +700,7 @@ def component_server(body: ComponentServerBody): else: block = app.get_blocks().blocks[component_id] fn = getattr(block, body.fn_name, None) - if not getattr(fn, "_is_server_fn", False): + if fn is None or not getattr(fn, "_is_server_fn", False): raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Function not found.", From 089a5c2bd5568493b7828df65e0f80eb32cd001a Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Wed, 27 Dec 2023 21:26:59 +0000 Subject: [PATCH 5/5] changes --- test/test_routes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_routes.py b/test/test_routes.py index 4b081677ad0c0..82c46d3b4b9c6 100644 --- a/test/test_routes.py +++ b/test/test_routes.py @@ -836,7 +836,7 @@ def test_show_api_true_when_is_wasm_false(self): def test_component_server_endpoints(connect): here = os.path.dirname(os.path.abspath(__file__)) with gr.Blocks() as demo: - gr.FileExplorer(root=here) + file_explorer = gr.FileExplorer(root=here) with closing(demo) as io: app, _, _ = io.launch(prevent_thread_lock=True) @@ -845,7 +845,7 @@ def test_component_server_endpoints(connect): "/component_server/", json={ "session_hash": "123", - "component_id": 1, + "component_id": file_explorer._id, "fn_name": "ls", "data": None, }, @@ -856,7 +856,7 @@ def test_component_server_endpoints(connect): "/component_server/", json={ "session_hash": "123", - "component_id": 1, + "component_id": file_explorer._id, "fn_name": "preprocess", "data": None, },