Skip to content

Commit d4d340d

Browse files
abidlabsclaudeCopilot
authored
Provide API endpoint for Workflow graph (actually an endpoint for each subgraph) (#13524)
* Add design plan for Workflow HTTP API endpoints Plan-only commit: expose each workflow subject as a named Gradio endpoint reusing /info, /call, and /api. Registration via explicit non-rendered components (B2). No implementation yet. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Workflow API: server-side executor + per-subject endpoints (B2) Phases 1-3 of the plan: - gradio/workflow_api.py: schema-v2 graph model, topo-sort, upstream-subgraph and free-input extraction (ports of the canvas's workflow-graph.ts), plus a Python WorkflowExecutor that runs a subject's upstream DAG by reusing the existing call_space/call_model/call_fn/fetch_dataset server functions. - Register one named API endpoint per subject via hidden real components (B2), reusing Gradio's /info + /call. Endpoint execution re-reads the current workflow.json so operator/wiring edits are live; the endpoint set + schema is a launch-time snapshot for now. - Tests cover graph parsing, topo/cycle, subgraph + free inputs, end-to-end execution (callers mocked), /info schema, and request/token injection. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Workflow API: live endpoint updates on save (Option 2) Replace the static registration with a WorkflowEndpointManager that re-derives the endpoint set on every save_workflow: it tears down the prior endpoints (unrender() their components + drop their event triggers from blocks.fns) and rebuilds from the saved graph, then refreshes the cached config and invalidates the App's /info cache. Adding, removing, renaming, or retyping an output now updates /info + /call live without a restart. To mutate the running Blocks safely we set the render context directly (Context.root_block/block) instead of `with self:`, whose __exit__ would re-run attach_load_events (duplicating the canvas's callable-value load event) and recreate the running App. Tests: add/remove/rename outputs reflect in get_api_info(), no fn leak after removal, and save_workflow triggers the re-sync. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Workflow API: 2-subject demo, end-to-end /call test, dataset hardening - demo/workflow_api: a self-contained workflow with two outputs (Loud, Reversed) fed by bound fn operators — exposes /loud and /reversed. - Tests: gradio_client end-to-end against real /info + /call (skipped unless the frontend is built, as it fetches the root page), plus a TestClient check that /gradio_api/info lists the endpoints (runs without a frontend build). - Harden dataset-operator output mapping ({src: url} cells -> file values). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Workflow API: "View API" panel + describe endpoint - Add get_workflow_api server function + describe_workflow_api(), returning one descriptor per output (api_name, typed params, return) — re-read live so it tracks edits. Registration now shares _slug_iter() so described names match registered ones exactly. - Frontend: WorkflowApiPanel.svelte, a canvas-styled overlay (dark theme, JetBrains Mono) opened from a "View API" toolbar button. Lists each output's endpoint with parameter/return types and copyable Python / JavaScript / curl snippets, language-toggle, against the app's own URL. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * changes * changes * Fix prettier formatting in workflow-graph countSubgraphs test Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Fix ty typecheck errors in workflow API - Annotate Workflow._api_endpoints as WorkflowEndpointManager | None and narrow directly so .sync() typechecks - Assert _api_endpoints / view_api dict result in tests before subscripting - Guard os.path.dirname against gr.__file__ being None Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Fix call_fn signature to match caller convention The API executor invokes every caller as caller(data, request, token), but call_fn only accepted (data, token), so calling an fn-backed workflow endpoint via the Gradio client raised a TypeError. Align call_fn with call_space/call_model/fetch_dataset's (data, request, token) signature. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Fix workflow API panel and schema validation * Use server OAuth fallback for workflow text generation * changes * changes * changes * Update changeset: endpoint per subgraph, not per output Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Fix test_save_allowed_with_write_access: use valid schema_version 2 payload save_workflow now validates schema_version; the gating test's legacy {"nodes": []} stub no longer passes validation. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Address Copilot review: map file ports + per-subgraph wording - port_to_component: map 'file' ports to gr.File(type='filepath') instead of falling through to Textbox, so /info schema matches _PY_TYPE/MEDIA_PORT_TYPES (+ regression test) - WorkflowApiPanel subtitle + get_workflow_api docstring: 'one endpoint per output' -> 'per subgraph', matching the per-subgraph endpoint grouping Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]>
1 parent 8eafb31 commit d4d340d

12 files changed

Lines changed: 2209 additions & 18 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/workflowcanvas": minor
3+
"gradio": minor
4+
---
5+
6+
feat:Run `gr.Workflow` subgraphs via the Gradio API — each subgraph is exposed as a named endpoint (returning all of its outputs) reusing `/info`, `/call`, and `/api`, with a "View API" panel in the canvas

‎demo/workflow_api/run.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import gradio as gr
2+
3+
4+
def shout(text: str) -> str:
5+
return (text or "").upper()
6+
7+
8+
def reverse(text: str) -> str:
9+
return (text or "")[::-1]
10+
11+
12+
# Two outputs ("Loud" and "Reversed") fed by a shared "Text" input. Because they
13+
# form a single connected subgraph, they're exposed as ONE API endpoint that
14+
# returns both outputs as a tuple — see the "View API" panel in the app, or
15+
# connect with the gradio_client.
16+
demo = gr.Workflow(graph="workflow.json", bind={"shout": shout, "reverse": reverse})
17+
18+
if __name__ == "__main__":
19+
demo.launch()

‎demo/workflow_api/workflow.json‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"schema_version": "2",
3+
"name": "Text Tools",
4+
"runtime": { "default": "client" },
5+
"references": [
6+
{
7+
"id": "ref_text", "label": "Text", "role": "reference", "asset_type": "text",
8+
"inputs": [{ "id": "in", "label": "Text", "type": "text" }],
9+
"outputs": [{ "id": "out", "label": "Text", "type": "text" }],
10+
"x": 80, "y": 120, "width": 220, "height": 120, "data": {}
11+
}
12+
],
13+
"operators": [
14+
{
15+
"id": "op_loud", "label": "shout", "role": "operator", "kind": "fn", "fn": "shout",
16+
"inputs": [{ "id": "in_text", "label": "text", "type": "text", "required": true }],
17+
"outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
18+
"x": 380, "y": 40, "width": 220, "height": 120, "data": {}
19+
},
20+
{
21+
"id": "op_rev", "label": "reverse", "role": "operator", "kind": "fn", "fn": "reverse",
22+
"inputs": [{ "id": "in_text", "label": "text", "type": "text", "required": true }],
23+
"outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
24+
"x": 380, "y": 240, "width": 220, "height": 120, "data": {}
25+
}
26+
],
27+
"subjects": [
28+
{
29+
"id": "sub_loud", "label": "Loud", "role": "subject", "asset_type": "text",
30+
"inputs": [{ "id": "in", "label": "Text", "type": "text" }],
31+
"outputs": [{ "id": "out", "label": "Text", "type": "text" }],
32+
"x": 700, "y": 40, "width": 220, "height": 107, "data": {}
33+
},
34+
{
35+
"id": "sub_rev", "label": "Reversed", "role": "subject", "asset_type": "text",
36+
"inputs": [{ "id": "in", "label": "Text", "type": "text" }],
37+
"outputs": [{ "id": "out", "label": "Text", "type": "text" }],
38+
"x": 700, "y": 240, "width": 220, "height": 107, "data": {}
39+
}
40+
],
41+
"edges": [
42+
{ "id": "e1", "from_node_id": "ref_text", "from_port_id": "out", "to_node_id": "op_loud", "to_port_id": "in_text", "type": "text" },
43+
{ "id": "e2", "from_node_id": "ref_text", "from_port_id": "out", "to_node_id": "op_rev", "to_port_id": "in_text", "type": "text" },
44+
{ "id": "e3", "from_node_id": "op_loud", "from_port_id": "out_0", "to_node_id": "sub_loud", "to_port_id": "in", "type": "text" },
45+
{ "id": "e4", "from_node_id": "op_rev", "from_port_id": "out_0", "to_node_id": "sub_rev", "to_port_id": "in", "type": "text" }
46+
],
47+
"view": { "default": "canvas" }
48+
}

‎gradio/workflow.py‎

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import webbrowser
1616
from collections.abc import Callable
1717
from concurrent.futures import ThreadPoolExecutor
18-
from typing import Optional, TypedDict
18+
from typing import TYPE_CHECKING, Optional, TypedDict
1919

2020
import httpx
2121
from huggingface_hub import HfApi
@@ -26,6 +26,9 @@
2626
from gradio.route_utils import Request
2727
from gradio.utils import get_space
2828

29+
if TYPE_CHECKING:
30+
from gradio.workflow_api import WorkflowEndpointManager
31+
2932
_HF_CLIENT = httpx.Client(
3033
base_url="https://huggingface.co",
3134
timeout=15,
@@ -997,14 +1000,18 @@ def is_curated(data, _token: Optional[OAuthToken] = None) -> str:
9971000
_QUICKSEARCH_TTL = 30.0
9981001

9991002

1000-
def search_quick(data, token: Optional[OAuthToken] = None) -> str:
1003+
def search_quick(
1004+
data,
1005+
request: Request | None = None,
1006+
token: Optional[OAuthToken] = None,
1007+
) -> str:
10011008
import time
10021009

10031010
query = data[0] if data and data[0] else ""
10041011
if not query or len(query) < 2:
10051012
return json.dumps({"spaces": [], "models": []})
10061013
try:
1007-
hf_token = _resolve_token(data, 1, token)
1014+
hf_token = _resolve_token(data, 1, token, request)
10081015

10091016
cache_key = query.lower().strip()
10101017
now = time.monotonic()
@@ -1069,10 +1076,14 @@ def _parse_repo_input(raw: str) -> tuple[str, Optional[str]]:
10691076
return "", None
10701077

10711078

1072-
def resolve_repo(data, token: Optional[OAuthToken] = None) -> str:
1079+
def resolve_repo(
1080+
data,
1081+
request: Request | None = None,
1082+
token: Optional[OAuthToken] = None,
1083+
) -> str:
10731084
raw = data[0] if data else ""
10741085
try:
1075-
hf_token = _resolve_token(data, 1, token)
1086+
hf_token = _resolve_token(data, 1, token, request)
10761087
repo_id, kind_hint = _parse_repo_input(raw)
10771088

10781089
def _try(rid: str, repo_type: str) -> Optional[dict]:
@@ -1274,6 +1285,10 @@ def _build(self):
12741285
import gradio as gr
12751286
from gradio.components.workflowcanvas import WorkflowCanvas
12761287

1288+
# Set once the API endpoints are registered (post UI build); save_workflow
1289+
# re-syncs it so /info + /call track edits to the graph.
1290+
self._api_endpoints: WorkflowEndpointManager | None = None
1291+
12771292
if self._edges and os.path.exists(self._workflow_file):
12781293
logger.warning(
12791294
"Workflow: edges= is ignored because '%s' already exists. "
@@ -1296,7 +1311,11 @@ def _load_initial() -> str | None:
12961311

12971312
bound = self._bound
12981313

1299-
def call_fn(data, _token: Optional[OAuthToken] = None) -> str:
1314+
def call_fn(
1315+
data,
1316+
_request: Optional[Request] = None,
1317+
_token: Optional[OAuthToken] = None,
1318+
) -> str:
13001319
fn_name = data[0] if data else ""
13011320
try:
13021321
args_json = data[1] if len(data) > 1 else "[]"
@@ -1365,6 +1384,16 @@ def list_bound_fns(_data=None, _token: Optional[OAuthToken] = None) -> str:
13651384
)
13661385
return json.dumps(templates)
13671386

1387+
def get_workflow_api(_data=None, _request: Optional[Request] = None) -> str:
1388+
"""Describe the workflow's API endpoints (one per subgraph) for the
1389+
frontend "View API" panel. Re-reads the current graph so it tracks
1390+
live edits, same as the registered endpoints."""
1391+
from gradio.workflow_api import WorkflowGraph, describe_workflow_api
1392+
1393+
graph = WorkflowGraph.from_json(_load_initial())
1394+
endpoints = describe_workflow_api(graph) if graph is not None else []
1395+
return json.dumps({"endpoints": endpoints})
1396+
13681397
def save_workflow(
13691398
data,
13701399
request: Optional[Request] = None,
@@ -1384,11 +1413,30 @@ def save_workflow(
13841413
if len(payload.encode()) > _max_workflow_bytes:
13851414
return json.dumps({"error": "Workflow payload exceeds 5 MB limit"})
13861415
try:
1387-
json.loads(payload)
1416+
parsed = json.loads(payload)
13881417
except json.JSONDecodeError as exc:
13891418
return json.dumps({"error": f"Invalid workflow JSON: {exc}"})
1419+
if not isinstance(parsed, dict) or parsed.get("schema_version") != "2":
1420+
return json.dumps(
1421+
{"error": "Workflow payload must use schema_version 2"}
1422+
)
1423+
try:
1424+
from gradio.workflow_api import WorkflowGraph
1425+
1426+
WorkflowGraph(parsed)
1427+
except ValueError as exc:
1428+
return json.dumps({"error": f"Invalid workflow schema: {exc}"})
13901429
with open(workflow_file, "w", encoding="utf-8") as f:
13911430
f.write(payload)
1431+
# Re-derive API endpoints so /info + /call track the saved graph
1432+
# (outputs added / removed / renamed / retyped).
1433+
if self._api_endpoints is not None:
1434+
try:
1435+
self._api_endpoints.sync()
1436+
except Exception:
1437+
logger.error(
1438+
"Workflow: endpoint sync after save failed", exc_info=True
1439+
)
13921440
return "ok"
13931441
except Exception as e:
13941442
logger.error("save_workflow failed: %s", e, exc_info=True)
@@ -1412,9 +1460,25 @@ def save_workflow(
14121460
get_dataset_schema,
14131461
call_fn,
14141462
list_bound_fns,
1463+
get_workflow_api,
14151464
save_workflow,
14161465
]
14171466

1467+
from gradio.workflow_api import WorkflowGraph, register_workflow_endpoints
1468+
1469+
# Operator-kind → server-function used to execute that node. The same
1470+
# functions back the canvas's client-side run; the API executor reuses
1471+
# them (with request/token threaded for token resolution).
1472+
callers = {
1473+
"space": call_space,
1474+
"model": call_model,
1475+
"fn": call_fn,
1476+
"dataset": fetch_dataset,
1477+
}
1478+
1479+
def _current_graph() -> WorkflowGraph | None:
1480+
return WorkflowGraph.from_json(_load_initial())
1481+
14181482
with self:
14191483
if get_space() is not None and os.getenv("OAUTH_CLIENT_ID"):
14201484
gr.LoginButton(visible=False)
@@ -1423,6 +1487,11 @@ def save_workflow(
14231487
server_functions=server_functions,
14241488
)
14251489

1490+
# Expose each subject (output) as a named API endpoint reusing /info +
1491+
# /call. The manager re-syncs on every save_workflow, so adding,
1492+
# removing, renaming, or retyping an output updates the live API.
1493+
self._api_endpoints = register_workflow_endpoints(self, _current_graph, callers)
1494+
14261495
def launch(self, *args, **kwargs): # type: ignore[override]
14271496
"""Launch the workflow as a Gradio app. Accepts the same arguments as `gr.Blocks.launch()`.
14281497
`call_space` / `_save_tmp` write inference outputs to the system tempdir

0 commit comments

Comments
 (0)