File size: 4,795 Bytes
9f5f544
 
 
f3cbbb0
 
4686e0a
f7d6307
9f5f544
fdf03b7
 
f3cbbb0
fdf03b7
 
f3cbbb0
fdf03b7
 
f7d6307
fdf03b7
 
f7d6307
fdf03b7
 
 
 
 
 
 
 
 
f7d6307
fdf03b7
 
 
 
 
f7d6307
fdf03b7
 
 
 
 
 
c854a31
 
3d319fd
fdf03b7
9f5f544
 
 
 
 
c854a31
9f5f544
 
fdf03b7
9f5f544
 
 
 
 
 
 
 
c854a31
9f5f544
 
 
 
 
f3cbbb0
9f5f544
 
c854a31
9f5f544
f7d6307
 
f3cbbb0
 
 
fdf03b7
f3cbbb0
6b28610
f3cbbb0
 
9f5f544
 
 
f3cbbb0
9f5f544
6b28610
 
c854a31
9f5f544
 
 
c854a31
9f5f544
c854a31
9f5f544
c854a31
9f5f544
 
c854a31
 
9f5f544
 
f7d6307
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import torch
import spaces
import gradio as gr
from diffusers import ZImagePipeline, ZImageTransformer2DModel
from huggingface_hub import hf_hub_download

print("πŸ”„ Initializing Z-Image-Turbo Pipeline...")

MODEL_FILENAME = "zimageTurboByStable_2603Fp8.safetensors"
REPO_ID = "ajsbsd/ZIT"

print(f"⬇️ Downloading {MODEL_FILENAME} from {REPO_ID}...")
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)

try:
    print("βš™οΈ Attempting to load as a FULL checkpoint (includes Text Encoders + VAE)...")
    # Load in bfloat16 to safely upcast FP8 weights and avoid missing CUDA kernels
    pipe = ZImagePipeline.from_single_file(
        model_path,
        torch_dtype=torch.bfloat16,
        low_cpu_mem_usage=True,
    )
    print("βœ… Successfully loaded as full checkpoint!")
except Exception as e:
    print(f"⚠️ Full checkpoint load failed ({str(e)[:100]}...).")
    print("βš™οΈ Falling back to loading as TRANSFORMER ONLY...")
    
    pipe = ZImagePipeline.from_pretrained(
        "Tongyi-MAI/Z-Image-Turbo",
        torch_dtype=torch.bfloat16,
        low_cpu_mem_usage=True,
    )
    
    transformer = ZImageTransformer2DModel.from_single_file(
        model_path,
        torch_dtype=torch.bfloat16,
        low_cpu_mem_usage=True,
    )
    pipe.transformer = transformer
    print("βœ… Successfully loaded custom transformer!")

# CRITICAL for ZeroGPU: Prevent Out-Of-Memory errors
pipe.enable_model_cpu_offload()
pipe.enable_attention_slicing()

print("πŸš€ Pipeline loaded and optimized for ZeroGPU! Ready to generate.")

@spaces.GPU
def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
    if randomize_seed:
        seed = torch.randint(0, 2**32 - 1, (1,)).item()

    generator = torch.Generator("cuda").manual_seed(int(seed))
    
    # Recommended settings: CFG 1.0 for Z-Image Turbo
    image = pipe(
        prompt=prompt,
        height=int(height),
        width=int(width),
        num_inference_steps=int(num_inference_steps),
        guidance_scale=1.0, 
        generator=generator,
    ).images[0]

    return image, int(seed)

examples = [
    ["Portrait of a young woman with natural skin texture, soft believable lighting, candid editorial style, highly detailed, photorealistic"],
    ["A candid full-body shot of a person walking in a softly lit urban street at dusk, natural real-life look, crisp faces, reliable anatomy"],
    ["Close-up portrait, natural real-photo realism, soft lighting, clean skin texture, no over-processed studio look, 85mm lens"]
]

custom_theme = gr.themes.Soft(primary_hue="emerald", secondary_hue="teal", neutral_hue="slate")

# Gradio 6.0+ Fix: Removed theme from Blocks constructor
with gr.Blocks(title="2603 ZIT β€” By Stable Yogi") as demo:
    gr.Markdown(
        """
        # πŸ“· 2603 ZIT β€” By Stable Yogi
        **Fast photoreal Z-Image Turbo** with a natural, real-life look. Believable skin, faces, and lighting.
        
        **Recommended Settings:** Steps 8–9 Β· CFG 1.0 Β· Resolution 1152Γ—896 or ~1024 square
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            prompt = gr.Textbox(label="✨ Prompt", placeholder="Describe the image you want to create...", lines=4)
            with gr.Accordion("βš™οΈ Advanced Settings", open=False):
                height = gr.Slider(minimum=512, maximum=1536, value=1152, step=64, label="Height")
                width = gr.Slider(minimum=512, maximum=1536, value=896, step=64, label="Width")
                num_inference_steps = gr.Slider(minimum=1, maximum=20, value=8, step=1, label="Inference Steps")
                with gr.Row():
                    randomize_seed = gr.Checkbox(label="🎲 Randomize Seed", value=True)
                    seed = gr.Number(label="Seed", value=42, precision=0, visible=False)
                randomize_seed.change(lambda r: gr.Number(visible=not r), inputs=[randomize_seed], outputs=[seed])
            generate_btn = gr.Button("πŸš€ Generate Image", variant="primary", size="lg")
            gr.Examples(examples=examples, inputs=[prompt], label="πŸ’‘ Try these prompts")
        with gr.Column(scale=1):
            output_image = gr.Image(label="Generated Image", type="pil", format="png", height=600)
            used_seed = gr.Number(label="🎲 Seed Used", interactive=False)

    generate_btn.click(fn=generate_image, inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed], outputs=[output_image, used_seed])
    prompt.submit(fn=generate_image, inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed], outputs=[output_image, used_seed])

if __name__ == "__main__":
    # Gradio 6.0+ Fix: Pass theme to launch()
    demo.launch(theme=custom_theme)