Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,126 +1,119 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from gradio_workflowbuilder import WorkflowBuilder
|
3 |
-
import json
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
def
|
7 |
-
"""
|
8 |
-
if not
|
9 |
-
return
|
10 |
-
|
|
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
#
|
|
|
|
|
14 |
with gr.Blocks(
|
15 |
-
title="π¨ Visual Workflow Builder",
|
16 |
theme=gr.themes.Soft(),
|
17 |
css="""
|
18 |
.main-container { max-width: 1600px; margin: 0 auto; }
|
19 |
.workflow-section { margin-bottom: 2rem; }
|
20 |
.button-row { display: flex; gap: 1rem; justify-content: center; margin: 1rem 0; }
|
21 |
-
|
22 |
.component-description {
|
23 |
padding: 24px;
|
24 |
-
background: linear-gradient(135deg
|
25 |
-
border-radius: 12px;
|
26 |
-
|
27 |
-
|
28 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
29 |
-
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
|
30 |
-
}
|
31 |
-
|
32 |
-
.component-description p {
|
33 |
-
margin: 10px 0;
|
34 |
-
line-height: 1.6;
|
35 |
-
color: #374151;
|
36 |
-
}
|
37 |
-
|
38 |
-
.base-description {
|
39 |
-
font-size: 17px;
|
40 |
-
font-weight: 600;
|
41 |
-
color: #1e293b;
|
42 |
-
}
|
43 |
-
|
44 |
-
.base-description strong {
|
45 |
-
color: #3b82f6;
|
46 |
-
font-weight: 700;
|
47 |
-
}
|
48 |
-
|
49 |
-
.feature-description {
|
50 |
-
font-size: 16px;
|
51 |
-
color: #1e293b;
|
52 |
-
font-weight: 500;
|
53 |
-
}
|
54 |
-
|
55 |
-
.customization-note {
|
56 |
-
font-size: 15px;
|
57 |
-
color: #64748b;
|
58 |
-
font-style: italic;
|
59 |
-
}
|
60 |
-
|
61 |
-
.sample-intro {
|
62 |
-
font-size: 15px;
|
63 |
-
color: #1e293b;
|
64 |
-
font-weight: 600;
|
65 |
-
margin-top: 16px;
|
66 |
-
border-top: 1px solid #e2e8f0;
|
67 |
-
padding-top: 16px;
|
68 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
"""
|
70 |
) as demo:
|
71 |
-
|
72 |
with gr.Column(elem_classes=["main-container"]):
|
73 |
-
gr.Markdown(""
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
""")
|
88 |
-
|
89 |
-
# Main workflow builder section
|
90 |
with gr.Column(elem_classes=["workflow-section"]):
|
91 |
workflow_builder = WorkflowBuilder(
|
92 |
label="π¨ Visual Workflow Designer",
|
93 |
-
info="Drag components from the sidebar β
|
94 |
)
|
95 |
-
|
96 |
-
# Export
|
97 |
-
gr.Markdown("## πΎ Export
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
with gr.Column():
|
101 |
-
export_output = gr.Code(
|
102 |
-
language="json",
|
103 |
-
label="Workflow Configuration",
|
104 |
-
lines=10
|
105 |
-
)
|
106 |
-
|
107 |
-
# Action button
|
108 |
with gr.Row(elem_classes=["button-row"]):
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
|
113 |
-
|
114 |
-
# Event handlers
|
115 |
-
export_btn.click(
|
116 |
-
fn=export_workflow,
|
117 |
-
inputs=[workflow_builder],
|
118 |
-
outputs=[export_output]
|
119 |
-
)
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
|
|
|
|
|
|
122 |
if __name__ == "__main__":
|
123 |
-
demo.launch(
|
124 |
-
server_name="0.0.0.0",
|
125 |
-
show_error=True
|
126 |
-
)
|
|
|
1 |
+
import os, json, typing, tempfile
|
2 |
import gradio as gr
|
3 |
from gradio_workflowbuilder import WorkflowBuilder
|
|
|
4 |
|
5 |
+
# -------------------------------------------------------------------
|
6 |
+
# π οΈ ν¬νΌ
|
7 |
+
# -------------------------------------------------------------------
|
8 |
+
def export_pretty(data: typing.Dict[str, typing.Any]) -> str:
|
9 |
+
"""μ½λλ·° λ―Έλ¦¬λ³΄κΈ°μ© JSON λ¬Έμμ΄."""
|
10 |
+
return json.dumps(data, indent=2, ensure_ascii=False) if data else "No workflow to export"
|
11 |
|
12 |
+
def export_file(data: typing.Dict[str, typing.Any]) -> str | None:
|
13 |
+
"""JSONμ μμ νμΌλ‘ μ μ₯ ν κ²½λ‘ λ°ν(DownloadButtonμ μ λ¬)."""
|
14 |
+
if not data:
|
15 |
+
return None
|
16 |
+
fd, path = tempfile.mkstemp(suffix=".json")
|
17 |
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
18 |
+
json.dump(data, f, ensure_ascii=False, indent=2)
|
19 |
+
return path
|
20 |
|
21 |
+
def import_workflow(file_obj: gr.File) -> tuple[dict | None, str]:
|
22 |
+
"""μ
λ‘λλ JSON β builder κ° & μ½λλ·° λ¬Έμμ΄ λμ λ°ν."""
|
23 |
+
if file_obj is None:
|
24 |
+
return None, "No workflow loaded"
|
25 |
+
with open(file_obj.name, "r", encoding="utf-8") as f:
|
26 |
+
data = json.load(f)
|
27 |
+
return data, json.dumps(data, indent=2, ensure_ascii=False)
|
28 |
|
29 |
+
# -------------------------------------------------------------------
|
30 |
+
# π¨ UI
|
31 |
+
# -------------------------------------------------------------------
|
32 |
with gr.Blocks(
|
33 |
+
title="π¨ Visual Workflow Builder",
|
34 |
theme=gr.themes.Soft(),
|
35 |
css="""
|
36 |
.main-container { max-width: 1600px; margin: 0 auto; }
|
37 |
.workflow-section { margin-bottom: 2rem; }
|
38 |
.button-row { display: flex; gap: 1rem; justify-content: center; margin: 1rem 0; }
|
|
|
39 |
.component-description {
|
40 |
padding: 24px;
|
41 |
+
background: linear-gradient(135deg,#f8fafc 0%,#e2e8f0 100%);
|
42 |
+
border-radius: 12px; border-left: 4px solid #3b82f6;
|
43 |
+
box-shadow: 0 2px 8px rgba(0,0,0,.05); margin: 16px 0;
|
44 |
+
font-family: -apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
}
|
46 |
+
.component-description p{margin:10px 0;line-height:1.6;color:#374151;}
|
47 |
+
.base-description{font-size:17px;font-weight:600;color:#1e293b;}
|
48 |
+
.base-description strong{color:#3b82f6;font-weight:700;}
|
49 |
+
.feature-description{font-size:16px;font-weight:500;color:#1e293b;}
|
50 |
+
.customization-note{font-size:15px;font-style:italic;color:#64748b;}
|
51 |
+
.sample-intro{font-size:15px;font-weight:600;color:#1e293b;margin-top:16px;
|
52 |
+
border-top:1px solid #e2e8f0;padding-top:16px;}
|
53 |
"""
|
54 |
) as demo:
|
55 |
+
|
56 |
with gr.Column(elem_classes=["main-container"]):
|
57 |
+
gr.Markdown("# π¨ Visual Workflow Builder\n**Create sophisticated workflows with drag-and-drop simplicity.**")
|
58 |
+
|
59 |
+
gr.HTML(
|
60 |
+
"""
|
61 |
+
<div class="component-description">
|
62 |
+
<p class="base-description">Base custom component powered by <strong>svelteflow</strong>.</p>
|
63 |
+
<p class="feature-description">Create custom workflows.</p>
|
64 |
+
<p class="customization-note">You can customise the nodes as well as the design of the workflow.</p>
|
65 |
+
<p class="sample-intro">Here is a sample:</p>
|
66 |
+
</div>
|
67 |
+
"""
|
68 |
+
)
|
69 |
+
|
70 |
+
# βββ Workflow Builder βββ
|
|
|
|
|
|
|
71 |
with gr.Column(elem_classes=["workflow-section"]):
|
72 |
workflow_builder = WorkflowBuilder(
|
73 |
label="π¨ Visual Workflow Designer",
|
74 |
+
info="Drag components from the sidebar β connect β click nodes to edit properties",
|
75 |
)
|
76 |
+
|
77 |
+
# βββ Export / Import μμ βββ
|
78 |
+
gr.Markdown("## πΎ Export Β· π Import")
|
79 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
with gr.Row(elem_classes=["button-row"]):
|
81 |
+
btn_preview = gr.Button("π Preview JSON")
|
82 |
+
btn_download = gr.DownloadButton("πΎ Save JSON file")
|
83 |
+
file_upload = gr.File(label="π Load JSON", file_types=[".json"])
|
84 |
|
85 |
+
export_output = gr.Code(language="json", label="Workflow Configuration", lines=12)
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
# βββ μ΄λ²€νΈ λ°μΈλ© βββ
|
88 |
+
btn_preview.click(
|
89 |
+
fn=export_pretty,
|
90 |
+
inputs=workflow_builder,
|
91 |
+
outputs=export_output
|
92 |
+
)
|
93 |
+
btn_download.click(
|
94 |
+
fn=export_file,
|
95 |
+
inputs=workflow_builder
|
96 |
+
)
|
97 |
+
file_upload.change(
|
98 |
+
fn=import_workflow,
|
99 |
+
inputs=file_upload,
|
100 |
+
outputs=[workflow_builder, export_output]
|
101 |
+
)
|
102 |
+
|
103 |
+
# (μ ν) κ°λ¨ μ¬μ©λ²
|
104 |
+
with gr.Accordion("π How to Use", open=False):
|
105 |
+
gr.Markdown(
|
106 |
+
"""
|
107 |
+
1. **Add Components** β drag items from the left sidebar onto the canvas
|
108 |
+
2. **Connect Nodes** β drag from blue circle (output) β gray circle (input)
|
109 |
+
3. **Edit Properties** β click a node to change its settings
|
110 |
+
4. **Save** β *Save JSON file* to download your workflow
|
111 |
+
5. **Load** β *Load JSON* to restore (nodes auto-position)
|
112 |
+
"""
|
113 |
+
)
|
114 |
|
115 |
+
# -------------------------------------------------------------------
|
116 |
+
# π μ€ν
|
117 |
+
# -------------------------------------------------------------------
|
118 |
if __name__ == "__main__":
|
119 |
+
demo.launch(server_name="0.0.0.0", show_error=True)
|
|
|
|
|
|