Spaces:
Runtime error
Runtime error
Kohaku-Blueleaf
commited on
Commit
Β·
4f72b76
1
Parent(s):
ff3b374
Modify introductions
Browse files
app.py
CHANGED
|
@@ -30,7 +30,7 @@ from kgen.formatter import seperate_tags, apply_format
|
|
| 30 |
from kgen.generate import generate
|
| 31 |
|
| 32 |
from diff import load_model, encode_prompts
|
| 33 |
-
from meta import DEFAULT_NEGATIVE_PROMPT
|
| 34 |
|
| 35 |
|
| 36 |
sdxl_pipe = load_model()
|
|
@@ -41,17 +41,6 @@ models.load_model(
|
|
| 41 |
subfolder="dan-cc-coyo_epoch2",
|
| 42 |
)
|
| 43 |
generate(max_new_tokens=4)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
DEFAULT_FORMAT = """<|special|>, <|characters|>, <|copyrights|>,
|
| 47 |
-
<|artist|>,
|
| 48 |
-
|
| 49 |
-
<|general|>,
|
| 50 |
-
|
| 51 |
-
<|extended|>.
|
| 52 |
-
|
| 53 |
-
<|quality|>, <|meta|>, <|rating|>
|
| 54 |
-
""".strip()
|
| 55 |
DEFAULT_TAGS = """
|
| 56 |
1girl, king halo (umamusume), umamusume,
|
| 57 |
ningen mame, ciloranko, ogipote, misu kasumi,
|
|
@@ -103,6 +92,7 @@ def generate(
|
|
| 103 |
nl_prompt,
|
| 104 |
black_list,
|
| 105 |
temp,
|
|
|
|
| 106 |
target_length,
|
| 107 |
top_p,
|
| 108 |
min_p,
|
|
@@ -110,6 +100,7 @@ def generate(
|
|
| 110 |
seed,
|
| 111 |
escape_brackets,
|
| 112 |
):
|
|
|
|
| 113 |
titpop.BAN_TAGS = [t.strip() for t in black_list.split(",") if t.strip()]
|
| 114 |
generation_setting = {
|
| 115 |
"seed": seed,
|
|
@@ -120,11 +111,11 @@ def generate(
|
|
| 120 |
}
|
| 121 |
inputs = seperate_tags(tags.split(","))
|
| 122 |
if nl_prompt:
|
| 123 |
-
if "<|extended|>" in
|
| 124 |
inputs["extended"] = nl_prompt
|
| 125 |
-
elif "<|generated|>" in
|
| 126 |
inputs["generated"] = nl_prompt
|
| 127 |
-
input_prompt = apply_format(inputs,
|
| 128 |
if escape_brackets:
|
| 129 |
input_prompt = re.sub(r"([()\[\]])", r"\\\1", input_prompt)
|
| 130 |
|
|
@@ -132,13 +123,13 @@ def generate(
|
|
| 132 |
seperate_tags(tags.split(",")),
|
| 133 |
nl_prompt,
|
| 134 |
tag_length_target=target_length,
|
| 135 |
-
generate_extra_nl_prompt="<|generated|>" in
|
| 136 |
)
|
| 137 |
t0 = time()
|
| 138 |
for result, timing in titpop.titpop_runner_generator(
|
| 139 |
meta, operations, general, nl_prompt, **generation_setting
|
| 140 |
):
|
| 141 |
-
result = apply_format(result,
|
| 142 |
if escape_brackets:
|
| 143 |
result = re.sub(r"([()\[\]])", r"\\\1", result)
|
| 144 |
timing["total"] = time() - t0
|
|
@@ -153,11 +144,11 @@ def generate_image(
|
|
| 153 |
prompt2,
|
| 154 |
):
|
| 155 |
torch.cuda.empty_cache()
|
|
|
|
| 156 |
prompt_embeds, negative_prompt_embeds, pooled_embeds2, neg_pooled_embeds2 = (
|
| 157 |
-
encode_prompts(sdxl_pipe,
|
| 158 |
)
|
| 159 |
-
|
| 160 |
-
result = sdxl_pipe(
|
| 161 |
prompt_embeds=prompt_embeds,
|
| 162 |
negative_prompt_embeds=negative_prompt_embeds,
|
| 163 |
pooled_prompt_embeds=pooled_embeds2,
|
|
@@ -167,11 +158,13 @@ def generate_image(
|
|
| 167 |
height=1024,
|
| 168 |
guidance_scale=6.0,
|
| 169 |
).images[0]
|
|
|
|
|
|
|
| 170 |
prompt_embeds, negative_prompt_embeds, pooled_embeds2, neg_pooled_embeds2 = (
|
| 171 |
-
encode_prompts(sdxl_pipe,
|
| 172 |
)
|
| 173 |
set_seed(seed)
|
| 174 |
-
|
| 175 |
prompt_embeds=prompt_embeds,
|
| 176 |
negative_prompt_embeds=negative_prompt_embeds,
|
| 177 |
pooled_prompt_embeds=pooled_embeds2,
|
|
@@ -182,19 +175,39 @@ def generate_image(
|
|
| 182 |
guidance_scale=6.0,
|
| 183 |
).images[0]
|
| 184 |
torch.cuda.empty_cache()
|
| 185 |
-
|
| 186 |
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
| 189 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 190 |
-
gr.Markdown("""# TITPOP DEMO""")
|
| 191 |
with gr.Accordion("Introduction and Instructions", open=False):
|
| 192 |
gr.Markdown(
|
| 193 |
"""
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
"""
|
| 199 |
)
|
| 200 |
with gr.Row():
|
|
@@ -203,7 +216,7 @@ TITPOP
|
|
| 203 |
with gr.Column(scale=3):
|
| 204 |
tags_input = gr.TextArea(
|
| 205 |
label="Danbooru Tags",
|
| 206 |
-
lines=
|
| 207 |
show_copy_button=True,
|
| 208 |
interactive=True,
|
| 209 |
value=DEFAULT_TAGS,
|
|
@@ -211,7 +224,7 @@ TITPOP
|
|
| 211 |
)
|
| 212 |
nl_prompt_input = gr.Textbox(
|
| 213 |
label="Natural Language Prompt",
|
| 214 |
-
lines=
|
| 215 |
show_copy_button=True,
|
| 216 |
interactive=True,
|
| 217 |
value=DEFAULT_NL,
|
|
@@ -225,6 +238,11 @@ TITPOP
|
|
| 225 |
placeholder="Enter tag/nl black list here",
|
| 226 |
)
|
| 227 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
target_length = gr.Dropdown(
|
| 229 |
label="Target Length",
|
| 230 |
choices=["very_short", "short", "long", "very_long"],
|
|
@@ -293,6 +311,7 @@ TITPOP
|
|
| 293 |
nl_prompt_input,
|
| 294 |
black_list,
|
| 295 |
temp,
|
|
|
|
| 296 |
target_length,
|
| 297 |
top_p,
|
| 298 |
min_p,
|
|
@@ -308,8 +327,13 @@ TITPOP
|
|
| 308 |
],
|
| 309 |
queue=True,
|
| 310 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
gen_img.click(
|
| 312 |
-
|
| 313 |
[seed, result, input_prompt],
|
| 314 |
[img1, img2, submit],
|
| 315 |
queue=True,
|
|
|
|
| 30 |
from kgen.generate import generate
|
| 31 |
|
| 32 |
from diff import load_model, encode_prompts
|
| 33 |
+
from meta import DEFAULT_NEGATIVE_PROMPT, DEFAULT_FORMAT
|
| 34 |
|
| 35 |
|
| 36 |
sdxl_pipe = load_model()
|
|
|
|
| 41 |
subfolder="dan-cc-coyo_epoch2",
|
| 42 |
)
|
| 43 |
generate(max_new_tokens=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
DEFAULT_TAGS = """
|
| 45 |
1girl, king halo (umamusume), umamusume,
|
| 46 |
ningen mame, ciloranko, ogipote, misu kasumi,
|
|
|
|
| 92 |
nl_prompt,
|
| 93 |
black_list,
|
| 94 |
temp,
|
| 95 |
+
output_format,
|
| 96 |
target_length,
|
| 97 |
top_p,
|
| 98 |
min_p,
|
|
|
|
| 100 |
seed,
|
| 101 |
escape_brackets,
|
| 102 |
):
|
| 103 |
+
default_format = DEFAULT_FORMAT[output_format]
|
| 104 |
titpop.BAN_TAGS = [t.strip() for t in black_list.split(",") if t.strip()]
|
| 105 |
generation_setting = {
|
| 106 |
"seed": seed,
|
|
|
|
| 111 |
}
|
| 112 |
inputs = seperate_tags(tags.split(","))
|
| 113 |
if nl_prompt:
|
| 114 |
+
if "<|extended|>" in default_format:
|
| 115 |
inputs["extended"] = nl_prompt
|
| 116 |
+
elif "<|generated|>" in default_format:
|
| 117 |
inputs["generated"] = nl_prompt
|
| 118 |
+
input_prompt = apply_format(inputs, default_format)
|
| 119 |
if escape_brackets:
|
| 120 |
input_prompt = re.sub(r"([()\[\]])", r"\\\1", input_prompt)
|
| 121 |
|
|
|
|
| 123 |
seperate_tags(tags.split(",")),
|
| 124 |
nl_prompt,
|
| 125 |
tag_length_target=target_length,
|
| 126 |
+
generate_extra_nl_prompt="<|generated|>" in default_format or not nl_prompt,
|
| 127 |
)
|
| 128 |
t0 = time()
|
| 129 |
for result, timing in titpop.titpop_runner_generator(
|
| 130 |
meta, operations, general, nl_prompt, **generation_setting
|
| 131 |
):
|
| 132 |
+
result = apply_format(result, default_format)
|
| 133 |
if escape_brackets:
|
| 134 |
result = re.sub(r"([()\[\]])", r"\\\1", result)
|
| 135 |
timing["total"] = time() - t0
|
|
|
|
| 144 |
prompt2,
|
| 145 |
):
|
| 146 |
torch.cuda.empty_cache()
|
| 147 |
+
set_seed(seed)
|
| 148 |
prompt_embeds, negative_prompt_embeds, pooled_embeds2, neg_pooled_embeds2 = (
|
| 149 |
+
encode_prompts(sdxl_pipe, prompt2, DEFAULT_NEGATIVE_PROMPT)
|
| 150 |
)
|
| 151 |
+
result2 = sdxl_pipe(
|
|
|
|
| 152 |
prompt_embeds=prompt_embeds,
|
| 153 |
negative_prompt_embeds=negative_prompt_embeds,
|
| 154 |
pooled_prompt_embeds=pooled_embeds2,
|
|
|
|
| 158 |
height=1024,
|
| 159 |
guidance_scale=6.0,
|
| 160 |
).images[0]
|
| 161 |
+
yield result2, None
|
| 162 |
+
|
| 163 |
prompt_embeds, negative_prompt_embeds, pooled_embeds2, neg_pooled_embeds2 = (
|
| 164 |
+
encode_prompts(sdxl_pipe, prompt, DEFAULT_NEGATIVE_PROMPT)
|
| 165 |
)
|
| 166 |
set_seed(seed)
|
| 167 |
+
result = sdxl_pipe(
|
| 168 |
prompt_embeds=prompt_embeds,
|
| 169 |
negative_prompt_embeds=negative_prompt_embeds,
|
| 170 |
pooled_prompt_embeds=pooled_embeds2,
|
|
|
|
| 175 |
guidance_scale=6.0,
|
| 176 |
).images[0]
|
| 177 |
torch.cuda.empty_cache()
|
| 178 |
+
yield result2, result
|
| 179 |
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 183 |
with gr.Accordion("Introduction and Instructions", open=False):
|
| 184 |
gr.Markdown(
|
| 185 |
"""
|
| 186 |
+
## TITPOP Demo
|
| 187 |
+
### What is this
|
| 188 |
+
TITPOP is a tool to extend, generate, refine the input prompt for T2I models.
|
| 189 |
+
<br>It can work on both Danbooru tags and Natural Language. Which means you can use it on almost all the existed T2I models.
|
| 190 |
+
<br>You can take it as "pro max" version of [DTG](https://huggingface.co/KBlueLeaf/DanTagGen-delta-rev2)
|
| 191 |
+
|
| 192 |
+
### How to use this demo
|
| 193 |
+
1. Enter your tags(optional): put the desired tags into "danboru tags" box
|
| 194 |
+
2. Enter your NL Prompt(optional): put the desired natural language prompt into "Natural Language Prompt" box
|
| 195 |
+
3. Enter your black list(optional): put the desired black list into "black list" box
|
| 196 |
+
4. Adjust the settings: length, temp, top_p, min_p, top_k, seed ...
|
| 197 |
+
4. Click "TITPOP" button: you will see refined prompt on "result" box
|
| 198 |
+
5. If you like the result, click "Generate Image From Result" button
|
| 199 |
+
* You will see 2 generated images, left one is based on your prompt, right one is based on refined prompt
|
| 200 |
+
* The backend is diffusers, there are no weighting mechanism, so Escape Brackets is default to False
|
| 201 |
|
| 202 |
+
### Why inference code is private? When will it be open sourced?
|
| 203 |
+
1. This model/tool is still under development, currently is early Alpha version.
|
| 204 |
+
2. I'm doing some research and projects based on this.
|
| 205 |
+
3. The model is released under CC-BY-NC-ND License currently. If you have interest, you can implement inference by yourself.
|
| 206 |
+
4. Once the project/research are done, I will open source all these models/codes with Apache2 license.
|
| 207 |
+
|
| 208 |
+
### Notification
|
| 209 |
+
**ITPOP is NOT a T2I model. It is Prompt Gen, or, Text-to-Text model.
|
| 210 |
+
<br>The generated image is come from [Kohaku-XL-Zeta](https://huggingface.co/KBlueLeaf/Kohaku-XL-Zeta) model**
|
| 211 |
"""
|
| 212 |
)
|
| 213 |
with gr.Row():
|
|
|
|
| 216 |
with gr.Column(scale=3):
|
| 217 |
tags_input = gr.TextArea(
|
| 218 |
label="Danbooru Tags",
|
| 219 |
+
lines=7,
|
| 220 |
show_copy_button=True,
|
| 221 |
interactive=True,
|
| 222 |
value=DEFAULT_TAGS,
|
|
|
|
| 224 |
)
|
| 225 |
nl_prompt_input = gr.Textbox(
|
| 226 |
label="Natural Language Prompt",
|
| 227 |
+
lines=7,
|
| 228 |
show_copy_button=True,
|
| 229 |
interactive=True,
|
| 230 |
value=DEFAULT_NL,
|
|
|
|
| 238 |
placeholder="Enter tag/nl black list here",
|
| 239 |
)
|
| 240 |
with gr.Column(scale=2):
|
| 241 |
+
output_format = gr.Dropdown(
|
| 242 |
+
label="Output Format",
|
| 243 |
+
choices=list(DEFAULT_FORMAT.keys()),
|
| 244 |
+
value="Both, tag first (recommend)"
|
| 245 |
+
)
|
| 246 |
target_length = gr.Dropdown(
|
| 247 |
label="Target Length",
|
| 248 |
choices=["very_short", "short", "long", "very_long"],
|
|
|
|
| 311 |
nl_prompt_input,
|
| 312 |
black_list,
|
| 313 |
temp,
|
| 314 |
+
output_format,
|
| 315 |
target_length,
|
| 316 |
top_p,
|
| 317 |
min_p,
|
|
|
|
| 327 |
],
|
| 328 |
queue=True,
|
| 329 |
)
|
| 330 |
+
|
| 331 |
+
def generate_image_wrapper(seed, result, input_prompt):
|
| 332 |
+
for img1, img2 in generate_image(seed, result, input_prompt):
|
| 333 |
+
yield img1, img2, gr.update(interactive=False)
|
| 334 |
+
yield img1, img2, gr.update(interactive=True)
|
| 335 |
gen_img.click(
|
| 336 |
+
generate_image_wrapper,
|
| 337 |
[seed, result, input_prompt],
|
| 338 |
[img1, img2, submit],
|
| 339 |
queue=True,
|
meta.py
CHANGED
|
@@ -8,47 +8,52 @@ DEFAULT_STYLE_LIST = {
|
|
| 8 |
"no style": "",
|
| 9 |
}
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
"
|
| 20 |
-
<|characters|>, <|copyrights|>,
|
| 21 |
<|artist|>,
|
| 22 |
|
| 23 |
-
<|general|>,
|
| 24 |
|
| 25 |
-
<|quality|>, <|meta|>, <|rating|>
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
<|artist|>,
|
| 29 |
|
| 30 |
-
<|general|>,
|
| 31 |
|
| 32 |
-
<|
|
| 33 |
-
|
| 34 |
-
<|
|
|
|
|
|
|
|
|
|
|
| 35 |
<|artist|>,
|
| 36 |
|
| 37 |
-
<|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
<|quality|>, <|meta|>, <|rating|>
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
<|artist|>,
|
| 43 |
|
| 44 |
-
<|
|
| 45 |
|
| 46 |
-
<|
|
| 47 |
-
}
|
| 48 |
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
multiple tails, multiple views, copyright name, watermark, artist name, signature
|
| 54 |
-
"""
|
|
|
|
| 8 |
"no style": "",
|
| 9 |
}
|
| 10 |
|
| 11 |
+
DEFAULT_NEGATIVE_PROMPT = """
|
| 12 |
+
low quality, worst quality, normal quality, text, signature, jpeg artifacts,
|
| 13 |
+
bad anatomy, old, early, mini skirt, nsfw, chibi, multiple girls, multiple boys,
|
| 14 |
+
multiple tails, multiple views, copyright name, watermark, artist name, signature
|
| 15 |
+
"""
|
|
|
|
| 16 |
|
| 17 |
+
DEFAULT_FORMAT = {
|
| 18 |
+
"tag only (DTG mode)":"""
|
| 19 |
+
<|special|>, <|characters|>, <|copyrights|>,
|
| 20 |
<|artist|>,
|
| 21 |
|
| 22 |
+
<|general|>,
|
| 23 |
|
| 24 |
+
<|quality|>, <|meta|>, <|rating|>
|
| 25 |
+
""".strip(),
|
| 26 |
+
"NL only (Tag to NL)": """<|extended|>.""".strip(),
|
| 27 |
+
"Both, tag first (recommend)": """
|
| 28 |
+
<|special|>, <|characters|>, <|copyrights|>,
|
| 29 |
<|artist|>,
|
| 30 |
|
| 31 |
+
<|general|>,
|
| 32 |
|
| 33 |
+
<|extended|>.
|
| 34 |
+
|
| 35 |
+
<|quality|>, <|meta|>, <|rating|>
|
| 36 |
+
""".strip(),
|
| 37 |
+
"Both, NL first (recommend)": """
|
| 38 |
+
<|special|>, <|characters|>, <|copyrights|>,
|
| 39 |
<|artist|>,
|
| 40 |
|
| 41 |
+
<|extended|>.
|
| 42 |
+
|
| 43 |
+
<|general|>,
|
| 44 |
|
| 45 |
+
<|quality|>, <|meta|>, <|rating|>
|
| 46 |
+
""".strip(),
|
| 47 |
+
"Both + generated NL": """
|
| 48 |
+
<|special|>, <|characters|>, <|copyrights|>,
|
| 49 |
<|artist|>,
|
| 50 |
|
| 51 |
+
<|generated|>.
|
| 52 |
|
| 53 |
+
<|general|>,
|
|
|
|
| 54 |
|
| 55 |
+
<|extended|>.
|
| 56 |
|
| 57 |
+
<|quality|>, <|meta|>, <|rating|>
|
| 58 |
+
""".strip()
|
| 59 |
+
}
|
|
|
|
|
|