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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import imp
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import io
|
| 4 |
+
from PIL import Image, PngImagePlugin
|
| 5 |
+
import base64
|
| 6 |
+
import requests
|
| 7 |
+
import json
|
| 8 |
+
import ui_functions as uifn
|
| 9 |
+
from css_and_js import js, call_JS
|
| 10 |
+
import re
|
| 11 |
+
|
| 12 |
+
txt2img_defaults = {
|
| 13 |
+
'prompt': '',
|
| 14 |
+
'ddim_steps': 50,
|
| 15 |
+
'toggles': [1, 2, 3],
|
| 16 |
+
'sampler_name': 'k_lms',
|
| 17 |
+
'ddim_eta': 0.0,
|
| 18 |
+
'n_iter': 1,
|
| 19 |
+
'batch_size': 1,
|
| 20 |
+
'cfg_scale': 7.5,
|
| 21 |
+
'seed': '',
|
| 22 |
+
'height': 512,
|
| 23 |
+
'width': 512,
|
| 24 |
+
'fp': None,
|
| 25 |
+
'variant_amount': 0.0,
|
| 26 |
+
'variant_seed': '',
|
| 27 |
+
'submit_on_enter': 'Yes',
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
img2img_defaults = {
|
| 31 |
+
'prompt': '',
|
| 32 |
+
'ddim_steps': 50,
|
| 33 |
+
'toggles': [1, 4, 5],
|
| 34 |
+
'sampler_name': 'k_lms',
|
| 35 |
+
'ddim_eta': 0.0,
|
| 36 |
+
'n_iter': 1,
|
| 37 |
+
'batch_size': 1,
|
| 38 |
+
'cfg_scale': 5.0,
|
| 39 |
+
'denoising_strength': 0.75,
|
| 40 |
+
'mask_mode': 1,
|
| 41 |
+
'resize_mode': 0,
|
| 42 |
+
'seed': '',
|
| 43 |
+
'height': 512,
|
| 44 |
+
'width': 512,
|
| 45 |
+
'fp': None,
|
| 46 |
+
}
|
| 47 |
+
sample_img2img = None
|
| 48 |
+
job_manager = None
|
| 49 |
+
RealESRGAN = True
|
| 50 |
+
show_embeddings = False
|
| 51 |
+
|
| 52 |
+
img2img_resize_modes = [
|
| 53 |
+
"Just resize",
|
| 54 |
+
"Crop and resize",
|
| 55 |
+
"Resize and fill",
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
img2img_toggles = [
|
| 59 |
+
'Create prompt matrix (separate multiple prompts using |, and get all combinations of them)',
|
| 60 |
+
'Normalize Prompt Weights (ensure sum of weights add up to 1.0)',
|
| 61 |
+
'Loopback (use images from previous batch when creating next batch)',
|
| 62 |
+
'Random loopback seed',
|
| 63 |
+
'Save individual images',
|
| 64 |
+
'Save grid',
|
| 65 |
+
'Sort samples by prompt',
|
| 66 |
+
'Write sample info files',
|
| 67 |
+
'Write sample info to one file',
|
| 68 |
+
'jpg samples',
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
img2img_toggle_defaults = [img2img_toggles[i] for i in img2img_defaults['toggles']]
|
| 72 |
+
|
| 73 |
+
def read_content(file_path: str) -> str:
|
| 74 |
+
"""read the content of target file
|
| 75 |
+
"""
|
| 76 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 77 |
+
content = f.read()
|
| 78 |
+
|
| 79 |
+
return content
|
| 80 |
+
|
| 81 |
+
def base2picture(resbase64):
|
| 82 |
+
res=resbase64.split(',')[1]
|
| 83 |
+
img_b64decode = base64.b64decode(res)
|
| 84 |
+
image = io.BytesIO(img_b64decode)
|
| 85 |
+
img = Image.open(image)
|
| 86 |
+
return img
|
| 87 |
+
|
| 88 |
+
def filter_content(raw_style: str):
|
| 89 |
+
if "(" in raw_style:
|
| 90 |
+
i = raw_style.index("(")
|
| 91 |
+
else :
|
| 92 |
+
i = -1
|
| 93 |
+
|
| 94 |
+
if i == -1:
|
| 95 |
+
return raw_style
|
| 96 |
+
else :
|
| 97 |
+
return raw_style[:i]
|
| 98 |
+
|
| 99 |
+
def request_images(raw_text, class_draw, style_draw, batch_size, sr_option):
|
| 100 |
+
if filter_content(class_draw) != "traditional Chinese painting":
|
| 101 |
+
if filter_content(class_draw) != "Universal":
|
| 102 |
+
raw_text = raw_text + f",{filter_content(class_draw)}"
|
| 103 |
+
|
| 104 |
+
for sty in style_draw:
|
| 105 |
+
raw_text = raw_text + f",{filter_content(sty)}"
|
| 106 |
+
print(f"raw text is {raw_text}")
|
| 107 |
+
url = "http://flagart.baai.ac.cn/api/general/"
|
| 108 |
+
elif filter_content(class_draw) == "traditional Chinese painting":
|
| 109 |
+
if raw_text.endswith("traditional Chinese painting"):
|
| 110 |
+
pass
|
| 111 |
+
else :
|
| 112 |
+
raw_text = raw_text + ",traditional Chinese painting"
|
| 113 |
+
url = "http://flagart.baai.ac.cn/api/guohua/"
|
| 114 |
+
|
| 115 |
+
d = {"data":[raw_text, int(batch_size), sr_option]}
|
| 116 |
+
r = requests.post(url, json=d, headers={"Content-Type": "application/json", "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive"})
|
| 117 |
+
result_text = r.text
|
| 118 |
+
content = json.loads(result_text)["data"][0]
|
| 119 |
+
images = []
|
| 120 |
+
for i in range(int(batch_size)):
|
| 121 |
+
# print(content[i])
|
| 122 |
+
images.append(base2picture(content[i]))
|
| 123 |
+
|
| 124 |
+
return images
|
| 125 |
+
|
| 126 |
+
def sr_request_images(img_str, idx):
|
| 127 |
+
idx_map = {
|
| 128 |
+
"1(img1)":0,
|
| 129 |
+
"2(img2)":1,
|
| 130 |
+
"3(img3)":2,
|
| 131 |
+
"4(img4)":3,
|
| 132 |
+
}
|
| 133 |
+
idx = idx_map[idx]
|
| 134 |
+
# image_data = re.sub('^data:image/.+;base64,', '', img_str[idx])
|
| 135 |
+
image_data = img_str[idx]
|
| 136 |
+
# print(image_data)
|
| 137 |
+
d = {"data":[image_data, 0, False]} # batch_size设置为0,sr_opt设置为False
|
| 138 |
+
url = "http://flagart.baai.ac.cn/api/general/"
|
| 139 |
+
r = requests.post(url, json=d, headers={"Content-Type": "application/json", "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive"})
|
| 140 |
+
result_text = r.text
|
| 141 |
+
# print(result_text)
|
| 142 |
+
content = json.loads(result_text)["data"][0]
|
| 143 |
+
# print(content)
|
| 144 |
+
images = [base2picture(content[0])]
|
| 145 |
+
|
| 146 |
+
return images
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def encode_pil_to_base64(pil_image):
|
| 150 |
+
with io.BytesIO() as output_bytes:
|
| 151 |
+
|
| 152 |
+
# Copy any text-only metadata
|
| 153 |
+
use_metadata = False
|
| 154 |
+
metadata = PngImagePlugin.PngInfo()
|
| 155 |
+
for key, value in pil_image.info.items():
|
| 156 |
+
if isinstance(key, str) and isinstance(value, str):
|
| 157 |
+
metadata.add_text(key, value)
|
| 158 |
+
use_metadata = True
|
| 159 |
+
|
| 160 |
+
pil_image.save(
|
| 161 |
+
output_bytes, "PNG", pnginfo=(metadata if use_metadata else None)
|
| 162 |
+
)
|
| 163 |
+
bytes_data = output_bytes.getvalue()
|
| 164 |
+
base64_str = str(base64.b64encode(bytes_data), "utf-8")
|
| 165 |
+
return "data:image/png;base64," + base64_str
|
| 166 |
+
|
| 167 |
+
def img2img(*args):
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
for i, item in enumerate(args):
|
| 171 |
+
# print(type(item))
|
| 172 |
+
if type(item) == dict:
|
| 173 |
+
args[i]['image'] = encode_pil_to_base64(item['image'])
|
| 174 |
+
args[i]['mask'] = encode_pil_to_base64(item['mask'])
|
| 175 |
+
# else:
|
| 176 |
+
# print(i,type(item))
|
| 177 |
+
# print(item)
|
| 178 |
+
|
| 179 |
+
batch_size = args[8]
|
| 180 |
+
|
| 181 |
+
url = "http://flagart.baai.ac.cn/api/img2img/"
|
| 182 |
+
d = {"data":args}
|
| 183 |
+
r = requests.post(url, json=d, headers={"Content-Type": "application/json", "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive"})
|
| 184 |
+
# print(r)
|
| 185 |
+
result_text = r.text
|
| 186 |
+
content = json.loads(result_text)["data"][0]
|
| 187 |
+
images = []
|
| 188 |
+
for i in range(batch_size):
|
| 189 |
+
# print(content[i])
|
| 190 |
+
images.append(base2picture(content[i]))
|
| 191 |
+
# content = json.loads(result_text)
|
| 192 |
+
# print(result_text)
|
| 193 |
+
|
| 194 |
+
return images
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
examples = [
|
| 198 |
+
'Ink Butterfly and Peony Flower, Chinese Painting',
|
| 199 |
+
'Vigorous and powerful ink bamboo, Chinese painting',
|
| 200 |
+
'Mechanical squirrel, science fiction',
|
| 201 |
+
'Chinese ink landscape painting, Chinese painting',
|
| 202 |
+
"Lighthouse in the storm",
|
| 203 |
+
"A dog",
|
| 204 |
+
"Landscape by Zhang Daqian",
|
| 205 |
+
"A tiger long bunny ears",
|
| 206 |
+
"A baby bird pencil sketch",
|
| 207 |
+
|
| 208 |
+
]
|
| 209 |
+
|
| 210 |
+
if __name__ == "__main__":
|
| 211 |
+
block = gr.Blocks(css=read_content('style.css'))
|
| 212 |
+
|
| 213 |
+
with block:
|
| 214 |
+
gr.HTML(read_content("header.html"))
|
| 215 |
+
|
| 216 |
+
with gr.Tabs(elem_id='tabss') as tabs:
|
| 217 |
+
|
| 218 |
+
with gr.TabItem("Vincent diagram(Text-to-img)", id='txt2img_tab'):
|
| 219 |
+
|
| 220 |
+
with gr.Group():
|
| 221 |
+
with gr.Box():
|
| 222 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
| 223 |
+
text = gr.Textbox(
|
| 224 |
+
label="Prompt",
|
| 225 |
+
show_label=False,
|
| 226 |
+
max_lines=1,
|
| 227 |
+
placeholder="Input text",
|
| 228 |
+
interactive=True,
|
| 229 |
+
).style(
|
| 230 |
+
border=(True, False, True, True),
|
| 231 |
+
rounded=(True, False, False, True),
|
| 232 |
+
container=False,
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
btn = gr.Button("Generate image").style(
|
| 236 |
+
margin=False,
|
| 237 |
+
rounded=(True, True, True, True),
|
| 238 |
+
)
|
| 239 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
| 240 |
+
class_draw = gr.Dropdown(["Universal(general)", "traditional Chinese painting",
|
| 241 |
+
"picture photography", "oil painting",
|
| 242 |
+
"Pencil sketch", "CG",
|
| 243 |
+
"watercolor painting", "ink and wash",
|
| 244 |
+
"illustrations)", "3D", "img2img"],
|
| 245 |
+
label="type",
|
| 246 |
+
show_label=True,
|
| 247 |
+
value="general")
|
| 248 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
| 249 |
+
style_draw = gr.CheckboxGroup(["steampunk", "film photography",
|
| 250 |
+
"concept art", "Warming lighting",
|
| 251 |
+
"Dramatic lighting", "Natural lighting",
|
| 252 |
+
"unreal engine", "4k", "8k",
|
| 253 |
+
"full details"],
|
| 254 |
+
label="style",
|
| 255 |
+
show_label=True,
|
| 256 |
+
)
|
| 257 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
| 258 |
+
# sample_size = gr.Slider(minimum=1,
|
| 259 |
+
# maximum=4,
|
| 260 |
+
# step=1,
|
| 261 |
+
# label="生成数量(number)",
|
| 262 |
+
# show_label=True,
|
| 263 |
+
# interactive=True,
|
| 264 |
+
# )
|
| 265 |
+
sample_size = gr.Radio(choices=["1","2","3","4"], value="1", show_label=True, label='number')
|
| 266 |
+
with gr.Row(visible=False).style(mobile_collapse=False, equal_height=True):
|
| 267 |
+
sr_option = gr.Checkbox(value=False, label="Whether to use super-resolution")
|
| 268 |
+
|
| 269 |
+
gallery = gr.Gallery(
|
| 270 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
| 271 |
+
).style(grid=[2,2])
|
| 272 |
+
gr.Examples(examples=examples, fn=request_images, inputs=text, outputs=gallery, examples_per_page=100)
|
| 273 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
| 274 |
+
img_choices = gr.Dropdown(["img1"],label='Please select a picture to send to Tushengtu or Super Score',show_label=True,value="img1")
|
| 275 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
| 276 |
+
output_txt2img_copy_to_input_btn = gr.Button("Sent the image to img2img").style(
|
| 277 |
+
margin=False,
|
| 278 |
+
rounded=(True, True, True, True),
|
| 279 |
+
)
|
| 280 |
+
output_txt2img_sr_btn = gr.Button("super-resolution").style(
|
| 281 |
+
margin=False,
|
| 282 |
+
rounded=(True, True, True, True),
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
sr_gallery = gr.Gallery(
|
| 286 |
+
label="SR images", show_label=True, elem_id="sr_gallery"
|
| 287 |
+
).style(grid=[1,1])
|
| 288 |
+
|
| 289 |
+
with gr.Row():
|
| 290 |
+
prompt = gr.Markdown("Prompt:", visible=False)
|
| 291 |
+
with gr.Row():
|
| 292 |
+
move_prompt_zh = gr.Markdown("Please move to the Graphing section for editing (pull to top)", visible=False)
|
| 293 |
+
with gr.Row():
|
| 294 |
+
move_prompt_en = gr.Markdown("Please move to the img2img section for editing(Pull to the top)", visible=False)
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
# go_to_img2img_btn = gr.Button("转至图生图(Go to img2img)").style(
|
| 300 |
+
# margin=False,
|
| 301 |
+
# rounded=(True, True, True, True),
|
| 302 |
+
# )
|
| 303 |
+
|
| 304 |
+
text.submit(request_images, inputs=[text, class_draw, style_draw, sample_size, sr_option], outputs=gallery)
|
| 305 |
+
btn.click(request_images, inputs=[text, class_draw, style_draw, sample_size, sr_option], outputs=gallery)
|
| 306 |
+
# to do here
|
| 307 |
+
output_txt2img_sr_btn.click(sr_request_images, inputs=[gallery, img_choices], outputs=[sr_gallery])
|
| 308 |
+
|
| 309 |
+
sample_size.change(
|
| 310 |
+
fn=uifn.change_img_choices,
|
| 311 |
+
inputs=[sample_size],
|
| 312 |
+
outputs=[img_choices]
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
with gr.TabItem("(Img-to-Img)", id="img2img_tab"):
|
| 316 |
+
with gr.Row(elem_id="prompt_row"):
|
| 317 |
+
img2img_prompt = gr.Textbox(label="Prompt",
|
| 318 |
+
elem_id='img2img_prompt_input',
|
| 319 |
+
placeholder="Magical forest, flowing river.",
|
| 320 |
+
lines=1,
|
| 321 |
+
max_lines=1 if txt2img_defaults['submit_on_enter'] == 'Yes' else 25,
|
| 322 |
+
value=img2img_defaults['prompt'],
|
| 323 |
+
show_label=False).style()
|
| 324 |
+
|
| 325 |
+
img2img_btn_mask = gr.Button("Generate", variant="primary", visible=False,
|
| 326 |
+
elem_id="img2img_mask_btn")
|
| 327 |
+
img2img_btn_editor = gr.Button("Generate", variant="primary", elem_id="img2img_edit_btn")
|
| 328 |
+
gr.Markdown('#### input image')
|
| 329 |
+
with gr.Row().style(equal_height=False):
|
| 330 |
+
#with gr.Column():
|
| 331 |
+
img2img_image_mask = gr.Image(
|
| 332 |
+
value=sample_img2img,
|
| 333 |
+
source="upload",
|
| 334 |
+
interactive=True,
|
| 335 |
+
tool="sketch",
|
| 336 |
+
type='pil',
|
| 337 |
+
elem_id="img2img_mask",
|
| 338 |
+
image_mode="RGBA"
|
| 339 |
+
)
|
| 340 |
+
img2img_image_editor = gr.Image(
|
| 341 |
+
value=sample_img2img,
|
| 342 |
+
source="upload",
|
| 343 |
+
interactive=True,
|
| 344 |
+
tool="select",
|
| 345 |
+
type='pil',
|
| 346 |
+
visible=False,
|
| 347 |
+
image_mode="RGBA",
|
| 348 |
+
elem_id="img2img_editor"
|
| 349 |
+
)
|
| 350 |
+
with gr.Tabs(visible=False):
|
| 351 |
+
with gr.TabItem("Edit settings"):
|
| 352 |
+
with gr.Row():
|
| 353 |
+
# disable Uncrop for now
|
| 354 |
+
choices=["Mask", "Crop", "Uncrop"]
|
| 355 |
+
img2img_image_editor_mode = gr.Radio(choices=["Mask"],
|
| 356 |
+
label="edit mode",
|
| 357 |
+
value="Mask", elem_id='edit_mode_select',
|
| 358 |
+
visible=True)
|
| 359 |
+
img2img_mask = gr.Radio(choices=["keep mask area", "generate mask area"],
|
| 360 |
+
label="Mask Way",
|
| 361 |
+
#value=img2img_mask_modes[img2img_defaults['mask_mode']],
|
| 362 |
+
value = "Generate mask area",
|
| 363 |
+
visible=True)
|
| 364 |
+
|
| 365 |
+
img2img_mask_blur_strength = gr.Slider(minimum=1, maximum=10, step=1,
|
| 366 |
+
label="How much blurry should the mask be? (to avoid hard edges)",
|
| 367 |
+
value=3, visible=False)
|
| 368 |
+
|
| 369 |
+
img2img_resize = gr.Radio(label="Resize mode",
|
| 370 |
+
choices=["Just resize", "Crop and resize",
|
| 371 |
+
"Resize and fill"],
|
| 372 |
+
value=img2img_resize_modes[
|
| 373 |
+
img2img_defaults['resize_mode']], visible=False)
|
| 374 |
+
|
| 375 |
+
img2img_painterro_btn = gr.Button("Advanced Editor",visible=False)
|
| 376 |
+
# with gr.TabItem("Hints",visible=False):
|
| 377 |
+
# img2img_help = gr.Markdown(visible=False, value=uifn.help_text)
|
| 378 |
+
gr.Markdown('#### edited image')
|
| 379 |
+
with gr.Row():
|
| 380 |
+
output_img2img_gallery = gr.Gallery(label="Images", elem_id="img2img_gallery_output").style(
|
| 381 |
+
grid=[4,4,4] )
|
| 382 |
+
img2img_job_ui = job_manager.draw_gradio_ui() if job_manager else None
|
| 383 |
+
with gr.Column(visible=False):
|
| 384 |
+
with gr.Tabs(visible=False):
|
| 385 |
+
with gr.TabItem("", id="img2img_actions_tab",visible=False):
|
| 386 |
+
gr.Markdown("Select an image, then press one of the buttons below")
|
| 387 |
+
with gr.Row():
|
| 388 |
+
output_img2img_copy_to_clipboard_btn = gr.Button("Copy to clipboard")
|
| 389 |
+
output_img2img_copy_to_input_btn = gr.Button("Push to img2img input")
|
| 390 |
+
output_img2img_copy_to_mask_btn = gr.Button("Push to img2img input mask")
|
| 391 |
+
|
| 392 |
+
gr.Markdown("Warning: This will clear your current image and mask settings!")
|
| 393 |
+
with gr.TabItem("", id="img2img_output_info_tab",visible=False):
|
| 394 |
+
output_img2img_params = gr.Textbox(label="Generation parameters")
|
| 395 |
+
with gr.Row():
|
| 396 |
+
output_img2img_copy_params = gr.Button("Copy full parameters").click(
|
| 397 |
+
inputs=output_img2img_params, outputs=[],
|
| 398 |
+
_js='(x) => {navigator.clipboard.writeText(x.replace(": ",":"))}', fn=None,
|
| 399 |
+
show_progress=False)
|
| 400 |
+
output_img2img_seed = gr.Number(label='Seed', interactive=False, visible=False)
|
| 401 |
+
output_img2img_copy_seed = gr.Button("Copy only seed").click(
|
| 402 |
+
inputs=output_img2img_seed, outputs=[],
|
| 403 |
+
_js=call_JS("gradioInputToClipboard"), fn=None, show_progress=False)
|
| 404 |
+
output_img2img_stats = gr.HTML(label='Stats')
|
| 405 |
+
with gr.Row():
|
| 406 |
+
gr.Markdown('(prompt):')
|
| 407 |
+
with gr.Row():
|
| 408 |
+
gr.Markdown('Please select an image to mask out a part of the area and enter a text description')
|
| 409 |
+
with gr.Row():
|
| 410 |
+
gr.Markdown('Please select an image to cover up a part of the area and enter a text description.')
|
| 411 |
+
gr.Markdown('# Edit settings',visible=False)
|
| 412 |
+
|
| 413 |
+
with gr.Row(visible=False):
|
| 414 |
+
with gr.Column():
|
| 415 |
+
img2img_width = gr.Slider(minimum=64, maximum=2048, step=64, label="image width",
|
| 416 |
+
value=img2img_defaults["width"])
|
| 417 |
+
img2img_height = gr.Slider(minimum=64, maximum=2048, step=64, label="image height",
|
| 418 |
+
value=img2img_defaults["height"])
|
| 419 |
+
img2img_cfg = gr.Slider(minimum=-40.0, maximum=30.0, step=0.5,
|
| 420 |
+
label='text lead strength',
|
| 421 |
+
value=img2img_defaults['cfg_scale'], elem_id='cfg_slider')
|
| 422 |
+
img2img_seed = gr.Textbox(label="random seed", lines=1, max_lines=1,
|
| 423 |
+
value=img2img_defaults["seed"])
|
| 424 |
+
img2img_batch_count = gr.Slider(minimum=1, maximum=50, step=1,
|
| 425 |
+
label='number of builds',
|
| 426 |
+
value=img2img_defaults['n_iter'])
|
| 427 |
+
img2img_dimensions_info_text_box = gr.Textbox(
|
| 428 |
+
label="Aspect ratio settings")
|
| 429 |
+
with gr.Column():
|
| 430 |
+
img2img_steps = gr.Slider(minimum=1, maximum=250, step=1, label="Sampling steps",
|
| 431 |
+
value=img2img_defaults['ddim_steps'])
|
| 432 |
+
|
| 433 |
+
img2img_sampling = gr.Dropdown(label='Sampling method',
|
| 434 |
+
choices=["DDIM", 'k_dpm_2_a', 'k_dpm_2', 'k_euler_a', 'k_euler',
|
| 435 |
+
'k_heun', 'k_lms'],
|
| 436 |
+
value=img2img_defaults['sampler_name'])
|
| 437 |
+
|
| 438 |
+
img2img_denoising = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising Strength',
|
| 439 |
+
value=img2img_defaults['denoising_strength'],visible=False)
|
| 440 |
+
|
| 441 |
+
img2img_toggles = gr.CheckboxGroup(label='', choices=img2img_toggles,
|
| 442 |
+
value=img2img_toggle_defaults,visible=False)
|
| 443 |
+
|
| 444 |
+
img2img_realesrgan_model_name = gr.Dropdown(label='RealESRGAN model',
|
| 445 |
+
choices=['RealESRGAN_x4plus',
|
| 446 |
+
'RealESRGAN_x4plus_anime_6B'],
|
| 447 |
+
value='RealESRGAN_x4plus',
|
| 448 |
+
visible=RealESRGAN is not None) # TODO: Feels like I shouldnt slot it in here.
|
| 449 |
+
|
| 450 |
+
img2img_embeddings = gr.File(label="Embeddings file for textual inversion",
|
| 451 |
+
visible=show_embeddings)
|
| 452 |
+
|
| 453 |
+
img2img_image_editor_mode.change(
|
| 454 |
+
uifn.change_image_editor_mode,
|
| 455 |
+
[img2img_image_editor_mode,
|
| 456 |
+
img2img_image_editor,
|
| 457 |
+
img2img_image_mask,
|
| 458 |
+
img2img_resize,
|
| 459 |
+
img2img_width,
|
| 460 |
+
img2img_height
|
| 461 |
+
],
|
| 462 |
+
[img2img_image_editor, img2img_image_mask, img2img_btn_editor, img2img_btn_mask,
|
| 463 |
+
img2img_painterro_btn, img2img_mask, img2img_mask_blur_strength]
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
img2img_image_editor_mode.change(
|
| 468 |
+
uifn.update_image_mask,
|
| 469 |
+
[img2img_image_editor, img2img_resize, img2img_width, img2img_height],
|
| 470 |
+
img2img_image_mask
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
+
|
| 474 |
+
# output_txt2img_copy_to_input_btn.click(
|
| 475 |
+
# uifn.copy_img_to_input,
|
| 476 |
+
# [gallery],
|
| 477 |
+
# [tabs, img2img_image_editor, img2img_image_mask],
|
| 478 |
+
# _js=call_JS("moveImageFromGallery",
|
| 479 |
+
# fromId="txt2img_gallery_output",
|
| 480 |
+
# toId="img2img_mask")
|
| 481 |
+
# )
|
| 482 |
+
output_txt2img_copy_to_input_btn.click(
|
| 483 |
+
uifn.copy_img_to_input,
|
| 484 |
+
[gallery, img_choices],
|
| 485 |
+
[tabs, img2img_image_editor, img2img_image_mask, move_prompt_zh, move_prompt_en, prompt]
|
| 486 |
+
)
|
| 487 |
+
|
| 488 |
+
# go_to_img2img_btn.click(
|
| 489 |
+
# uifn.go_to_img2img,
|
| 490 |
+
# [],
|
| 491 |
+
# [tabs],
|
| 492 |
+
# )
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
output_img2img_copy_to_input_btn.click(
|
| 496 |
+
uifn.copy_img_to_edit,
|
| 497 |
+
[output_img2img_gallery],
|
| 498 |
+
[img2img_image_editor, tabs, img2img_image_editor_mode],
|
| 499 |
+
_js=call_JS("moveImageFromGallery",
|
| 500 |
+
fromId="gallery",
|
| 501 |
+
toId="img2img_editor")
|
| 502 |
+
)
|
| 503 |
+
output_img2img_copy_to_mask_btn.click(
|
| 504 |
+
uifn.copy_img_to_mask,
|
| 505 |
+
[output_img2img_gallery],
|
| 506 |
+
[img2img_image_mask, tabs, img2img_image_editor_mode],
|
| 507 |
+
_js=call_JS("moveImageFromGallery",
|
| 508 |
+
fromId="img2img_gallery_output",
|
| 509 |
+
toId="img2img_editor")
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
output_img2img_copy_to_clipboard_btn.click(fn=None, inputs=output_img2img_gallery, outputs=[],
|
| 513 |
+
_js=call_JS("copyImageFromGalleryToClipboard",
|
| 514 |
+
fromId="img2img_gallery_output")
|
| 515 |
+
)
|
| 516 |
+
|
| 517 |
+
img2img_func = img2img
|
| 518 |
+
img2img_inputs = [img2img_prompt, img2img_image_editor_mode, img2img_mask,
|
| 519 |
+
img2img_mask_blur_strength, img2img_steps, img2img_sampling, img2img_toggles,
|
| 520 |
+
img2img_realesrgan_model_name, img2img_batch_count, img2img_cfg,
|
| 521 |
+
img2img_denoising, img2img_seed, img2img_height, img2img_width, img2img_resize,
|
| 522 |
+
img2img_image_mask]
|
| 523 |
+
# img2img_outputs = [output_img2img_gallery, output_img2img_seed, output_img2img_params,
|
| 524 |
+
# output_img2img_stats]
|
| 525 |
+
|
| 526 |
+
img2img_outputs = [output_img2img_gallery]
|
| 527 |
+
|
| 528 |
+
# If a JobManager was passed in then wrap the Generate functions
|
| 529 |
+
if img2img_job_ui:
|
| 530 |
+
img2img_func, img2img_inputs, img2img_outputs = img2img_job_ui.wrap_func(
|
| 531 |
+
func=img2img_func,
|
| 532 |
+
inputs=img2img_inputs,
|
| 533 |
+
outputs=img2img_outputs,
|
| 534 |
+
)
|
| 535 |
+
|
| 536 |
+
img2img_btn_mask.click(
|
| 537 |
+
img2img_func,
|
| 538 |
+
img2img_inputs,
|
| 539 |
+
img2img_outputs
|
| 540 |
+
)
|
| 541 |
+
|
| 542 |
+
def img2img_submit_params():
|
| 543 |
+
# print([img2img_prompt, img2img_image_editor_mode, img2img_mask,
|
| 544 |
+
# img2img_mask_blur_strength, img2img_steps, img2img_sampling, img2img_toggles,
|
| 545 |
+
# img2img_realesrgan_model_name, img2img_batch_count, img2img_cfg,
|
| 546 |
+
# img2img_denoising, img2img_seed, img2img_height, img2img_width, img2img_resize,
|
| 547 |
+
# img2img_image_editor, img2img_image_mask, img2img_embeddings])
|
| 548 |
+
return (img2img_func,
|
| 549 |
+
img2img_inputs,
|
| 550 |
+
img2img_outputs)
|
| 551 |
+
|
| 552 |
+
img2img_btn_editor.click(*img2img_submit_params())
|
| 553 |
+
|
| 554 |
+
# GENERATE ON ENTER
|
| 555 |
+
img2img_prompt.submit(None, None, None,
|
| 556 |
+
_js=call_JS("clickFirstVisibleButton",
|
| 557 |
+
rowId="prompt_row"))
|
| 558 |
+
|
| 559 |
+
img2img_painterro_btn.click(None,
|
| 560 |
+
[img2img_image_editor, img2img_image_mask, img2img_image_editor_mode],
|
| 561 |
+
[img2img_image_editor, img2img_image_mask],
|
| 562 |
+
_js=call_JS("Painterro.init", toId="img2img_editor")
|
| 563 |
+
)
|
| 564 |
+
|
| 565 |
+
img2img_width.change(fn=uifn.update_dimensions_info, inputs=[img2img_width, img2img_height],
|
| 566 |
+
outputs=img2img_dimensions_info_text_box)
|
| 567 |
+
img2img_height.change(fn=uifn.update_dimensions_info, inputs=[img2img_width, img2img_height],
|
| 568 |
+
outputs=img2img_dimensions_info_text_box)
|
| 569 |
+
|
| 570 |
+
gr.HTML(read_content("footer.html"))
|
| 571 |
+
# gr.Image('./contributors.png')
|
| 572 |
+
|
| 573 |
+
block.queue(max_size=50, concurrency_count=20).launch()
|