Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,104 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
import random
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
import
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
import torch
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(model_repo_id, torch_dtype=torch.float16)
|
| 13 |
-
pipe = pipe.to(device)
|
| 14 |
|
| 15 |
MAX_SEED = np.iinfo(np.int32).max
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if randomize_seed:
|
| 32 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
guidance_scale=guidance_scale,
|
| 40 |
-
num_inference_steps=num_inference_steps,
|
| 41 |
-
width=width,
|
| 42 |
-
height=height,
|
| 43 |
-
generator=generator,
|
| 44 |
-
).images[0]
|
| 45 |
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
examples = [
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
|
|
|
|
|
|
|
|
|
| 53 |
]
|
| 54 |
|
| 55 |
-
css =
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
}
|
| 60 |
-
|
| 61 |
-
|
| 62 |
with gr.Blocks(css=css) as demo:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
with gr.Row():
|
| 67 |
prompt = gr.Text(
|
| 68 |
label="Prompt",
|
|
@@ -71,79 +107,94 @@ with gr.Blocks(css=css) as demo:
|
|
| 71 |
placeholder="Enter your prompt",
|
| 72 |
container=False,
|
| 73 |
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
with gr.Accordion("Advanced Settings", open=True):
|
| 80 |
negative_prompt = gr.Text(
|
| 81 |
label="Negative prompt",
|
| 82 |
-
max_lines=
|
|
|
|
| 83 |
placeholder="Enter a negative prompt",
|
| 84 |
-
|
|
|
|
| 85 |
)
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
step=1,
|
| 92 |
-
value=
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
step=32,
|
| 111 |
-
value=768, # Replace with defaults that work for your model
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
with gr.Row():
|
| 115 |
-
guidance_scale = gr.Slider(
|
| 116 |
-
label="Guidance scale",
|
| 117 |
-
minimum=0.0,
|
| 118 |
-
maximum=10.0,
|
| 119 |
-
step=0.1,
|
| 120 |
-
value=1, # Replace with defaults that work for your model
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
num_inference_steps = gr.Slider(
|
| 124 |
-
label="Number of inference steps",
|
| 125 |
-
minimum=1,
|
| 126 |
-
maximum=50,
|
| 127 |
-
step=1,
|
| 128 |
-
value=28, # Replace with defaults that work for your model
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 132 |
gr.on(
|
| 133 |
-
triggers=[
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
inputs=[
|
| 136 |
prompt,
|
| 137 |
negative_prompt,
|
|
|
|
| 138 |
seed,
|
| 139 |
-
randomize_seed,
|
| 140 |
width,
|
| 141 |
height,
|
| 142 |
guidance_scale,
|
| 143 |
num_inference_steps,
|
|
|
|
| 144 |
],
|
| 145 |
outputs=[result, seed],
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
|
|
|
| 2 |
import random
|
| 3 |
+
import uuid
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import spaces
|
| 10 |
import torch
|
| 11 |
+
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 12 |
|
| 13 |
+
if not torch.cuda.is_available():
|
| 14 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
MAX_SEED = np.iinfo(np.int32).max
|
| 17 |
+
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
|
| 18 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
| 19 |
+
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
| 20 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
| 21 |
+
|
| 22 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 23 |
+
|
| 24 |
+
if torch.cuda.is_available():
|
| 25 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 26 |
+
"anon4ik/noobaiXLNAIXL_epsilonPred05Version",
|
| 27 |
+
torch_dtype=torch.float16,
|
| 28 |
+
use_safetensors=True,
|
| 29 |
+
add_watermarker=False
|
| 30 |
+
)
|
| 31 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 32 |
+
pipe.to("cuda")
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def save_image(img):
|
| 36 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
| 37 |
+
img.save(unique_name)
|
| 38 |
+
return unique_name
|
| 39 |
+
|
| 40 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 41 |
if randomize_seed:
|
| 42 |
seed = random.randint(0, MAX_SEED)
|
| 43 |
+
return seed
|
| 44 |
+
|
| 45 |
+
@spaces.GPU(queue=False,duration=30)
|
| 46 |
+
def generate(
|
| 47 |
+
prompt: str,
|
| 48 |
+
negative_prompt: str = "",
|
| 49 |
+
use_negative_prompt: bool = False,
|
| 50 |
+
seed: int = 1,
|
| 51 |
+
width: int = 1024,
|
| 52 |
+
height: int = 1024,
|
| 53 |
+
guidance_scale: float = 3,
|
| 54 |
+
num_inference_steps: int = 30,
|
| 55 |
+
randomize_seed: bool = False,
|
| 56 |
+
use_resolution_binning: bool = True,
|
| 57 |
+
progress=gr.Progress(track_tqdm=True),
|
| 58 |
+
):
|
| 59 |
+
pipe.to(device)
|
| 60 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 61 |
+
generator = torch.Generator().manual_seed(seed)
|
| 62 |
|
| 63 |
+
options = {
|
| 64 |
+
"prompt":prompt,
|
| 65 |
+
"negative_prompt":negative_prompt,
|
| 66 |
+
"width":width,
|
| 67 |
+
"height":height,
|
| 68 |
+
"guidance_scale":guidance_scale,
|
| 69 |
+
"num_inference_steps":num_inference_steps,
|
| 70 |
+
"generator":generator,
|
| 71 |
+
"use_resolution_binning":use_resolution_binning,
|
| 72 |
+
"output_type":"pil",
|
| 73 |
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
images = pipe(**options).images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
image_paths = [save_image(img) for img in images]
|
| 79 |
+
return image_paths, seed
|
| 80 |
|
| 81 |
|
| 82 |
examples = [
|
| 83 |
+
"a cat eating a piece of cheese",
|
| 84 |
+
"a ROBOT riding a BLUE horse on Mars, photorealistic, 4k",
|
| 85 |
+
"Ironman VS Hulk, ultrarealistic",
|
| 86 |
+
"Astronaut in a jungle, cold color palette, oil pastel, detailed, 8k",
|
| 87 |
+
"An alien holding sign board contain word 'Flash', futuristic, neonpunk",
|
| 88 |
+
"Kids going to school, Anime style"
|
| 89 |
]
|
| 90 |
|
| 91 |
+
css = '''
|
| 92 |
+
.gradio-container{max-width: 560px !important}
|
| 93 |
+
h1{text-align:center}
|
| 94 |
+
footer {
|
| 95 |
+
visibility: hidden
|
| 96 |
}
|
| 97 |
+
'''
|
|
|
|
| 98 |
with gr.Blocks(css=css) as demo:
|
| 99 |
+
gr.Markdown("""# SDXL Flash
|
| 100 |
+
### First Image processing takes time then images generate faster.""")
|
| 101 |
+
with gr.Group():
|
| 102 |
with gr.Row():
|
| 103 |
prompt = gr.Text(
|
| 104 |
label="Prompt",
|
|
|
|
| 107 |
placeholder="Enter your prompt",
|
| 108 |
container=False,
|
| 109 |
)
|
| 110 |
+
run_button = gr.Button("Run", scale=0)
|
| 111 |
+
result = gr.Gallery(label="Result", columns=1)
|
| 112 |
+
with gr.Accordion("Advanced options", open=False):
|
| 113 |
+
with gr.Row():
|
| 114 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
|
|
|
| 115 |
negative_prompt = gr.Text(
|
| 116 |
label="Negative prompt",
|
| 117 |
+
max_lines=5,
|
| 118 |
+
lines=4,
|
| 119 |
placeholder="Enter a negative prompt",
|
| 120 |
+
value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",
|
| 121 |
+
visible=True,
|
| 122 |
)
|
| 123 |
+
seed = gr.Slider(
|
| 124 |
+
label="Seed",
|
| 125 |
+
minimum=0,
|
| 126 |
+
maximum=MAX_SEED,
|
| 127 |
+
step=1,
|
| 128 |
+
value=0,
|
| 129 |
+
)
|
| 130 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 131 |
+
with gr.Row(visible=True):
|
| 132 |
+
width = gr.Slider(
|
| 133 |
+
label="Width",
|
| 134 |
+
minimum=512,
|
| 135 |
+
maximum=MAX_IMAGE_SIZE,
|
| 136 |
+
step=64,
|
| 137 |
+
value=1024,
|
| 138 |
+
)
|
| 139 |
+
height = gr.Slider(
|
| 140 |
+
label="Height",
|
| 141 |
+
minimum=512,
|
| 142 |
+
maximum=MAX_IMAGE_SIZE,
|
| 143 |
+
step=64,
|
| 144 |
+
value=1024,
|
| 145 |
+
)
|
| 146 |
+
with gr.Row():
|
| 147 |
+
guidance_scale = gr.Slider(
|
| 148 |
+
label="Guidance Scale",
|
| 149 |
+
minimum=0.1,
|
| 150 |
+
maximum=6,
|
| 151 |
+
step=0.1,
|
| 152 |
+
value=3.0,
|
| 153 |
+
)
|
| 154 |
+
num_inference_steps = gr.Slider(
|
| 155 |
+
label="Number of inference steps",
|
| 156 |
+
minimum=1,
|
| 157 |
+
maximum=15,
|
| 158 |
step=1,
|
| 159 |
+
value=8,
|
| 160 |
)
|
| 161 |
|
| 162 |
+
gr.Examples(
|
| 163 |
+
examples=examples,
|
| 164 |
+
inputs=prompt,
|
| 165 |
+
outputs=[result, seed],
|
| 166 |
+
fn=generate,
|
| 167 |
+
cache_examples=CACHE_EXAMPLES,
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
use_negative_prompt.change(
|
| 171 |
+
fn=lambda x: gr.update(visible=x),
|
| 172 |
+
inputs=use_negative_prompt,
|
| 173 |
+
outputs=negative_prompt,
|
| 174 |
+
api_name=False,
|
| 175 |
+
)
|
| 176 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
gr.on(
|
| 178 |
+
triggers=[
|
| 179 |
+
prompt.submit,
|
| 180 |
+
negative_prompt.submit,
|
| 181 |
+
run_button.click,
|
| 182 |
+
],
|
| 183 |
+
fn=generate,
|
| 184 |
inputs=[
|
| 185 |
prompt,
|
| 186 |
negative_prompt,
|
| 187 |
+
use_negative_prompt,
|
| 188 |
seed,
|
|
|
|
| 189 |
width,
|
| 190 |
height,
|
| 191 |
guidance_scale,
|
| 192 |
num_inference_steps,
|
| 193 |
+
randomize_seed,
|
| 194 |
],
|
| 195 |
outputs=[result, seed],
|
| 196 |
+
api_name="run",
|
| 197 |
)
|
| 198 |
|
| 199 |
if __name__ == "__main__":
|
| 200 |
+
demo.queue(max_size=20).launch()
|