Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,137 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            import numpy as np
         | 
| 3 | 
            +
            import random
         | 
| 4 | 
            +
            import torch
         | 
| 5 | 
            +
            import spaces
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            from PIL import Image
         | 
| 8 | 
            +
            from diffusers import QwenImageEditPipeline
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            import os
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            # --- Model Loading ---
         | 
| 13 | 
            +
            dtype = torch.bfloat16
         | 
| 14 | 
            +
            device = "cuda" if torch.cuda.is_available() else "cpu"
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            # Load the model pipeline
         | 
| 17 | 
            +
            pipe = QwenImageEditPipeline.from_pretrained("Qwen/Qwen-Image-Edit", torch_dtype=dtype).to(device)
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            # --- UI Constants and Helpers ---
         | 
| 20 | 
            +
            MAX_SEED = np.iinfo(np.int32).max
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            # --- Main Inference Function (with hardcoded negative prompt) ---
         | 
| 23 | 
            +
            @spaces.GPU(duration=120)
         | 
| 24 | 
            +
            def infer(
         | 
| 25 | 
            +
                image,
         | 
| 26 | 
            +
                prompt,
         | 
| 27 | 
            +
                seed=42,
         | 
| 28 | 
            +
                randomize_seed=False,
         | 
| 29 | 
            +
                guidance_scale=4.0,
         | 
| 30 | 
            +
                num_inference_steps=50,
         | 
| 31 | 
            +
                progress=gr.Progress(track_tqdm=True),
         | 
| 32 | 
            +
            ):
         | 
| 33 | 
            +
                """
         | 
| 34 | 
            +
                Generates an image using the local Qwen-Image diffusers pipeline.
         | 
| 35 | 
            +
                """
         | 
| 36 | 
            +
                # Hardcode the negative prompt as requested
         | 
| 37 | 
            +
                negative_prompt = "text, watermark, copyright, blurry, low resolution"
         | 
| 38 | 
            +
                
         | 
| 39 | 
            +
                if randomize_seed:
         | 
| 40 | 
            +
                    seed = random.randint(0, MAX_SEED)
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                # Set up the generator for reproducibility
         | 
| 43 | 
            +
                generator = torch.Generator(device=device).manual_seed(seed)
         | 
| 44 | 
            +
                
         | 
| 45 | 
            +
                print(f"Calling pipeline with prompt: '{prompt}'")
         | 
| 46 | 
            +
                print(f"Negative Prompt: '{negative_prompt}'")
         | 
| 47 | 
            +
                print(f"Seed: {seed}, Steps: {num_inference_steps}, Guidance: {guidance_scale}")
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                # Generate the image
         | 
| 50 | 
            +
                image = pipe(
         | 
| 51 | 
            +
                    image,
         | 
| 52 | 
            +
                    prompt=prompt,
         | 
| 53 | 
            +
                    negative_prompt=negative_prompt,
         | 
| 54 | 
            +
                    num_inference_steps=num_inference_steps,
         | 
| 55 | 
            +
                    generator=generator,
         | 
| 56 | 
            +
                    true_cfg_scale=guidance_scale,
         | 
| 57 | 
            +
                    guidance_scale=1.0  # Use a fixed default for distilled guidance
         | 
| 58 | 
            +
                ).images[0]
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                return image, seed
         | 
| 61 | 
            +
             | 
| 62 | 
            +
            # --- Examples and UI Layout ---
         | 
| 63 | 
            +
            examples = []
         | 
| 64 | 
            +
             | 
| 65 | 
            +
            css = """
         | 
| 66 | 
            +
            #col-container {
         | 
| 67 | 
            +
                margin: 0 auto;
         | 
| 68 | 
            +
                max-width: 1024px;
         | 
| 69 | 
            +
            }
         | 
| 70 | 
            +
            """
         | 
| 71 | 
            +
             | 
| 72 | 
            +
            with gr.Blocks(css=css) as demo:
         | 
| 73 | 
            +
                with gr.Column(elem_id="col-container"):
         | 
| 74 | 
            +
                    gr.Markdown('<img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_logo.png" alt="Qwen-Image Logo" width="400" style="display: block; margin: 0 auto;">')
         | 
| 75 | 
            +
                    gr.Markdown("[Learn more](https://github.com/QwenLM/Qwen-Image) about the Qwen-Image series. Try on [Qwen Chat](https://chat.qwen.ai/), or [download model](https://huggingface.co/Qwen/Qwen-Image-Edit) to run locally with ComfyUI or diffusers.")
         | 
| 76 | 
            +
                    with gr.Row():
         | 
| 77 | 
            +
                        with gr.Column():
         | 
| 78 | 
            +
                            input_image = gr.Image(label="Input Image", show_label=False, type="pil")
         | 
| 79 | 
            +
                            prompt = gr.Text(
         | 
| 80 | 
            +
                                label="Prompt",
         | 
| 81 | 
            +
                                show_label=False,
         | 
| 82 | 
            +
                                placeholder="describe the edit instruction",
         | 
| 83 | 
            +
                                container=False,
         | 
| 84 | 
            +
                            )
         | 
| 85 | 
            +
                            run_button = gr.Button("Run", scale=0, variant="primary")
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                        result = gr.Image(label="Result", show_label=False, type="pil")
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                    with gr.Accordion("Advanced Settings", open=False):
         | 
| 90 | 
            +
                        # Negative prompt UI element is removed here
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                        seed = gr.Slider(
         | 
| 93 | 
            +
                            label="Seed",
         | 
| 94 | 
            +
                            minimum=0,
         | 
| 95 | 
            +
                            maximum=MAX_SEED,
         | 
| 96 | 
            +
                            step=1,
         | 
| 97 | 
            +
                            value=0,
         | 
| 98 | 
            +
                        )
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
         | 
| 101 | 
            +
             | 
| 102 | 
            +
                        with gr.Row():
         | 
| 103 | 
            +
                            guidance_scale = gr.Slider(
         | 
| 104 | 
            +
                                label="Guidance scale",
         | 
| 105 | 
            +
                                minimum=0.0,
         | 
| 106 | 
            +
                                maximum=10.0,
         | 
| 107 | 
            +
                                step=0.1,
         | 
| 108 | 
            +
                                value=4.0,
         | 
| 109 | 
            +
                            )
         | 
| 110 | 
            +
             | 
| 111 | 
            +
                            num_inference_steps = gr.Slider(
         | 
| 112 | 
            +
                                label="Number of inference steps",
         | 
| 113 | 
            +
                                minimum=1,
         | 
| 114 | 
            +
                                maximum=50,
         | 
| 115 | 
            +
                                step=1,
         | 
| 116 | 
            +
                                value=30,
         | 
| 117 | 
            +
                            )
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                    # gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
         | 
| 120 | 
            +
             | 
| 121 | 
            +
                gr.on(
         | 
| 122 | 
            +
                    triggers=[run_button.click, prompt.submit],
         | 
| 123 | 
            +
                    fn=infer,
         | 
| 124 | 
            +
                    inputs=[
         | 
| 125 | 
            +
                        input_image,
         | 
| 126 | 
            +
                        prompt,
         | 
| 127 | 
            +
                        # negative_prompt is no longer an input from the UI
         | 
| 128 | 
            +
                        seed,
         | 
| 129 | 
            +
                        randomize_seed,
         | 
| 130 | 
            +
                        guidance_scale,
         | 
| 131 | 
            +
                        num_inference_steps,
         | 
| 132 | 
            +
                    ],
         | 
| 133 | 
            +
                    outputs=[result, seed],
         | 
| 134 | 
            +
                )
         | 
| 135 | 
            +
             | 
| 136 | 
            +
            if __name__ == "__main__":
         | 
| 137 | 
            +
                demo.launch()
         | 
 
			
