File size: 2,514 Bytes
c68afbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
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()