cpuai commited on
Commit
befc765
·
verified ·
1 Parent(s): 4cf56ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -1
app.py CHANGED
@@ -419,4 +419,138 @@ def generate(
419
  steps: int,
420
  shift: float,
421
  enhance: bool,
422
- random_s_
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419
  steps: int,
420
  shift: float,
421
  enhance: bool,
422
+ random_seed: bool,
423
+ gallery_images,
424
+ progress=gr.Progress(track_tqdm=True),
425
+ ):
426
+ global pipe
427
+
428
+ # 进入该函数时,ZeroGPU 会尝试分配 GPU;若失败,torch.cuda.is_available 可能为 False
429
+ if pipe is None:
430
+ # 启动阶段不加载,第一次生成时再加载(CPU)
431
+ load_models_cpu_only(MODEL_PATH)
432
+
433
+ if torch.cuda.is_available():
434
+ move_pipe_to_device(torch.device("cuda"))
435
+ warmup_if_needed()
436
+ else:
437
+ # 在 ZeroGPU 排队/额度不足/未正确启用硬件时,给出友好报错
438
+ raise gr.Error(
439
+ "当前未获得 ZeroGPU 的 CUDA 资源(torch.cuda.is_available()==False)。"
440
+ "请确认 Space 硬件选择为 ZeroGPU,并在有额度/队列可用时重试。"
441
+ )
442
+
443
+ final_prompt = prompt
444
+ if enhance:
445
+ final_prompt, _ = prompt_enhance(prompt, True)
446
+
447
+ if random_seed:
448
+ seed = random.randint(1, 1_000_000)
449
+ else:
450
+ seed = int(seed) if int(seed) != -1 else random.randint(1, 1_000_000)
451
+
452
+ try:
453
+ resolution_str = resolution.split(" ")[0] # "1024x1024 (1:1)" -> "1024x1024"
454
+ except Exception:
455
+ resolution_str = "1024x1024"
456
+
457
+ image, used_seed_str = generate_image(
458
+ prompt=final_prompt,
459
+ resolution=resolution_str,
460
+ seed=seed,
461
+ guidance_scale=0.0,
462
+ num_inference_steps=int(steps) + 1,
463
+ shift=float(shift),
464
+ progress=progress,
465
+ )
466
+
467
+ if gallery_images is None:
468
+ gallery_images = []
469
+ gallery_images.append(image)
470
+
471
+ return gallery_images, used_seed_str
472
+
473
+
474
+ # -------------------- UI --------------------
475
+ init_prompt_expander()
476
+
477
+ with gr.Blocks(title="Z-Image Demo") as demo:
478
+ gr.Markdown(
479
+ """<div align="center">
480
+
481
+ # Z-Image Generation Demo
482
+
483
+ *An Efficient Image Generation Foundation Model with Single-Stream Diffusion Transformer*
484
+
485
+ </div>"""
486
+ )
487
+
488
+ with gr.Row():
489
+ with gr.Column(scale=1):
490
+ prompt_input = gr.Textbox(label="Prompt", lines=3, placeholder="Enter your prompt here...")
491
+
492
+ with gr.Row():
493
+ choices = [int(k) for k in RES_CHOICES.keys()]
494
+ res_cat = gr.Dropdown(value=1024, choices=choices, label="Resolution Category")
495
+ initial_res_choices = RES_CHOICES["1024"]
496
+ resolution = gr.Dropdown(
497
+ value=initial_res_choices[0],
498
+ choices=initial_res_choices,
499
+ label="Width x Height (Ratio)",
500
+ )
501
+
502
+ with gr.Row():
503
+ seed = gr.Number(label="Seed", value=-1, precision=0)
504
+ random_seed = gr.Checkbox(label="Random Seed", value=True)
505
+
506
+ with gr.Row():
507
+ steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=8, step=1, interactive=True)
508
+ shift = gr.Slider(label="Time Shift", minimum=1.0, maximum=10.0, value=3.0, step=0.1)
509
+
510
+ enhance = gr.Checkbox(label="Enhance Prompt (DashScope)", value=False)
511
+
512
+ generate_btn = gr.Button("Generate", variant="primary")
513
+
514
+ gr.Markdown("### Example Prompts")
515
+ gr.Examples(examples=EXAMPLE_PROMPTS, inputs=prompt_input, label=None)
516
+
517
+ with gr.Column(scale=1):
518
+ output_gallery = gr.Gallery(
519
+ label="Generated Images",
520
+ columns=2,
521
+ rows=2,
522
+ height=600,
523
+ object_fit="contain",
524
+ format="png",
525
+ interactive=False,
526
+ )
527
+ used_seed = gr.Textbox(label="Seed Used", interactive=False)
528
+
529
+ def update_res_choices(_res_cat):
530
+ key = str(_res_cat)
531
+ res_choices = RES_CHOICES.get(key, RES_CHOICES["1024"])
532
+ return gr.update(value=res_choices[0], choices=res_choices)
533
+
534
+ res_cat.change(update_res_choices, inputs=res_cat, outputs=resolution)
535
+
536
+ def update_seed(current_seed, random_seed_enabled):
537
+ if random_seed_enabled:
538
+ new_seed = random.randint(1, 1_000_000)
539
+ else:
540
+ new_seed = int(current_seed) if int(current_seed) != -1 else random.randint(1, 1_000_000)
541
+ return gr.update(value=new_seed)
542
+
543
+ generate_btn.click(update_seed, inputs=[seed, random_seed], outputs=[seed])
544
+
545
+ generate_btn.click(
546
+ generate,
547
+ inputs=[prompt_input, resolution, seed, steps, shift, enhance, random_seed, output_gallery],
548
+ outputs=[output_gallery, used_seed],
549
+ )
550
+
551
+ css = """
552
+ .fillable { max-width: 1230px !important; }
553
+ """
554
+
555
+ if __name__ == "__main__":
556
+ demo.queue().launch(css=css)