Spaces:
Runtime error
Runtime error
luoxiang
commited on
Commit
•
8423730
1
Parent(s):
589c459
init
Browse files- app.py +494 -0
- footer.html +18 -0
- header.html +37 -0
- share_btn.py +60 -0
- style.css +81 -0
- ui_functions.py +219 -0
app.py
ADDED
@@ -0,0 +1,494 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import io
|
3 |
+
from PIL import Image, PngImagePlugin
|
4 |
+
import base64
|
5 |
+
import requests
|
6 |
+
import json
|
7 |
+
import ui_functions as uifn
|
8 |
+
|
9 |
+
txt2img_defaults = {
|
10 |
+
'prompt': '',
|
11 |
+
'ddim_steps': 50,
|
12 |
+
'toggles': [1, 2, 3],
|
13 |
+
'sampler_name': 'k_lms',
|
14 |
+
'ddim_eta': 0.0,
|
15 |
+
'n_iter': 1,
|
16 |
+
'batch_size': 1,
|
17 |
+
'cfg_scale': 7.5,
|
18 |
+
'seed': '',
|
19 |
+
'height': 512,
|
20 |
+
'width': 512,
|
21 |
+
'fp': None,
|
22 |
+
'variant_amount': 0.0,
|
23 |
+
'variant_seed': '',
|
24 |
+
'submit_on_enter': 'Yes',
|
25 |
+
}
|
26 |
+
|
27 |
+
img2img_defaults = {
|
28 |
+
'prompt': '',
|
29 |
+
'ddim_steps': 50,
|
30 |
+
'toggles': [1, 4, 5],
|
31 |
+
'sampler_name': 'k_lms',
|
32 |
+
'ddim_eta': 0.0,
|
33 |
+
'n_iter': 1,
|
34 |
+
'batch_size': 1,
|
35 |
+
'cfg_scale': 5.0,
|
36 |
+
'denoising_strength': 0.75,
|
37 |
+
'mask_mode': 1,
|
38 |
+
'resize_mode': 0,
|
39 |
+
'seed': '',
|
40 |
+
'height': 512,
|
41 |
+
'width': 512,
|
42 |
+
'fp': None,
|
43 |
+
}
|
44 |
+
sample_img2img = None
|
45 |
+
job_manager = None
|
46 |
+
RealESRGAN = True
|
47 |
+
show_embeddings = False
|
48 |
+
|
49 |
+
img2img_resize_modes = [
|
50 |
+
"Just resize",
|
51 |
+
"Crop and resize",
|
52 |
+
"Resize and fill",
|
53 |
+
]
|
54 |
+
|
55 |
+
img2img_toggles = [
|
56 |
+
'Create prompt matrix (separate multiple prompts using |, and get all combinations of them)',
|
57 |
+
'Normalize Prompt Weights (ensure sum of weights add up to 1.0)',
|
58 |
+
'Loopback (use images from previous batch when creating next batch)',
|
59 |
+
'Random loopback seed',
|
60 |
+
'Save individual images',
|
61 |
+
'Save grid',
|
62 |
+
'Sort samples by prompt',
|
63 |
+
'Write sample info files',
|
64 |
+
'Write sample info to one file',
|
65 |
+
'jpg samples',
|
66 |
+
]
|
67 |
+
|
68 |
+
img2img_toggle_defaults = [img2img_toggles[i] for i in img2img_defaults['toggles']]
|
69 |
+
|
70 |
+
def read_content(file_path: str) -> str:
|
71 |
+
"""read the content of target file
|
72 |
+
"""
|
73 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
74 |
+
content = f.read()
|
75 |
+
|
76 |
+
return content
|
77 |
+
|
78 |
+
def base2picture(resbase64):
|
79 |
+
res=resbase64.split(',')[1]
|
80 |
+
img_b64decode = base64.b64decode(res)
|
81 |
+
image = io.BytesIO(img_b64decode)
|
82 |
+
img = Image.open(image)
|
83 |
+
return img
|
84 |
+
|
85 |
+
def filter_content(raw_style: str):
|
86 |
+
if "(" in raw_style:
|
87 |
+
i = raw_style.index("(")
|
88 |
+
else :
|
89 |
+
i = -1
|
90 |
+
|
91 |
+
if i == -1:
|
92 |
+
return raw_style
|
93 |
+
else :
|
94 |
+
return raw_style[:i]
|
95 |
+
|
96 |
+
def request_images(raw_text, class_draw, style_draw, batch_size):
|
97 |
+
if filter_content(class_draw) != "国画":
|
98 |
+
if filter_content(class_draw) != "通用":
|
99 |
+
raw_text = raw_text + f",{filter_content(class_draw)}"
|
100 |
+
|
101 |
+
for sty in style_draw:
|
102 |
+
raw_text = raw_text + f",{filter_content(sty)}"
|
103 |
+
print(f"raw text is {raw_text}")
|
104 |
+
url = "http://flagart.baai.ac.cn/api/general/"
|
105 |
+
elif filter_content(class_draw) == "国画":
|
106 |
+
if raw_text.endswith("国画"):
|
107 |
+
pass
|
108 |
+
else :
|
109 |
+
raw_text = raw_text + ",国画"
|
110 |
+
url = "http://flagart.baai.ac.cn/api/guohua/"
|
111 |
+
|
112 |
+
d = {"data":[raw_text, batch_size]}
|
113 |
+
r = requests.post(url, json=d, headers={"Content-Type": "application/json", "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive"})
|
114 |
+
result_text = r.text
|
115 |
+
content = json.loads(result_text)["data"][0]
|
116 |
+
images = []
|
117 |
+
for i in range(batch_size):
|
118 |
+
# print(content[i])
|
119 |
+
images.append(base2picture(content[i]))
|
120 |
+
|
121 |
+
return images
|
122 |
+
|
123 |
+
def call_JS(sd_method, **kwargs):
|
124 |
+
param_str = json.dumps(kwargs)
|
125 |
+
return f"async (...x) => {{ return await SD.{sd_method}({{ x, ...{param_str} }}) ?? []; }}"
|
126 |
+
|
127 |
+
def encode_pil_to_base64(pil_image):
|
128 |
+
with io.BytesIO() as output_bytes:
|
129 |
+
|
130 |
+
# Copy any text-only metadata
|
131 |
+
use_metadata = False
|
132 |
+
metadata = PngImagePlugin.PngInfo()
|
133 |
+
for key, value in pil_image.info.items():
|
134 |
+
if isinstance(key, str) and isinstance(value, str):
|
135 |
+
metadata.add_text(key, value)
|
136 |
+
use_metadata = True
|
137 |
+
|
138 |
+
pil_image.save(
|
139 |
+
output_bytes, "PNG", pnginfo=(metadata if use_metadata else None)
|
140 |
+
)
|
141 |
+
bytes_data = output_bytes.getvalue()
|
142 |
+
base64_str = str(base64.b64encode(bytes_data), "utf-8")
|
143 |
+
return "data:image/png;base64," + base64_str
|
144 |
+
|
145 |
+
def img2img(*args):
|
146 |
+
|
147 |
+
# 处理image
|
148 |
+
for i, item in enumerate(args):
|
149 |
+
# print(type(item))
|
150 |
+
if type(item) == dict:
|
151 |
+
args[i]['image'] = encode_pil_to_base64(item['image'])
|
152 |
+
args[i]['mask'] = encode_pil_to_base64(item['mask'])
|
153 |
+
else:
|
154 |
+
print(i,type(item))
|
155 |
+
print(item)
|
156 |
+
|
157 |
+
batch_size = args[8]
|
158 |
+
|
159 |
+
url = "http://flagart.baai.ac.cn/api/img2img/"
|
160 |
+
d = {"data":args}
|
161 |
+
r = requests.post(url, json=d, headers={"Content-Type": "application/json", "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive"})
|
162 |
+
print(r)
|
163 |
+
result_text = r.text
|
164 |
+
content = json.loads(result_text)["data"][0]
|
165 |
+
images = []
|
166 |
+
for i in range(batch_size):
|
167 |
+
# print(content[i])
|
168 |
+
images.append(base2picture(content[i]))
|
169 |
+
# content = json.loads(result_text)
|
170 |
+
# print(result_text)
|
171 |
+
# print("服务器已经把东西返回来啦!!!!!!!乌拉乌拉!!!!!")
|
172 |
+
return images
|
173 |
+
|
174 |
+
|
175 |
+
examples = [
|
176 |
+
'水墨蝴蝶和牡丹花,国画',
|
177 |
+
'苍劲有力的墨竹,国画',
|
178 |
+
'暴风雨中的灯塔',
|
179 |
+
'机械小松鼠,科学幻想',
|
180 |
+
'中国水墨山水画,国画',
|
181 |
+
"Lighthouse in the storm",
|
182 |
+
"A dog",
|
183 |
+
"Landscape by 张大千",
|
184 |
+
"A tiger 长了兔子耳朵",
|
185 |
+
"A baby bird 铅笔素描",
|
186 |
+
|
187 |
+
]
|
188 |
+
|
189 |
+
if __name__ == "__main__":
|
190 |
+
block = gr.Blocks(css=read_content('style.css'))
|
191 |
+
|
192 |
+
with block:
|
193 |
+
gr.HTML(read_content("header.html"))
|
194 |
+
|
195 |
+
with gr.Tabs() as tabs:
|
196 |
+
|
197 |
+
with gr.TabItem("文生图(Text-to-img)"):
|
198 |
+
|
199 |
+
with gr.Group():
|
200 |
+
with gr.Box():
|
201 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
202 |
+
text = gr.Textbox(
|
203 |
+
label="Prompt",
|
204 |
+
show_label=False,
|
205 |
+
max_lines=1,
|
206 |
+
placeholder="Input text(输入文字)",
|
207 |
+
interactive=True,
|
208 |
+
).style(
|
209 |
+
border=(True, False, True, True),
|
210 |
+
rounded=(True, False, False, True),
|
211 |
+
container=False,
|
212 |
+
)
|
213 |
+
|
214 |
+
btn = gr.Button("Generate image").style(
|
215 |
+
margin=False,
|
216 |
+
rounded=(True, True, True, True),
|
217 |
+
)
|
218 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
219 |
+
class_draw = gr.Dropdown(["通用(general)", "国画(traditional Chinese painting)",
|
220 |
+
"照片,摄影(picture photography)", "油画(oil painting)",
|
221 |
+
"铅笔素描(pencil sketch)", "CG",
|
222 |
+
"水彩画(watercolor painting)", "水墨画(ink and wash)",
|
223 |
+
"插画(illustrations)", "3D", "图生图(img2img)"],
|
224 |
+
label="生成类型(type)",
|
225 |
+
show_label=True,
|
226 |
+
value="通用(general)")
|
227 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
228 |
+
style_draw = gr.CheckboxGroup(["蒸汽朋克(steampunk)", "电影摄影风格(film photography)",
|
229 |
+
"概念艺术(concept art)", "Warming lighting",
|
230 |
+
"Dramatic lighting", "Natural lighting",
|
231 |
+
"虚幻引擎(unreal engine)", "4k", "8k",
|
232 |
+
"充满细节(full details)"],
|
233 |
+
label="画面风格(style)",
|
234 |
+
show_label=True,
|
235 |
+
)
|
236 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
237 |
+
sample_size = gr.Slider(minimum=1,
|
238 |
+
maximum=4,
|
239 |
+
step=1,
|
240 |
+
label="生成数量(number)",
|
241 |
+
show_label=True,
|
242 |
+
interactive=True,
|
243 |
+
)
|
244 |
+
|
245 |
+
gallery = gr.Gallery(
|
246 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
247 |
+
).style(grid=[2], height="auto")
|
248 |
+
|
249 |
+
gr.Examples(examples=examples, fn=request_images, inputs=text, outputs=gallery, examples_per_page=100)
|
250 |
+
text.submit(request_images, inputs=[text, class_draw, style_draw, sample_size], outputs=gallery)
|
251 |
+
btn.click(request_images, inputs=[text, class_draw, style_draw, sample_size], outputs=gallery)
|
252 |
+
|
253 |
+
with gr.TabItem("图生图(Img-to-Img)", id="img2img_tab"):
|
254 |
+
with gr.Row(elem_id="prompt_row"):
|
255 |
+
img2img_prompt = gr.Textbox(label="Prompt",
|
256 |
+
elem_id='img2img_prompt_input',
|
257 |
+
placeholder="神奇的森林,流淌的河流.",
|
258 |
+
lines=1,
|
259 |
+
max_lines=1 if txt2img_defaults['submit_on_enter'] == 'Yes' else 25,
|
260 |
+
value=img2img_defaults['prompt'],
|
261 |
+
show_label=False).style()
|
262 |
+
|
263 |
+
img2img_btn_mask = gr.Button("Generate", variant="primary", visible=False,
|
264 |
+
elem_id="img2img_mask_btn")
|
265 |
+
img2img_btn_editor = gr.Button("Generate", variant="primary", elem_id="img2img_edit_btn")
|
266 |
+
with gr.Row().style(equal_height=False):
|
267 |
+
with gr.Column():
|
268 |
+
gr.Markdown('#### 输入图像')
|
269 |
+
img2img_image_mask = gr.Image(
|
270 |
+
value=sample_img2img,
|
271 |
+
source="upload",
|
272 |
+
interactive=True,
|
273 |
+
tool="sketch",
|
274 |
+
type='pil',
|
275 |
+
elem_id="img2img_mask",
|
276 |
+
image_mode="RGBA"
|
277 |
+
)
|
278 |
+
img2img_image_editor = gr.Image(
|
279 |
+
value=sample_img2img,
|
280 |
+
source="upload",
|
281 |
+
interactive=True,
|
282 |
+
tool="select",
|
283 |
+
type='pil',
|
284 |
+
visible=False,
|
285 |
+
image_mode="RGBA",
|
286 |
+
elem_id="img2img_editor"
|
287 |
+
)
|
288 |
+
|
289 |
+
with gr.Tabs():
|
290 |
+
with gr.TabItem("编辑设置"):
|
291 |
+
with gr.Row():
|
292 |
+
# disable Uncrop for now
|
293 |
+
choices=["Mask", "Crop", "Uncrop"]
|
294 |
+
img2img_image_editor_mode = gr.Radio(choices=["Mask"],
|
295 |
+
label="编辑模式",
|
296 |
+
value="Mask", elem_id='edit_mode_select',
|
297 |
+
visible=True)
|
298 |
+
img2img_mask = gr.Radio(choices=["保留mask区域", "生成mask区域"],
|
299 |
+
label="Mask 方式",
|
300 |
+
#value=img2img_mask_modes[img2img_defaults['mask_mode']],
|
301 |
+
value = "生成mask区域",
|
302 |
+
visible=True)
|
303 |
+
|
304 |
+
img2img_mask_blur_strength = gr.Slider(minimum=1, maximum=10, step=1,
|
305 |
+
label="How much blurry should the mask be? (to avoid hard edges)",
|
306 |
+
value=3, visible=False)
|
307 |
+
|
308 |
+
img2img_resize = gr.Radio(label="Resize mode",
|
309 |
+
choices=["Just resize", "Crop and resize",
|
310 |
+
"Resize and fill"],
|
311 |
+
value=img2img_resize_modes[
|
312 |
+
img2img_defaults['resize_mode']], visible=False)
|
313 |
+
|
314 |
+
img2img_painterro_btn = gr.Button("Advanced Editor",visible=False)
|
315 |
+
# with gr.TabItem("Hints",visible=False):
|
316 |
+
# img2img_help = gr.Markdown(visible=False, value=uifn.help_text)
|
317 |
+
|
318 |
+
with gr.Column():
|
319 |
+
gr.Markdown('#### 编辑后的图片')
|
320 |
+
output_img2img_gallery = gr.Gallery(label="Images", elem_id="img2img_gallery_output").style(
|
321 |
+
grid=[4, 4, 4])
|
322 |
+
img2img_job_ui = job_manager.draw_gradio_ui() if job_manager else None
|
323 |
+
with gr.Column(visible=False):
|
324 |
+
with gr.Tabs(visible=False):
|
325 |
+
with gr.TabItem("", id="img2img_actions_tab",visible=False):
|
326 |
+
gr.Markdown("Select an image, then press one of the buttons below")
|
327 |
+
with gr.Row():
|
328 |
+
output_img2img_copy_to_clipboard_btn = gr.Button("Copy to clipboard")
|
329 |
+
output_img2img_copy_to_input_btn = gr.Button("Push to img2img input")
|
330 |
+
output_img2img_copy_to_mask_btn = gr.Button("Push to img2img input mask")
|
331 |
+
|
332 |
+
gr.Markdown("Warning: This will clear your current image and mask settings!")
|
333 |
+
with gr.TabItem("", id="img2img_output_info_tab",visible=False):
|
334 |
+
output_img2img_params = gr.Textbox(label="Generation parameters")
|
335 |
+
with gr.Row():
|
336 |
+
output_img2img_copy_params = gr.Button("Copy full parameters").click(
|
337 |
+
inputs=output_img2img_params, outputs=[],
|
338 |
+
_js='(x) => {navigator.clipboard.writeText(x.replace(": ",":"))}', fn=None,
|
339 |
+
show_progress=False)
|
340 |
+
output_img2img_seed = gr.Number(label='Seed', interactive=False, visible=False)
|
341 |
+
output_img2img_copy_seed = gr.Button("Copy only seed").click(
|
342 |
+
inputs=output_img2img_seed, outputs=[],
|
343 |
+
_js=call_JS("gradioInputToClipboard"), fn=None, show_progress=False)
|
344 |
+
output_img2img_stats = gr.HTML(label='Stats')
|
345 |
+
|
346 |
+
gr.Markdown('# 编辑设置')
|
347 |
+
|
348 |
+
with gr.Row():
|
349 |
+
with gr.Column():
|
350 |
+
img2img_width = gr.Slider(minimum=64, maximum=2048, step=64, label="图片宽度",
|
351 |
+
value=img2img_defaults["width"])
|
352 |
+
img2img_height = gr.Slider(minimum=64, maximum=2048, step=64, label="图片高度",
|
353 |
+
value=img2img_defaults["height"])
|
354 |
+
img2img_cfg = gr.Slider(minimum=-40.0, maximum=30.0, step=0.5,
|
355 |
+
label='文本引导强度',
|
356 |
+
value=img2img_defaults['cfg_scale'], elem_id='cfg_slider')
|
357 |
+
img2img_seed = gr.Textbox(label="随机种子", lines=1, max_lines=1,
|
358 |
+
value=img2img_defaults["seed"])
|
359 |
+
img2img_batch_count = gr.Slider(minimum=1, maximum=50, step=1,
|
360 |
+
label='生成数量',
|
361 |
+
value=img2img_defaults['n_iter'])
|
362 |
+
img2img_dimensions_info_text_box = gr.Textbox(
|
363 |
+
label="长宽比设置")
|
364 |
+
with gr.Column():
|
365 |
+
img2img_steps = gr.Slider(minimum=1, maximum=250, step=1, label="采样步数",
|
366 |
+
value=img2img_defaults['ddim_steps'])
|
367 |
+
|
368 |
+
img2img_sampling = gr.Dropdown(label='采样方式',
|
369 |
+
choices=["DDIM", 'k_dpm_2_a', 'k_dpm_2', 'k_euler_a', 'k_euler',
|
370 |
+
'k_heun', 'k_lms'],
|
371 |
+
value=img2img_defaults['sampler_name'])
|
372 |
+
|
373 |
+
img2img_denoising = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising Strength',
|
374 |
+
value=img2img_defaults['denoising_strength'],visible=False)
|
375 |
+
|
376 |
+
img2img_toggles = gr.CheckboxGroup(label='', choices=img2img_toggles,
|
377 |
+
value=img2img_toggle_defaults,visible=False)
|
378 |
+
|
379 |
+
img2img_realesrgan_model_name = gr.Dropdown(label='RealESRGAN model',
|
380 |
+
choices=['RealESRGAN_x4plus',
|
381 |
+
'RealESRGAN_x4plus_anime_6B'],
|
382 |
+
value='RealESRGAN_x4plus',
|
383 |
+
visible=RealESRGAN is not None) # TODO: Feels like I shouldnt slot it in here.
|
384 |
+
|
385 |
+
img2img_embeddings = gr.File(label="Embeddings file for textual inversion",
|
386 |
+
visible=show_embeddings)
|
387 |
+
|
388 |
+
img2img_image_editor_mode.change(
|
389 |
+
uifn.change_image_editor_mode,
|
390 |
+
[img2img_image_editor_mode,
|
391 |
+
img2img_image_editor,
|
392 |
+
img2img_image_mask,
|
393 |
+
img2img_resize,
|
394 |
+
img2img_width,
|
395 |
+
img2img_height
|
396 |
+
],
|
397 |
+
[img2img_image_editor, img2img_image_mask, img2img_btn_editor, img2img_btn_mask,
|
398 |
+
img2img_painterro_btn, img2img_mask, img2img_mask_blur_strength]
|
399 |
+
)
|
400 |
+
|
401 |
+
# img2img_image_editor_mode.change(
|
402 |
+
# uifn.update_image_mask,
|
403 |
+
# [img2img_image_editor, img2img_resize, img2img_width, img2img_height],
|
404 |
+
# img2img_image_mask
|
405 |
+
# )
|
406 |
+
|
407 |
+
# output_txt2img_copy_to_input_btn.click(
|
408 |
+
# uifn.copy_img_to_input,
|
409 |
+
# [output_txt2img_gallery],
|
410 |
+
# [img2img_image_editor, img2img_image_mask, tabs],
|
411 |
+
# _js=call_JS("moveImageFromGallery",
|
412 |
+
# fromId="txt2img_gallery_output",
|
413 |
+
# toId="img2img_editor")
|
414 |
+
# )
|
415 |
+
|
416 |
+
output_img2img_copy_to_input_btn.click(
|
417 |
+
uifn.copy_img_to_edit,
|
418 |
+
[output_img2img_gallery],
|
419 |
+
[img2img_image_editor, tabs, img2img_image_editor_mode],
|
420 |
+
_js=call_JS("moveImageFromGallery",
|
421 |
+
fromId="img2img_gallery_output",
|
422 |
+
toId="img2img_editor")
|
423 |
+
)
|
424 |
+
output_img2img_copy_to_mask_btn.click(
|
425 |
+
uifn.copy_img_to_mask,
|
426 |
+
[output_img2img_gallery],
|
427 |
+
[img2img_image_mask, tabs, img2img_image_editor_mode],
|
428 |
+
_js=call_JS("moveImageFromGallery",
|
429 |
+
fromId="img2img_gallery_output",
|
430 |
+
toId="img2img_editor")
|
431 |
+
)
|
432 |
+
|
433 |
+
output_img2img_copy_to_clipboard_btn.click(fn=None, inputs=output_img2img_gallery, outputs=[],
|
434 |
+
_js=call_JS("copyImageFromGalleryToClipboard",
|
435 |
+
fromId="img2img_gallery_output")
|
436 |
+
)
|
437 |
+
|
438 |
+
img2img_func = img2img
|
439 |
+
img2img_inputs = [img2img_prompt, img2img_image_editor_mode, img2img_mask,
|
440 |
+
img2img_mask_blur_strength, img2img_steps, img2img_sampling, img2img_toggles,
|
441 |
+
img2img_realesrgan_model_name, img2img_batch_count, img2img_cfg,
|
442 |
+
img2img_denoising, img2img_seed, img2img_height, img2img_width, img2img_resize,
|
443 |
+
img2img_image_mask]
|
444 |
+
# img2img_outputs = [output_img2img_gallery, output_img2img_seed, output_img2img_params,
|
445 |
+
# output_img2img_stats]
|
446 |
+
|
447 |
+
img2img_outputs = [output_img2img_gallery]
|
448 |
+
|
449 |
+
# If a JobManager was passed in then wrap the Generate functions
|
450 |
+
if img2img_job_ui:
|
451 |
+
img2img_func, img2img_inputs, img2img_outputs = img2img_job_ui.wrap_func(
|
452 |
+
func=img2img_func,
|
453 |
+
inputs=img2img_inputs,
|
454 |
+
outputs=img2img_outputs,
|
455 |
+
)
|
456 |
+
|
457 |
+
img2img_btn_mask.click(
|
458 |
+
img2img_func,
|
459 |
+
img2img_inputs,
|
460 |
+
img2img_outputs
|
461 |
+
)
|
462 |
+
|
463 |
+
def img2img_submit_params():
|
464 |
+
# print([img2img_prompt, img2img_image_editor_mode, img2img_mask,
|
465 |
+
# img2img_mask_blur_strength, img2img_steps, img2img_sampling, img2img_toggles,
|
466 |
+
# img2img_realesrgan_model_name, img2img_batch_count, img2img_cfg,
|
467 |
+
# img2img_denoising, img2img_seed, img2img_height, img2img_width, img2img_resize,
|
468 |
+
# img2img_image_editor, img2img_image_mask, img2img_embeddings])
|
469 |
+
return (img2img_func,
|
470 |
+
img2img_inputs,
|
471 |
+
img2img_outputs)
|
472 |
+
|
473 |
+
img2img_btn_editor.click(*img2img_submit_params())
|
474 |
+
|
475 |
+
# GENERATE ON ENTER
|
476 |
+
img2img_prompt.submit(None, None, None,
|
477 |
+
_js=call_JS("clickFirstVisibleButton",
|
478 |
+
rowId="prompt_row"))
|
479 |
+
|
480 |
+
img2img_painterro_btn.click(None,
|
481 |
+
[img2img_image_editor, img2img_image_mask, img2img_image_editor_mode],
|
482 |
+
[img2img_image_editor, img2img_image_mask],
|
483 |
+
_js=call_JS("Painterro.init", toId="img2img_editor")
|
484 |
+
)
|
485 |
+
|
486 |
+
img2img_width.change(fn=uifn.update_dimensions_info, inputs=[img2img_width, img2img_height],
|
487 |
+
outputs=img2img_dimensions_info_text_box)
|
488 |
+
img2img_height.change(fn=uifn.update_dimensions_info, inputs=[img2img_width, img2img_height],
|
489 |
+
outputs=img2img_dimensions_info_text_box)
|
490 |
+
|
491 |
+
gr.HTML(read_content("footer.html"))
|
492 |
+
# gr.Image('./contributors.png')
|
493 |
+
|
494 |
+
block.queue(max_size=50, concurrency_count=20).launch()
|
footer.html
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<div class="footer">
|
2 |
+
<p> - Model by <a href="https://github.com/FlagAI-Open/FlagAI"><img src="https://github-link-card.s3.ap-northeast-1.amazonaws.com/FlagAI-Open/FlagAI.png" width="1200px"></a > - Gradio Demo by 🤗 Hugging Face
|
3 |
+
</p>
|
4 |
+
<!-- <p><a href="https://github.com/FlagAI-Open/FlagAI"><img src="https://raw.githubusercontent.com/920232796/test/master/contributors.png" width="1200px"></a >
|
5 |
+
</p> -->
|
6 |
+
|
7 |
+
<div class="acknowledgments">
|
8 |
+
<p><h4 style="font-weight: bold; font-size: 20px; margin-top: 20px;">LICENSE</h4>
|
9 |
+
The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a>
|
10 |
+
</p>
|
11 |
+
|
12 |
+
<p><h4 style="font-weight: bold; font-size: 20px;">Contributing</h4>
|
13 |
+
Thanks for your interest in contributing! There are many ways to get involved; start with our <a href="https://github.com/FlagAI-Open/FlagAI" style="text-decoration: underline;" target="_blank">contributor guidelines</a> and then check these <a href="https://github.com/FlagAI-Open/FlagAI" style="text-decoration: underline;" target="_blank"> open issues</a> for specific tasks.
|
14 |
+
</p>
|
15 |
+
</div>
|
16 |
+
</div>
|
17 |
+
|
18 |
+
|
header.html
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
2 |
+
<div
|
3 |
+
style="
|
4 |
+
display: inline-flex;
|
5 |
+
gap: 0.8rem;
|
6 |
+
font-size: 1.75rem;
|
7 |
+
margin-bottom: 10px;
|
8 |
+
width: 600px;
|
9 |
+
height: 200px;
|
10 |
+
margin: 0 auto;
|
11 |
+
/* border: 1px solid red; */
|
12 |
+
justify-content: center;
|
13 |
+
"
|
14 |
+
|
15 |
+
<a href="https://github.com/FlagAI-Open/FlagAI"><img src="https://raw.githubusercontent.com/920232796/test/master/WechatIMG6906.png" alt="FlagAI" width="80%" height="80%" style="margin: 0 auto;"></a>
|
16 |
+
</div>
|
17 |
+
<div
|
18 |
+
style="
|
19 |
+
display: inline-flex;
|
20 |
+
align-items: center;
|
21 |
+
gap: 0.8rem;
|
22 |
+
font-size: 1.75rem;
|
23 |
+
margin-bottom: 10px;
|
24 |
+
justify-content: center;
|
25 |
+
">
|
26 |
+
<a href="https://github.com/FlagAI-Open/FlagAI"><h1 style="font-weight: 900; margin-bottom: 7px;">
|
27 |
+
FlagStudio
|
28 |
+
</h1></a>
|
29 |
+
</div>
|
30 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
31 |
+
FlagStudio 项目致力于贡献优秀AI生成艺术作品。此双语文生图模型项目基于 <a href="https://huggingface.co/CompVis/stable-diffusion" style="text-decoration: underline;">stable diffusion</a>,由BAAI旗下的FlagAI团队提供支持,相关代码和模型权重会在FlagAI中进行开源。
|
32 |
+
</p>
|
33 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
34 |
+
FlagStudio aims to provide high quality AI-generated artwork. Our current model is based on the original <a href="https://huggingface.co/CompVis/stable-diffusion" style="text-decoration: underline;">stable diffusion</a> model and is capable to generate images from both Chinese and English text. FlagStudio is developed and supported by the FlagAI team. Relevant code and model weights will be released soon in FlagAI. We will continue to improve our model and release newer versions. Any suggestions and feedback is welcome! ([email protected])
|
35 |
+
</p>
|
36 |
+
|
37 |
+
</div>
|
share_btn.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
community_icon_html = """<svg id="share-btn-share-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32">
|
2 |
+
<path d="M20.6081 3C21.7684 3 22.8053 3.49196 23.5284 4.38415C23.9756 4.93678 24.4428 5.82749 24.4808 7.16133C24.9674 7.01707 25.4353 6.93643 25.8725 6.93643C26.9833 6.93643 27.9865 7.37587 28.696 8.17411C29.6075 9.19872 30.0124 10.4579 29.8361 11.7177C29.7523 12.3177 29.5581 12.8555 29.2678 13.3534C29.8798 13.8646 30.3306 14.5763 30.5485 15.4322C30.719 16.1032 30.8939 17.5006 29.9808 18.9403C30.0389 19.0342 30.0934 19.1319 30.1442 19.2318C30.6932 20.3074 30.7283 21.5229 30.2439 22.6548C29.5093 24.3704 27.6841 25.7219 24.1397 27.1727C21.9347 28.0753 19.9174 28.6523 19.8994 28.6575C16.9842 29.4379 14.3477 29.8345 12.0653 29.8345C7.87017 29.8345 4.8668 28.508 3.13831 25.8921C0.356375 21.6797 0.754104 17.8269 4.35369 14.1131C6.34591 12.058 7.67023 9.02782 7.94613 8.36275C8.50224 6.39343 9.97271 4.20438 12.4172 4.20438H12.4179C12.6236 4.20438 12.8314 4.2214 13.0364 4.25468C14.107 4.42854 15.0428 5.06476 15.7115 6.02205C16.4331 5.09583 17.134 4.359 17.7682 3.94323C18.7242 3.31737 19.6794 3 20.6081 3ZM20.6081 5.95917C20.2427 5.95917 19.7963 6.1197 19.3039 6.44225C17.7754 7.44319 14.8258 12.6772 13.7458 14.7131C13.3839 15.3952 12.7655 15.6837 12.2086 15.6837C11.1036 15.6837 10.2408 14.5497 12.1076 13.1085C14.9146 10.9402 13.9299 7.39584 12.5898 7.1776C12.5311 7.16799 12.4731 7.16355 12.4172 7.16355C11.1989 7.16355 10.6615 9.33114 10.6615 9.33114C10.6615 9.33114 9.0863 13.4148 6.38031 16.206C3.67434 18.998 3.5346 21.2388 5.50675 24.2246C6.85185 26.2606 9.42666 26.8753 12.0653 26.8753C14.8021 26.8753 17.6077 26.2139 19.1799 25.793C19.2574 25.7723 28.8193 22.984 27.6081 20.6107C27.4046 20.212 27.0693 20.0522 26.6471 20.0522C24.9416 20.0522 21.8393 22.6726 20.5057 22.6726C20.2076 22.6726 19.9976 22.5416 19.9116 22.222C19.3433 20.1173 28.552 19.2325 27.7758 16.1839C27.639 15.6445 27.2677 15.4256 26.746 15.4263C24.4923 15.4263 19.4358 19.5181 18.3759 19.5181C18.2949 19.5181 18.2368 19.4937 18.2053 19.4419C17.6743 18.557 17.9653 17.9394 21.7082 15.6009C25.4511 13.2617 28.0783 11.8545 26.5841 10.1752C26.4121 9.98141 26.1684 9.8956 25.8725 9.8956C23.6001 9.89634 18.2311 14.9403 18.2311 14.9403C18.2311 14.9403 16.7821 16.496 15.9057 16.496C15.7043 16.496 15.533 16.4139 15.4169 16.2112C14.7956 15.1296 21.1879 10.1286 21.5484 8.06535C21.7928 6.66715 21.3771 5.95917 20.6081 5.95917Z" fill="#FF9D00"></path>
|
3 |
+
<path d="M5.50686 24.2246C3.53472 21.2387 3.67446 18.9979 6.38043 16.206C9.08641 13.4147 10.6615 9.33111 10.6615 9.33111C10.6615 9.33111 11.2499 6.95933 12.59 7.17757C13.93 7.39581 14.9139 10.9401 12.1069 13.1084C9.29997 15.276 12.6659 16.7489 13.7459 14.713C14.8258 12.6772 17.7747 7.44316 19.304 6.44221C20.8326 5.44128 21.9089 6.00204 21.5484 8.06532C21.188 10.1286 14.795 15.1295 15.4171 16.2118C16.0391 17.2934 18.2312 14.9402 18.2312 14.9402C18.2312 14.9402 25.0907 8.49588 26.5842 10.1752C28.0776 11.8545 25.4512 13.2616 21.7082 15.6008C17.9646 17.9393 17.6744 18.557 18.2054 19.4418C18.7372 20.3266 26.9998 13.1351 27.7759 16.1838C28.5513 19.2324 19.3434 20.1173 19.9117 22.2219C20.48 24.3274 26.3979 18.2382 27.6082 20.6107C28.8193 22.9839 19.2574 25.7722 19.18 25.7929C16.0914 26.62 8.24723 28.3726 5.50686 24.2246Z" fill="#FFD21E"></path>
|
4 |
+
</svg>"""
|
5 |
+
|
6 |
+
loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin"
|
7 |
+
style="color: #ffffff;
|
8 |
+
"
|
9 |
+
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
|
10 |
+
|
11 |
+
share_js = """async () => {
|
12 |
+
async function uploadFile(file){
|
13 |
+
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
14 |
+
const response = await fetch(UPLOAD_URL, {
|
15 |
+
method: 'POST',
|
16 |
+
headers: {
|
17 |
+
'Content-Type': file.type,
|
18 |
+
'X-Requested-With': 'XMLHttpRequest',
|
19 |
+
},
|
20 |
+
body: file, /// <- File inherits from Blob
|
21 |
+
});
|
22 |
+
const url = await response.text();
|
23 |
+
return url;
|
24 |
+
}
|
25 |
+
const gradioEl = document.querySelector('body > gradio-app');
|
26 |
+
const imgEls = gradioEl.querySelectorAll('#gallery img');
|
27 |
+
const promptTxt = gradioEl.querySelector('#prompt-text-input input').value;
|
28 |
+
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
29 |
+
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
30 |
+
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
31 |
+
if(!imgEls.length){
|
32 |
+
return;
|
33 |
+
};
|
34 |
+
shareBtnEl.style.pointerEvents = 'none';
|
35 |
+
shareIconEl.style.display = 'none';
|
36 |
+
loadingIconEl.style.removeProperty('display');
|
37 |
+
const files = await Promise.all(
|
38 |
+
[...imgEls].map(async (imgEl) => {
|
39 |
+
const res = await fetch(imgEl.src);
|
40 |
+
const blob = await res.blob();
|
41 |
+
const imgId = Date.now() % 200;
|
42 |
+
const fileName = `diffuse-the-rest-${{imgId}}.png`;
|
43 |
+
return new File([blob], fileName, { type: 'image/png' });
|
44 |
+
})
|
45 |
+
);
|
46 |
+
const urls = await Promise.all(files.map((f) => uploadFile(f)));
|
47 |
+
const htmlImgs = urls.map(url => `<img src='${url}' width='400' height='400'>`);
|
48 |
+
const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
|
49 |
+
${htmlImgs.join(`\n`)}
|
50 |
+
</div>`;
|
51 |
+
const params = new URLSearchParams({
|
52 |
+
title: promptTxt,
|
53 |
+
description: descriptionMd,
|
54 |
+
});
|
55 |
+
const paramsStr = params.toString();
|
56 |
+
window.open(`https://huggingface.co/spaces/stabilityai/stable-diffusion/discussions/new?${paramsStr}`, '_blank');
|
57 |
+
shareBtnEl.style.removeProperty('pointer-events');
|
58 |
+
shareIconEl.style.removeProperty('display');
|
59 |
+
loadingIconEl.style.display = 'none';
|
60 |
+
}"""
|
style.css
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.gradio-container {
|
2 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
3 |
+
}
|
4 |
+
.gr-button {
|
5 |
+
color: white;
|
6 |
+
/* border-color: black; */
|
7 |
+
/* background: black; */
|
8 |
+
background: rgb(60, 145, 238);
|
9 |
+
}
|
10 |
+
/* input[type='range'] {
|
11 |
+
accent-color: rgb(60, 145, 238);
|
12 |
+
}
|
13 |
+
.dark input[type='range'] {
|
14 |
+
accent-color: #dfdfdf;
|
15 |
+
} */
|
16 |
+
.container {
|
17 |
+
max-width: 900px;
|
18 |
+
margin: auto;
|
19 |
+
padding-top: 1.5rem;
|
20 |
+
}
|
21 |
+
#gallery {
|
22 |
+
min-height: 22rem;
|
23 |
+
margin-bottom: 15px;
|
24 |
+
margin-left: auto;
|
25 |
+
margin-right: auto;
|
26 |
+
border-bottom-right-radius: .5rem !important;
|
27 |
+
border-bottom-left-radius: .5rem !important;
|
28 |
+
}
|
29 |
+
#gallery>div>.h-full {
|
30 |
+
min-height: 20rem;
|
31 |
+
}
|
32 |
+
.details:hover {
|
33 |
+
text-decoration: underline;
|
34 |
+
}
|
35 |
+
.gr-button {
|
36 |
+
white-space: nowrap;
|
37 |
+
}
|
38 |
+
/* .gr-button:focus {
|
39 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
40 |
+
outline: none;
|
41 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
42 |
+
--tw-border-opacity: 1;
|
43 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
44 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
45 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
46 |
+
--tw-ring-opacity: .5;
|
47 |
+
} */
|
48 |
+
.footer {
|
49 |
+
margin-bottom: 45px;
|
50 |
+
margin-top: 20px;
|
51 |
+
/* text-align: center; */
|
52 |
+
border-bottom: 1px solid #e5e5e5;
|
53 |
+
}
|
54 |
+
.footer>p {
|
55 |
+
font-size: .8rem;
|
56 |
+
display: inline-block;
|
57 |
+
padding: 0 10px;
|
58 |
+
transform: translateY(10px);
|
59 |
+
background: white;
|
60 |
+
}
|
61 |
+
.footer>p>h4 {
|
62 |
+
font-size: .20rem;
|
63 |
+
display: inline-block;
|
64 |
+
padding: 0 10px;
|
65 |
+
transform: translateY(10px);
|
66 |
+
background: white;
|
67 |
+
font-weight: bold;
|
68 |
+
}
|
69 |
+
.dark .footer {
|
70 |
+
/* border-color: #303030; */
|
71 |
+
border-color: rgb(60, 145, 238);
|
72 |
+
}
|
73 |
+
.dark .footer>p {
|
74 |
+
/* background: #0b0f19; */
|
75 |
+
background: rgb(60, 145, 238);
|
76 |
+
}
|
77 |
+
.prompt h4{
|
78 |
+
margin: 1.25em 0 .25em 0;
|
79 |
+
font-weight: bold;
|
80 |
+
font-size: 115%;
|
81 |
+
}
|
ui_functions.py
ADDED
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image, ImageFont, ImageDraw, ImageFilter, ImageOps
|
4 |
+
from io import BytesIO
|
5 |
+
import base64
|
6 |
+
import re
|
7 |
+
|
8 |
+
|
9 |
+
def change_image_editor_mode(choice, cropped_image, masked_image, resize_mode, width, height):
|
10 |
+
if choice == "Mask":
|
11 |
+
update_image_result = update_image_mask(cropped_image, resize_mode, width, height)
|
12 |
+
return [gr.update(visible=False), update_image_result, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)]
|
13 |
+
|
14 |
+
update_image_result = update_image_mask(masked_image["image"] if masked_image is not None else None, resize_mode, width, height)
|
15 |
+
return [update_image_result, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
|
16 |
+
|
17 |
+
def update_image_mask(cropped_image, resize_mode, width, height):
|
18 |
+
resized_cropped_image = resize_image(resize_mode, cropped_image, width, height) if cropped_image else None
|
19 |
+
return gr.update(value=resized_cropped_image, visible=True)
|
20 |
+
|
21 |
+
def toggle_options_gfpgan(selection):
|
22 |
+
if 0 in selection:
|
23 |
+
return gr.update(visible=True)
|
24 |
+
else:
|
25 |
+
return gr.update(visible=False)
|
26 |
+
|
27 |
+
def toggle_options_upscalers(selection):
|
28 |
+
if 1 in selection:
|
29 |
+
return gr.update(visible=True)
|
30 |
+
else:
|
31 |
+
return gr.update(visible=False)
|
32 |
+
|
33 |
+
def toggle_options_realesrgan(selection):
|
34 |
+
if selection == 0 or selection == 1 or selection == 3:
|
35 |
+
return gr.update(visible=True)
|
36 |
+
else:
|
37 |
+
return gr.update(visible=False)
|
38 |
+
|
39 |
+
def toggle_options_gobig(selection):
|
40 |
+
if selection == 1:
|
41 |
+
#print(selection)
|
42 |
+
return gr.update(visible=True)
|
43 |
+
if selection == 3:
|
44 |
+
return gr.update(visible=True)
|
45 |
+
else:
|
46 |
+
return gr.update(visible=False)
|
47 |
+
|
48 |
+
def toggle_options_ldsr(selection):
|
49 |
+
if selection == 2 or selection == 3:
|
50 |
+
return gr.update(visible=True)
|
51 |
+
else:
|
52 |
+
return gr.update(visible=False)
|
53 |
+
|
54 |
+
def increment_down(value):
|
55 |
+
return value - 1
|
56 |
+
|
57 |
+
def increment_up(value):
|
58 |
+
return value + 1
|
59 |
+
|
60 |
+
def copy_img_to_lab(img):
|
61 |
+
try:
|
62 |
+
image_data = re.sub('^data:image/.+;base64,', '', img)
|
63 |
+
processed_image = Image.open(BytesIO(base64.b64decode(image_data)))
|
64 |
+
tab_update = gr.update(selected='imgproc_tab')
|
65 |
+
img_update = gr.update(value=processed_image)
|
66 |
+
return processed_image, tab_update,
|
67 |
+
except IndexError:
|
68 |
+
return [None, None]
|
69 |
+
def copy_img_params_to_lab(params):
|
70 |
+
try:
|
71 |
+
prompt = params[0][0].replace('\n', ' ').replace('\r', '')
|
72 |
+
seed = int(params[1][1])
|
73 |
+
steps = int(params[7][1])
|
74 |
+
cfg_scale = float(params[9][1])
|
75 |
+
sampler = params[11][1]
|
76 |
+
return prompt,seed,steps,cfg_scale,sampler
|
77 |
+
except IndexError:
|
78 |
+
return [None, None]
|
79 |
+
def copy_img_to_input(img):
|
80 |
+
try:
|
81 |
+
image_data = re.sub('^data:image/.+;base64,', '', img)
|
82 |
+
processed_image = Image.open(BytesIO(base64.b64decode(image_data)))
|
83 |
+
tab_update = gr.update(selected='img2img_tab')
|
84 |
+
img_update = gr.update(value=processed_image)
|
85 |
+
return processed_image, processed_image , tab_update
|
86 |
+
except IndexError:
|
87 |
+
return [None, None]
|
88 |
+
|
89 |
+
def copy_img_to_edit(img):
|
90 |
+
try:
|
91 |
+
image_data = re.sub('^data:image/.+;base64,', '', img)
|
92 |
+
processed_image = Image.open(BytesIO(base64.b64decode(image_data)))
|
93 |
+
tab_update = gr.update(selected='img2img_tab')
|
94 |
+
img_update = gr.update(value=processed_image)
|
95 |
+
mode_update = gr.update(value='Crop')
|
96 |
+
return processed_image, tab_update, mode_update
|
97 |
+
except IndexError:
|
98 |
+
return [None, None]
|
99 |
+
|
100 |
+
def copy_img_to_mask(img):
|
101 |
+
try:
|
102 |
+
image_data = re.sub('^data:image/.+;base64,', '', img)
|
103 |
+
processed_image = Image.open(BytesIO(base64.b64decode(image_data)))
|
104 |
+
tab_update = gr.update(selected='img2img_tab')
|
105 |
+
img_update = gr.update(value=processed_image)
|
106 |
+
mode_update = gr.update(value='Mask')
|
107 |
+
return processed_image, tab_update, mode_update
|
108 |
+
except IndexError:
|
109 |
+
return [None, None]
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
def copy_img_to_upscale_esrgan(img):
|
114 |
+
tabs_update = gr.update(selected='realesrgan_tab')
|
115 |
+
image_data = re.sub('^data:image/.+;base64,', '', img)
|
116 |
+
processed_image = Image.open(BytesIO(base64.b64decode(image_data)))
|
117 |
+
return processed_image, tabs_update
|
118 |
+
|
119 |
+
|
120 |
+
help_text = """
|
121 |
+
## Mask/Crop
|
122 |
+
* Masking is not inpainting. You will probably get better results manually masking your images in photoshop instead.
|
123 |
+
* Built-in masking/cropping is very temperamental.
|
124 |
+
* It may take some time for the image to show when switching from Crop to Mask.
|
125 |
+
* If the image doesn't appear after switching to Mask, switch back to Crop and then back again to Mask
|
126 |
+
* If the mask appears distorted (the brush is weirdly shaped instead of round), switch back to Crop and then back again to Mask.
|
127 |
+
|
128 |
+
## Advanced Editor
|
129 |
+
* Click 💾 Save to send your editor changes to the img2img workflow
|
130 |
+
* Click ❌ Clear to discard your editor changes
|
131 |
+
|
132 |
+
If anything breaks, try switching modes again, switch tabs, clear the image, or reload.
|
133 |
+
"""
|
134 |
+
|
135 |
+
def resize_image(resize_mode, im, width, height):
|
136 |
+
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
|
137 |
+
if resize_mode == 0:
|
138 |
+
res = im.resize((width, height), resample=LANCZOS)
|
139 |
+
elif resize_mode == 1:
|
140 |
+
ratio = width / height
|
141 |
+
src_ratio = im.width / im.height
|
142 |
+
|
143 |
+
src_w = width if ratio > src_ratio else im.width * height // im.height
|
144 |
+
src_h = height if ratio <= src_ratio else im.height * width // im.width
|
145 |
+
|
146 |
+
resized = im.resize((src_w, src_h), resample=LANCZOS)
|
147 |
+
res = Image.new("RGBA", (width, height))
|
148 |
+
res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
|
149 |
+
else:
|
150 |
+
ratio = width / height
|
151 |
+
src_ratio = im.width / im.height
|
152 |
+
|
153 |
+
src_w = width if ratio < src_ratio else im.width * height // im.height
|
154 |
+
src_h = height if ratio >= src_ratio else im.height * width // im.width
|
155 |
+
|
156 |
+
resized = im.resize((src_w, src_h), resample=LANCZOS)
|
157 |
+
res = Image.new("RGBA", (width, height))
|
158 |
+
res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
|
159 |
+
|
160 |
+
if ratio < src_ratio:
|
161 |
+
fill_height = height // 2 - src_h // 2
|
162 |
+
res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
|
163 |
+
res.paste(resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)), box=(0, fill_height + src_h))
|
164 |
+
elif ratio > src_ratio:
|
165 |
+
fill_width = width // 2 - src_w // 2
|
166 |
+
res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
|
167 |
+
res.paste(resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)), box=(fill_width + src_w, 0))
|
168 |
+
|
169 |
+
return res
|
170 |
+
|
171 |
+
def update_dimensions_info(width, height):
|
172 |
+
pixel_count_formated = "{:,.0f}".format(width * height)
|
173 |
+
return f"Aspect ratio: {round(width / height, 5)}\nTotal pixel count: {pixel_count_formated}"
|
174 |
+
|
175 |
+
def get_png_nfo( image: Image ):
|
176 |
+
info_text = ""
|
177 |
+
visible = bool(image and any(image.info))
|
178 |
+
if visible:
|
179 |
+
for key,value in image.info.items():
|
180 |
+
info_text += f"{key}: {value}\n"
|
181 |
+
info_text = info_text.rstrip('\n')
|
182 |
+
return gr.Textbox.update(value=info_text, visible=visible)
|
183 |
+
|
184 |
+
def load_settings(*values):
|
185 |
+
new_settings, key_names, checkboxgroup_info = values[-3:]
|
186 |
+
values = list(values[:-3])
|
187 |
+
|
188 |
+
if new_settings:
|
189 |
+
if type(new_settings) is str:
|
190 |
+
if os.path.exists(new_settings):
|
191 |
+
with open(new_settings, "r", encoding="utf8") as f:
|
192 |
+
new_settings = yaml.safe_load(f)
|
193 |
+
elif new_settings.startswith("file://") and os.path.exists(new_settings[7:]):
|
194 |
+
with open(new_settings[7:], "r", encoding="utf8") as f:
|
195 |
+
new_settings = yaml.safe_load(f)
|
196 |
+
else:
|
197 |
+
new_settings = yaml.safe_load(new_settings)
|
198 |
+
if type(new_settings) is not dict:
|
199 |
+
new_settings = {"prompt": new_settings}
|
200 |
+
if "txt2img" in new_settings:
|
201 |
+
new_settings = new_settings["txt2img"]
|
202 |
+
target = new_settings.pop("target", "txt2img")
|
203 |
+
if target != "txt2img":
|
204 |
+
print(f"Warning: applying settings to txt2img even though {target} is specified as target.", file=sys.stderr)
|
205 |
+
|
206 |
+
skipped_settings = {}
|
207 |
+
for key in new_settings.keys():
|
208 |
+
if key in key_names:
|
209 |
+
values[key_names.index(key)] = new_settings[key]
|
210 |
+
else:
|
211 |
+
skipped_settings[key] = new_settings[key]
|
212 |
+
if skipped_settings:
|
213 |
+
print(f"Settings could not be applied: {skipped_settings}", file=sys.stderr)
|
214 |
+
|
215 |
+
# Convert lists of checkbox indices to lists of checkbox labels:
|
216 |
+
for (cbg_index, cbg_choices) in checkboxgroup_info:
|
217 |
+
values[cbg_index] = [cbg_choices[i] for i in values[cbg_index]]
|
218 |
+
|
219 |
+
return values
|