Download app.py from nioLi/2img: direct link, hf CLI and curl.
- Browser
- Download file 5.3 kB
-
https://huggingface.co/spaces/nioLi/2img/resolve/main/app.py
- Command line
-
hf download hf://spaces/nioLi/2img/app.py
-
curl -L -o app.py https://huggingface.co/spaces/nioLi/2img/resolve/main/app.py
5.3 kB
| import random | |
| import spaces | |
| import gradio as gr | |
| import torch | |
| from diffusers import ( | |
| GGUFQuantizationConfig, | |
| QwenImage21Pipeline, | |
| QwenImage21Transformer2DModel, | |
| ) | |
| from huggingface_hub import hf_hub_download | |
| from diffusers.quantizers.gguf.utils import ( | |
| GGUFLinear, GGUFParameter, dequantize_gguf_tensor, | |
| ) | |
| MODEL_ID = "abenzerps/Qwen-Image-2.1-Uncensored-Quantized" | |
| MODEL_REVISION = "7959e658915f52274897fb573ffeda49de8dc16b" | |
| MODEL_FILE = "qwen-image-2.1-UC-Q4_K_M.gguf" | |
| BASE_MODEL_ID = "Qwen/Qwen-Image-2.1" | |
| MAX_SEED = 2**31 - 1 | |
| MAX_IMAGE_SIZE = 1024 | |
| # Load the quantized transformer from the selected repository. | |
| # The upstream pipeline supplies the compatible processor, text encoder, VAE and scheduler. | |
| checkpoint = hf_hub_download( | |
| MODEL_ID, MODEL_FILE, revision=MODEL_REVISION | |
| ) | |
| transformer = QwenImage21Transformer2DModel.from_single_file( | |
| checkpoint, | |
| config=BASE_MODEL_ID, | |
| subfolder="transformer", | |
| quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16), | |
| dtype=torch.bfloat16, | |
| ) | |
| # GGUF stores some non-linear parameters (notably RMSNorm weights) as packed | |
| # BF16 bytes. Only GGUFLinear performs runtime decoding, so decode the other | |
| # small parameters once here while retaining quantized linear weights. | |
| decoded_parameters = 0 | |
| for module in transformer.modules(): | |
| for name, parameter in list(module.named_parameters(recurse=False)): | |
| if isinstance(parameter, GGUFParameter): | |
| if isinstance(module, GGUFLinear) and name == "weight": | |
| continue | |
| decoded = dequantize_gguf_tensor(parameter).to(torch.bfloat16) | |
| module.register_parameter( | |
| name, torch.nn.Parameter(decoded, requires_grad=False) | |
| ) | |
| decoded_parameters += 1 | |
| print(f"Decoded {decoded_parameters} non-linear GGUF parameters.", flush=True) | |
| # ZeroGPU emulates CUDA during startup and allocates a real GPU in infer(). | |
| pipe = QwenImage21Pipeline.from_pretrained( | |
| BASE_MODEL_ID, transformer=transformer, dtype=torch.bfloat16 | |
| ).to("cuda") | |
| pipe.vae.enable_slicing() | |
| pipe.vae.enable_tiling() | |
| def infer( | |
| prompt, | |
| negative_prompt, | |
| seed, | |
| randomize_seed, | |
| width, | |
| height, | |
| guidance_scale, | |
| num_inference_steps, | |
| progress=gr.Progress(track_tqdm=True), | |
| ): | |
| if not prompt or not prompt.strip(): | |
| raise gr.Error("Please enter a prompt.") | |
| seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed) | |
| generator = torch.Generator(device="cpu").manual_seed(seed) | |
| image = pipe( | |
| prompt=prompt.strip(), | |
| negative_prompt=negative_prompt if guidance_scale > 1 else None, | |
| true_cfg_scale=float(guidance_scale), | |
| num_inference_steps=int(num_inference_steps), | |
| width=int(width), | |
| height=int(height), | |
| generator=generator, | |
| ).images[0] | |
| return image, seed | |
| examples = [ | |
| "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", | |
| "An astronaut riding a green horse", | |
| "A delicious ceviche cheesecake slice", | |
| ] | |
| css = """ | |
| #col-container { | |
| margin: 0 auto; | |
| max-width: 640px; | |
| } | |
| """ | |
| with gr.Blocks() as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.Markdown("# Qwen Image 2.1 · Q4_K_M") | |
| with gr.Row(): | |
| prompt = gr.Text( | |
| label="Prompt", | |
| show_label=False, | |
| max_lines=1, | |
| placeholder="Enter your prompt", | |
| container=False, | |
| ) | |
| run_button = gr.Button("Run", scale=0, variant="primary") | |
| result = gr.Image(label="Result", show_label=False) | |
| with gr.Accordion("Advanced Settings", open=False): | |
| negative_prompt = gr.Text( | |
| label="Negative prompt", | |
| max_lines=1, | |
| placeholder="Used when CFG scale is greater than 1", | |
| ) | |
| seed = gr.Slider( | |
| label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0 | |
| ) | |
| randomize_seed = gr.Checkbox(label="Randomize seed", value=True) | |
| with gr.Row(): | |
| width = gr.Slider( | |
| label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, | |
| step=32, value=1024, | |
| ) | |
| height = gr.Slider( | |
| label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, | |
| step=32, value=1024, | |
| ) | |
| with gr.Row(): | |
| guidance_scale = gr.Slider( | |
| label="CFG scale", minimum=1.0, maximum=10.0, | |
| step=0.1, value=1.0, | |
| ) | |
| num_inference_steps = gr.Slider( | |
| label="Number of inference steps", minimum=1, | |
| maximum=50, step=1, value=40, | |
| ) | |
| gr.Examples(examples=examples, inputs=[prompt]) | |
| gr.on( | |
| triggers=[run_button.click, prompt.submit], | |
| fn=infer, | |
| inputs=[ | |
| prompt, negative_prompt, seed, randomize_seed, | |
| width, height, guidance_scale, num_inference_steps, | |
| ], | |
| outputs=[result, seed], | |
| concurrency_limit=1, | |
| api_name="infer", | |
| ) | |
| if __name__ == "__main__": | |
| demo.queue().launch(css=css) | |