gradio-pr-bot commited on
Commit
4a7563e
·
verified ·
1 Parent(s): 7722947

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. run.py +48 -370
README.md CHANGED
@@ -5,7 +5,7 @@ emoji: 🔥
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
- sdk_version: 6.17.3
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
 
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 6.18.0
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
run.py CHANGED
@@ -1,377 +1,55 @@
1
- """Interactive walkthrough for gr.Workflow, built on gr.Walkthrough + gr.WorkflowCanvas.
2
-
3
- Drop into a Space to replace the bare Workflow demo at gradio/workflow with a
4
- guided tour. Each step ships its own pre-loaded canvas so users see the
5
- concept immediately and can play with it.
6
- """
7
-
8
- import json
9
  import os
10
- import tempfile
11
 
12
  import gradio as gr
13
- from gradio.workflow import (
14
- call_model,
15
- call_space,
16
- fetch_dataset,
17
- get_dataset_schema,
18
- get_token,
19
- search_datasets,
20
- search_models,
21
- search_spaces,
22
- )
23
-
24
- SERVER_FUNCTIONS = [
25
- get_token,
26
- call_space,
27
- call_model,
28
- fetch_dataset,
29
- search_spaces,
30
- search_models,
31
- search_datasets,
32
- get_dataset_schema,
33
- ]
34
-
35
- BASE = {"schema_version": "2", "runtime": {"default": "client"}, "view": {"default": "canvas"}}
36
-
37
- WELCOME = json.dumps(
38
- {
39
- **BASE,
40
- "name": "Welcome",
41
- "references": [
42
- {
43
- "id": "ref_text",
44
- "role": "reference",
45
- "asset_type": "text",
46
- "label": "Your prompt",
47
- "inputs": [{"id": "in", "label": "Text", "type": "text"}],
48
- "outputs": [{"id": "out", "label": "Text", "type": "text"}],
49
- "x": 80, "y": 120, "width": 240, "height": 120,
50
- "data": {"in": "a corgi wearing sunglasses"},
51
- }
52
- ],
53
- "operators": [],
54
- "subjects": [
55
- {
56
- "id": "out_text",
57
- "role": "subject",
58
- "asset_type": "text",
59
- "label": "Output",
60
- "inputs": [{"id": "in", "label": "Text", "type": "text"}],
61
- "outputs": [{"id": "out", "label": "Text", "type": "text"}],
62
- "x": 420, "y": 120, "width": 240, "height": 120,
63
- "data": {},
64
- }
65
- ],
66
- "edges": [
67
- {"id": "e1", "from_node_id": "ref_text", "from_port_id": "out",
68
- "to_node_id": "out_text", "to_port_id": "in", "type": "text"}
69
- ],
70
- }
71
- )
72
-
73
- USE_SPACE = json.dumps(
74
- {
75
- **BASE,
76
- "name": "Use a Space",
77
- "references": [
78
- {
79
- "id": "ref_prompt",
80
- "role": "reference", "asset_type": "text", "label": "Prompt",
81
- "inputs": [{"id": "in", "label": "Text", "type": "text"}],
82
- "outputs": [{"id": "out", "label": "Text", "type": "text"}],
83
- "x": 80, "y": 120, "width": 240, "height": 120,
84
- "data": {"in": "a corgi wearing sunglasses, studio lighting"},
85
- }
86
- ],
87
- "operators": [
88
- {
89
- "id": "op_flux",
90
- "role": "operator", "kind": "space",
91
- "source": "hf://spaces/black-forest-labs/FLUX.1-schnell",
92
- "space_id": "black-forest-labs/FLUX.1-schnell",
93
- "label": "FLUX.1-schnell",
94
- "inputs": [{"id": "in_0", "label": "Prompt", "type": "text", "required": True}],
95
- "outputs": [{"id": "out_0", "label": "Image", "type": "image"}],
96
- "x": 380, "y": 120, "width": 260, "height": 124,
97
- "data": {},
98
- }
99
- ],
100
- "subjects": [
101
- {
102
- "id": "out_image",
103
- "role": "subject", "asset_type": "image", "label": "Result",
104
- "inputs": [{"id": "in", "label": "Image", "type": "image"}],
105
- "outputs": [{"id": "out", "label": "Image", "type": "image"}],
106
- "x": 700, "y": 120, "width": 240, "height": 124,
107
- "data": {},
108
- }
109
- ],
110
- "edges": [
111
- {"id": "e1", "from_node_id": "ref_prompt", "from_port_id": "out",
112
- "to_node_id": "op_flux", "to_port_id": "in_0", "type": "text"},
113
- {"id": "e2", "from_node_id": "op_flux", "from_port_id": "out_0",
114
- "to_node_id": "out_image", "to_port_id": "in", "type": "image"},
115
- ],
116
- }
117
- )
118
-
119
- INFERENCE_PROVIDERS = json.dumps(
120
- {
121
- **BASE,
122
- "name": "Inference Providers model",
123
- "references": [
124
- {
125
- "id": "ref_prompt",
126
- "role": "reference", "asset_type": "text", "label": "Prompt",
127
- "inputs": [{"id": "in", "label": "Text", "type": "text"}],
128
- "outputs": [{"id": "out", "label": "Text", "type": "text"}],
129
- "x": 80, "y": 120, "width": 240, "height": 120,
130
- "data": {"in": "a corgi wearing sunglasses, studio lighting"},
131
- }
132
- ],
133
- "operators": [
134
- {
135
- "id": "op_flux_dev",
136
- "role": "operator", "kind": "model", "source": "model",
137
- "model_id": "black-forest-labs/FLUX.1-dev",
138
- "pipeline_tag": "text-to-image",
139
- "label": "FLUX.1-dev",
140
- "inputs": [{"id": "prompt", "label": "Prompt", "type": "text", "required": True}],
141
- "outputs": [{"id": "image", "label": "Image", "type": "image"}],
142
- "x": 380, "y": 120, "width": 260, "height": 124,
143
- "data": {},
144
- }
145
- ],
146
- "subjects": [
147
- {
148
- "id": "out_image",
149
- "role": "subject", "asset_type": "image", "label": "Result",
150
- "inputs": [{"id": "in", "label": "Image", "type": "image"}],
151
- "outputs": [{"id": "out", "label": "Image", "type": "image"}],
152
- "x": 700, "y": 120, "width": 240, "height": 124,
153
- "data": {},
154
- }
155
- ],
156
- "edges": [
157
- {"id": "e1", "from_node_id": "ref_prompt", "from_port_id": "out",
158
- "to_node_id": "op_flux_dev", "to_port_id": "prompt", "type": "text"},
159
- {"id": "e2", "from_node_id": "op_flux_dev", "from_port_id": "image",
160
- "to_node_id": "out_image", "to_port_id": "in", "type": "image"},
161
- ],
162
- }
163
- )
164
-
165
- BRANCHING = json.dumps(
166
- {
167
- **BASE,
168
- "name": "Branch into multiple outputs",
169
- "references": [
170
- {
171
- "id": "ref_prompt",
172
- "role": "reference", "asset_type": "text", "label": "Prompt",
173
- "inputs": [{"id": "in", "label": "Text", "type": "text"}],
174
- "outputs": [{"id": "out", "label": "Text", "type": "text"}],
175
- "x": 80, "y": 200, "width": 240, "height": 120,
176
- "data": {"in": "a tiny astronaut hatching from an egg on the moon"},
177
- }
178
- ],
179
- "operators": [
180
- {
181
- "id": "op_flux_schnell",
182
- "role": "operator", "kind": "model", "source": "model",
183
- "model_id": "black-forest-labs/FLUX.1-schnell",
184
- "pipeline_tag": "text-to-image",
185
- "label": "FLUX.1-schnell",
186
- "inputs": [{"id": "prompt", "label": "Prompt", "type": "text", "required": True}],
187
- "outputs": [{"id": "image", "label": "Image", "type": "image"}],
188
- "x": 380, "y": 80, "width": 260, "height": 124,
189
- "data": {},
190
- },
191
- {
192
- "id": "op_sd35",
193
- "role": "operator", "kind": "model", "source": "model",
194
- "model_id": "stabilityai/stable-diffusion-3.5-large-turbo",
195
- "pipeline_tag": "text-to-image",
196
- "label": "SD 3.5 Large Turbo",
197
- "inputs": [{"id": "prompt", "label": "Prompt", "type": "text", "required": True}],
198
- "outputs": [{"id": "image", "label": "Image", "type": "image"}],
199
- "x": 380, "y": 320, "width": 260, "height": 124,
200
- "data": {},
201
- },
202
- ],
203
- "subjects": [
204
- {
205
- "id": "out_a",
206
- "role": "subject", "asset_type": "image", "label": "FLUX result",
207
- "inputs": [{"id": "in", "label": "Image", "type": "image"}],
208
- "outputs": [{"id": "out", "label": "Image", "type": "image"}],
209
- "x": 700, "y": 80, "width": 240, "height": 124,
210
- "data": {},
211
- },
212
- {
213
- "id": "out_b",
214
- "role": "subject", "asset_type": "image", "label": "SD result",
215
- "inputs": [{"id": "in", "label": "Image", "type": "image"}],
216
- "outputs": [{"id": "out", "label": "Image", "type": "image"}],
217
- "x": 700, "y": 320, "width": 240, "height": 124,
218
- "data": {},
219
- },
220
- ],
221
- "edges": [
222
- {"id": "e1", "from_node_id": "ref_prompt", "from_port_id": "out",
223
- "to_node_id": "op_flux_schnell", "to_port_id": "prompt", "type": "text"},
224
- {"id": "e2", "from_node_id": "ref_prompt", "from_port_id": "out",
225
- "to_node_id": "op_sd35", "to_port_id": "prompt", "type": "text"},
226
- {"id": "e3", "from_node_id": "op_flux_schnell", "from_port_id": "image",
227
- "to_node_id": "out_a", "to_port_id": "in", "type": "image"},
228
- {"id": "e4", "from_node_id": "op_sd35", "from_port_id": "image",
229
- "to_node_id": "out_b", "to_port_id": "in", "type": "image"},
230
- ],
231
- }
232
- )
233
-
234
- CHAIN = json.dumps(
235
- {
236
- **BASE,
237
- "name": "Chain models",
238
- "references": [
239
- {
240
- "id": "ref_image",
241
- "role": "reference", "asset_type": "image", "label": "Source image",
242
- "inputs": [{"id": "in", "label": "Image", "type": "image"}],
243
- "outputs": [{"id": "out", "label": "Image", "type": "image"}],
244
- "x": 80, "y": 120, "width": 240, "height": 124,
245
- "data": {},
246
- }
247
- ],
248
- "operators": [
249
- {
250
- "id": "op_caption",
251
- "role": "operator", "kind": "space",
252
- "source": "hf://spaces/vikhyatk/moondream2",
253
- "space_id": "vikhyatk/moondream2",
254
- "endpoint": "/answer_question",
255
- "label": "Caption (Moondream)",
256
- "inputs": [
257
- {"id": "in_0", "label": "Image", "type": "image", "required": True},
258
- {"id": "in_1", "label": "Question", "type": "text", "required": False,
259
- "default_value": "Describe this image briefly."},
260
- ],
261
- "outputs": [{"id": "out_0", "label": "Caption", "type": "text"}],
262
- "x": 380, "y": 120, "width": 280, "height": 144,
263
- "data": {},
264
- },
265
- {
266
- "id": "op_gen",
267
- "role": "operator", "kind": "model", "source": "model",
268
- "model_id": "black-forest-labs/FLUX.1-schnell",
269
- "pipeline_tag": "text-to-image",
270
- "label": "FLUX.1-schnell",
271
- "inputs": [{"id": "prompt", "label": "Prompt", "type": "text", "required": True}],
272
- "outputs": [{"id": "image", "label": "Image", "type": "image"}],
273
- "x": 720, "y": 120, "width": 260, "height": 124,
274
- "data": {},
275
- },
276
- ],
277
- "subjects": [
278
- {
279
- "id": "out_image",
280
- "role": "subject", "asset_type": "image", "label": "Reimagined",
281
- "inputs": [{"id": "in", "label": "Image", "type": "image"}],
282
- "outputs": [{"id": "out", "label": "Image", "type": "image"}],
283
- "x": 1040, "y": 120, "width": 240, "height": 124,
284
- "data": {},
285
- }
286
- ],
287
- "edges": [
288
- {"id": "e1", "from_node_id": "ref_image", "from_port_id": "out",
289
- "to_node_id": "op_caption", "to_port_id": "in_0", "type": "image"},
290
- {"id": "e2", "from_node_id": "op_caption", "from_port_id": "out_0",
291
- "to_node_id": "op_gen", "to_port_id": "prompt", "type": "text"},
292
- {"id": "e3", "from_node_id": "op_gen", "from_port_id": "image",
293
- "to_node_id": "out_image", "to_port_id": "in", "type": "image"},
294
- ],
295
- }
296
- )
297
-
298
- SHIP_IT_CODE = """\
299
- import gradio as gr
300
-
301
- # Build it once in the visual editor, export the JSON, then ship it as code:
302
- demo = gr.Workflow(value=open("my_workflow.json").read())
303
- demo.launch()
304
- """
305
-
306
-
307
- with gr.Blocks(
308
- title="Gradio Workflow walkthrough",
309
- fill_height=True,
310
- fill_width=True,
311
- ) as demo:
312
- if os.getenv("OAUTH_CLIENT_ID"):
313
- gr.LoginButton(visible=False)
314
-
315
- with gr.Walkthrough(selected=1):
316
- with gr.Step("Welcome", id=1):
317
- gr.Markdown(
318
- "## What is a Workflow?\n"
319
- "A **Workflow** is a visual pipeline of AI models. Each box is a "
320
- "node — a piece of data or an operation — and edges between them "
321
- "carry values. Click **Run** to execute end-to-end.\n\n"
322
- "Use the steps above to see what you can do."
323
- )
324
- gr.WorkflowCanvas(value=WELCOME, server_functions=SERVER_FUNCTIONS)
325
-
326
- with gr.Step("Use a Space", id=2):
327
- gr.Markdown(
328
- "## Drop in any Gradio Space\n"
329
- "Workflow inspects a Space's API on the fly and exposes each "
330
- "parameter as a port you can wire up. Here we route a prompt "
331
- "into [FLUX.1-schnell](https://huggingface.co/spaces/black-forest-labs/FLUX.1-schnell)."
332
- )
333
- gr.WorkflowCanvas(value=USE_SPACE, server_functions=SERVER_FUNCTIONS)
334
-
335
- with gr.Step("Inference Providers", id=3):
336
- gr.Markdown(
337
- "## Skip the GPU quota with Inference Providers\n"
338
- "Pick a model directly from the Hub (instead of a Space) and "
339
- "Workflow routes it through HF Inference Providers — generally "
340
- "faster, no ZeroGPU quota, and load-balanced across providers."
341
- )
342
- gr.WorkflowCanvas(value=INFERENCE_PROVIDERS, server_functions=SERVER_FUNCTIONS)
343
-
344
- with gr.Step("Branch", id=4):
345
- gr.Markdown(
346
- "## Fan out, compare, combine\n"
347
- "An output port can feed multiple downstream nodes. Here a "
348
- "single prompt drives two different image generators so you "
349
- "can compare them side by side."
350
- )
351
- gr.WorkflowCanvas(value=BRANCHING, server_functions=SERVER_FUNCTIONS)
352
-
353
- with gr.Step("Chain", id=5):
354
- gr.Markdown(
355
- "## Chain models together\n"
356
- "The output of one model becomes the input to the next. This "
357
- "pipeline captions an image with Moondream, then sends the "
358
- "caption to FLUX to re-imagine it."
359
- )
360
- gr.WorkflowCanvas(value=CHAIN, server_functions=SERVER_FUNCTIONS)
361
 
362
- with gr.Step("Ship it", id=6):
363
- gr.Markdown(
364
- "## From canvas to app\n"
365
- "When you're happy, export the workflow JSON and drop it into "
366
- "a `gr.Workflow(...)` in Python — same canvas, embeddable "
367
- "anywhere a Gradio app runs."
368
- )
369
- gr.Code(value=SHIP_IT_CODE, language="python", interactive=False)
370
- gr.Markdown(
371
- "📚 [Workflow docs](https://gradio.app/docs/gradio/workflow) · "
372
- "💻 [Source on GitHub](https://github.com/gradio-app/gradio)"
373
- )
374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
 
376
  if __name__ == "__main__":
377
- demo.launch(allowed_paths=[tempfile.gettempdir()])
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import random
3
 
4
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ def describe_product(image) -> str:
8
+ if image is None:
9
+ return "a premium product"
10
+ img_path = image.get("path") if isinstance(image, dict) else str(image)
11
+ img_url = image.get("url", "") if isinstance(image, dict) else str(image)
12
+ try:
13
+ from gradio_client import Client, handle_file
14
+ source = img_path if (img_path and os.path.exists(img_path)) else img_url
15
+ if not source:
16
+ return "a premium product"
17
+ client = Client("vikhyatk/moondream2", verbose=False)
18
+ result = client.predict(
19
+ handle_file(source),
20
+ "Describe this product briefly and concisely. What is it?",
21
+ api_name="/answer_question",
22
+ )
23
+ return str(result).strip() if result else "a premium product"
24
+ except Exception:
25
+ return "a premium product"
26
+
27
+
28
+ def craft_marketing_prompt(caption: str) -> str:
29
+ if not caption:
30
+ caption = "a premium product"
31
+ caption = caption.strip().rstrip(".")
32
+ styles = [
33
+ (
34
+ f"Professional product advertisement photograph of {caption}, "
35
+ "studio lighting, clean white background, commercial photography, "
36
+ "ultra-sharp, 8K quality"
37
+ ),
38
+ (
39
+ f"Cinematic product shot of {caption}, dramatic lighting, "
40
+ "aspirational lifestyle context, premium brand aesthetic, "
41
+ "shot on Hasselblad, magazine cover quality"
42
+ ),
43
+ (
44
+ f"Bold marketing campaign visual of {caption}, vibrant colors, "
45
+ "dynamic composition, modern editorial style, "
46
+ "award-winning commercial photography"
47
+ ),
48
+ ]
49
+ return random.choice(styles)
50
+
51
+
52
+ demo = gr.Workflow(bind=[describe_product, craft_marketing_prompt])
53
 
54
  if __name__ == "__main__":
55
+ demo.launch()