Upload app (3).py
Browse files- app (3).py +198 -0
app (3).py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
import torch
|
5 |
+
from PIL import Image
|
6 |
+
import os
|
7 |
+
|
8 |
+
from transformers import CLIPVisionModelWithProjection, CLIPImageProcessor
|
9 |
+
from kolors.pipelines.pipeline_stable_diffusion_xl_chatglm_256_ipadapter import StableDiffusionXLPipeline
|
10 |
+
from kolors.models.modeling_chatglm import ChatGLMModel
|
11 |
+
from kolors.models.tokenization_chatglm import ChatGLMTokenizer
|
12 |
+
from kolors.models.unet_2d_condition import UNet2DConditionModel
|
13 |
+
from diffusers import AutoencoderKL, EulerDiscreteScheduler
|
14 |
+
|
15 |
+
from huggingface_hub import snapshot_download
|
16 |
+
import spaces
|
17 |
+
|
18 |
+
device = "cuda"
|
19 |
+
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
20 |
+
ckpt_dir = f'{root_dir}/weights/Kolors'
|
21 |
+
|
22 |
+
snapshot_download(repo_id="Kwai-Kolors/Kolors", local_dir=ckpt_dir)
|
23 |
+
snapshot_download(repo_id="Kwai-Kolors/Kolors-IP-Adapter-Plus", local_dir=f"{root_dir}/weights/Kolors-IP-Adapter-Plus")
|
24 |
+
|
25 |
+
# Load models
|
26 |
+
text_encoder = ChatGLMModel.from_pretrained(f'{ckpt_dir}/text_encoder', torch_dtype=torch.float16).half().to(device)
|
27 |
+
tokenizer = ChatGLMTokenizer.from_pretrained(f'{ckpt_dir}/text_encoder')
|
28 |
+
vae = AutoencoderKL.from_pretrained(f"{ckpt_dir}/vae", revision=None).half().to(device)
|
29 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(f"{ckpt_dir}/scheduler")
|
30 |
+
unet = UNet2DConditionModel.from_pretrained(f"{ckpt_dir}/unet", revision=None).half().to(device)
|
31 |
+
|
32 |
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
33 |
+
f'{root_dir}/weights/Kolors-IP-Adapter-Plus/image_encoder',
|
34 |
+
ignore_mismatched_sizes=True
|
35 |
+
).to(dtype=torch.float16, device=device)
|
36 |
+
|
37 |
+
ip_img_size = 336
|
38 |
+
clip_image_processor = CLIPImageProcessor(size=ip_img_size, crop_size=ip_img_size)
|
39 |
+
|
40 |
+
pipe = StableDiffusionXLPipeline(
|
41 |
+
vae=vae,
|
42 |
+
text_encoder=text_encoder,
|
43 |
+
tokenizer=tokenizer,
|
44 |
+
unet=unet,
|
45 |
+
scheduler=scheduler,
|
46 |
+
image_encoder=image_encoder,
|
47 |
+
feature_extractor=clip_image_processor,
|
48 |
+
force_zeros_for_empty_prompt=False
|
49 |
+
).to(device)
|
50 |
+
|
51 |
+
#pipe = pipe.to(device)
|
52 |
+
#pipe.enable_model_cpu_offload()
|
53 |
+
|
54 |
+
if hasattr(pipe.unet, 'encoder_hid_proj'):
|
55 |
+
pipe.unet.text_encoder_hid_proj = pipe.unet.encoder_hid_proj
|
56 |
+
|
57 |
+
pipe.load_ip_adapter(f'{root_dir}/weights/Kolors-IP-Adapter-Plus', subfolder="", weight_name=["ip_adapter_plus_general.bin"])
|
58 |
+
|
59 |
+
MAX_SEED = np.iinfo(np.int32).max
|
60 |
+
MAX_IMAGE_SIZE = 1024
|
61 |
+
|
62 |
+
@spaces.GPU
|
63 |
+
def infer(prompt, ip_adapter_image, ip_adapter_scale=0.5, negative_prompt="", seed=100, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=50, progress=gr.Progress(track_tqdm=True)):
|
64 |
+
if randomize_seed:
|
65 |
+
seed = random.randint(0, MAX_SEED)
|
66 |
+
|
67 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
68 |
+
pipe.to("cuda")
|
69 |
+
image_encoder.to("cuda")
|
70 |
+
pipe.image_encoder = image_encoder
|
71 |
+
pipe.set_ip_adapter_scale([ip_adapter_scale])
|
72 |
+
|
73 |
+
image = pipe(
|
74 |
+
prompt=prompt,
|
75 |
+
ip_adapter_image=[ip_adapter_image],
|
76 |
+
negative_prompt=negative_prompt,
|
77 |
+
height=height,
|
78 |
+
width=width,
|
79 |
+
num_inference_steps=num_inference_steps,
|
80 |
+
guidance_scale=guidance_scale,
|
81 |
+
num_images_per_prompt=1,
|
82 |
+
generator=generator,
|
83 |
+
).images[0]
|
84 |
+
|
85 |
+
return image, seed
|
86 |
+
|
87 |
+
examples = [
|
88 |
+
["A dog", "minta.jpeg", 0.4],
|
89 |
+
["A capybara", "king-min.png", 0.5],
|
90 |
+
["A cat", "blue_hair.png", 0.5],
|
91 |
+
["", "meow.jpeg", 1.0],
|
92 |
+
]
|
93 |
+
|
94 |
+
css="""
|
95 |
+
#col-container {
|
96 |
+
margin: 0 auto;
|
97 |
+
max-width: 720px;
|
98 |
+
}
|
99 |
+
#result img{
|
100 |
+
object-position: top;
|
101 |
+
}
|
102 |
+
#result .image-container{
|
103 |
+
height: 100%
|
104 |
+
}
|
105 |
+
"""
|
106 |
+
|
107 |
+
with gr.Blocks(css=css) as demo:
|
108 |
+
with gr.Column(elem_id="col-container"):
|
109 |
+
gr.Markdown(f"""
|
110 |
+
# Kolors IP-Adapter - image reference and variations
|
111 |
+
""")
|
112 |
+
|
113 |
+
with gr.Row():
|
114 |
+
prompt = gr.Text(
|
115 |
+
label="Prompt",
|
116 |
+
show_label=False,
|
117 |
+
max_lines=1,
|
118 |
+
placeholder="Enter your prompt",
|
119 |
+
container=False,
|
120 |
+
)
|
121 |
+
run_button = gr.Button("Run", scale=0)
|
122 |
+
|
123 |
+
with gr.Row():
|
124 |
+
with gr.Column():
|
125 |
+
ip_adapter_image = gr.Image(label="IP-Adapter Image", type="pil")
|
126 |
+
ip_adapter_scale = gr.Slider(
|
127 |
+
label="Image influence scale",
|
128 |
+
info="Use 1 for creating variations",
|
129 |
+
minimum=0.0,
|
130 |
+
maximum=1.0,
|
131 |
+
step=0.05,
|
132 |
+
value=0.5,
|
133 |
+
)
|
134 |
+
result = gr.Image(label="Result", elem_id="result")
|
135 |
+
|
136 |
+
with gr.Accordion("Advanced Settings", open=False):
|
137 |
+
negative_prompt = gr.Text(
|
138 |
+
label="Negative prompt",
|
139 |
+
max_lines=1,
|
140 |
+
placeholder="Enter a negative prompt",
|
141 |
+
)
|
142 |
+
seed = gr.Slider(
|
143 |
+
label="Seed",
|
144 |
+
minimum=0,
|
145 |
+
maximum=MAX_SEED,
|
146 |
+
step=1,
|
147 |
+
value=0,
|
148 |
+
)
|
149 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
150 |
+
with gr.Row():
|
151 |
+
width = gr.Slider(
|
152 |
+
label="Width",
|
153 |
+
minimum=256,
|
154 |
+
maximum=MAX_IMAGE_SIZE,
|
155 |
+
step=32,
|
156 |
+
value=1024,
|
157 |
+
)
|
158 |
+
height = gr.Slider(
|
159 |
+
label="Height",
|
160 |
+
minimum=256,
|
161 |
+
maximum=MAX_IMAGE_SIZE,
|
162 |
+
step=32,
|
163 |
+
value=1024,
|
164 |
+
)
|
165 |
+
with gr.Row():
|
166 |
+
guidance_scale = gr.Slider(
|
167 |
+
label="Guidance scale",
|
168 |
+
minimum=0.0,
|
169 |
+
maximum=10.0,
|
170 |
+
step=0.1,
|
171 |
+
value=5.0,
|
172 |
+
)
|
173 |
+
num_inference_steps = gr.Slider(
|
174 |
+
label="Number of inference steps",
|
175 |
+
minimum=1,
|
176 |
+
maximum=100,
|
177 |
+
step=1,
|
178 |
+
value=100,
|
179 |
+
)
|
180 |
+
|
181 |
+
gr.Examples(
|
182 |
+
examples=examples,
|
183 |
+
fn=infer,
|
184 |
+
inputs=[prompt, ip_adapter_image, ip_adapter_scale],
|
185 |
+
outputs=[result, seed],
|
186 |
+
cache_examples="lazy"
|
187 |
+
)
|
188 |
+
|
189 |
+
gr.on(
|
190 |
+
triggers=[run_button.click, prompt.submit],
|
191 |
+
fn=infer,
|
192 |
+
inputs=[prompt, ip_adapter_image, ip_adapter_scale, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
193 |
+
outputs=[result, seed]
|
194 |
+
)
|
195 |
+
|
196 |
+
# 포트 7890 설정, 대기열 활성화, API 활성화
|
197 |
+
demo.launch()
|
198 |
+
|