Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,35 +10,33 @@ from typing import Tuple
|
|
| 10 |
import torch
|
| 11 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 12 |
|
| 13 |
-
|
| 14 |
DESCRIPTION = """# InterDiffusion-4.0
|
| 15 |
### [https://huggingface.co/cutycat2000x/InterDiffusion-4.0](https://huggingface.co/cutycat2000x/InterDiffusion-4.0)"""
|
| 16 |
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def save_image(img):
|
| 19 |
-
|
| 20 |
-
img.save(
|
| 21 |
-
return
|
| 22 |
|
| 23 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 24 |
-
if randomize_seed
|
| 25 |
-
seed = random.randint(0, MAX_SEED)
|
| 26 |
-
return seed
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 31 |
-
|
| 32 |
-
if not torch.cuda.is_available():
|
| 33 |
-
DESCRIPTION += "\n<p>Running on CPU, This may not work on CPU.</p>"
|
| 34 |
-
|
| 35 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 36 |
-
|
| 37 |
-
USE_TORCH_COMPILE = 0
|
| 38 |
-
ENABLE_CPU_OFFLOAD = 0
|
| 39 |
-
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if torch.cuda.is_available():
|
| 44 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
@@ -46,55 +44,18 @@ if torch.cuda.is_available():
|
|
| 46 |
torch_dtype=torch.float16,
|
| 47 |
use_safetensors=True,
|
| 48 |
)
|
| 49 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 50 |
pipe.load_lora_weights("cutycat2000x/LoRA2", weight_name="lora.safetensors", adapter_name="adapt")
|
| 51 |
pipe.set_adapters("adapt")
|
| 52 |
pipe.to("cuda")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
style_list = [
|
| 59 |
-
{
|
| 60 |
-
"name": "(LoRA)",
|
| 61 |
-
"prompt": "{prompt}",
|
| 62 |
-
"negative_prompt": "",
|
| 63 |
-
},
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
]
|
| 67 |
-
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
| 68 |
-
STYLE_NAMES = list(styles.keys())
|
| 69 |
-
DEFAULT_STYLE_NAME = "(LoRA)"
|
| 70 |
-
|
| 71 |
-
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
| 72 |
-
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 73 |
-
if not negative:
|
| 74 |
-
negative = ""
|
| 75 |
-
return p.replace("{prompt}", positive), n + negative
|
| 76 |
-
|
| 77 |
@spaces.GPU(enable_queue=True)
|
| 78 |
-
def generate(
|
| 79 |
-
|
| 80 |
-
negative_prompt: str = "",
|
| 81 |
-
style: str = DEFAULT_STYLE_NAME,
|
| 82 |
-
use_negative_prompt: bool = False,
|
| 83 |
-
num_inference_steps: int = 30,
|
| 84 |
-
num_images_per_prompt: int = 2,
|
| 85 |
-
seed: int = 0,
|
| 86 |
-
width: int = 1024,
|
| 87 |
-
height: int = 1024,
|
| 88 |
-
guidance_scale: float = 3,
|
| 89 |
-
randomize_seed: bool = False,
|
| 90 |
-
progress=gr.Progress(track_tqdm=True),
|
| 91 |
-
):
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 95 |
|
|
|
|
| 96 |
if not use_negative_prompt:
|
| 97 |
-
negative_prompt = ""
|
| 98 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
| 99 |
|
| 100 |
images = pipe(
|
|
@@ -106,160 +67,83 @@ def generate(
|
|
| 106 |
num_inference_steps=num_inference_steps,
|
| 107 |
num_images_per_prompt=num_images_per_prompt,
|
| 108 |
cross_attention_kwargs={"scale": 0.65},
|
| 109 |
-
output_type="pil"
|
| 110 |
).images
|
|
|
|
| 111 |
image_paths = [save_image(img) for img in images]
|
| 112 |
-
print(image_paths)
|
| 113 |
return image_paths, seed
|
| 114 |
|
| 115 |
examples = [
|
| 116 |
'a smiling girl with sparkles in her eyes, walking in a garden, in the morning --style anime',
|
| 117 |
-
'firewatch landscape, Graphic Novel, Pastel Art
|
| 118 |
'Cat on a tree sitting in between parrots.',
|
| 119 |
-
'cat, 4k,
|
| 120 |
-
'
|
| 121 |
'frozen elsa',
|
| 122 |
'A rainbow tree, anime style, tree in focus',
|
| 123 |
-
'A cat holding a sign that reads "Hello World"
|
| 124 |
-
'Odette the butterfly goddess
|
| 125 |
]
|
| 126 |
|
| 127 |
css = '''
|
| 128 |
.gradio-container{max-width: 560px !important}
|
| 129 |
h1{text-align:center}
|
| 130 |
-
footer {
|
| 131 |
-
visibility: hidden
|
| 132 |
-
}
|
| 133 |
'''
|
| 134 |
|
| 135 |
with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
|
| 136 |
gr.Markdown(DESCRIPTION)
|
| 137 |
-
gr.DuplicateButton(
|
| 138 |
-
value="Duplicate Space for private use",
|
| 139 |
-
elem_id="duplicate-button",
|
| 140 |
-
visible=False,
|
| 141 |
-
)
|
| 142 |
|
| 143 |
with gr.Group():
|
| 144 |
with gr.Row():
|
| 145 |
-
prompt = gr.
|
| 146 |
-
label="Prompt",
|
| 147 |
-
show_label=False,
|
| 148 |
-
max_lines=1,
|
| 149 |
-
placeholder="Enter your prompt",
|
| 150 |
-
container=False,
|
| 151 |
)
|
| 152 |
run_button = gr.Button("Run")
|
|
|
|
| 153 |
result = gr.Gallery(label="Result", columns=1, preview=True)
|
|
|
|
| 154 |
with gr.Accordion("Advanced options", open=False):
|
| 155 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False
|
| 156 |
-
negative_prompt = gr.
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
visible=True,
|
| 161 |
-
)
|
| 162 |
-
with gr.Row():
|
| 163 |
-
num_inference_steps = gr.Slider(
|
| 164 |
-
label="Steps",
|
| 165 |
-
minimum=10,
|
| 166 |
-
maximum=60,
|
| 167 |
-
step=1,
|
| 168 |
-
value=30,
|
| 169 |
-
)
|
| 170 |
-
with gr.Row():
|
| 171 |
-
num_images_per_prompt = gr.Slider(
|
| 172 |
-
label="Images",
|
| 173 |
-
minimum=1,
|
| 174 |
-
maximum=5,
|
| 175 |
-
step=1,
|
| 176 |
-
value=2,
|
| 177 |
-
)
|
| 178 |
-
seed = gr.Slider(
|
| 179 |
-
label="Seed",
|
| 180 |
-
minimum=0,
|
| 181 |
-
maximum=MAX_SEED,
|
| 182 |
-
step=1,
|
| 183 |
-
value=0,
|
| 184 |
-
visible=True
|
| 185 |
-
)
|
| 186 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
maximum=2048,
|
| 192 |
-
step=8,
|
| 193 |
-
value=1024,
|
| 194 |
-
)
|
| 195 |
-
height = gr.Slider(
|
| 196 |
-
label="Height",
|
| 197 |
-
minimum=512,
|
| 198 |
-
maximum=2048,
|
| 199 |
-
step=8,
|
| 200 |
-
value=1024,
|
| 201 |
-
)
|
| 202 |
-
with gr.Row():
|
| 203 |
-
guidance_scale = gr.Slider(
|
| 204 |
-
label="Guidance Scale",
|
| 205 |
-
minimum=0.1,
|
| 206 |
-
maximum=20.0,
|
| 207 |
-
step=0.1,
|
| 208 |
-
value=6,
|
| 209 |
-
)
|
| 210 |
-
with gr.Row(visible=True):
|
| 211 |
-
style_selection = gr.Radio(
|
| 212 |
-
show_label=True,
|
| 213 |
-
container=True,
|
| 214 |
-
interactive=True,
|
| 215 |
-
choices=STYLE_NAMES,
|
| 216 |
-
value=DEFAULT_STYLE_NAME,
|
| 217 |
-
label="Image Style",
|
| 218 |
-
)
|
| 219 |
-
|
| 220 |
|
| 221 |
gr.Examples(
|
| 222 |
examples=examples,
|
| 223 |
inputs=prompt,
|
| 224 |
outputs=[result, seed],
|
| 225 |
fn=generate,
|
| 226 |
-
cache_examples=False
|
| 227 |
)
|
| 228 |
|
| 229 |
use_negative_prompt.change(
|
| 230 |
-
|
| 231 |
inputs=use_negative_prompt,
|
| 232 |
outputs=negative_prompt,
|
| 233 |
-
api_name=False,
|
| 234 |
)
|
| 235 |
-
|
| 236 |
|
| 237 |
-
|
| 238 |
-
gr.on(
|
| 239 |
-
triggers=[
|
| 240 |
-
prompt.submit,
|
| 241 |
-
negative_prompt.submit,
|
| 242 |
-
run_button.click,
|
| 243 |
-
],
|
| 244 |
fn=generate,
|
| 245 |
-
inputs=[
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
style_selection,
|
| 249 |
-
use_negative_prompt,
|
| 250 |
-
num_inference_steps,
|
| 251 |
-
num_images_per_prompt,
|
| 252 |
-
seed,
|
| 253 |
-
width,
|
| 254 |
-
height,
|
| 255 |
-
guidance_scale,
|
| 256 |
-
randomize_seed,
|
| 257 |
-
],
|
| 258 |
outputs=[result, seed],
|
| 259 |
-
api_name="run",
|
| 260 |
)
|
| 261 |
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
if __name__ == "__main__":
|
| 265 |
-
demo.queue(max_size=20).launch(show_api=False, debug=False)
|
|
|
|
| 10 |
import torch
|
| 11 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 12 |
|
|
|
|
| 13 |
DESCRIPTION = """# InterDiffusion-4.0
|
| 14 |
### [https://huggingface.co/cutycat2000x/InterDiffusion-4.0](https://huggingface.co/cutycat2000x/InterDiffusion-4.0)"""
|
| 15 |
|
| 16 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 17 |
+
DEFAULT_STYLE_NAME = "(LoRA)"
|
| 18 |
|
| 19 |
def save_image(img):
|
| 20 |
+
filename = str(uuid.uuid4()) + ".png"
|
| 21 |
+
img.save(filename)
|
| 22 |
+
return filename
|
| 23 |
|
| 24 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 25 |
+
return random.randint(0, MAX_SEED) if randomize_seed else seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
style_list = [
|
| 28 |
+
{
|
| 29 |
+
"name": DEFAULT_STYLE_NAME,
|
| 30 |
+
"prompt": "{prompt}",
|
| 31 |
+
"negative_prompt": "",
|
| 32 |
+
},
|
| 33 |
+
]
|
| 34 |
+
styles = {s["name"]: (s["prompt"], s["negative_prompt"]) for s in style_list}
|
| 35 |
+
STYLE_NAMES = list(styles.keys())
|
| 36 |
|
| 37 |
+
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
| 38 |
+
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 39 |
+
return p.replace("{prompt}", positive), n + negative
|
| 40 |
|
| 41 |
if torch.cuda.is_available():
|
| 42 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
|
|
| 44 |
torch_dtype=torch.float16,
|
| 45 |
use_safetensors=True,
|
| 46 |
)
|
| 47 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 48 |
pipe.load_lora_weights("cutycat2000x/LoRA2", weight_name="lora.safetensors", adapter_name="adapt")
|
| 49 |
pipe.set_adapters("adapt")
|
| 50 |
pipe.to("cuda")
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
@spaces.GPU(enable_queue=True)
|
| 53 |
+
def generate(prompt, negative_prompt, style, use_negative_prompt, num_inference_steps,
|
| 54 |
+
num_images_per_prompt, seed, width, height, guidance_scale, randomize_seed, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
seed = randomize_seed_fn(seed, randomize_seed)
|
| 57 |
if not use_negative_prompt:
|
| 58 |
+
negative_prompt = ""
|
| 59 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
| 60 |
|
| 61 |
images = pipe(
|
|
|
|
| 67 |
num_inference_steps=num_inference_steps,
|
| 68 |
num_images_per_prompt=num_images_per_prompt,
|
| 69 |
cross_attention_kwargs={"scale": 0.65},
|
| 70 |
+
output_type="pil"
|
| 71 |
).images
|
| 72 |
+
|
| 73 |
image_paths = [save_image(img) for img in images]
|
|
|
|
| 74 |
return image_paths, seed
|
| 75 |
|
| 76 |
examples = [
|
| 77 |
'a smiling girl with sparkles in her eyes, walking in a garden, in the morning --style anime',
|
| 78 |
+
'firewatch landscape, Graphic Novel, Pastel Art...',
|
| 79 |
'Cat on a tree sitting in between parrots.',
|
| 80 |
+
'cat, 4k, hyperrealistic, Cinematic, unreal engine 5',
|
| 81 |
+
'cinematic closeup of burning skull',
|
| 82 |
'frozen elsa',
|
| 83 |
'A rainbow tree, anime style, tree in focus',
|
| 84 |
+
'A cat holding a sign that reads "Hello World"',
|
| 85 |
+
'Odette the butterfly goddess wondering in the cosmos'
|
| 86 |
]
|
| 87 |
|
| 88 |
css = '''
|
| 89 |
.gradio-container{max-width: 560px !important}
|
| 90 |
h1{text-align:center}
|
| 91 |
+
footer { visibility: hidden }
|
|
|
|
|
|
|
| 92 |
'''
|
| 93 |
|
| 94 |
with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
|
| 95 |
gr.Markdown(DESCRIPTION)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
with gr.Group():
|
| 98 |
with gr.Row():
|
| 99 |
+
prompt = gr.Textbox(
|
| 100 |
+
label="Prompt", placeholder="Enter your prompt", lines=1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
run_button = gr.Button("Run")
|
| 103 |
+
|
| 104 |
result = gr.Gallery(label="Result", columns=1, preview=True)
|
| 105 |
+
|
| 106 |
with gr.Accordion("Advanced options", open=False):
|
| 107 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
|
| 108 |
+
negative_prompt = gr.Textbox(label="Negative prompt", lines=1, visible=True)
|
| 109 |
+
num_inference_steps = gr.Slider(label="Steps", minimum=10, maximum=60, step=1, value=30)
|
| 110 |
+
num_images_per_prompt = gr.Slider(label="Images", minimum=1, maximum=5, step=1, value=2)
|
| 111 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 113 |
+
width = gr.Slider(label="Width", minimum=512, maximum=2048, step=8, value=1024)
|
| 114 |
+
height = gr.Slider(label="Height", minimum=512, maximum=2048, step=8, value=1024)
|
| 115 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=20.0, step=0.1, value=6.0)
|
| 116 |
+
style_selection = gr.Radio(label="Image Style", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
gr.Examples(
|
| 119 |
examples=examples,
|
| 120 |
inputs=prompt,
|
| 121 |
outputs=[result, seed],
|
| 122 |
fn=generate,
|
| 123 |
+
cache_examples=False
|
| 124 |
)
|
| 125 |
|
| 126 |
use_negative_prompt.change(
|
| 127 |
+
lambda x: gr.update(visible=x),
|
| 128 |
inputs=use_negative_prompt,
|
| 129 |
outputs=negative_prompt,
|
|
|
|
| 130 |
)
|
|
|
|
| 131 |
|
| 132 |
+
prompt.submit(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
fn=generate,
|
| 134 |
+
inputs=[prompt, negative_prompt, style_selection, use_negative_prompt,
|
| 135 |
+
num_inference_steps, num_images_per_prompt, seed,
|
| 136 |
+
width, height, guidance_scale, randomize_seed],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
outputs=[result, seed],
|
|
|
|
| 138 |
)
|
| 139 |
|
| 140 |
+
run_button.click(
|
| 141 |
+
fn=generate,
|
| 142 |
+
inputs=[prompt, negative_prompt, style_selection, use_negative_prompt,
|
| 143 |
+
num_inference_steps, num_images_per_prompt, seed,
|
| 144 |
+
width, height, guidance_scale, randomize_seed],
|
| 145 |
+
outputs=[result, seed],
|
| 146 |
+
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
+
demo.queue(max_size=20).launch(show_api=False, debug=False)
|