Skip to content

Commit 39ce23f

Browse files
committed
add the bitton to paste parameters into UI for txt2img, img2img, and pnginfo tabs
fixed some [send to..] buttons to work properly with all tabs
1 parent 9c92a1a commit 39ce23f

File tree

6 files changed

+236
-40
lines changed

6 files changed

+236
-40
lines changed

javascript/hints.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ titles = {
1313
"Seed": "A value that determines the output of random number generator - if you create an image with same parameters and seed as another image, you'll get the same result",
1414
"\u{1f3b2}\ufe0f": "Set seed to -1, which will cause a new random number to be used every time",
1515
"\u267b\ufe0f": "Reuse seed from last generation, mostly useful if it was randomed",
16+
"\u{1f3a8}": "Add a random artist to the prompt.",
17+
"\u2199\ufe0f": "Read generation parameters from prompt into user interface.",
1618

1719
"Inpaint a part of image": "Draw a mask over an image, and the script will regenerate the masked area with content according to prompt",
1820
"SD upscale": "Upscale image normally, split result into tiles, improve each tile using img2img, merge whole image back",
@@ -48,8 +50,6 @@ titles = {
4850
"Tiling": "Produce an image that can be tiled.",
4951
"Tile overlap": "For SD upscale, how much overlap in pixels should there be between tiles. Tiles overlap so that when they are merged back into one picture, there is no clearly visible seam.",
5052

51-
"Roll": "Add a random artist to the prompt.",
52-
5353
"Variation seed": "Seed of a different picture to be mixed into the generation.",
5454
"Variation strength": "How strong of a variation to produce. At 0, there will be no effect. At 1, you will get the complete picture with variation seed (except for ancestral samplers, where you will just get something).",
5555
"Resize seed from height": "Make an attempt to produce a picture similar to what would have been produced with same seed at specified resolution",

javascript/ui.js

+46-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,57 @@ function extract_image_from_gallery(gallery){
2525
return gallery[index];
2626
}
2727

28-
function extract_image_from_gallery_img2img(gallery){
28+
function args_to_array(args){
29+
res = []
30+
for(var i=0;i<args.length;i++){
31+
res.push(args[i])
32+
}
33+
return res
34+
}
35+
36+
function switch_to_txt2img(){
37+
gradioApp().querySelectorAll('button')[0].click();
38+
39+
return args_to_array(arguments);
40+
}
41+
42+
function switch_to_img2img_img2img(){
43+
gradioApp().querySelectorAll('button')[1].click();
44+
gradioApp().getElementById('mode_img2img').querySelectorAll('button')[0].click();
45+
46+
return args_to_array(arguments);
47+
}
48+
49+
function switch_to_img2img_inpaint(){
2950
gradioApp().querySelectorAll('button')[1].click();
51+
gradioApp().getElementById('mode_img2img').querySelectorAll('button')[1].click();
52+
53+
return args_to_array(arguments);
54+
}
55+
56+
function switch_to_extras(){
57+
gradioApp().querySelectorAll('button')[2].click();
58+
59+
return args_to_array(arguments);
60+
}
61+
62+
function extract_image_from_gallery_txt2img(gallery){
63+
switch_to_txt2img()
64+
return extract_image_from_gallery(gallery);
65+
}
66+
67+
function extract_image_from_gallery_img2img(gallery){
68+
switch_to_img2img_img2img()
69+
return extract_image_from_gallery(gallery);
70+
}
71+
72+
function extract_image_from_gallery_inpaint(gallery){
73+
switch_to_img2img_inpaint()
3074
return extract_image_from_gallery(gallery);
3175
}
3276

3377
function extract_image_from_gallery_extras(gallery){
34-
gradioApp().querySelectorAll('button')[2].click();
78+
switch_to_extras()
3579
return extract_image_from_gallery(gallery);
3680
}
3781

modules/extras.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def run_pnginfo(image):
102102
return '', '', ''
103103

104104
items = image.info
105+
geninfo = ''
105106

106107
if "exif" in image.info:
107108
exif = piexif.load(image.info["exif"])
@@ -111,13 +112,14 @@ def run_pnginfo(image):
111112
except ValueError:
112113
exif_comment = exif_comment.decode('utf8', errors="ignore")
113114

114-
115115
items['exif comment'] = exif_comment
116+
geninfo = exif_comment
116117

117118
for field in ['jfif', 'jfif_version', 'jfif_unit', 'jfif_density', 'dpi', 'exif',
118119
'loop', 'background', 'timestamp', 'duration']:
119120
items.pop(field, None)
120121

122+
geninfo = items.get('parameters', geninfo)
121123

122124
info = ''
123125
for key, text in items.items():
@@ -132,4 +134,4 @@ def run_pnginfo(image):
132134
message = "Nothing found in the image."
133135
info = f"<div><p>{message}<p></div>"
134136

135-
return '', '', info
137+
return '', geninfo, info
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from collections import namedtuple
2+
import re
3+
import gradio as gr
4+
5+
re_param = re.compile(r"\s*([\w ]+):\s*([^,]+)(?:,|$)")
6+
re_imagesize = re.compile(r"^(\d+)x(\d+)$")
7+
8+
9+
def parse_generation_parameters(x: str):
10+
"""parses generation parameters string, the one you see in text field under the picture in UI:
11+
```
12+
girl with an artist's beret, determined, blue eyes, desert scene, computer monitors, heavy makeup, by Alphonse Mucha and Charlie Bowater, ((eyeshadow)), (coquettish), detailed, intricate
13+
Negative prompt: ugly, fat, obese, chubby, (((deformed))), [blurry], bad anatomy, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), messy drawing
14+
Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model hash: 45dee52b
15+
```
16+
17+
returns a dict with field values
18+
"""
19+
20+
res = {}
21+
22+
prompt = ""
23+
negative_prompt = ""
24+
25+
done_with_prompt = False
26+
27+
*lines, lastline = x.strip().split("\n")
28+
for i, line in enumerate(lines):
29+
line = line.strip()
30+
if line.startswith("Negative prompt:"):
31+
done_with_prompt = True
32+
line = line[16:].strip()
33+
34+
if done_with_prompt:
35+
negative_prompt += line
36+
else:
37+
prompt += line
38+
39+
if len(prompt) > 0:
40+
res["Prompt"] = prompt
41+
42+
if len(negative_prompt) > 0:
43+
res["Negative prompt"] = negative_prompt
44+
45+
for k, v in re_param.findall(lastline):
46+
m = re_imagesize.match(v)
47+
if m is not None:
48+
res[k+"-1"] = m.group(1)
49+
res[k+"-2"] = m.group(2)
50+
else:
51+
res[k] = v
52+
53+
return res
54+
55+
56+
def connect_paste(button, d, input_comp, js=None):
57+
items = []
58+
outputs = []
59+
60+
def paste_func(prompt):
61+
params = parse_generation_parameters(prompt)
62+
res = []
63+
64+
for key, output in zip(items, outputs):
65+
v = params.get(key, None)
66+
67+
if v is None:
68+
res.append(gr.update())
69+
else:
70+
try:
71+
valtype = type(output.value)
72+
val = valtype(v)
73+
res.append(gr.update(value=val))
74+
except Exception:
75+
res.append(gr.update())
76+
77+
return res
78+
79+
for k, v in d.items():
80+
items.append(k)
81+
outputs.append(v)
82+
83+
button.click(
84+
fn=paste_func,
85+
_js=js,
86+
inputs=[input_comp],
87+
outputs=outputs,
88+
)

0 commit comments

Comments
 (0)