File size: 7,387 Bytes
742d895
b1f69aa
4508931
 
 
 
e4dd967
a00ca4e
502e234
 
 
 
72457cd
2137fdf
4508931
12e4277
502e234
12e4277
368f337
0a12c49
1d944c5
4508931
742d895
f0aff57
 
302e4bb
1d944c5
98775f1
12e4277
 
 
e0deb99
502e234
84d7485
 
 
502e234
 
 
 
 
a00ca4e
4dc2497
 
4508931
 
 
 
c564be9
502e234
4508931
 
502e234
 
12e4277
 
4508931
 
a359dcd
 
 
502e234
 
4508931
 
 
 
 
762f660
4508931
 
 
 
751ed09
b9f5d1b
4508931
bd1a442
502e234
bd1a442
727aea3
4508931
 
be239af
b9f5d1b
7e60999
 
562912b
4508931
 
be239af
b9f5d1b
7e60999
 
4508931
 
 
be239af
502e234
7e60999
 
4508931
1cae561
c564be9
4508931
6102167
 
662c196
502e234
6102167
 
 
 
4508931
 
502e234
 
3ac3698
 
502e234
1051b3b
4508931
 
 
 
 
 
 
 
cce656e
0245b21
4508931
da26a59
4508931
 
24b4440
4508931
 
 
751ed09
502e234
 
a83ae72
4508931
 
502e234
7bf3d66
4508931
 
 
 
 
 
 
 
502e234
 
4508931
7f9e2a9
4508931
 
 
 
 
 
 
 
63c4892
c616813
502e234
4508931
 
502e234
4508931
 
 
502e234
 
eef0e16
502e234
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
from diffusers import AutoPipelineForText2Image, EulerAncestralDiscreteScheduler
from diffusers import UniPCMultistepScheduler
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 safetensors import safe_open
from compel import Compel, ReturnedEmbeddingsType
from safetensors.torch import load_file
import spaces

model_id = "aipicasso/emix-0-5"
auth_token=os.environ["ACCESS_TOKEN"]
repo = "ByteDance/SDXL-Lightning"
ckpt = "sdxl_lightning_8step_lora.safetensors"

scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler",token=auth_token)

pipe = AutoPipelineForText2Image.from_pretrained(
  model_id,
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
  scheduler=scheduler,
  token=auth_token)

pipe.load_lora_weights(hf_hub_download(repo, ckpt))
pipe.fuse_lora()

pipe.enable_freeu(s1=0.9, s2=0.2, b1=1.3, b2=1.4)

state_dict = load_file("unaestheticXLv31.safetensors")
pipe.load_textual_inversion(state_dict["clip_g"], token="unaestheticXLv31", text_encoder=pipe.text_encoder_2, tokenizer=pipe.tokenizer_2)
pipe.load_textual_inversion(state_dict["clip_l"], token="unaestheticXLv31", text_encoder=pipe.text_encoder, tokenizer=pipe.tokenizer)

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])

pipe=pipe.to("cuda")

def error_str(error, title="Error"):
    return f"""#### {title}
            {error}"""  if error else ""

@spaces.GPU
def inference(prompt, guidance, steps, seed=0, neg_prompt="", disable_auto_prompt_correction=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)

  height=1024
  width=1024

  print(prompt,neg_prompt)

  result=txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator)
  return result, None

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"
        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"{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"{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"{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_conditioning, neg_pooled = compel(neg_prompt)

    result = pipe(
        prompt_embeds=conditioning,
        pooled_prompt_embeds=pooled, 
        negative_prompt_embeds=neg_conditioning, 
        negative_pooled_prompt_embeds=neg_pooled,
        num_inference_steps = int(steps),
        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>Emix 0.5 Lightning Demo</h1>
              </div>
              <p>
               You can use new model: <a href="https://huggingface.co/spaces/aipicasso/emi-2-demo">Emi 2</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 : 1boy, focus, wavy short hair, blue eyes, black shirt, white background, simple background
              </p>
              <p>
              <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.
              </p>
            </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()
          error_output = gr.Markdown()

        with gr.Column(scale=45):
            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():
                guidance = gr.Slider(label="Guidance scale", value=1.5, maximum=10, step=0.1)
                steps = gr.Slider(label="Steps", value=8, minimum=1, maximum=20, step=1)
    
              seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)

    inputs = [prompt, guidance, steps, seed, 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()
demo.launch()