Spaces:
Runtime error
Runtime error
File size: 11,918 Bytes
9075da4 3e6f170 9075da4 7f64564 d8b8c6e 9e19de5 3e6f170 d39e8b2 4be1339 3e6f170 e43178a 12b30cf e43178a cd9fed4 3e6f170 e775d9e 3e6f170 bd32138 3e6f170 e775d9e fb9705e 3e6f170 0213ff4 d10e112 791174e 5810836 82cc289 0213ff4 27f6b96 82cc289 d10e112 485fdd6 5810836 485fdd6 0213ff4 9e42346 485fdd6 d10e112 485fdd6 5810836 485fdd6 0213ff4 9e42346 0213ff4 485fdd6 b9f307f b92d8c2 b9f307f 3e6f170 87379a7 3e6f170 4ee25e6 3e6f170 416fcc5 3e6f170 416fcc5 3e6f170 416fcc5 3e6f170 0f06269 3e6f170 c548949 94b8a22 3e6f170 c2acc27 3e6f170 38003a2 94b8a22 3e6f170 32d0db2 94b8a22 3e6f170 38003a2 abe034a 3e6f170 94714d0 b9f307f 3e6f170 b9f307f 3e6f170 d39e8b2 3e6f170 d39e8b2 3e6f170 34306fc 3e6f170 94b8a22 3e6f170 676f6dd 5c4f47a 676f6dd 105a53e 3e6f170 d39e8b2 3e6f170 0f06269 3e6f170 e775d9e 5967c31 3e6f170 87379a7 3e6f170 0f06269 3e6f170 9be22c6 3e6f170 e3bd452 e43178a 3e6f170 0f06269 3e6f170 0f06269 3e6f170 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, EulerAncestralDiscreteScheduler
import gradio as gr
import torch
from PIL import Image
import random
import os
from huggingface_hub import hf_hub_download
import torch
from torch import autocast
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
from safetensors import safe_open
from compel import Compel, ReturnedEmbeddingsType
from huggingface_hub import hf_hub_download
model_id = 'aipicasso/emi'
auth_token=os.environ["ACCESS_TOKEN"]
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler", use_auth_token=auth_token)
pipe = StableDiffusionXLPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
scheduler=scheduler, use_auth_token=auth_token)
#pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
#ckpt_file=hf_hub_download(repo_id=model_id, filename="v2.safetensors", token=auth_token)
#pipe = StableDiffusionXLPipeline.from_single_file(
# ckpt_file,
# torch_dtype=torch.float16,
# scheduler=scheduler
#)
#pipe.load_lora_weights("manual.safetensors")
pipe_i2i = StableDiffusionXLImg2ImgPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
scheduler=scheduler,
use_auth_token=auth_token
)
pipe=pipe.to("cuda")
pipe_i2i=pipe_i2i.to("cuda")
token_num=65
unaestheticXLv31=""
embeddings_dict = {}
with safe_open("unaestheticXLv31.safetensors", framework="pt") as f:
for k in f.keys():
embeddings_dict[k] = f.get_tensor(k)
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer),pad_to_multiple_of=128)
pipe.text_encoder_2.resize_token_embeddings(len(pipe.tokenizer),pad_to_multiple_of=128)
for i in range(len(embeddings_dict["clip_l"])):
token = f"sksd{chr(token_num)}"
token_num+=1
unaestheticXLv31 += token
pipe.tokenizer.add_tokens(token)
token_id = pipe.tokenizer.convert_tokens_to_ids(token)
pipe.text_encoder.get_input_embeddings().weight.data[token_id] = embeddings_dict["clip_l"][i]
pipe.text_encoder_2.get_input_embeddings().weight.data[token_id] = embeddings_dict["clip_g"][i]
unaestheticXLv1=""
embeddings_dict = {}
with safe_open("unaestheticXLv1.safetensors", framework="pt") as f:
for k in f.keys():
embeddings_dict[k] = f.get_tensor(k)
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer),pad_to_multiple_of=128)
pipe.text_encoder_2.resize_token_embeddings(len(pipe.tokenizer),pad_to_multiple_of=128)
for i in range(len(embeddings_dict["clip_l"])):
token = f"sksd{chr(token_num)}"
token_num+=1
unaestheticXLv1 += token
pipe.tokenizer.add_tokens(token)
token_id = pipe.tokenizer.convert_tokens_to_ids(token)
pipe.text_encoder.get_input_embeddings().weight.data[token_id] = embeddings_dict["clip_l"][i]
pipe.text_encoder_2.get_input_embeddings().weight.data[token_id] = embeddings_dict["clip_g"][i]
unaestheticXLv13=""
embeddings_dict = {}
with safe_open("unaestheticXLv13.safetensors", framework="pt") as f:
for k in f.keys():
embeddings_dict[k] = f.get_tensor(k)
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer),pad_to_multiple_of=128)
pipe.text_encoder_2.resize_token_embeddings(len(pipe.tokenizer),pad_to_multiple_of=128)
for i in range(len(embeddings_dict["clip_l"])):
token = f"sksd{chr(token_num)}"
unaestheticXLv13 += token
token_num+=1
pipe.tokenizer.add_tokens(token)
token_id = pipe.tokenizer.convert_tokens_to_ids(token)
pipe.text_encoder.get_input_embeddings().weight.data[token_id] = embeddings_dict["clip_l"][i]
pipe.text_encoder_2.get_input_embeddings().weight.data[token_id] = embeddings_dict["clip_g"][i]
compel = Compel(tokenizer=[pipe.tokenizer, pipe.tokenizer_2] ,
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
requires_pooled=[False, True])
def error_str(error, title="Error"):
return f"""#### {title}
{error}""" if error else ""
def inference(prompt, guidance, steps, image_size="Landscape", seed=0, img=None, strength=0.5, neg_prompt="", disable_auto_prompt_correction=False, image_style="Animetic", original_model=False):
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
prompt,neg_prompt=auto_prompt_correction(prompt,neg_prompt,disable_auto_prompt_correction)
if(image_size=="Portrait"):
height=1344
width=768
elif(image_size=="Landscape"):
height=768
width=1344
else:
height=1024
width=1024
print(prompt,neg_prompt)
try:
if img is not None:
return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
else:
return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
except Exception as e:
return None, error_str(e)
def auto_prompt_correction(prompt_ui,neg_prompt_ui,disable_auto_prompt_correction):
# auto prompt correction
prompt=str(prompt_ui)
neg_prompt=str(neg_prompt_ui)
prompt=prompt.lower()
neg_prompt=neg_prompt.lower()
if(disable_auto_prompt_correction):
return prompt, neg_prompt
if(prompt=="" and neg_prompt==""):
prompt="1girl++, smile--, brown bob+++ hair, brown eyes, sunflowers, sky, transparent++"
neg_prompt=f"({unaestheticXLv31})---, photo, deformed, realism, disfigured, low contrast, bad hand"
return prompt, neg_prompt
splited_prompt=prompt.replace(","," ").replace("_"," ").replace("+"," ").split(" ")
human_words=["1girl","girl","maid","maids","female","1woman","woman","girls","2girls","3girls","4girls","5girls","a couple of girls","women","1boy","boy","boys","a couple of boys","2boys","male","1man","1handsome","1bishounen","man","men","guy","guys"]
for word in human_words:
if( word in splited_prompt):
prompt=f"ime artwork, anime style, {prompt}"
neg_prompt=f"({unaestheticXLv31})---,{neg_prompt}, photo, deformed, realism, disfigured, low contrast, bad hand"
return prompt, neg_prompt
animal_words=["cat","dog","bird","pigeon","rabbit","bunny","horse"]
for word in animal_words:
if( word in splited_prompt):
prompt=f"anime style, a {prompt}, 4k, detailed"
neg_prompt=f"{neg_prompt},({unaestheticXLv31})---"
return prompt, neg_prompt
background_words=["mount fuji","mt. fuji","building", "buildings", "tokyo", "kyoto", "nara", "shibuya", "shinjuku"]
for word in background_words:
if( word in splited_prompt):
prompt=f"ime artwork, anime style, {prompt}, highly detailed"
neg_prompt=f"girl, deformed+++, {neg_prompt}, girl, boy, photo, people, low quality, ui, error, lowres, jpeg artifacts, 2d, 3d, cg, text"
return prompt, neg_prompt
return prompt,neg_prompt
def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
conditioning, pooled = compel([prompt, neg_prompt])
result = pipe(
prompt_embeds=conditioning[0:1],
pooled_prompt_embeds=pooled[0:1],
negative_prompt_embeds=conditioning[1:2],
negative_pooled_prompt_embeds=pooled[1:2],
num_inference_steps = int(steps),
guidance_scale = guidance,
width = width,
height = height,
generator = generator)
return result.images[0]
def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
ratio = min(height / img.height, width / img.width)
img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
result = pipe_i2i(
prompt,
negative_prompt = neg_prompt,
image = img,
num_inference_steps = int(steps),
strength = strength,
guidance_scale = guidance,
generator = generator)
return result.images[0]
css = """.main-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.main-div div h1{font-weight:900;margin-bottom:7px}.main-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
"""
with gr.Blocks(css=css) as demo:
gr.HTML(
f"""
<div class="main-div">
<div>
<h1>Emi Demo</h1>
</div>
<p>
Demo for <a href="https://huggingface.co/aipicasso/emi">Emi</a><br>
</p>
<p>
サンプル: そのままGenerateボタンを押してください。<br>
sample : Click "Generate" button without any prompts.
</p>
<p>
sample prompt1 : 1girl++, cool+, smile--, colorful long hair, colorful eyes, stars, night, pastel color, transparent+
</p>
<p>
sample prompt2 : 1man+, focus, wavy short hair, blue eyes, black shirt, white background, simple background
</p>
<p>
sample prompt3 : anime style, 1girl++
</p>
<p>
共有ボタンを押してみんなに画像を共有しましょう。Please push share button to share your image.
</p>
<p>
Running on {"<b>GPU 🔥</b>" if torch.cuda.is_available() else f"<b>CPU 🥶</b>. For faster inference it is recommended to <b>upgrade to GPU in <a href='https://huggingface.co/spaces/akhaliq/cool-japan-diffusion-2-1-0/settings'>Settings</a></b>"} <br>
<a style="display:inline-block" href="https://huggingface.co/spaces/aipicasso/emi-latest-demo?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a> to say goodbye from waiting for the generating.
</div>
"""
)
with gr.Row():
with gr.Column(scale=55):
with gr.Group():
with gr.Row():
prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder="[your prompt]")
generate = gr.Button(value="Generate")
image_out = gr.Image(height=768,width=1344)
error_output = gr.Markdown()
with gr.Column(scale=45):
with gr.Tab("Options"):
with gr.Group():
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
disable_auto_prompt_correction = gr.Checkbox(label="Disable auto prompt corretion.")
with gr.Row():
image_size=gr.Radio(["Portrait","Landscape","Square"])
image_size.show_label=False
image_size.value="Square"
with gr.Row():
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=25)
steps = gr.Slider(label="Steps", value=20, minimum=2, maximum=75, step=1)
seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
with gr.Tab("Image to image"):
with gr.Group():
image = gr.Image(label="Image", height=256, tool="editor", type="pil")
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
inputs = [prompt, guidance, steps, image_size, seed, image, strength, neg_prompt, disable_auto_prompt_correction]
outputs = [image_out, error_output]
prompt.submit(inference, inputs=inputs, outputs=outputs)
generate.click(inference, inputs=inputs, outputs=outputs)
demo.queue(concurrency_count=1)
demo.launch() |