Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
"""
|
2 |
-
app.py
|
3 |
-
|
4 |
-
|
5 |
-
β π Load JSON : JSON μ
λ‘λ β λ
ΈλΒ·μ’νΒ·μ°κ²° μ¦μ 볡μ
|
6 |
"""
|
7 |
|
8 |
import os, json, typing, tempfile
|
@@ -10,14 +9,12 @@ import gradio as gr
|
|
10 |
from gradio_workflowbuilder import WorkflowBuilder
|
11 |
|
12 |
# -------------------------------------------------------------------
|
13 |
-
# π οΈ ν¬νΌ
|
14 |
# -------------------------------------------------------------------
|
15 |
def export_pretty(data: typing.Dict[str, typing.Any]) -> str:
|
16 |
-
"""μ½λλ·° λ―Έλ¦¬λ³΄κΈ°μ© λ¬Έμμ΄."""
|
17 |
return json.dumps(data, indent=2, ensure_ascii=False) if data else "No workflow to export"
|
18 |
|
19 |
-
def export_file(data: typing.Dict[str, typing.Any]) -> str
|
20 |
-
"""μμ JSON νμΌ κ²½λ‘ λ°ν(DownloadButton μ©)."""
|
21 |
if not data:
|
22 |
return None
|
23 |
fd, path = tempfile.mkstemp(suffix=".json")
|
@@ -25,17 +22,19 @@ def export_file(data: typing.Dict[str, typing.Any]) -> str | None:
|
|
25 |
json.dump(data, f, ensure_ascii=False, indent=2)
|
26 |
return path
|
27 |
|
28 |
-
def import_workflow(file_obj: gr.File) ->
|
29 |
-
"""
|
|
|
|
|
|
|
30 |
if file_obj is None:
|
31 |
return None, "No workflow loaded"
|
32 |
with open(file_obj.name, "r", encoding="utf-8") as f:
|
33 |
data = json.load(f)
|
34 |
-
# gr.update λ‘ κ°μΈμΌ μκ°μ μΌλ‘ μ¬λ λλ§λ¨
|
35 |
return gr.update(value=data), json.dumps(data, indent=2, ensure_ascii=False)
|
36 |
|
37 |
# -------------------------------------------------------------------
|
38 |
-
# π¨
|
39 |
# -------------------------------------------------------------------
|
40 |
CSS = """
|
41 |
.main-container{max-width:1600px;margin:0 auto;}
|
@@ -62,7 +61,7 @@ CSS = """
|
|
62 |
with gr.Blocks(title="π¨ Visual Workflow Builder", theme=gr.themes.Soft(), css=CSS) as demo:
|
63 |
|
64 |
with gr.Column(elem_classes=["main-container"]):
|
65 |
-
gr.Markdown("# π¨ Visual Workflow Builder
|
66 |
|
67 |
gr.HTML(
|
68 |
"""
|
@@ -82,7 +81,7 @@ with gr.Blocks(title="π¨ Visual Workflow Builder", theme=gr.themes.Soft(), css
|
|
82 |
info="Drag from output β input β’ Click nodes to edit properties",
|
83 |
)
|
84 |
|
85 |
-
# βββ Export / Import
|
86 |
gr.Markdown("## πΎ Export Β· π Import")
|
87 |
|
88 |
with gr.Row(elem_classes=["button-row"]):
|
@@ -97,12 +96,11 @@ with gr.Blocks(title="π¨ Visual Workflow Builder", theme=gr.themes.Soft(), css
|
|
97 |
btn_download.click(fn=export_file, inputs=workflow_builder)
|
98 |
file_upload.change(fn=import_workflow, inputs=file_upload, outputs=[workflow_builder, code_view])
|
99 |
|
100 |
-
# μ¬μ©λ²
|
101 |
with gr.Accordion("π How to Use", open=False):
|
102 |
gr.Markdown(
|
103 |
"""
|
104 |
-
1. **Add
|
105 |
-
2. **Connect
|
106 |
3. **Edit** β click any node to change its properties
|
107 |
4. **Save** β *Save JSON file* to download your workflow
|
108 |
5. **Load** β *Load JSON* to restore (nodes auto-position)
|
|
|
1 |
"""
|
2 |
+
app.py ββββββββββββββββββββββββββββββββββββββββββββββββ
|
3 |
+
β Preview Β· Save Β· Load κΈ°λ₯ ν¬ν¨
|
4 |
+
β Load μ λ
Έλ/μ’ν/μ£μ§ μλ 볡μ
|
|
|
5 |
"""
|
6 |
|
7 |
import os, json, typing, tempfile
|
|
|
9 |
from gradio_workflowbuilder import WorkflowBuilder
|
10 |
|
11 |
# -------------------------------------------------------------------
|
12 |
+
# π οΈ ν¬νΌ
|
13 |
# -------------------------------------------------------------------
|
14 |
def export_pretty(data: typing.Dict[str, typing.Any]) -> str:
|
|
|
15 |
return json.dumps(data, indent=2, ensure_ascii=False) if data else "No workflow to export"
|
16 |
|
17 |
+
def export_file(data: typing.Dict[str, typing.Any]) -> typing.Optional[str]:
|
|
|
18 |
if not data:
|
19 |
return None
|
20 |
fd, path = tempfile.mkstemp(suffix=".json")
|
|
|
22 |
json.dump(data, f, ensure_ascii=False, indent=2)
|
23 |
return path
|
24 |
|
25 |
+
def import_workflow(file_obj: gr.File) -> typing.Tuple[typing.Any, str]:
|
26 |
+
"""
|
27 |
+
μ
λ‘λλ JSON β WorkflowBuilder κ° & μ½λλ·° κ°±μ .
|
28 |
+
첫 λ°νκ°μ gr.update(value=...) λ‘ κ°μΈ μκ°μ μ¬λ°°μΉ μ λ.
|
29 |
+
"""
|
30 |
if file_obj is None:
|
31 |
return None, "No workflow loaded"
|
32 |
with open(file_obj.name, "r", encoding="utf-8") as f:
|
33 |
data = json.load(f)
|
|
|
34 |
return gr.update(value=data), json.dumps(data, indent=2, ensure_ascii=False)
|
35 |
|
36 |
# -------------------------------------------------------------------
|
37 |
+
# π¨ CSS
|
38 |
# -------------------------------------------------------------------
|
39 |
CSS = """
|
40 |
.main-container{max-width:1600px;margin:0 auto;}
|
|
|
61 |
with gr.Blocks(title="π¨ Visual Workflow Builder", theme=gr.themes.Soft(), css=CSS) as demo:
|
62 |
|
63 |
with gr.Column(elem_classes=["main-container"]):
|
64 |
+
gr.Markdown("# π¨ Visual Workflow Builder\n**Create sophisticated workflows with drag-and-drop simplicity.**")
|
65 |
|
66 |
gr.HTML(
|
67 |
"""
|
|
|
81 |
info="Drag from output β input β’ Click nodes to edit properties",
|
82 |
)
|
83 |
|
84 |
+
# βββ Export / Import βββ
|
85 |
gr.Markdown("## πΎ Export Β· π Import")
|
86 |
|
87 |
with gr.Row(elem_classes=["button-row"]):
|
|
|
96 |
btn_download.click(fn=export_file, inputs=workflow_builder)
|
97 |
file_upload.change(fn=import_workflow, inputs=file_upload, outputs=[workflow_builder, code_view])
|
98 |
|
|
|
99 |
with gr.Accordion("π How to Use", open=False):
|
100 |
gr.Markdown(
|
101 |
"""
|
102 |
+
1. **Add Nodes** β drag items from the sidebar onto the canvas
|
103 |
+
2. **Connect** β blue handle (output) β gray handle (input)
|
104 |
3. **Edit** β click any node to change its properties
|
105 |
4. **Save** β *Save JSON file* to download your workflow
|
106 |
5. **Load** β *Load JSON* to restore (nodes auto-position)
|