Spaces:
Running
on
Zero
Running
on
Zero
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, EulerAncestralDiscreteScheduler | |
from transformers import CLIPFeatureExtractor | |
import gradio as gr | |
import torch | |
from PIL import Image | |
import random | |
import os | |
from huggingface_hub import hf_hub_download | |
model_id = 'aipicasso/picasso-diffusion-1-0-demo' | |
auth_token=os.environ.get("ACCESS_TOKEN") | |
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler", use_auth_token=auth_token) | |
feature_extractor = CLIPFeatureExtractor.from_pretrained(model_id, use_auth_token=auth_token) | |
pipe_merged = StableDiffusionPipeline.from_pretrained( | |
model_id, | |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, | |
scheduler=scheduler, use_auth_token=auth_token) | |
pipe_i2i_merged = StableDiffusionImg2ImgPipeline.from_pretrained( | |
model_id, | |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, | |
scheduler=scheduler, | |
requires_safety_checker=False, | |
safety_checker=None, | |
feature_extractor=feature_extractor, use_auth_token=auth_token | |
) | |
pipe=pipe_merged.to("cuda") | |
pipe_i2i=pipe_i2i_merged.to("cuda") | |
pipe.enable_xformers_memory_efficient_attention() | |
pipe_i2i.enable_xformers_memory_efficient_attention() | |
embeddings_path=hf_hub_download(repo_id=model_id, filename="nfixer.pt", use_auth_token=auth_token) | |
embeddings_dict=torch.load(embeddings_path) | |
print(embeddings_dict) | |
if "string_to_param" in embeddings_dict: | |
embeddings = next(iter(embeddings_dict['string_to_param'].values())) | |
nfixer = "" | |
for i, emb in enumerate(embeddings): | |
token = f"sksd{chr(i+98)} " | |
nfixer += token | |
pipe.tokenizer.add_tokens(token) | |
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer)) | |
token_id = pipe.tokenizer.convert_tokens_to_ids(token) | |
pipe.text_encoder.get_input_embeddings().weight.data[token_id] = emb | |
else: | |
nfixer = list(embeddings_dict.keys())[0] | |
embeddings = embeddings_dict[nfixer].to(pipe.text_encoder.get_input_embeddings().weight.dtype) | |
pipe.tokenizer.add_tokens(placeholder_token) | |
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer)) | |
placeholder_token_id = pipe.tokenizer.convert_tokens_to_ids(placeholder_token) | |
pipe.text_encoder.get_input_embeddings().weight.data[placeholder_token_id] = embeddings | |
embeddings_path=hf_hub_download(repo_id=model_id, filename="embellish2.pt", use_auth_token=auth_token) | |
embeddings_dict=torch.load(embeddings_path) | |
print(embeddings_dict) | |
if "string_to_param" in embeddings_dict: | |
embeddings = next(iter(embeddings_dict['string_to_param'].values())) | |
embellish2 = "" | |
for i, emb in enumerate(embeddings): | |
token = f"kskd{chr(i+98)} " | |
embellish2 += token | |
pipe.tokenizer.add_tokens(token) | |
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer)) | |
token_id = pipe.tokenizer.convert_tokens_to_ids(token) | |
pipe.text_encoder.get_input_embeddings().weight.data[token_id] = emb | |
else: | |
embellish2 = list(embeddings_dict.keys())[0] | |
embeddings = embeddings_dict[embellish2].to(pipe.text_encoder.get_input_embeddings().weight.dtype) | |
pipe.tokenizer.add_tokens(placeholder_token) | |
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer)) | |
placeholder_token_id = pipe.tokenizer.convert_tokens_to_ids(placeholder_token) | |
pipe.text_encoder.get_input_embeddings().weight.data[placeholder_token_id] = embeddings | |
def error_str(error, title="Error"): | |
return f"""#### {title} | |
{error}""" if error else "" | |
def inference(prompt, guidance, steps, image_size="Square", seed=0, img=None, strength=0.5, neg_prompt="", disable_auto_prompt_correction=False, original_model=False): | |
global pipe,pipe_i2i | |
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=1024 | |
width=768 | |
elif(image_size=="Landscape"): | |
height=768 | |
width=1024 | |
elif(image_size=="Highreso."): | |
height=1024 | |
width=1024 | |
else: | |
height=768 | |
width=768 | |
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): | |
prompt=f"anime, {prompt}" | |
return prompt, neg_prompt | |
if(prompt=="" and neg_prompt==""): | |
prompt=f"{embellish2},anime, masterpiece, portrait, a girl with flowers, good pupil, 4k, detailed" | |
neg_prompt=f"{nfixer},(((deformed))), blurry, ((((bad anatomy)))), bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, (mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 3d, cg, text" | |
return prompt, neg_prompt | |
splited_prompt=prompt.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"anime, masterpiece, {prompt}, good pupil, 4k, detailed" | |
neg_prompt=f"{nfixer},(((deformed))), blurry, ((((bad anatomy)))), {neg_prompt}, bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, (mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 3d, cg, text" | |
return prompt, neg_prompt | |
animal_words=["cat","dog","bird"] | |
for word in animal_words: | |
if( word in splited_prompt): | |
prompt=f"anime, a {prompt}, 4k, detailed" | |
neg_prompt=f"{nfixer}, girl, (((deformed))), blurry, ((((bad anatomy)))), {neg_prompt}, bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, (mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 3d, cg, text" | |
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"anime, shinkai makoto, {word}, 4k, 8k, 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): | |
result = pipe( | |
prompt, | |
negative_prompt = neg_prompt, | |
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, | |
init_image = img, | |
num_inference_steps = int(steps), | |
strength = strength, | |
guidance_scale = guidance, | |
#width = width, | |
#height = height, | |
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>Picasso Diffusion 1.0 +α Demo</h1> | |
</div> | |
<p> | |
Demo for <a href="https://huggingface.co/aipicasso/picasso-diffusion-1-0">Picasso Diffusion 1.0 (Comming soon)</a> + <a href="https://twitter.com/cac0e/status/1622381533892952069?s=20&t=0zTDN_4D14LP7w2eVvLzOg">cacoe's model</a>.<br> | |
</p> | |
<p> | |
サンプル: そのままGenerateボタンを押してください。<br> | |
sample : Click "Generate" button without any prompts. | |
</p> | |
<p> | |
sample prompt1 : girl, kimono | |
</p> | |
<p> | |
sample prompt2 : boy, armor | |
</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/picasso-diffusion-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]").style(container=False) | |
generate = gr.Button(value="Generate").style(rounded=(False, True, True, False)) | |
image_out = gr.Image(height=768,width=768) | |
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.") | |
#original_model = gr.Checkbox(label="Change the model into the original model.") | |
with gr.Row(): | |
image_size=gr.Radio(["Portrait","Landscape","Square","Highreso."]) | |
image_size.show_label=False | |
image_size.value="Square" | |
with gr.Row(): | |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15) | |
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]#, original_model] | |
outputs = [image_out, error_output] | |
prompt.submit(inference, inputs=inputs, outputs=outputs) | |
generate.click(inference, inputs=inputs, outputs=outputs,api_name="generate") | |
demo.queue(concurrency_count=1) | |
demo.launch() | |