SpaceNugget commited on
Commit
ddb78fd
·
1 Parent(s): 5a95890

feat: generate model from text prompt; restore garment upload

Browse files
Files changed (2) hide show
  1. app.py +64 -1
  2. workflow_model_api.json +78 -0
app.py CHANGED
@@ -415,6 +415,59 @@ def generate(target_img, clothing_img, progress=gr.Progress(track_tqdm=True)):
415
  raise gr.Error("No output image was returned by ComfyUI.") # noqa: returns (image, file) on success
416
 
417
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
418
  # ---------------------------------------------------------------------------
419
  # Access code — set the same code somewhere visible in your CV
420
  # ---------------------------------------------------------------------------
@@ -530,7 +583,16 @@ with gr.Blocks(title="Virtual Try On", theme=gr.themes.Soft(), js=_gate_js) as d
530
  )
531
  with gr.Row():
532
  target_input = gr.Image(label="Person", type="numpy", height=350)
533
- clothing_input = gr.Image(label="Selected garment", type="numpy", height=350, interactive=False)
 
 
 
 
 
 
 
 
 
534
 
535
  with gr.Row():
536
  with gr.Column():
@@ -561,6 +623,7 @@ with gr.Blocks(title="Virtual Try On", theme=gr.themes.Soft(), js=_gate_js) as d
561
  )
562
 
563
  btn.click(fn=generate, inputs=[target_input, clothing_input], outputs=[output, download, garment_label])
 
564
 
565
  # Unlock is handled entirely client-side via _gate_js — no server call needed.
566
 
 
415
  raise gr.Error("No output image was returned by ComfyUI.") # noqa: returns (image, file) on success
416
 
417
 
418
+ @spaces.GPU(duration=90)
419
+ def generate_model(description):
420
+ """Generate a standing fashion model from a text description."""
421
+ import numpy as np
422
+
423
+ start_comfyui()
424
+
425
+ try:
426
+ requests.post(f"{COMFYUI_URL}/free", json={"unload_models": False, "free_memory": True})
427
+ except Exception:
428
+ pass
429
+
430
+ seed = int(time.time() * 1000) % (2 ** 32)
431
+
432
+ with open("workflow_model_api.json") as f:
433
+ workflow = json.load(f)
434
+
435
+ base = "professional fashion model, full body, standing, neutral pose, plain white background, high quality photograph"
436
+ prompt = f"{base}, {description.strip()}" if description.strip() else base
437
+ workflow["m_text"]["inputs"]["text"] = prompt
438
+ workflow["m_noise"]["inputs"]["noise_seed"] = seed
439
+
440
+ client_id = uuid.uuid4().hex
441
+ resp = requests.post(f"{COMFYUI_URL}/prompt", json={"prompt": workflow, "client_id": client_id})
442
+ resp.raise_for_status()
443
+ prompt_id = resp.json()["prompt_id"]
444
+
445
+ started = time.time()
446
+ while True:
447
+ history = requests.get(f"{COMFYUI_URL}/history/{prompt_id}").json()
448
+ if prompt_id in history:
449
+ entry = history[prompt_id]
450
+ if entry.get("status", {}).get("status_str") == "error" or entry.get("error"):
451
+ raise gr.Error(f"Model generation failed: {entry.get('error', 'unknown error')}")
452
+ break
453
+ if time.time() - started > 80:
454
+ raise gr.Error("Model generation timed out.")
455
+ time.sleep(3)
456
+
457
+ outputs = history[prompt_id].get("outputs", {})
458
+ for node_output in outputs.values():
459
+ if "images" in node_output:
460
+ img_info = node_output["images"][0]
461
+ img_bytes = requests.get(
462
+ f"{COMFYUI_URL}/view",
463
+ params={"filename": img_info["filename"], "subfolder": img_info.get("subfolder", ""), "type": img_info.get("type", "output")},
464
+ ).content
465
+ img = Image.open(BytesIO(img_bytes)).convert("RGB")
466
+ return np.array(img)
467
+
468
+ raise gr.Error("No image returned by model generation.")
469
+
470
+
471
  # ---------------------------------------------------------------------------
472
  # Access code — set the same code somewhere visible in your CV
473
  # ---------------------------------------------------------------------------
 
583
  )
584
  with gr.Row():
585
  target_input = gr.Image(label="Person", type="numpy", height=350)
586
+ clothing_input = gr.Image(label="Garment", type="numpy", height=350)
587
+
588
+ with gr.Row():
589
+ model_prompt = gr.Textbox(
590
+ label="Or generate a model",
591
+ placeholder="e.g. woman with curly red hair, 30s / young man, athletic build",
592
+ lines=1,
593
+ scale=5,
594
+ )
595
+ gen_model_btn = gr.Button("Generate Model", scale=1)
596
 
597
  with gr.Row():
598
  with gr.Column():
 
623
  )
624
 
625
  btn.click(fn=generate, inputs=[target_input, clothing_input], outputs=[output, download, garment_label])
626
+ gen_model_btn.click(fn=generate_model, inputs=[model_prompt], outputs=[target_input])
627
 
628
  # Unlock is handled entirely client-side via _gate_js — no server call needed.
629
 
workflow_model_api.json ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "m_unet": {
3
+ "inputs": { "unet_name": "flux-2-klein-4b-Q8_0.gguf" },
4
+ "class_type": "UnetLoaderGGUF",
5
+ "_meta": { "title": "Load UNet" }
6
+ },
7
+ "m_clip": {
8
+ "inputs": { "clip_name": "qwen_3_4b.safetensors", "type": "flux2", "device": "default" },
9
+ "class_type": "CLIPLoader",
10
+ "_meta": { "title": "Load CLIP" }
11
+ },
12
+ "m_vae": {
13
+ "inputs": { "vae_name": "flux2-vae.safetensors" },
14
+ "class_type": "VAELoader",
15
+ "_meta": { "title": "Load VAE" }
16
+ },
17
+ "m_text": {
18
+ "inputs": { "text": "", "clip": ["m_clip", 0] },
19
+ "class_type": "CLIPTextEncode",
20
+ "_meta": { "title": "Prompt" }
21
+ },
22
+ "m_neg": {
23
+ "inputs": { "conditioning": ["m_text", 0] },
24
+ "class_type": "ConditioningZeroOut",
25
+ "_meta": { "title": "Negative" }
26
+ },
27
+ "m_latent": {
28
+ "inputs": { "width": 768, "height": 1216, "batch_size": 1 },
29
+ "class_type": "EmptyFlux2LatentImage",
30
+ "_meta": { "title": "Empty Latent" }
31
+ },
32
+ "m_noise": {
33
+ "inputs": { "noise_seed": 0 },
34
+ "class_type": "RandomNoise",
35
+ "_meta": { "title": "Noise" }
36
+ },
37
+ "m_sampler": {
38
+ "inputs": { "sampler_name": "euler" },
39
+ "class_type": "KSamplerSelect",
40
+ "_meta": { "title": "Sampler" }
41
+ },
42
+ "m_sched": {
43
+ "inputs": { "steps": 20, "width": 768, "height": 1216 },
44
+ "class_type": "Flux2Scheduler",
45
+ "_meta": { "title": "Scheduler" }
46
+ },
47
+ "m_cfg": {
48
+ "inputs": {
49
+ "cfg": 1,
50
+ "model": ["m_unet", 0],
51
+ "positive": ["m_text", 0],
52
+ "negative": ["m_neg", 0]
53
+ },
54
+ "class_type": "CFGGuider",
55
+ "_meta": { "title": "CFG Guider" }
56
+ },
57
+ "m_run": {
58
+ "inputs": {
59
+ "noise": ["m_noise", 0],
60
+ "guider": ["m_cfg", 0],
61
+ "sampler": ["m_sampler", 0],
62
+ "sigmas": ["m_sched", 0],
63
+ "latent_image": ["m_latent", 0]
64
+ },
65
+ "class_type": "SamplerCustomAdvanced",
66
+ "_meta": { "title": "Run Sampler" }
67
+ },
68
+ "m_decode": {
69
+ "inputs": { "samples": ["m_run", 0], "vae": ["m_vae", 0] },
70
+ "class_type": "VAEDecode",
71
+ "_meta": { "title": "Decode" }
72
+ },
73
+ "m_save": {
74
+ "inputs": { "filename_prefix": "model_gen", "images": ["m_decode", 0] },
75
+ "class_type": "SaveImage",
76
+ "_meta": { "title": "Save" }
77
+ }
78
+ }