| """ |
| Concept D — a SPACE node (call any Gradio Space on the Hub) |
| ========================================================== |
| |
| An operator with `kind: "space"` calls a Gradio Space's API. Set `space_id` and |
| `endpoint` (its api_name, e.g. "/image"); inputs are passed POSITIONALLY in port |
| order, and file inputs/outputs are handled for you. Find the endpoint + arg |
| order on the Space's "Use via API" panel. |
| |
| Graph: reference(image) → space background-removal (/image) → subject(image) |
| |
| The `/image` endpoint returns (original, cutout); the subject's output port uses |
| `output_index: 1` to keep the cutout. |
| |
| Needs a token to RUN; imports and renders without one. |
| |
| Run it: python concepts/d_space_node.py |
| """ |
|
|
| import json |
| import os |
|
|
| import gradio as gr |
|
|
| GRAPH = { |
| "schema_version": "2", |
| "name": "Background Remover", |
| "references": [ |
| {"id": "ref_img", "role": "reference", "label": "Image", "asset_type": "image", |
| "inputs": [{"id": "in", "label": "Image", "type": "image"}], |
| "outputs": [{"id": "out", "label": "Image", "type": "image"}], |
| "data": {}, "x": 60, "y": 120, "width": 220, "height": 150} |
| ], |
| "operators": [ |
| {"id": "op_bg", "role": "operator", "kind": "space", |
| "space_id": "hf-applications/background-removal", "endpoint": "/image", |
| "label": "background-removal", |
| "inputs": [{"id": "in_image", "label": "Image", "type": "image", "required": True}], |
| "outputs": [{"id": "out_0", "label": "Cutout", "type": "image", "output_index": 1}], |
| "data": {}, "x": 340, "y": 120, "width": 240, "height": 120} |
| ], |
| "subjects": [ |
| {"id": "sub_cut", "role": "subject", "label": "Cutout", "asset_type": "image", |
| "inputs": [{"id": "in", "label": "Cutout", "type": "image"}], |
| "outputs": [{"id": "out", "label": "Cutout", "type": "image"}], |
| "data": {}, "x": 640, "y": 120, "width": 240, "height": 220} |
| ], |
| "edges": [ |
| {"id": "e1", "from_node_id": "ref_img", "from_port_id": "out", |
| "to_node_id": "op_bg", "to_port_id": "in_image", "type": "image"}, |
| {"id": "e2", "from_node_id": "op_bg", "from_port_id": "out_0", |
| "to_node_id": "sub_cut", "to_port_id": "in", "type": "image"}, |
| ], |
| } |
|
|
| GRAPH_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "d_space_node.json") |
| with open(GRAPH_PATH, "w", encoding="utf-8") as f: |
| json.dump(GRAPH, f, indent=2) |
|
|
| demo = gr.Workflow(GRAPH_PATH) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|