Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,114 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
-
import
|
|
|
|
| 8 |
|
|
|
|
|
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
model_repo_id = "Abrahamm3r/Z-Image-SDNQ-uint4-svd-r32" # Replace to the model you would like to use
|
| 11 |
-
|
| 12 |
-
if torch.cuda.is_available():
|
| 13 |
-
torch_dtype = torch.float16
|
| 14 |
-
else:
|
| 15 |
-
torch_dtype = torch.float32
|
| 16 |
-
|
| 17 |
-
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 18 |
-
pipe = pipe.to(device)
|
| 19 |
-
|
| 20 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
MAX_IMAGE_SIZE = 1024
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
-
def infer(
|
| 26 |
-
prompt,
|
| 27 |
-
negative_prompt,
|
| 28 |
-
seed,
|
| 29 |
-
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
-
guidance_scale,
|
| 33 |
-
num_inference_steps,
|
| 34 |
-
progress=gr.Progress(track_tqdm=True),
|
| 35 |
-
):
|
| 36 |
-
if randomize_seed:
|
| 37 |
-
seed = random.randint(0, MAX_SEED)
|
| 38 |
-
|
| 39 |
-
generator = torch.Generator().manual_seed(seed)
|
| 40 |
-
|
| 41 |
-
image = pipe(
|
| 42 |
-
prompt=prompt,
|
| 43 |
-
negative_prompt=negative_prompt,
|
| 44 |
-
guidance_scale=guidance_scale,
|
| 45 |
-
num_inference_steps=num_inference_steps,
|
| 46 |
-
width=width,
|
| 47 |
-
height=height,
|
| 48 |
-
generator=generator,
|
| 49 |
-
).images[0]
|
| 50 |
-
|
| 51 |
-
return image, seed
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
examples = [
|
| 55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 56 |
-
"An astronaut riding a green horse",
|
| 57 |
-
"A delicious ceviche cheesecake slice",
|
| 58 |
-
]
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
"""
|
| 66 |
|
| 67 |
-
with gr.Blocks(css=
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown("
|
| 70 |
-
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
-
|
| 73 |
-
label="Prompt",
|
| 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 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
-
|
| 102 |
-
with gr.Row():
|
| 103 |
-
width = gr.Slider(
|
| 104 |
-
label="Width",
|
| 105 |
-
minimum=256,
|
| 106 |
-
maximum=MAX_IMAGE_SIZE,
|
| 107 |
-
step=32,
|
| 108 |
-
value=1024, # Replace with defaults that work for your model
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
height = gr.Slider(
|
| 112 |
-
label="Height",
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
with gr.Row():
|
| 120 |
-
guidance_scale = gr.Slider(
|
| 121 |
-
label="Guidance scale",
|
| 122 |
-
minimum=0.0,
|
| 123 |
-
maximum=10.0,
|
| 124 |
-
step=0.1,
|
| 125 |
-
value=0.0, # Replace with defaults that work for your model
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
num_inference_steps = gr.Slider(
|
| 129 |
-
label="Number of inference steps",
|
| 130 |
-
minimum=1,
|
| 131 |
-
maximum=50,
|
| 132 |
-
step=1,
|
| 133 |
-
value=2, # Replace with defaults that work for your model
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
-
gr.on(
|
| 138 |
-
triggers=[run_button.click, prompt.submit],
|
| 139 |
-
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
prompt,
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import random
|
|
|
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
+
from sdnq import SDNQConfig
|
| 6 |
+
from sdnq.loader import apply_sdnq_options_to_model
|
| 7 |
|
| 8 |
+
# --- Model Configuration ---
|
| 9 |
+
MODEL_ID = "Abrahamm3r/Z-Image-SDNQ-uint4-svd-r32"
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
print(f"Loading model: {MODEL_ID} on {device}...")
|
| 13 |
+
|
| 14 |
+
# 1. Load the pipeline with trust_remote_code=True for Z-Image architecture
|
| 15 |
+
# We use bfloat16 as it is standard for these newer flux/z-image models
|
| 16 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 17 |
+
MODEL_ID,
|
| 18 |
+
torch_dtype=torch.bfloat16,
|
| 19 |
+
trust_remote_code=True
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# 2. Apply SDNQ quantization hooks to the transformer
|
| 23 |
+
# This is critical for the model to run with the compressed weights
|
| 24 |
+
pipe.transformer = apply_sdnq_options_to_model(pipe.transformer)
|
| 25 |
+
|
| 26 |
+
# 3. Optimize memory
|
| 27 |
+
if device == "cuda":
|
| 28 |
+
pipe.to(device)
|
| 29 |
+
# Enable if you are on a smaller GPU (e.g., T4 16GB) to save VRAM
|
| 30 |
+
# pipe.enable_model_cpu_offload()
|
| 31 |
+
|
| 32 |
+
print("Model loaded successfully!")
|
| 33 |
+
|
| 34 |
+
# --- Helper Functions ---
|
| 35 |
+
|
| 36 |
+
# Preset resolutions for Aspect Ratios
|
| 37 |
+
# Z-Image handles various resolutions, but these are safe standard presets
|
| 38 |
+
ASPECT_RATIOS = {
|
| 39 |
+
"1:1 (Square)": (1024, 1024),
|
| 40 |
+
"16:9 (Cinematic)": (1280, 720),
|
| 41 |
+
"9:16 (Portrait)": (720, 1280),
|
| 42 |
+
"4:3 (Photo)": (1152, 864),
|
| 43 |
+
"3:4 (Portrait Photo)": (864, 1152),
|
| 44 |
+
"21:9 (Ultrawide)": (1536, 640)
|
| 45 |
}
|
| 46 |
+
|
| 47 |
+
def generate_image(prompt, negative_prompt, steps, aspect_ratio_choice, seed, guidance_scale):
|
| 48 |
+
# Determine Width/Height from preset
|
| 49 |
+
width, height = ASPECT_RATIOS.get(aspect_ratio_choice, (1024, 1024))
|
| 50 |
+
|
| 51 |
+
# Handle Seed
|
| 52 |
+
if seed == -1:
|
| 53 |
+
seed = random.randint(0, 2**32 - 1)
|
| 54 |
+
generator = torch.Generator(device=device).manual_seed(int(seed))
|
| 55 |
+
|
| 56 |
+
print(f"Generating: '{prompt}' | Steps: {steps} | Size: {width}x{height} | Seed: {seed}")
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
image = pipe(
|
| 60 |
+
prompt=prompt,
|
| 61 |
+
negative_prompt=negative_prompt,
|
| 62 |
+
width=width,
|
| 63 |
+
height=height,
|
| 64 |
+
num_inference_steps=steps,
|
| 65 |
+
guidance_scale=guidance_scale,
|
| 66 |
+
generator=generator
|
| 67 |
+
).images[0]
|
| 68 |
+
return image, seed
|
| 69 |
+
except Exception as e:
|
| 70 |
+
raise gr.Error(f"Generation failed: {str(e)}")
|
| 71 |
+
|
| 72 |
+
# --- Gradio UI ---
|
| 73 |
+
|
| 74 |
+
custom_css = """
|
| 75 |
+
#col-container { max-width: 800px; margin: 0 auto; }
|
| 76 |
+
#generate-btn { background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%); border: none; color: white; }
|
| 77 |
"""
|
| 78 |
|
| 79 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 80 |
with gr.Column(elem_id="col-container"):
|
| 81 |
+
gr.Markdown(f"# ⚡ Z-Image SDNQ (uint4-svd-r32) Generator")
|
| 82 |
+
gr.Markdown(f"Running `{MODEL_ID}`. This uses Structured Decomposable Neural Quantization for high efficiency.")
|
| 83 |
+
|
| 84 |
with gr.Row():
|
| 85 |
+
with gr.Column(scale=2):
|
| 86 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Describe the image you want...", lines=3)
|
| 87 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Low quality, blurry, ugly...", value="low quality, bad anatomy, worst quality, distortion, blurry")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
aspect_ratio = gr.Dropdown(
|
| 91 |
+
label="Aspect Ratio",
|
| 92 |
+
choices=list(ASPECT_RATIOS.keys()),
|
| 93 |
+
value="1:1 (Square)"
|
| 94 |
+
)
|
| 95 |
+
steps = gr.Slider(label="Inference Steps", minimum=4, maximum=50, step=1, value=25)
|
| 96 |
+
|
| 97 |
+
with gr.Row():
|
| 98 |
+
guidance = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=3.5)
|
| 99 |
+
seed = gr.Number(label="Seed (-1 for Random)", value=-1, precision=0)
|
| 100 |
+
|
| 101 |
+
run_btn = gr.Button("Generate Image", elem_id="generate-btn", size="lg")
|
| 102 |
+
|
| 103 |
+
with gr.Column(scale=2):
|
| 104 |
+
result_image = gr.Image(label="Generated Image", type="pil")
|
| 105 |
+
seed_output = gr.Label(label="Used Seed")
|
| 106 |
+
|
| 107 |
+
run_btn.click(
|
| 108 |
+
fn=generate_image,
|
| 109 |
+
inputs=[prompt, negative_prompt, steps, aspect_ratio, seed, guidance],
|
| 110 |
+
outputs=[result_image, seed_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
+
demo.queue().launch()
|