Update app.py
Browse files
app.py
CHANGED
|
@@ -2,44 +2,63 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
from diffusers import DiffusionPipeline
|
|
|
|
| 5 |
import torch
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
if torch.cuda.is_available():
|
| 10 |
-
torch.cuda.max_memory_allocated(device=device)
|
| 11 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
| 12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
| 13 |
-
pipe = pipe.to(device)
|
| 14 |
-
else:
|
| 15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
| 16 |
-
pipe = pipe.to(device)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
MAX_IMAGE_SIZE = 1024
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
if randomize_seed:
|
| 24 |
-
seed = random.randint(0, MAX_SEED)
|
| 25 |
-
|
| 26 |
-
generator = torch.Generator().manual_seed(seed)
|
| 27 |
-
|
| 28 |
image = pipe(
|
| 29 |
-
prompt = prompt,
|
| 30 |
-
negative_prompt =
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
).images[0]
|
| 37 |
|
| 38 |
return image
|
| 39 |
|
|
|
|
| 40 |
examples = [
|
| 41 |
-
"
|
| 42 |
-
"
|
| 43 |
"A delicious ceviche cheesecake slice",
|
| 44 |
]
|
| 45 |
|
|
@@ -50,88 +69,29 @@ css="""
|
|
| 50 |
}
|
| 51 |
"""
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
else:
|
| 56 |
-
power_device = "CPU"
|
| 57 |
|
| 58 |
with gr.Blocks(css=css) as demo:
|
| 59 |
|
| 60 |
with gr.Column(elem_id="col-container"):
|
| 61 |
gr.Markdown(f"""
|
| 62 |
-
#
|
| 63 |
Currently running on {power_device}.
|
| 64 |
""")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
-
|
| 68 |
prompt = gr.Text(
|
| 69 |
label="Prompt",
|
| 70 |
show_label=False,
|
| 71 |
max_lines=1,
|
| 72 |
placeholder="Enter your prompt",
|
| 73 |
container=False,
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
run_button = gr.Button("Run", scale=0)
|
| 77 |
|
| 78 |
result = gr.Image(label="Result", show_label=False)
|
| 79 |
|
| 80 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 81 |
-
|
| 82 |
-
negative_prompt = gr.Text(
|
| 83 |
-
label="Negative prompt",
|
| 84 |
-
max_lines=1,
|
| 85 |
-
placeholder="Enter a negative prompt",
|
| 86 |
-
visible=False,
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
seed = gr.Slider(
|
| 90 |
-
label="Seed",
|
| 91 |
-
minimum=0,
|
| 92 |
-
maximum=MAX_SEED,
|
| 93 |
-
step=1,
|
| 94 |
-
value=0,
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 98 |
-
|
| 99 |
-
with gr.Row():
|
| 100 |
-
|
| 101 |
-
width = gr.Slider(
|
| 102 |
-
label="Width",
|
| 103 |
-
minimum=256,
|
| 104 |
-
maximum=MAX_IMAGE_SIZE,
|
| 105 |
-
step=32,
|
| 106 |
-
value=512,
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
height = gr.Slider(
|
| 110 |
-
label="Height",
|
| 111 |
-
minimum=256,
|
| 112 |
-
maximum=MAX_IMAGE_SIZE,
|
| 113 |
-
step=32,
|
| 114 |
-
value=512,
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
with gr.Row():
|
| 118 |
-
|
| 119 |
-
guidance_scale = gr.Slider(
|
| 120 |
-
label="Guidance scale",
|
| 121 |
-
minimum=0.0,
|
| 122 |
-
maximum=10.0,
|
| 123 |
-
step=0.1,
|
| 124 |
-
value=0.0,
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
num_inference_steps = gr.Slider(
|
| 128 |
-
label="Number of inference steps",
|
| 129 |
-
minimum=1,
|
| 130 |
-
maximum=12,
|
| 131 |
-
step=1,
|
| 132 |
-
value=2,
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
gr.Examples(
|
| 136 |
examples = examples,
|
| 137 |
inputs = [prompt]
|
|
@@ -139,7 +99,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 139 |
|
| 140 |
run_button.click(
|
| 141 |
fn = infer,
|
| 142 |
-
inputs = [prompt
|
| 143 |
outputs = [result]
|
| 144 |
)
|
| 145 |
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
+
from optimum.intel.openvino.modeling_diffusion import OVModelVaeDecoder, OVBaseModel, OVStableDiffusionPipeline
|
| 6 |
import torch
|
| 7 |
+
from huggingface_hub import snapshot_download
|
| 8 |
+
import openvino.runtime as ov
|
| 9 |
+
from typing import Optional, Dict
|
| 10 |
|
| 11 |
+
model_id = "Disty0/LCM_SoteMix"
|
| 12 |
+
batch_size = -1
|
| 13 |
+
class CustomOVModelVaeDecoder(OVModelVaeDecoder):
|
| 14 |
+
def __init__(
|
| 15 |
+
self, model: ov.Model, parent_model: OVBaseModel, ov_config: Optional[Dict[str, str]] = None, model_dir: str = None,
|
| 16 |
+
):
|
| 17 |
+
super(OVModelVaeDecoder, self).__init__(model, parent_model, ov_config, "vae_decoder", model_dir)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
pipe = OVStableDiffusionPipeline.from_pretrained(model_id, compile = False, ov_config = {"CACHE_DIR":""})
|
|
|
|
| 21 |
|
| 22 |
+
taesd_dir = snapshot_download(repo_id="deinferno/taesd-openvino")
|
| 23 |
+
pipe.vae_decoder = CustomOVModelVaeDecoder(model = OVBaseModel.load_model(f"{taesd_dir}/vae_decoder/openvino_model.xml"), parent_model = pipe, model_dir = taesd_dir)
|
| 24 |
+
|
| 25 |
+
pipe.reshape( batch_size=-1, height=512, width=512, num_images_per_prompt=1)
|
| 26 |
+
pipe.compile()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def infer(prompt,negative_prompt):
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
image = pipe(
|
| 32 |
+
prompt = prompt+"score_8_up,score_7_up,score_6_up,score_9,score_8_up,score_7,masterpiece,best quality,source_anime,bangs,",
|
| 33 |
+
negative_prompt = "score_6,score_5,score_4,source_furry,pathway,walkway,face mask,heterochromia,\
|
| 34 |
+
tattoos,muscular,deformed iris,deformed pupils,long body,long neck,text,error,print,signature,\
|
| 35 |
+
logo,watermark,deformed,distorted,disfigured,bad anatomy,wrong anatomy,ugly,disgusting,\
|
| 36 |
+
cropped,crooked teeth,multiple views,bad proportions,gross proportions,cloned face,\
|
| 37 |
+
worst quality,low quality,normal quality,bad quality,lowres,poorly drawn,semi-realistic,\
|
| 38 |
+
3d,render,cg,cgi,imperfect,partial,unfinished,incomplete,monochrome,grayscale,sepia,fat,\
|
| 39 |
+
wrinkle,fat leg,fat ass,blurry,hazy,sagging breasts,longbody,lowres,\
|
| 40 |
+
bad anatomy,bad hands,missing fingers,extra digit,fewer digits,worst quality,\
|
| 41 |
+
low quality,normal quality,watermark,artist name,signature,(bad anatomy)), ((bad art)),\
|
| 42 |
+
(((bad proportions))), (b&w), (black/white), (black and white), blurry, body out of frame,\
|
| 43 |
+
canvas frame, cloned face, ((close up)), cross-eye, ((deformed)), ((disfigured)), (((duplicate))), \
|
| 44 |
+
(((extra arms))), extra fingers, (((extra legs))), ((extra limbs)), (fused fingers), gross proportions, \
|
| 45 |
+
((morbid)), (malformed limbs), ((missing arms)), ((missing legs)), mutated, mutated hands, \
|
| 46 |
+
(((mutation))), ((mutilated)), (out of frame), ((poorly drawn face)), poorly drawn feet, \
|
| 47 |
+
((poorly drawn hands)), tiling, (too many fingers), ((ugly)), wierd colors, (((long neck))), \
|
| 48 |
+
ugly, words, wrinkles, writing",
|
| 49 |
+
width = 512,
|
| 50 |
+
height = 512,
|
| 51 |
+
guidance_scale=1.0,
|
| 52 |
+
num_inference_steps=8,
|
| 53 |
+
num_images_per_prompt=1,
|
| 54 |
).images[0]
|
| 55 |
|
| 56 |
return image
|
| 57 |
|
| 58 |
+
|
| 59 |
examples = [
|
| 60 |
+
"A cute kitten, Japanese cartoon style.",
|
| 61 |
+
"A sweet family, dad stands next to mom, mom holds baby girl.",
|
| 62 |
"A delicious ceviche cheesecake slice",
|
| 63 |
]
|
| 64 |
|
|
|
|
| 69 |
}
|
| 70 |
"""
|
| 71 |
|
| 72 |
+
|
| 73 |
+
power_device = "CPU"
|
|
|
|
|
|
|
| 74 |
|
| 75 |
with gr.Blocks(css=css) as demo:
|
| 76 |
|
| 77 |
with gr.Column(elem_id="col-container"):
|
| 78 |
gr.Markdown(f"""
|
| 79 |
+
# Disty0/LCM_SoteMix 512x512
|
| 80 |
Currently running on {power_device}.
|
| 81 |
""")
|
| 82 |
|
| 83 |
with gr.Row():
|
|
|
|
| 84 |
prompt = gr.Text(
|
| 85 |
label="Prompt",
|
| 86 |
show_label=False,
|
| 87 |
max_lines=1,
|
| 88 |
placeholder="Enter your prompt",
|
| 89 |
container=False,
|
| 90 |
+
)
|
|
|
|
| 91 |
run_button = gr.Button("Run", scale=0)
|
| 92 |
|
| 93 |
result = gr.Image(label="Result", show_label=False)
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
gr.Examples(
|
| 96 |
examples = examples,
|
| 97 |
inputs = [prompt]
|
|
|
|
| 99 |
|
| 100 |
run_button.click(
|
| 101 |
fn = infer,
|
| 102 |
+
inputs = [prompt],
|
| 103 |
outputs = [result]
|
| 104 |
)
|
| 105 |
|