forked from lllyasviel/Fooocus
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathui_evolve.py
114 lines (105 loc) · 3.79 KB
/
ui_evolve.py
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
import gradio as gr
from shared import tokenizer
import random
import re
from random_prompt.build_dynamic_prompt import createpromptvariant
def add_evolve_tab(prompt, image_number, run_event):
def tokenize_and_randomize(prompt, strength):
all_tokens = list(tokenizer.get_vocab().keys())
tokens = tokenizer.tokenize(prompt)
res = []
for token in tokens:
if random.random() < float(strength / 100.0):
res += [
all_tokens[random.randint(0, len(all_tokens) - 3)]
] # Skip <|startoftext> & <|endoftext|>
else:
res += [token]
return tokenizer.convert_tokens_to_string(res).strip()
def randomize_words(prompt, strength):
with open("wildcards_official/words.txt", "r", encoding="utf-8") as f:
word_list = f.read().lower().splitlines()
words = re.split(r"\b", prompt)
res = []
for word in words:
if (
not word.isdigit()
and word.lower() in word_list
and random.random() < float(strength / 100.0)
):
res += [word_list[random.randint(0, len(word_list) - 1)]]
else:
res += [word]
return "".join(res).strip()
def four_evolved_prompts(prompt, mode, strength):
res = []
for i in range(4):
match mode:
case "Words":
res.append(randomize_words(prompt, strength))
case "OBP Variant":
res.append(
createpromptvariant(
prompt, max(int(strength / 10), 3), advancedprompting=False
)
)
case _: # Use "Tokens" as default
res.append(tokenize_and_randomize(prompt, strength))
return res
def evolve(
button,
mode,
strength,
prompt,
run_event,
):
prompts = prompt.split("---")
in_txt = prompts[min(int(button), len(prompts)) - 1]
res = (
four_evolved_prompts(in_txt, mode, strength)
+ [in_txt]
+ four_evolved_prompts(in_txt, mode, strength)
)
return gr.update(value="\n---\n".join(res)), gr.update(value=1), run_event + 1
with gr.Accordion(label="Evolve", open=False):
evolve_btn = {}
for x in range(0, 3):
with gr.Row():
for y in range(1, 4):
evolve_btn[3 * x + y] = gr.Button(
value=str(3 * x + y),
min_width=1,
)
with gr.Group():
evolve_modes = [
"Tokens",
"Words",
"OBP Variant",
]
evolve_mode = gr.Dropdown(
evolve_modes,
value=evolve_modes[0],
label="Mode",
)
evolve_strength = gr.Slider(
minimum=0, maximum=100, value=10, step=1, label="Evolve chance %:"
)
# with gr.Row():
# evo_help = gr.HTML(value='''
# Start with any prompt or random chunk of letters.<br>
# Click on the number of the image you like best.<br>
# Repeat.<br>
# For best result, set a static seed.<br>
# ''')
for i in range(1, 10):
evolve_btn[i].click(
evolve,
inputs=[
evolve_btn[i],
evolve_mode,
evolve_strength,
prompt,
run_event,
],
outputs=[prompt, image_number, run_event],
)