Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Commit 
							
							·
						
						9b6b78e
	
1
								Parent(s):
							
							7419ef7
								
Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,202 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            #!/usr/bin/env python
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            from __future__ import annotations
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            import os
         | 
| 6 | 
            +
            import random
         | 
| 7 | 
            +
            import uuid
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            import gradio as gr
         | 
| 10 | 
            +
            import numpy as np
         | 
| 11 | 
            +
            from PIL import Image
         | 
| 12 | 
            +
            import torch
         | 
| 13 | 
            +
            from diffusers import DiffusionPipeline
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            DESCRIPTION = """# Playground v2"""
         | 
| 16 | 
            +
            if not torch.cuda.is_available():
         | 
| 17 | 
            +
                DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            MAX_SEED = np.iinfo(np.int32).max
         | 
| 20 | 
            +
            CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
         | 
| 21 | 
            +
            MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
         | 
| 22 | 
            +
            USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "1") == "1"
         | 
| 23 | 
            +
            ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            NUM_IMAGES_PER_PROMPT = 1
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            if torch.cuda.is_available():
         | 
| 30 | 
            +
                pipe = DiffusionPipeline.from_pretrained(
         | 
| 31 | 
            +
                    "playgroundai/playground-v2-1024px-aesthetic",
         | 
| 32 | 
            +
                    torch_dtype=torch.float16,
         | 
| 33 | 
            +
                    use_safetensors=True,
         | 
| 34 | 
            +
                    add_watermarker=False,
         | 
| 35 | 
            +
                    variant="fp16"
         | 
| 36 | 
            +
                )
         | 
| 37 | 
            +
                if ENABLE_CPU_OFFLOAD:
         | 
| 38 | 
            +
                    pipe.enable_model_cpu_offload()
         | 
| 39 | 
            +
                else:
         | 
| 40 | 
            +
                    pipe.to(device)
         | 
| 41 | 
            +
                    print("Loaded on Device!")
         | 
| 42 | 
            +
                
         | 
| 43 | 
            +
                if USE_TORCH_COMPILE:
         | 
| 44 | 
            +
                    pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
         | 
| 45 | 
            +
                    print("Model Compiled!")
         | 
| 46 | 
            +
             | 
| 47 | 
            +
             | 
| 48 | 
            +
            def save_image(img):
         | 
| 49 | 
            +
                unique_name = str(uuid.uuid4()) + ".png"
         | 
| 50 | 
            +
                img.save(unique_name)
         | 
| 51 | 
            +
                return unique_name
         | 
| 52 | 
            +
             | 
| 53 | 
            +
             | 
| 54 | 
            +
            def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
         | 
| 55 | 
            +
                if randomize_seed:
         | 
| 56 | 
            +
                    seed = random.randint(0, MAX_SEED)
         | 
| 57 | 
            +
                return seed
         | 
| 58 | 
            +
             | 
| 59 | 
            +
             | 
| 60 | 
            +
            def generate(
         | 
| 61 | 
            +
                prompt: str,
         | 
| 62 | 
            +
                negative_prompt: str = "",
         | 
| 63 | 
            +
                use_negative_prompt: bool = False,
         | 
| 64 | 
            +
                seed: int = 0,
         | 
| 65 | 
            +
                width: int = 1024,
         | 
| 66 | 
            +
                height: int = 1024,
         | 
| 67 | 
            +
                guidance_scale: float = 3,
         | 
| 68 | 
            +
                randomize_seed: bool = False,
         | 
| 69 | 
            +
                use_resolution_binning: bool = True,
         | 
| 70 | 
            +
                progress=gr.Progress(track_tqdm=True),
         | 
| 71 | 
            +
            ):
         | 
| 72 | 
            +
                seed = int(randomize_seed_fn(seed, randomize_seed))
         | 
| 73 | 
            +
                generator = torch.Generator().manual_seed(seed)
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                if not use_negative_prompt:
         | 
| 76 | 
            +
                    negative_prompt = None  # type: ignore
         | 
| 77 | 
            +
                prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
         | 
| 78 | 
            +
                
         | 
| 79 | 
            +
                images = pipe(
         | 
| 80 | 
            +
                    prompt=prompt,
         | 
| 81 | 
            +
                    negative_prompt=negative_prompt,
         | 
| 82 | 
            +
                    width=width,
         | 
| 83 | 
            +
                    height=height,
         | 
| 84 | 
            +
                    guidance_scale=guidance_scale,
         | 
| 85 | 
            +
                    num_inference_steps=num_inference_steps,
         | 
| 86 | 
            +
                    generator=generator,
         | 
| 87 | 
            +
                    num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
         | 
| 88 | 
            +
                    use_resolution_binning=use_resolution_binning,
         | 
| 89 | 
            +
                    output_type="pil",
         | 
| 90 | 
            +
                ).images
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                image_paths = [save_image(img) for img in images]
         | 
| 93 | 
            +
                print(image_paths)
         | 
| 94 | 
            +
                return image_paths, seed
         | 
| 95 | 
            +
             | 
| 96 | 
            +
             | 
| 97 | 
            +
            examples = [
         | 
| 98 | 
            +
                "neon holography crystal cat",
         | 
| 99 | 
            +
                "a cat eating a piece of cheese",
         | 
| 100 | 
            +
                "an astronaut riding a horse in space",
         | 
| 101 | 
            +
                "a cartoon of a boy playing with a tiger",
         | 
| 102 | 
            +
                "a cute robot artist painting on an easel, concept art",
         | 
| 103 | 
            +
                "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
         | 
| 104 | 
            +
            ]
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            with gr.Blocks(css="style.css") as demo:
         | 
| 107 | 
            +
                gr.Markdown(DESCRIPTION)
         | 
| 108 | 
            +
                gr.DuplicateButton(
         | 
| 109 | 
            +
                    value="Duplicate Space for private use",
         | 
| 110 | 
            +
                    elem_id="duplicate-button",
         | 
| 111 | 
            +
                    visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
         | 
| 112 | 
            +
                )
         | 
| 113 | 
            +
                with gr.Group():
         | 
| 114 | 
            +
                    with gr.Row():
         | 
| 115 | 
            +
                        prompt = gr.Text(
         | 
| 116 | 
            +
                            label="Prompt",
         | 
| 117 | 
            +
                            show_label=False,
         | 
| 118 | 
            +
                            max_lines=1,
         | 
| 119 | 
            +
                            placeholder="Enter your prompt",
         | 
| 120 | 
            +
                            container=False,
         | 
| 121 | 
            +
                        )
         | 
| 122 | 
            +
                        run_button = gr.Button("Run", scale=0)
         | 
| 123 | 
            +
                    result = gr.Gallery(label="Result", columns=NUM_IMAGES_PER_PROMPT, show_label=False)
         | 
| 124 | 
            +
                with gr.Accordion("Advanced options", open=False):
         | 
| 125 | 
            +
                    with gr.Row():
         | 
| 126 | 
            +
                        use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
         | 
| 127 | 
            +
                        negative_prompt = gr.Text(
         | 
| 128 | 
            +
                            label="Negative prompt",
         | 
| 129 | 
            +
                            max_lines=1,
         | 
| 130 | 
            +
                            placeholder="Enter a negative prompt",
         | 
| 131 | 
            +
                            visible=True,
         | 
| 132 | 
            +
                        )
         | 
| 133 | 
            +
                    seed = gr.Slider(
         | 
| 134 | 
            +
                        label="Seed",
         | 
| 135 | 
            +
                        minimum=0,
         | 
| 136 | 
            +
                        maximum=MAX_SEED,
         | 
| 137 | 
            +
                        step=1,
         | 
| 138 | 
            +
                        value=0,
         | 
| 139 | 
            +
                    )
         | 
| 140 | 
            +
                    randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
         | 
| 141 | 
            +
                    with gr.Row(visible=True):
         | 
| 142 | 
            +
                        width = gr.Slider(
         | 
| 143 | 
            +
                            label="Width",
         | 
| 144 | 
            +
                            minimum=256,
         | 
| 145 | 
            +
                            maximum=MAX_IMAGE_SIZE,
         | 
| 146 | 
            +
                            step=32,
         | 
| 147 | 
            +
                            value=1024,
         | 
| 148 | 
            +
                        )
         | 
| 149 | 
            +
                        height = gr.Slider(
         | 
| 150 | 
            +
                            label="Height",
         | 
| 151 | 
            +
                            minimum=256,
         | 
| 152 | 
            +
                            maximum=MAX_IMAGE_SIZE,
         | 
| 153 | 
            +
                            step=32,
         | 
| 154 | 
            +
                            value=1024,
         | 
| 155 | 
            +
                        )
         | 
| 156 | 
            +
                    with gr.Row():
         | 
| 157 | 
            +
                        guidance_scale = gr.Slider(
         | 
| 158 | 
            +
                            label="Guidance Scale",
         | 
| 159 | 
            +
                            minimum=0.1,
         | 
| 160 | 
            +
                            maximum=20,
         | 
| 161 | 
            +
                            step=0.1,
         | 
| 162 | 
            +
                            value=3.0,
         | 
| 163 | 
            +
                        )
         | 
| 164 | 
            +
             | 
| 165 | 
            +
                gr.Examples(
         | 
| 166 | 
            +
                    examples=examples,
         | 
| 167 | 
            +
                    inputs=prompt,
         | 
| 168 | 
            +
                    outputs=[result, seed],
         | 
| 169 | 
            +
                    fn=generate,
         | 
| 170 | 
            +
                    cache_examples=CACHE_EXAMPLES,
         | 
| 171 | 
            +
                )
         | 
| 172 | 
            +
             | 
| 173 | 
            +
                use_negative_prompt.change(
         | 
| 174 | 
            +
                    fn=lambda x: gr.update(visible=x),
         | 
| 175 | 
            +
                    inputs=use_negative_prompt,
         | 
| 176 | 
            +
                    outputs=negative_prompt,
         | 
| 177 | 
            +
                    api_name=False,
         | 
| 178 | 
            +
                )
         | 
| 179 | 
            +
             | 
| 180 | 
            +
                gr.on(
         | 
| 181 | 
            +
                    triggers=[
         | 
| 182 | 
            +
                        prompt.submit,
         | 
| 183 | 
            +
                        negative_prompt.submit,
         | 
| 184 | 
            +
                        run_button.click,
         | 
| 185 | 
            +
                    ],
         | 
| 186 | 
            +
                    fn=generate,
         | 
| 187 | 
            +
                    inputs=[
         | 
| 188 | 
            +
                        prompt,
         | 
| 189 | 
            +
                        negative_prompt,
         | 
| 190 | 
            +
                        use_negative_prompt,
         | 
| 191 | 
            +
                        seed,
         | 
| 192 | 
            +
                        width,
         | 
| 193 | 
            +
                        height,
         | 
| 194 | 
            +
                        guidance_scale,
         | 
| 195 | 
            +
                        randomize_seed,
         | 
| 196 | 
            +
                    ],
         | 
| 197 | 
            +
                    outputs=[result, seed],
         | 
| 198 | 
            +
                    api_name="run",
         | 
| 199 | 
            +
                )
         | 
| 200 | 
            +
             | 
| 201 | 
            +
            if __name__ == "__main__":
         | 
| 202 | 
            +
                demo.queue(max_size=20).launch()
         | 
 
			

