Upload 13 files
Browse files- README.md +14 -12
- app.py +185 -0
- model.py +13 -0
- multit2i.py +502 -0
- requirements.txt +9 -0
- tagger/character_series_dict.csv +0 -0
- tagger/danbooru_e621.csv +0 -0
- tagger/fl2sd3longcap.py +82 -0
- tagger/output.py +16 -0
- tagger/tag_group.csv +0 -0
- tagger/tagger.py +556 -0
- tagger/utils.py +50 -0
- tagger/v2.py +260 -0
README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
-
---
|
2 |
-
title: Recent Illustrious Models
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 5.0.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: 10+ Recent SDXL Illustrious Text-to-Image Models Playground (John6666)
|
3 |
+
emoji: ๐ผ๏ธ๐ด๐
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 5.0.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
short_description: Text-to-Image
|
11 |
+
license: mit
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from model import models
|
3 |
+
from multit2i import (load_models, infer_fn, infer_rand_fn, save_gallery,
|
4 |
+
change_model, warm_model, get_model_info_md, loaded_models,
|
5 |
+
get_positive_prefix, get_positive_suffix, get_negative_prefix, get_negative_suffix,
|
6 |
+
get_recom_prompt_type, set_recom_prompt_preset, get_tag_type, randomize_seed, translate_to_en)
|
7 |
+
from tagger.tagger import (predict_tags_wd, remove_specific_prompt, convert_danbooru_to_e621_prompt,
|
8 |
+
insert_recom_prompt, compose_prompt_to_copy)
|
9 |
+
from tagger.fl2sd3longcap import predict_tags_fl2_sd3
|
10 |
+
from tagger.v2 import V2_ALL_MODELS, v2_random_prompt
|
11 |
+
from tagger.utils import (V2_ASPECT_RATIO_OPTIONS, V2_RATING_OPTIONS,
|
12 |
+
V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
|
13 |
+
|
14 |
+
max_images = 6
|
15 |
+
MAX_SEED = 2**32-1
|
16 |
+
load_models(models)
|
17 |
+
|
18 |
+
css = """
|
19 |
+
.model_info { text-align: center; }
|
20 |
+
.output { width=112px; height=112px; max_width=112px; max_height=112px; !important; }
|
21 |
+
.gallery { min_width=512px; min_height=512px; max_height=1024px; !important; }
|
22 |
+
"""
|
23 |
+
|
24 |
+
with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
|
25 |
+
with gr.Tab("Image Generator"):
|
26 |
+
with gr.Row():
|
27 |
+
with gr.Column(scale=10):
|
28 |
+
with gr.Group():
|
29 |
+
with gr.Accordion("Prompt from Image File", open=False):
|
30 |
+
tagger_image = gr.Image(label="Input image", type="pil", format="png", sources=["upload", "clipboard"], height=256)
|
31 |
+
with gr.Accordion(label="Advanced options", open=False):
|
32 |
+
with gr.Row():
|
33 |
+
tagger_general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
|
34 |
+
tagger_character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
|
35 |
+
tagger_tag_type = gr.Radio(label="Convert tags to", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
|
36 |
+
with gr.Row():
|
37 |
+
tagger_recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
|
38 |
+
tagger_keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
|
39 |
+
tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
|
40 |
+
tagger_generate_from_image = gr.Button(value="Generate Tags from Image", variant="secondary")
|
41 |
+
with gr.Accordion("Prompt Transformer", open=False):
|
42 |
+
with gr.Row():
|
43 |
+
v2_character = gr.Textbox(label="Character", placeholder="hatsune miku", scale=2)
|
44 |
+
v2_series = gr.Textbox(label="Series", placeholder="vocaloid", scale=2)
|
45 |
+
with gr.Row():
|
46 |
+
v2_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="sfw")
|
47 |
+
v2_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square", visible=False)
|
48 |
+
v2_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="long")
|
49 |
+
with gr.Row():
|
50 |
+
v2_identity = gr.Radio(label="Keep identity", info="How strictly to keep the identity of the character or subject. If you specify the detail of subject in the prompt, you should choose `strict`. Otherwise, choose `none` or `lax`. `none` is very creative but sometimes ignores the input prompt.", choices=list(V2_IDENTITY_OPTIONS), value="lax")
|
51 |
+
v2_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
|
52 |
+
v2_tag_type = gr.Radio(label="Tag Type", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru", visible=False)
|
53 |
+
v2_model = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
|
54 |
+
v2_copy = gr.Button(value="Copy to clipboard", variant="secondary", size="sm", interactive=False)
|
55 |
+
random_prompt = gr.Button(value="Extend ๐ฒ", variant="secondary")
|
56 |
+
prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
|
57 |
+
with gr.Accordion("Advanced options", open=False):
|
58 |
+
neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
|
59 |
+
with gr.Row():
|
60 |
+
width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
61 |
+
height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
62 |
+
steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
63 |
+
with gr.Row():
|
64 |
+
cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
65 |
+
seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
66 |
+
seed_rand = gr.Button("Randomize Seed ๐ฒ", size="sm", variant="secondary")
|
67 |
+
recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
|
68 |
+
with gr.Row():
|
69 |
+
positive_prefix = gr.CheckboxGroup(label="Use Positive Prefix", choices=get_positive_prefix(), value=[])
|
70 |
+
positive_suffix = gr.CheckboxGroup(label="Use Positive Suffix", choices=get_positive_suffix(), value=["Common"])
|
71 |
+
negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
|
72 |
+
negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
|
73 |
+
with gr.Row():
|
74 |
+
image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=2)
|
75 |
+
trans_prompt = gr.Button(value="Translate ๐", variant="secondary", size="sm", scale=2)
|
76 |
+
clear_prompt = gr.Button(value="Clear ๐๏ธ", variant="secondary", size="sm", scale=1)
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
run_button = gr.Button("Generate Image", variant="primary", scale=6)
|
80 |
+
random_button = gr.Button("Random Model ๐ฒ", variant="secondary", scale=3)
|
81 |
+
#stop_button = gr.Button('Stop', variant="stop", interactive=False, scale=1)
|
82 |
+
with gr.Group():
|
83 |
+
model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
|
84 |
+
model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_classes="model_info")
|
85 |
+
with gr.Column(scale=10):
|
86 |
+
with gr.Group():
|
87 |
+
with gr.Row():
|
88 |
+
output = [gr.Image(label='', elem_classes="output", type="filepath", format="png",
|
89 |
+
show_download_button=True, show_share_button=False, show_label=False,
|
90 |
+
interactive=False, min_width=80, visible=True, width=112, height=112) for _ in range(max_images)]
|
91 |
+
with gr.Group():
|
92 |
+
results = gr.Gallery(label="Gallery", elem_classes="gallery", interactive=False, show_download_button=True, show_share_button=False,
|
93 |
+
container=True, format="png", object_fit="cover", columns=2, rows=2)
|
94 |
+
image_files = gr.Files(label="Download", interactive=False)
|
95 |
+
clear_results = gr.Button("Clear Gallery / Download ๐๏ธ", variant="secondary")
|
96 |
+
with gr.Column():
|
97 |
+
examples = gr.Examples(
|
98 |
+
examples = [
|
99 |
+
["souryuu asuka langley, 1girl, neon genesis evangelion, plugsuit, pilot suit, red bodysuit, sitting, crossing legs, black eye patch, cat hat, throne, symmetrical, looking down, from bottom, looking at viewer, outdoors"],
|
100 |
+
["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
|
101 |
+
["kafuu chino, 1girl, solo"],
|
102 |
+
["1girl"],
|
103 |
+
["beautiful sunset"],
|
104 |
+
],
|
105 |
+
inputs=[prompt],
|
106 |
+
cache_examples=False,
|
107 |
+
)
|
108 |
+
with gr.Tab("PNG Info"):
|
109 |
+
def extract_exif_data(image):
|
110 |
+
if image is None: return ""
|
111 |
+
try:
|
112 |
+
metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
|
113 |
+
for key in metadata_keys:
|
114 |
+
if key in image.info:
|
115 |
+
return image.info[key]
|
116 |
+
return str(image.info)
|
117 |
+
except Exception as e:
|
118 |
+
return f"Error extracting metadata: {str(e)}"
|
119 |
+
with gr.Row():
|
120 |
+
with gr.Column():
|
121 |
+
image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
|
122 |
+
with gr.Column():
|
123 |
+
result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
|
124 |
+
|
125 |
+
image_metadata.change(
|
126 |
+
fn=extract_exif_data,
|
127 |
+
inputs=[image_metadata],
|
128 |
+
outputs=[result_metadata],
|
129 |
+
)
|
130 |
+
gr.Markdown(
|
131 |
+
f"""This demo was created in reference to the following demos.<br>
|
132 |
+
[Nymbo/Flood](https://huggingface.co/spaces/Nymbo/Flood),
|
133 |
+
[Yntec/ToyWorldXL](https://huggingface.co/spaces/Yntec/ToyWorldXL),
|
134 |
+
[Yntec/Diffusion80XX](https://huggingface.co/spaces/Yntec/Diffusion80XX).
|
135 |
+
"""
|
136 |
+
)
|
137 |
+
gr.DuplicateButton(value="Duplicate Space")
|
138 |
+
gr.Markdown(f"Just a few edits to *model.py* are all it takes to complete your own collection.")
|
139 |
+
|
140 |
+
#gr.on(triggers=[run_button.click, prompt.submit, random_button.click], fn=lambda: gr.update(interactive=True), inputs=None, outputs=stop_button, show_api=False)
|
141 |
+
model_name.change(change_model, [model_name], [model_info], queue=False, show_api=False)\
|
142 |
+
.success(warm_model, [model_name], None, queue=False, show_api=False)
|
143 |
+
for i, o in enumerate(output):
|
144 |
+
img_i = gr.Number(i, visible=False)
|
145 |
+
image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
|
146 |
+
gen_event = gr.on(triggers=[run_button.click, prompt.submit],
|
147 |
+
fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4: infer_fn(m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4) if (i < n) else None,
|
148 |
+
inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
|
149 |
+
positive_prefix, positive_suffix, negative_prefix, negative_suffix],
|
150 |
+
outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
|
151 |
+
gen_event2 = gr.on(triggers=[random_button.click],
|
152 |
+
fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4: infer_rand_fn(m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4) if (i < n) else None,
|
153 |
+
inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
|
154 |
+
positive_prefix, positive_suffix, negative_prefix, negative_suffix],
|
155 |
+
outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
|
156 |
+
o.change(save_gallery, [o, results], [results, image_files], show_api=False)
|
157 |
+
#stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
|
158 |
+
|
159 |
+
clear_prompt.click(lambda: (None, None, None, None), None, [prompt, neg_prompt, v2_character, v2_series], queue=False, show_api=False)
|
160 |
+
clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
|
161 |
+
recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
|
162 |
+
[positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=False, show_api=False)
|
163 |
+
seed_rand.click(randomize_seed, None, [seed], queue=False, show_api=False)
|
164 |
+
trans_prompt.click(translate_to_en, [prompt], [prompt], queue=False, show_api=False)\
|
165 |
+
.then(translate_to_en, [neg_prompt], [neg_prompt], queue=False, show_api=False)
|
166 |
+
|
167 |
+
random_prompt.click(
|
168 |
+
v2_random_prompt, [prompt, v2_series, v2_character, v2_rating, v2_aspect_ratio, v2_length,
|
169 |
+
v2_identity, v2_ban_tags, v2_model], [prompt, v2_series, v2_character], show_api=False,
|
170 |
+
).success(get_tag_type, [positive_prefix, positive_suffix, negative_prefix, negative_suffix], [v2_tag_type], queue=False, show_api=False
|
171 |
+
).success(convert_danbooru_to_e621_prompt, [prompt, v2_tag_type], [prompt], queue=False, show_api=False)
|
172 |
+
tagger_generate_from_image.click(lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
|
173 |
+
).success(
|
174 |
+
predict_tags_wd,
|
175 |
+
[tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
|
176 |
+
[v2_series, v2_character, prompt, v2_copy],
|
177 |
+
show_api=False,
|
178 |
+
).success(predict_tags_fl2_sd3, [tagger_image, prompt, tagger_algorithms], [prompt], show_api=False,
|
179 |
+
).success(remove_specific_prompt, [prompt, tagger_keep_tags], [prompt], queue=False, show_api=False,
|
180 |
+
).success(convert_danbooru_to_e621_prompt, [prompt, tagger_tag_type], [prompt], queue=False, show_api=False,
|
181 |
+
).success(insert_recom_prompt, [prompt, neg_prompt, tagger_recom_prompt], [prompt, neg_prompt], queue=False, show_api=False,
|
182 |
+
).success(compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False)
|
183 |
+
|
184 |
+
#demo.queue(default_concurrency_limit=200, max_size=200)
|
185 |
+
demo.launch(max_threads=400)
|
model.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from multit2i import find_model_list
|
2 |
+
|
3 |
+
|
4 |
+
models = find_model_list("John6666", ["illustrious"], "", "last_modified", 250)
|
5 |
+
|
6 |
+
|
7 |
+
# Examples:
|
8 |
+
#models = ['yodayo-ai/kivotos-xl-2.0', 'yodayo-ai/holodayo-xl-2.1'] # specific models
|
9 |
+
#models = find_model_list("John6666", [], "", "last_modified", 20) # John6666's latest 20 models
|
10 |
+
#models = find_model_list("John6666", ["anime"], "", "last_modified", 20) # John6666's latest 20 models with 'anime' tag
|
11 |
+
#models = find_model_list("John6666", [], "anime", "last_modified", 20) # John6666's latest 20 models without 'anime' tag
|
12 |
+
#models = find_model_list("", [], "", "last_modified", 20) # latest 20 text-to-image models of huggingface
|
13 |
+
#models = find_model_list("", [], "", "downloads", 20) # monthly most downloaded 20 text-to-image models of huggingface
|
multit2i.py
ADDED
@@ -0,0 +1,502 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import asyncio
|
3 |
+
from threading import RLock
|
4 |
+
from pathlib import Path
|
5 |
+
from huggingface_hub import InferenceClient
|
6 |
+
import os
|
7 |
+
|
8 |
+
|
9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.
|
10 |
+
server_timeout = 600
|
11 |
+
inference_timeout = 300
|
12 |
+
|
13 |
+
|
14 |
+
lock = RLock()
|
15 |
+
loaded_models = {}
|
16 |
+
model_info_dict = {}
|
17 |
+
|
18 |
+
|
19 |
+
def to_list(s):
|
20 |
+
return [x.strip() for x in s.split(",")]
|
21 |
+
|
22 |
+
|
23 |
+
def list_sub(a, b):
|
24 |
+
return [e for e in a if e not in b]
|
25 |
+
|
26 |
+
|
27 |
+
def list_uniq(l):
|
28 |
+
return sorted(set(l), key=l.index)
|
29 |
+
|
30 |
+
|
31 |
+
def is_repo_name(s):
|
32 |
+
import re
|
33 |
+
return re.fullmatch(r'^[^/]+?/[^/]+?$', s)
|
34 |
+
|
35 |
+
|
36 |
+
def get_status(model_name: str):
|
37 |
+
from huggingface_hub import InferenceClient
|
38 |
+
client = InferenceClient(token=HF_TOKEN, timeout=10)
|
39 |
+
return client.get_model_status(model_name)
|
40 |
+
|
41 |
+
|
42 |
+
def is_loadable(model_name: str, force_gpu: bool = False):
|
43 |
+
try:
|
44 |
+
status = get_status(model_name)
|
45 |
+
except Exception as e:
|
46 |
+
print(e)
|
47 |
+
print(f"Couldn't load {model_name}.")
|
48 |
+
return False
|
49 |
+
gpu_state = isinstance(status.compute_type, dict) and "gpu" in status.compute_type.keys()
|
50 |
+
if status is None or status.state not in ["Loadable", "Loaded"] or (force_gpu and not gpu_state):
|
51 |
+
print(f"Couldn't load {model_name}. Model state:'{status.state}', GPU:{gpu_state}")
|
52 |
+
return status is not None and status.state in ["Loadable", "Loaded"] and (not force_gpu or gpu_state)
|
53 |
+
|
54 |
+
|
55 |
+
def find_model_list(author: str="", tags: list[str]=[], not_tag="", sort: str="last_modified", limit: int=30, force_gpu=False, check_status=False):
|
56 |
+
from huggingface_hub import HfApi
|
57 |
+
api = HfApi(token=HF_TOKEN)
|
58 |
+
default_tags = ["diffusers"]
|
59 |
+
if not sort: sort = "last_modified"
|
60 |
+
limit = limit * 20 if check_status and force_gpu else limit * 5
|
61 |
+
models = []
|
62 |
+
try:
|
63 |
+
model_infos = api.list_models(author=author, #task="text-to-image",
|
64 |
+
tags=list_uniq(default_tags + tags), cardData=True, sort=sort, limit=limit)
|
65 |
+
except Exception as e:
|
66 |
+
print(f"Error: Failed to list models.")
|
67 |
+
print(e)
|
68 |
+
return models
|
69 |
+
for model in model_infos:
|
70 |
+
if not model.private and not model.gated or HF_TOKEN is not None:
|
71 |
+
loadable = is_loadable(model.id, force_gpu) if check_status else True
|
72 |
+
if not_tag and not_tag in model.tags or not loadable: continue
|
73 |
+
models.append(model.id)
|
74 |
+
if len(models) == limit: break
|
75 |
+
return models
|
76 |
+
|
77 |
+
|
78 |
+
def get_t2i_model_info_dict(repo_id: str):
|
79 |
+
from huggingface_hub import HfApi
|
80 |
+
api = HfApi(token=HF_TOKEN)
|
81 |
+
info = {"md": "None"}
|
82 |
+
try:
|
83 |
+
if not is_repo_name(repo_id) or not api.repo_exists(repo_id=repo_id): return info
|
84 |
+
model = api.model_info(repo_id=repo_id, token=HF_TOKEN)
|
85 |
+
except Exception as e:
|
86 |
+
print(f"Error: Failed to get {repo_id}'s info.")
|
87 |
+
print(e)
|
88 |
+
return info
|
89 |
+
if model.private or model.gated and HF_TOKEN is None: return info
|
90 |
+
try:
|
91 |
+
tags = model.tags
|
92 |
+
except Exception as e:
|
93 |
+
print(e)
|
94 |
+
return info
|
95 |
+
if not 'diffusers' in model.tags: return info
|
96 |
+
if 'diffusers:FluxPipeline' in tags: info["ver"] = "FLUX.1"
|
97 |
+
elif 'diffusers:StableDiffusionXLPipeline' in tags: info["ver"] = "SDXL"
|
98 |
+
elif 'diffusers:StableDiffusionPipeline' in tags: info["ver"] = "SD1.5"
|
99 |
+
elif 'diffusers:StableDiffusion3Pipeline' in tags: info["ver"] = "SD3"
|
100 |
+
else: info["ver"] = "Other"
|
101 |
+
info["url"] = f"https://huggingface.co/{repo_id}/"
|
102 |
+
info["tags"] = model.card_data.tags if model.card_data and model.card_data.tags else []
|
103 |
+
info["downloads"] = model.downloads
|
104 |
+
info["likes"] = model.likes
|
105 |
+
info["last_modified"] = model.last_modified.strftime("lastmod: %Y-%m-%d")
|
106 |
+
un_tags = ['text-to-image', 'stable-diffusion', 'stable-diffusion-api', 'safetensors', 'stable-diffusion-xl']
|
107 |
+
descs = [info["ver"]] + list_sub(info["tags"], un_tags) + [f'DLs: {info["downloads"]}'] + [f'โค: {info["likes"]}'] + [info["last_modified"]]
|
108 |
+
info["md"] = f'Model Info: {", ".join(descs)} [Model Repo]({info["url"]})'
|
109 |
+
return info
|
110 |
+
|
111 |
+
|
112 |
+
def rename_image(image_path: str | None, model_name: str, save_path: str | None = None):
|
113 |
+
import shutil
|
114 |
+
from datetime import datetime, timezone, timedelta
|
115 |
+
if image_path is None: return None
|
116 |
+
dt_now = datetime.now(timezone(timedelta(hours=9)))
|
117 |
+
filename = f"{model_name.split('/')[-1]}_{dt_now.strftime('%Y%m%d_%H%M%S')}.png"
|
118 |
+
try:
|
119 |
+
if Path(image_path).exists():
|
120 |
+
png_path = "image.png"
|
121 |
+
if str(Path(image_path).resolve()) != str(Path(png_path).resolve()): shutil.copy(image_path, png_path)
|
122 |
+
if save_path is not None:
|
123 |
+
new_path = str(Path(png_path).resolve().rename(Path(save_path).resolve()))
|
124 |
+
else:
|
125 |
+
new_path = str(Path(png_path).resolve().rename(Path(filename).resolve()))
|
126 |
+
return new_path
|
127 |
+
else:
|
128 |
+
return None
|
129 |
+
except Exception as e:
|
130 |
+
print(e)
|
131 |
+
return None
|
132 |
+
|
133 |
+
|
134 |
+
def save_gallery(image_path: str | None, images: list[tuple] | None):
|
135 |
+
if images is None: images = []
|
136 |
+
files = [i[0] for i in images]
|
137 |
+
if image_path is None: return images, files
|
138 |
+
files.insert(0, str(image_path))
|
139 |
+
images.insert(0, (str(image_path), Path(image_path).stem))
|
140 |
+
return images, files
|
141 |
+
|
142 |
+
|
143 |
+
# https://github.com/gradio-app/gradio/blob/main/gradio/external.py
|
144 |
+
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
|
145 |
+
from typing import Literal
|
146 |
+
def load_from_model(model_name: str, hf_token: str | Literal[False] | None = None):
|
147 |
+
import httpx
|
148 |
+
import huggingface_hub
|
149 |
+
from gradio.exceptions import ModelNotFoundError, TooManyRequestsError
|
150 |
+
model_url = f"https://huggingface.co/{model_name}"
|
151 |
+
api_url = f"https://api-inference.huggingface.co/models/{model_name}"
|
152 |
+
print(f"Fetching model from: {model_url}")
|
153 |
+
|
154 |
+
headers = ({} if hf_token in [False, None] else {"Authorization": f"Bearer {hf_token}"})
|
155 |
+
response = httpx.request("GET", api_url, headers=headers)
|
156 |
+
if response.status_code != 200:
|
157 |
+
raise ModelNotFoundError(
|
158 |
+
f"Could not find model: {model_name}. If it is a private or gated model, please provide your Hugging Face access token (https://huggingface.co/settings/tokens) as the argument for the `hf_token` parameter."
|
159 |
+
)
|
160 |
+
p = response.json().get("pipeline_tag")
|
161 |
+
if p != "text-to-image": raise ModelNotFoundError(f"This model isn't for text-to-image or unsupported: {model_name}.")
|
162 |
+
headers["X-Wait-For-Model"] = "true"
|
163 |
+
client = huggingface_hub.InferenceClient(model=model_name, headers=headers,
|
164 |
+
token=hf_token, timeout=server_timeout)
|
165 |
+
inputs = gr.components.Textbox(label="Input")
|
166 |
+
outputs = gr.components.Image(label="Output")
|
167 |
+
fn = client.text_to_image
|
168 |
+
|
169 |
+
def query_huggingface_inference_endpoints(*data, **kwargs):
|
170 |
+
try:
|
171 |
+
data = fn(*data, **kwargs) # type: ignore
|
172 |
+
except huggingface_hub.utils.HfHubHTTPError as e:
|
173 |
+
if "429" in str(e):
|
174 |
+
raise TooManyRequestsError() from e
|
175 |
+
except Exception as e:
|
176 |
+
raise Exception() from e
|
177 |
+
return data
|
178 |
+
|
179 |
+
interface_info = {
|
180 |
+
"fn": query_huggingface_inference_endpoints,
|
181 |
+
"inputs": inputs,
|
182 |
+
"outputs": outputs,
|
183 |
+
"title": model_name,
|
184 |
+
}
|
185 |
+
return gr.Interface(**interface_info)
|
186 |
+
|
187 |
+
|
188 |
+
def load_model(model_name: str):
|
189 |
+
global loaded_models
|
190 |
+
global model_info_dict
|
191 |
+
if model_name in loaded_models.keys(): return loaded_models[model_name]
|
192 |
+
try:
|
193 |
+
loaded_models[model_name] = load_from_model(model_name, hf_token=HF_TOKEN)
|
194 |
+
print(f"Loaded: {model_name}")
|
195 |
+
except Exception as e:
|
196 |
+
if model_name in loaded_models.keys(): del loaded_models[model_name]
|
197 |
+
print(f"Failed to load: {model_name}")
|
198 |
+
print(e)
|
199 |
+
return None
|
200 |
+
try:
|
201 |
+
model_info_dict[model_name] = get_t2i_model_info_dict(model_name)
|
202 |
+
print(f"Assigned: {model_name}")
|
203 |
+
except Exception as e:
|
204 |
+
if model_name in model_info_dict.keys(): del model_info_dict[model_name]
|
205 |
+
print(f"Failed to assigned: {model_name}")
|
206 |
+
print(e)
|
207 |
+
return loaded_models[model_name]
|
208 |
+
|
209 |
+
|
210 |
+
def load_model_api(model_name: str):
|
211 |
+
global loaded_models
|
212 |
+
global model_info_dict
|
213 |
+
if model_name in loaded_models.keys(): return loaded_models[model_name]
|
214 |
+
try:
|
215 |
+
client = InferenceClient(timeout=5)
|
216 |
+
status = client.get_model_status(model_name, token=HF_TOKEN)
|
217 |
+
if status is None or status.framework != "diffusers" or status.state not in ["Loadable", "Loaded"]:
|
218 |
+
print(f"Failed to load by API: {model_name}")
|
219 |
+
return None
|
220 |
+
else:
|
221 |
+
loaded_models[model_name] = InferenceClient(model_name, token=HF_TOKEN, timeout=server_timeout)
|
222 |
+
print(f"Loaded by API: {model_name}")
|
223 |
+
except Exception as e:
|
224 |
+
if model_name in loaded_models.keys(): del loaded_models[model_name]
|
225 |
+
print(f"Failed to load by API: {model_name}")
|
226 |
+
print(e)
|
227 |
+
return None
|
228 |
+
try:
|
229 |
+
model_info_dict[model_name] = get_t2i_model_info_dict(model_name)
|
230 |
+
print(f"Assigned by API: {model_name}")
|
231 |
+
except Exception as e:
|
232 |
+
if model_name in model_info_dict.keys(): del model_info_dict[model_name]
|
233 |
+
print(f"Failed to assigned by API: {model_name}")
|
234 |
+
print(e)
|
235 |
+
return loaded_models[model_name]
|
236 |
+
|
237 |
+
|
238 |
+
def load_models(models: list):
|
239 |
+
for model in models:
|
240 |
+
load_model(model)
|
241 |
+
|
242 |
+
|
243 |
+
positive_prefix = {
|
244 |
+
"Pony": to_list("score_9, score_8_up, score_7_up"),
|
245 |
+
"Pony Anime": to_list("source_anime, anime, score_9, score_8_up, score_7_up"),
|
246 |
+
}
|
247 |
+
positive_suffix = {
|
248 |
+
"Common": to_list("highly detailed, masterpiece, best quality, very aesthetic, absurdres"),
|
249 |
+
"Anime": to_list("anime artwork, anime style, studio anime, highly detailed"),
|
250 |
+
}
|
251 |
+
negative_prefix = {
|
252 |
+
"Pony": to_list("score_6, score_5, score_4"),
|
253 |
+
"Pony Anime": to_list("score_6, score_5, score_4, source_pony, source_furry, source_cartoon"),
|
254 |
+
"Pony Real": to_list("score_6, score_5, score_4, source_anime, source_pony, source_furry, source_cartoon"),
|
255 |
+
}
|
256 |
+
negative_suffix = {
|
257 |
+
"Common": to_list("lowres, (bad), bad hands, bad feet, text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]"),
|
258 |
+
"Pony Anime": to_list("busty, ugly face, mutated hands, low res, blurry face, black and white, the simpsons, overwatch, apex legends"),
|
259 |
+
"Pony Real": to_list("ugly, airbrushed, simple background, cgi, cartoon, anime"),
|
260 |
+
}
|
261 |
+
positive_all = negative_all = []
|
262 |
+
for k, v in (positive_prefix | positive_suffix).items():
|
263 |
+
positive_all = positive_all + v + [s.replace("_", " ") for s in v]
|
264 |
+
positive_all = list_uniq(positive_all)
|
265 |
+
for k, v in (negative_prefix | negative_suffix).items():
|
266 |
+
negative_all = negative_all + v + [s.replace("_", " ") for s in v]
|
267 |
+
positive_all = list_uniq(positive_all)
|
268 |
+
|
269 |
+
|
270 |
+
def recom_prompt(prompt: str = "", neg_prompt: str = "", pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = []):
|
271 |
+
def flatten(src):
|
272 |
+
return [item for row in src for item in row]
|
273 |
+
prompts = to_list(prompt)
|
274 |
+
neg_prompts = to_list(neg_prompt)
|
275 |
+
prompts = list_sub(prompts, positive_all)
|
276 |
+
neg_prompts = list_sub(neg_prompts, negative_all)
|
277 |
+
last_empty_p = [""] if not prompts and type != "None" else []
|
278 |
+
last_empty_np = [""] if not neg_prompts and type != "None" else []
|
279 |
+
prefix_ps = flatten([positive_prefix.get(s, []) for s in pos_pre])
|
280 |
+
suffix_ps = flatten([positive_suffix.get(s, []) for s in pos_suf])
|
281 |
+
prefix_nps = flatten([negative_prefix.get(s, []) for s in neg_pre])
|
282 |
+
suffix_nps = flatten([negative_suffix.get(s, []) for s in neg_suf])
|
283 |
+
prompt = ", ".join(list_uniq(prefix_ps + prompts + suffix_ps) + last_empty_p)
|
284 |
+
neg_prompt = ", ".join(list_uniq(prefix_nps + neg_prompts + suffix_nps) + last_empty_np)
|
285 |
+
return prompt, neg_prompt
|
286 |
+
|
287 |
+
|
288 |
+
recom_prompt_type = {
|
289 |
+
"None": ([], [], [], []),
|
290 |
+
"Auto": ([], [], [], []),
|
291 |
+
"Common": ([], ["Common"], [], ["Common"]),
|
292 |
+
"Animagine": ([], ["Common", "Anime"], [], ["Common"]),
|
293 |
+
"Pony": (["Pony"], ["Common"], ["Pony"], ["Common"]),
|
294 |
+
"Pony Anime": (["Pony", "Pony Anime"], ["Common", "Anime"], ["Pony", "Pony Anime"], ["Common", "Pony Anime"]),
|
295 |
+
"Pony Real": (["Pony"], ["Common"], ["Pony", "Pony Real"], ["Common", "Pony Real"]),
|
296 |
+
}
|
297 |
+
|
298 |
+
|
299 |
+
enable_auto_recom_prompt = False
|
300 |
+
def insert_recom_prompt(prompt: str = "", neg_prompt: str = "", type: str = "None"):
|
301 |
+
global enable_auto_recom_prompt
|
302 |
+
if type == "Auto": enable_auto_recom_prompt = True
|
303 |
+
else: enable_auto_recom_prompt = False
|
304 |
+
pos_pre, pos_suf, neg_pre, neg_suf = recom_prompt_type.get(type, ([], [], [], []))
|
305 |
+
return recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
306 |
+
|
307 |
+
|
308 |
+
def set_recom_prompt_preset(type: str = "None"):
|
309 |
+
pos_pre, pos_suf, neg_pre, neg_suf = recom_prompt_type.get(type, ([], [], [], []))
|
310 |
+
return pos_pre, pos_suf, neg_pre, neg_suf
|
311 |
+
|
312 |
+
|
313 |
+
def get_recom_prompt_type():
|
314 |
+
type = list(recom_prompt_type.keys())
|
315 |
+
type.remove("Auto")
|
316 |
+
return type
|
317 |
+
|
318 |
+
|
319 |
+
def get_positive_prefix():
|
320 |
+
return list(positive_prefix.keys())
|
321 |
+
|
322 |
+
|
323 |
+
def get_positive_suffix():
|
324 |
+
return list(positive_suffix.keys())
|
325 |
+
|
326 |
+
|
327 |
+
def get_negative_prefix():
|
328 |
+
return list(negative_prefix.keys())
|
329 |
+
|
330 |
+
|
331 |
+
def get_negative_suffix():
|
332 |
+
return list(negative_suffix.keys())
|
333 |
+
|
334 |
+
|
335 |
+
def get_tag_type(pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = []):
|
336 |
+
tag_type = "danbooru"
|
337 |
+
words = pos_pre + pos_suf + neg_pre + neg_suf
|
338 |
+
for word in words:
|
339 |
+
if "Pony" in word:
|
340 |
+
tag_type = "e621"
|
341 |
+
break
|
342 |
+
return tag_type
|
343 |
+
|
344 |
+
|
345 |
+
def get_model_info_md(model_name: str):
|
346 |
+
if model_name in model_info_dict.keys(): return model_info_dict[model_name].get("md", "")
|
347 |
+
|
348 |
+
|
349 |
+
def change_model(model_name: str):
|
350 |
+
load_model_api(model_name)
|
351 |
+
return get_model_info_md(model_name)
|
352 |
+
|
353 |
+
|
354 |
+
def warm_model(model_name: str):
|
355 |
+
model = load_model_api(model_name)
|
356 |
+
if model:
|
357 |
+
try:
|
358 |
+
print(f"Warming model: {model_name}")
|
359 |
+
infer_body(model, " ")
|
360 |
+
except Exception as e:
|
361 |
+
print(e)
|
362 |
+
|
363 |
+
|
364 |
+
# https://huggingface.co/docs/api-inference/detailed_parameters
|
365 |
+
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
|
366 |
+
def infer_body(client: InferenceClient | gr.Interface | object, model_str: str, prompt: str, neg_prompt: str = "",
|
367 |
+
height: int = 0, width: int = 0, steps: int = 0, cfg: int = 0, seed: int = -1):
|
368 |
+
png_path = "image.png"
|
369 |
+
kwargs = {}
|
370 |
+
if height > 0: kwargs["height"] = height
|
371 |
+
if width > 0: kwargs["width"] = width
|
372 |
+
if steps > 0: kwargs["num_inference_steps"] = steps
|
373 |
+
if cfg > 0: cfg = kwargs["guidance_scale"] = cfg
|
374 |
+
if seed == -1: kwargs["seed"] = randomize_seed()
|
375 |
+
else: kwargs["seed"] = seed
|
376 |
+
try:
|
377 |
+
if isinstance(client, InferenceClient):
|
378 |
+
image = client.text_to_image(prompt=prompt, negative_prompt=neg_prompt, **kwargs, token=HF_TOKEN)
|
379 |
+
elif isinstance(client, gr.Interface):
|
380 |
+
image = client.fn(prompt=prompt, negative_prompt=neg_prompt, **kwargs, token=HF_TOKEN)
|
381 |
+
else: return None
|
382 |
+
if isinstance(image, tuple): return None
|
383 |
+
return save_image(image, png_path, model_str, prompt, neg_prompt, height, width, steps, cfg, seed)
|
384 |
+
except Exception as e:
|
385 |
+
print(e)
|
386 |
+
raise Exception() from e
|
387 |
+
|
388 |
+
|
389 |
+
async def infer(model_name: str, prompt: str, neg_prompt: str ="", height: int = 0, width: int = 0,
|
390 |
+
steps: int = 0, cfg: int = 0, seed: int = -1,
|
391 |
+
save_path: str | None = None, timeout: float = inference_timeout):
|
392 |
+
model = load_model(model_name)
|
393 |
+
if not model: return None
|
394 |
+
task = asyncio.create_task(asyncio.to_thread(infer_body, model, model_name, prompt, neg_prompt,
|
395 |
+
height, width, steps, cfg, seed))
|
396 |
+
await asyncio.sleep(0)
|
397 |
+
try:
|
398 |
+
result = await asyncio.wait_for(task, timeout=timeout)
|
399 |
+
except asyncio.TimeoutError as e:
|
400 |
+
print(e)
|
401 |
+
print(f"Task timed out: {model_name}")
|
402 |
+
if not task.done(): task.cancel()
|
403 |
+
result = None
|
404 |
+
raise Exception(f"Task timed out: {model_name}") from e
|
405 |
+
except Exception as e:
|
406 |
+
print(e)
|
407 |
+
if not task.done(): task.cancel()
|
408 |
+
result = None
|
409 |
+
raise Exception() from e
|
410 |
+
if task.done() and result is not None:
|
411 |
+
with lock:
|
412 |
+
image = rename_image(result, model_name, save_path)
|
413 |
+
return image
|
414 |
+
return None
|
415 |
+
|
416 |
+
|
417 |
+
# https://github.com/aio-libs/pytest-aiohttp/issues/8 # also AsyncInferenceClient is buggy.
|
418 |
+
def infer_fn(model_name: str, prompt: str, neg_prompt: str = "", height: int = 0, width: int = 0,
|
419 |
+
steps: int = 0, cfg: int = 0, seed: int = -1,
|
420 |
+
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
|
421 |
+
if model_name == 'NA':
|
422 |
+
return None
|
423 |
+
try:
|
424 |
+
loop = asyncio.get_running_loop()
|
425 |
+
except Exception:
|
426 |
+
loop = asyncio.new_event_loop()
|
427 |
+
try:
|
428 |
+
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
429 |
+
result = loop.run_until_complete(infer(model_name, prompt, neg_prompt, height, width,
|
430 |
+
steps, cfg, seed, save_path, inference_timeout))
|
431 |
+
except (Exception, asyncio.CancelledError) as e:
|
432 |
+
print(e)
|
433 |
+
print(f"Task aborted: {model_name}, Error: {e}")
|
434 |
+
result = None
|
435 |
+
raise gr.Error(f"Task aborted: {model_name}, Error: {e}")
|
436 |
+
finally:
|
437 |
+
loop.close()
|
438 |
+
return result
|
439 |
+
|
440 |
+
|
441 |
+
def infer_rand_fn(model_name_dummy: str, prompt: str, neg_prompt: str = "", height: int = 0, width: int = 0,
|
442 |
+
steps: int = 0, cfg: int = 0, seed: int = -1,
|
443 |
+
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
|
444 |
+
import random
|
445 |
+
if model_name_dummy == 'NA':
|
446 |
+
return None
|
447 |
+
random.seed()
|
448 |
+
model_name = random.choice(list(loaded_models.keys()))
|
449 |
+
try:
|
450 |
+
loop = asyncio.get_running_loop()
|
451 |
+
except Exception:
|
452 |
+
loop = asyncio.new_event_loop()
|
453 |
+
try:
|
454 |
+
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
455 |
+
result = loop.run_until_complete(infer(model_name, prompt, neg_prompt, height, width,
|
456 |
+
steps, cfg, seed, save_path, inference_timeout))
|
457 |
+
except (Exception, asyncio.CancelledError) as e:
|
458 |
+
print(e)
|
459 |
+
print(f"Task aborted: {model_name}, Error: {e}")
|
460 |
+
result = None
|
461 |
+
raise gr.Error(f"Task aborted: {model_name}, Error: {e}")
|
462 |
+
finally:
|
463 |
+
loop.close()
|
464 |
+
return result
|
465 |
+
|
466 |
+
|
467 |
+
def save_image(image, savefile, modelname, prompt, nprompt, height=0, width=0, steps=0, cfg=0, seed=-1):
|
468 |
+
from PIL import Image, PngImagePlugin
|
469 |
+
import json
|
470 |
+
try:
|
471 |
+
metadata = {"prompt": prompt, "negative_prompt": nprompt, "Model": {"Model": modelname.split("/")[-1]}}
|
472 |
+
if steps > 0: metadata["num_inference_steps"] = steps
|
473 |
+
if cfg > 0: metadata["guidance_scale"] = cfg
|
474 |
+
if seed != -1: metadata["seed"] = seed
|
475 |
+
if width > 0 and height > 0: metadata["resolution"] = f"{width} x {height}"
|
476 |
+
metadata_str = json.dumps(metadata)
|
477 |
+
info = PngImagePlugin.PngInfo()
|
478 |
+
info.add_text("metadata", metadata_str)
|
479 |
+
image.save(savefile, "PNG", pnginfo=info)
|
480 |
+
return str(Path(savefile).resolve())
|
481 |
+
except Exception as e:
|
482 |
+
print(f"Failed to save image file: {e}")
|
483 |
+
raise Exception(f"Failed to save image file:") from e
|
484 |
+
|
485 |
+
|
486 |
+
def randomize_seed():
|
487 |
+
from random import seed, randint
|
488 |
+
MAX_SEED = 2**32-1
|
489 |
+
seed()
|
490 |
+
rseed = randint(0, MAX_SEED)
|
491 |
+
return rseed
|
492 |
+
|
493 |
+
|
494 |
+
from translatepy import Translator
|
495 |
+
translator = Translator()
|
496 |
+
def translate_to_en(input: str):
|
497 |
+
try:
|
498 |
+
output = str(translator.translate(input, 'English'))
|
499 |
+
except Exception as e:
|
500 |
+
output = input
|
501 |
+
print(e)
|
502 |
+
return output
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
torch==2.2.0
|
3 |
+
torchvision
|
4 |
+
accelerate
|
5 |
+
transformers==4.44.0
|
6 |
+
optimum[onnxruntime]
|
7 |
+
dartrs
|
8 |
+
translatepy
|
9 |
+
timm
|
tagger/character_series_dict.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tagger/danbooru_e621.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tagger/fl2sd3longcap.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
3 |
+
import re
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
|
7 |
+
import subprocess
|
8 |
+
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
9 |
+
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
|
12 |
+
try:
|
13 |
+
fl_model = AutoModelForCausalLM.from_pretrained('gokaygokay/Florence-2-SD3-Captioner', trust_remote_code=True).to("cpu").eval()
|
14 |
+
fl_processor = AutoProcessor.from_pretrained('gokaygokay/Florence-2-SD3-Captioner', trust_remote_code=True)
|
15 |
+
except Exception as e:
|
16 |
+
print(e)
|
17 |
+
fl_model = fl_processor = None
|
18 |
+
|
19 |
+
def fl_modify_caption(caption: str) -> str:
|
20 |
+
"""
|
21 |
+
Removes specific prefixes from captions if present, otherwise returns the original caption.
|
22 |
+
Args:
|
23 |
+
caption (str): A string containing a caption.
|
24 |
+
Returns:
|
25 |
+
str: The caption with the prefix removed if it was present, or the original caption.
|
26 |
+
"""
|
27 |
+
# Define the prefixes to remove
|
28 |
+
prefix_substrings = [
|
29 |
+
('captured from ', ''),
|
30 |
+
('captured at ', '')
|
31 |
+
]
|
32 |
+
|
33 |
+
# Create a regex pattern to match any of the prefixes
|
34 |
+
pattern = '|'.join([re.escape(opening) for opening, _ in prefix_substrings])
|
35 |
+
replacers = {opening.lower(): replacer for opening, replacer in prefix_substrings}
|
36 |
+
|
37 |
+
# Function to replace matched prefix with its corresponding replacement
|
38 |
+
def replace_fn(match):
|
39 |
+
return replacers[match.group(0).lower()]
|
40 |
+
|
41 |
+
# Apply the regex to the caption
|
42 |
+
modified_caption = re.sub(pattern, replace_fn, caption, count=1, flags=re.IGNORECASE)
|
43 |
+
|
44 |
+
# If the caption was modified, return the modified version; otherwise, return the original
|
45 |
+
return modified_caption if modified_caption != caption else caption
|
46 |
+
|
47 |
+
|
48 |
+
@spaces.GPU(duration=30)
|
49 |
+
def fl_run_example(image):
|
50 |
+
task_prompt = "<DESCRIPTION>"
|
51 |
+
prompt = task_prompt + "Describe this image in great detail."
|
52 |
+
|
53 |
+
# Ensure the image is in RGB mode
|
54 |
+
if image.mode != "RGB":
|
55 |
+
image = image.convert("RGB")
|
56 |
+
|
57 |
+
fl_model.to(device)
|
58 |
+
inputs = fl_processor(text=prompt, images=image, return_tensors="pt").to(device)
|
59 |
+
generated_ids = fl_model.generate(
|
60 |
+
input_ids=inputs["input_ids"],
|
61 |
+
pixel_values=inputs["pixel_values"],
|
62 |
+
max_new_tokens=1024,
|
63 |
+
num_beams=3
|
64 |
+
)
|
65 |
+
fl_model.to("cpu")
|
66 |
+
generated_text = fl_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
67 |
+
parsed_answer = fl_processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
|
68 |
+
return fl_modify_caption(parsed_answer["<DESCRIPTION>"])
|
69 |
+
|
70 |
+
|
71 |
+
def predict_tags_fl2_sd3(image: Image.Image, input_tags: str, algo: list[str]):
|
72 |
+
def to_list(s):
|
73 |
+
return [x.strip() for x in s.split(",") if not s == ""]
|
74 |
+
|
75 |
+
def list_uniq(l):
|
76 |
+
return sorted(set(l), key=l.index)
|
77 |
+
|
78 |
+
if not "Use Florence-2-SD3-Long-Captioner" in algo:
|
79 |
+
return input_tags
|
80 |
+
tag_list = list_uniq(to_list(input_tags) + to_list(fl_run_example(image) + ", "))
|
81 |
+
tag_list.remove("")
|
82 |
+
return ", ".join(tag_list)
|
tagger/output.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
|
3 |
+
|
4 |
+
@dataclass
|
5 |
+
class UpsamplingOutput:
|
6 |
+
upsampled_tags: str
|
7 |
+
|
8 |
+
copyright_tags: str
|
9 |
+
character_tags: str
|
10 |
+
general_tags: str
|
11 |
+
rating_tag: str
|
12 |
+
aspect_ratio_tag: str
|
13 |
+
length_tag: str
|
14 |
+
identity_tag: str
|
15 |
+
|
16 |
+
elapsed_time: float = 0.0
|
tagger/tag_group.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tagger/tagger.py
ADDED
@@ -0,0 +1,556 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
import gradio as gr
|
5 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
|
9 |
+
WD_MODEL_NAMES = ["p1atdev/wd-swinv2-tagger-v3-hf"]
|
10 |
+
WD_MODEL_NAME = WD_MODEL_NAMES[0]
|
11 |
+
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
default_device = device
|
14 |
+
|
15 |
+
try:
|
16 |
+
wd_model = AutoModelForImageClassification.from_pretrained(WD_MODEL_NAME, trust_remote_code=True).to(default_device).eval()
|
17 |
+
wd_processor = AutoImageProcessor.from_pretrained(WD_MODEL_NAME, trust_remote_code=True)
|
18 |
+
except Exception as e:
|
19 |
+
print(e)
|
20 |
+
wd_model = wd_processor = None
|
21 |
+
|
22 |
+
def _people_tag(noun: str, minimum: int = 1, maximum: int = 5):
|
23 |
+
return (
|
24 |
+
[f"1{noun}"]
|
25 |
+
+ [f"{num}{noun}s" for num in range(minimum + 1, maximum + 1)]
|
26 |
+
+ [f"{maximum+1}+{noun}s"]
|
27 |
+
)
|
28 |
+
|
29 |
+
|
30 |
+
PEOPLE_TAGS = (
|
31 |
+
_people_tag("girl") + _people_tag("boy") + _people_tag("other") + ["no humans"]
|
32 |
+
)
|
33 |
+
|
34 |
+
|
35 |
+
RATING_MAP = {
|
36 |
+
"sfw": "safe",
|
37 |
+
"general": "safe",
|
38 |
+
"sensitive": "sensitive",
|
39 |
+
"questionable": "nsfw",
|
40 |
+
"explicit": "explicit, nsfw",
|
41 |
+
}
|
42 |
+
DANBOORU_TO_E621_RATING_MAP = {
|
43 |
+
"sfw": "rating_safe",
|
44 |
+
"general": "rating_safe",
|
45 |
+
"safe": "rating_safe",
|
46 |
+
"sensitive": "rating_safe",
|
47 |
+
"nsfw": "rating_explicit",
|
48 |
+
"explicit, nsfw": "rating_explicit",
|
49 |
+
"explicit": "rating_explicit",
|
50 |
+
"rating:safe": "rating_safe",
|
51 |
+
"rating:general": "rating_safe",
|
52 |
+
"rating:sensitive": "rating_safe",
|
53 |
+
"rating:questionable, nsfw": "rating_explicit",
|
54 |
+
"rating:explicit, nsfw": "rating_explicit",
|
55 |
+
}
|
56 |
+
|
57 |
+
|
58 |
+
# https://github.com/toriato/stable-diffusion-webui-wd14-tagger/blob/a9eacb1eff904552d3012babfa28b57e1d3e295c/tagger/ui.py#L368
|
59 |
+
kaomojis = [
|
60 |
+
"0_0",
|
61 |
+
"(o)_(o)",
|
62 |
+
"+_+",
|
63 |
+
"+_-",
|
64 |
+
"._.",
|
65 |
+
"<o>_<o>",
|
66 |
+
"<|>_<|>",
|
67 |
+
"=_=",
|
68 |
+
">_<",
|
69 |
+
"3_3",
|
70 |
+
"6_9",
|
71 |
+
">_o",
|
72 |
+
"@_@",
|
73 |
+
"^_^",
|
74 |
+
"o_o",
|
75 |
+
"u_u",
|
76 |
+
"x_x",
|
77 |
+
"|_|",
|
78 |
+
"||_||",
|
79 |
+
]
|
80 |
+
|
81 |
+
|
82 |
+
def replace_underline(x: str):
|
83 |
+
return x.strip().replace("_", " ") if x not in kaomojis else x.strip()
|
84 |
+
|
85 |
+
|
86 |
+
def to_list(s):
|
87 |
+
return [x.strip() for x in s.split(",") if not s == ""]
|
88 |
+
|
89 |
+
|
90 |
+
def list_sub(a, b):
|
91 |
+
return [e for e in a if e not in b]
|
92 |
+
|
93 |
+
|
94 |
+
def list_uniq(l):
|
95 |
+
return sorted(set(l), key=l.index)
|
96 |
+
|
97 |
+
|
98 |
+
def load_dict_from_csv(filename):
|
99 |
+
dict = {}
|
100 |
+
if not Path(filename).exists():
|
101 |
+
if Path('./tagger/', filename).exists(): filename = str(Path('./tagger/', filename))
|
102 |
+
else: return dict
|
103 |
+
try:
|
104 |
+
with open(filename, 'r', encoding="utf-8") as f:
|
105 |
+
lines = f.readlines()
|
106 |
+
except Exception:
|
107 |
+
print(f"Failed to open dictionary file: {filename}")
|
108 |
+
return dict
|
109 |
+
for line in lines:
|
110 |
+
parts = line.strip().split(',')
|
111 |
+
dict[parts[0]] = parts[1]
|
112 |
+
return dict
|
113 |
+
|
114 |
+
|
115 |
+
anime_series_dict = load_dict_from_csv('character_series_dict.csv')
|
116 |
+
|
117 |
+
|
118 |
+
def character_list_to_series_list(character_list):
|
119 |
+
output_series_tag = []
|
120 |
+
series_tag = ""
|
121 |
+
series_dict = anime_series_dict
|
122 |
+
for tag in character_list:
|
123 |
+
series_tag = series_dict.get(tag, "")
|
124 |
+
if tag.endswith(")"):
|
125 |
+
tags = tag.split("(")
|
126 |
+
character_tag = "(".join(tags[:-1])
|
127 |
+
if character_tag.endswith(" "):
|
128 |
+
character_tag = character_tag[:-1]
|
129 |
+
series_tag = tags[-1].replace(")", "")
|
130 |
+
|
131 |
+
if series_tag:
|
132 |
+
output_series_tag.append(series_tag)
|
133 |
+
|
134 |
+
return output_series_tag
|
135 |
+
|
136 |
+
|
137 |
+
def select_random_character(series: str, character: str):
|
138 |
+
from random import seed, randrange
|
139 |
+
seed()
|
140 |
+
character_list = list(anime_series_dict.keys())
|
141 |
+
character = character_list[randrange(len(character_list) - 1)]
|
142 |
+
series = anime_series_dict.get(character.split(",")[0].strip(), "")
|
143 |
+
return series, character
|
144 |
+
|
145 |
+
|
146 |
+
def danbooru_to_e621(dtag, e621_dict):
|
147 |
+
def d_to_e(match, e621_dict):
|
148 |
+
dtag = match.group(0)
|
149 |
+
etag = e621_dict.get(replace_underline(dtag), "")
|
150 |
+
if etag:
|
151 |
+
return etag
|
152 |
+
else:
|
153 |
+
return dtag
|
154 |
+
|
155 |
+
import re
|
156 |
+
tag = re.sub(r'[\w ]+', lambda wrapper: d_to_e(wrapper, e621_dict), dtag, 2)
|
157 |
+
return tag
|
158 |
+
|
159 |
+
|
160 |
+
danbooru_to_e621_dict = load_dict_from_csv('danbooru_e621.csv')
|
161 |
+
|
162 |
+
|
163 |
+
def convert_danbooru_to_e621_prompt(input_prompt: str = "", prompt_type: str = "danbooru"):
|
164 |
+
if prompt_type == "danbooru": return input_prompt
|
165 |
+
tags = input_prompt.split(",") if input_prompt else []
|
166 |
+
people_tags: list[str] = []
|
167 |
+
other_tags: list[str] = []
|
168 |
+
rating_tags: list[str] = []
|
169 |
+
|
170 |
+
e621_dict = danbooru_to_e621_dict
|
171 |
+
for tag in tags:
|
172 |
+
tag = replace_underline(tag)
|
173 |
+
tag = danbooru_to_e621(tag, e621_dict)
|
174 |
+
if tag in PEOPLE_TAGS:
|
175 |
+
people_tags.append(tag)
|
176 |
+
elif tag in DANBOORU_TO_E621_RATING_MAP.keys():
|
177 |
+
rating_tags.append(DANBOORU_TO_E621_RATING_MAP.get(tag.replace(" ",""), ""))
|
178 |
+
else:
|
179 |
+
other_tags.append(tag)
|
180 |
+
|
181 |
+
rating_tags = sorted(set(rating_tags), key=rating_tags.index)
|
182 |
+
rating_tags = [rating_tags[0]] if rating_tags else []
|
183 |
+
rating_tags = ["explicit, nsfw"] if rating_tags and rating_tags[0] == "explicit" else rating_tags
|
184 |
+
|
185 |
+
output_prompt = ", ".join(people_tags + other_tags + rating_tags)
|
186 |
+
|
187 |
+
return output_prompt
|
188 |
+
|
189 |
+
|
190 |
+
from translatepy import Translator
|
191 |
+
translator = Translator()
|
192 |
+
def translate_prompt_old(prompt: str = ""):
|
193 |
+
def translate_to_english(input: str):
|
194 |
+
try:
|
195 |
+
output = str(translator.translate(input, 'English'))
|
196 |
+
except Exception as e:
|
197 |
+
output = input
|
198 |
+
print(e)
|
199 |
+
return output
|
200 |
+
|
201 |
+
def is_japanese(s):
|
202 |
+
import unicodedata
|
203 |
+
for ch in s:
|
204 |
+
name = unicodedata.name(ch, "")
|
205 |
+
if "CJK UNIFIED" in name or "HIRAGANA" in name or "KATAKANA" in name:
|
206 |
+
return True
|
207 |
+
return False
|
208 |
+
|
209 |
+
def to_list(s):
|
210 |
+
return [x.strip() for x in s.split(",")]
|
211 |
+
|
212 |
+
prompts = to_list(prompt)
|
213 |
+
outputs = []
|
214 |
+
for p in prompts:
|
215 |
+
p = translate_to_english(p) if is_japanese(p) else p
|
216 |
+
outputs.append(p)
|
217 |
+
|
218 |
+
return ", ".join(outputs)
|
219 |
+
|
220 |
+
|
221 |
+
def translate_prompt(input: str):
|
222 |
+
try:
|
223 |
+
output = str(translator.translate(input, 'English'))
|
224 |
+
except Exception as e:
|
225 |
+
output = input
|
226 |
+
print(e)
|
227 |
+
return output
|
228 |
+
|
229 |
+
|
230 |
+
def translate_prompt_to_ja(prompt: str = ""):
|
231 |
+
def translate_to_japanese(input: str):
|
232 |
+
try:
|
233 |
+
output = str(translator.translate(input, 'Japanese'))
|
234 |
+
except Exception as e:
|
235 |
+
output = input
|
236 |
+
print(e)
|
237 |
+
return output
|
238 |
+
|
239 |
+
def is_japanese(s):
|
240 |
+
import unicodedata
|
241 |
+
for ch in s:
|
242 |
+
name = unicodedata.name(ch, "")
|
243 |
+
if "CJK UNIFIED" in name or "HIRAGANA" in name or "KATAKANA" in name:
|
244 |
+
return True
|
245 |
+
return False
|
246 |
+
|
247 |
+
def to_list(s):
|
248 |
+
return [x.strip() for x in s.split(",")]
|
249 |
+
|
250 |
+
prompts = to_list(prompt)
|
251 |
+
outputs = []
|
252 |
+
for p in prompts:
|
253 |
+
p = translate_to_japanese(p) if not is_japanese(p) else p
|
254 |
+
outputs.append(p)
|
255 |
+
|
256 |
+
return ", ".join(outputs)
|
257 |
+
|
258 |
+
|
259 |
+
def tags_to_ja(itag, dict):
|
260 |
+
def t_to_j(match, dict):
|
261 |
+
tag = match.group(0)
|
262 |
+
ja = dict.get(replace_underline(tag), "")
|
263 |
+
if ja:
|
264 |
+
return ja
|
265 |
+
else:
|
266 |
+
return tag
|
267 |
+
|
268 |
+
import re
|
269 |
+
tag = re.sub(r'[\w ]+', lambda wrapper: t_to_j(wrapper, dict), itag, 2)
|
270 |
+
|
271 |
+
return tag
|
272 |
+
|
273 |
+
|
274 |
+
def convert_tags_to_ja(input_prompt: str = ""):
|
275 |
+
tags = input_prompt.split(",") if input_prompt else []
|
276 |
+
out_tags = []
|
277 |
+
|
278 |
+
tags_to_ja_dict = load_dict_from_csv('all_tags_ja_ext.csv')
|
279 |
+
dict = tags_to_ja_dict
|
280 |
+
for tag in tags:
|
281 |
+
tag = replace_underline(tag)
|
282 |
+
tag = tags_to_ja(tag, dict)
|
283 |
+
out_tags.append(tag)
|
284 |
+
|
285 |
+
return ", ".join(out_tags)
|
286 |
+
|
287 |
+
|
288 |
+
enable_auto_recom_prompt = True
|
289 |
+
|
290 |
+
|
291 |
+
animagine_ps = to_list("masterpiece, best quality, very aesthetic, absurdres")
|
292 |
+
animagine_nps = to_list("lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]")
|
293 |
+
pony_ps = to_list("score_9, score_8_up, score_7_up, masterpiece, best quality, very aesthetic, absurdres")
|
294 |
+
pony_nps = to_list("source_pony, score_6, score_5, score_4, busty, ugly face, mutated hands, low res, blurry face, black and white, the simpsons, overwatch, apex legends")
|
295 |
+
other_ps = to_list("anime artwork, anime style, studio anime, highly detailed, cinematic photo, 35mm photograph, film, bokeh, professional, 4k, highly detailed")
|
296 |
+
other_nps = to_list("photo, deformed, black and white, realism, disfigured, low contrast, drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly")
|
297 |
+
default_ps = to_list("highly detailed, masterpiece, best quality, very aesthetic, absurdres")
|
298 |
+
default_nps = to_list("score_6, score_5, score_4, lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]")
|
299 |
+
def insert_recom_prompt(prompt: str = "", neg_prompt: str = "", type: str = "None"):
|
300 |
+
global enable_auto_recom_prompt
|
301 |
+
prompts = to_list(prompt)
|
302 |
+
neg_prompts = to_list(neg_prompt)
|
303 |
+
|
304 |
+
prompts = list_sub(prompts, animagine_ps + pony_ps)
|
305 |
+
neg_prompts = list_sub(neg_prompts, animagine_nps + pony_nps)
|
306 |
+
|
307 |
+
last_empty_p = [""] if not prompts and type != "None" else []
|
308 |
+
last_empty_np = [""] if not neg_prompts and type != "None" else []
|
309 |
+
|
310 |
+
if type == "Auto":
|
311 |
+
enable_auto_recom_prompt = True
|
312 |
+
else:
|
313 |
+
enable_auto_recom_prompt = False
|
314 |
+
if type == "Animagine":
|
315 |
+
prompts = prompts + animagine_ps
|
316 |
+
neg_prompts = neg_prompts + animagine_nps
|
317 |
+
elif type == "Pony":
|
318 |
+
prompts = prompts + pony_ps
|
319 |
+
neg_prompts = neg_prompts + pony_nps
|
320 |
+
|
321 |
+
prompt = ", ".join(list_uniq(prompts) + last_empty_p)
|
322 |
+
neg_prompt = ", ".join(list_uniq(neg_prompts) + last_empty_np)
|
323 |
+
|
324 |
+
return prompt, neg_prompt
|
325 |
+
|
326 |
+
|
327 |
+
def load_model_prompt_dict():
|
328 |
+
import json
|
329 |
+
dict = {}
|
330 |
+
path = 'model_dict.json' if Path('model_dict.json').exists() else './tagger/model_dict.json'
|
331 |
+
try:
|
332 |
+
with open('model_dict.json', encoding='utf-8') as f:
|
333 |
+
dict = json.load(f)
|
334 |
+
except Exception:
|
335 |
+
pass
|
336 |
+
return dict
|
337 |
+
|
338 |
+
|
339 |
+
model_prompt_dict = load_model_prompt_dict()
|
340 |
+
|
341 |
+
|
342 |
+
def insert_model_recom_prompt(prompt: str = "", neg_prompt: str = "", model_name: str = "None"):
|
343 |
+
if not model_name or not enable_auto_recom_prompt: return prompt, neg_prompt
|
344 |
+
prompts = to_list(prompt)
|
345 |
+
neg_prompts = to_list(neg_prompt)
|
346 |
+
prompts = list_sub(prompts, animagine_ps + pony_ps + other_ps)
|
347 |
+
neg_prompts = list_sub(neg_prompts, animagine_nps + pony_nps + other_nps)
|
348 |
+
last_empty_p = [""] if not prompts and type != "None" else []
|
349 |
+
last_empty_np = [""] if not neg_prompts and type != "None" else []
|
350 |
+
ps = []
|
351 |
+
nps = []
|
352 |
+
if model_name in model_prompt_dict.keys():
|
353 |
+
ps = to_list(model_prompt_dict[model_name]["prompt"])
|
354 |
+
nps = to_list(model_prompt_dict[model_name]["negative_prompt"])
|
355 |
+
else:
|
356 |
+
ps = default_ps
|
357 |
+
nps = default_nps
|
358 |
+
prompts = prompts + ps
|
359 |
+
neg_prompts = neg_prompts + nps
|
360 |
+
prompt = ", ".join(list_uniq(prompts) + last_empty_p)
|
361 |
+
neg_prompt = ", ".join(list_uniq(neg_prompts) + last_empty_np)
|
362 |
+
return prompt, neg_prompt
|
363 |
+
|
364 |
+
|
365 |
+
tag_group_dict = load_dict_from_csv('tag_group.csv')
|
366 |
+
|
367 |
+
|
368 |
+
def remove_specific_prompt(input_prompt: str = "", keep_tags: str = "all"):
|
369 |
+
def is_dressed(tag):
|
370 |
+
import re
|
371 |
+
p = re.compile(r'dress|cloth|uniform|costume|vest|sweater|coat|shirt|jacket|blazer|apron|leotard|hood|sleeve|skirt|shorts|pant|loafer|ribbon|necktie|bow|collar|glove|sock|shoe|boots|wear|emblem')
|
372 |
+
return p.search(tag)
|
373 |
+
|
374 |
+
def is_background(tag):
|
375 |
+
import re
|
376 |
+
p = re.compile(r'background|outline|light|sky|build|day|screen|tree|city')
|
377 |
+
return p.search(tag)
|
378 |
+
|
379 |
+
un_tags = ['solo']
|
380 |
+
group_list = ['groups', 'body_parts', 'attire', 'posture', 'objects', 'creatures', 'locations', 'disambiguation_pages', 'commonly_misused_tags', 'phrases', 'verbs_and_gerunds', 'subjective', 'nudity', 'sex_objects', 'sex', 'sex_acts', 'image_composition', 'artistic_license', 'text', 'year_tags', 'metatags']
|
381 |
+
keep_group_dict = {
|
382 |
+
"body": ['groups', 'body_parts'],
|
383 |
+
"dress": ['groups', 'body_parts', 'attire'],
|
384 |
+
"all": group_list,
|
385 |
+
}
|
386 |
+
|
387 |
+
def is_necessary(tag, keep_tags, group_dict):
|
388 |
+
if keep_tags == "all":
|
389 |
+
return True
|
390 |
+
elif tag in un_tags or group_dict.get(tag, "") in explicit_group:
|
391 |
+
return False
|
392 |
+
elif keep_tags == "body" and is_dressed(tag):
|
393 |
+
return False
|
394 |
+
elif is_background(tag):
|
395 |
+
return False
|
396 |
+
else:
|
397 |
+
return True
|
398 |
+
|
399 |
+
if keep_tags == "all": return input_prompt
|
400 |
+
keep_group = keep_group_dict.get(keep_tags, keep_group_dict["body"])
|
401 |
+
explicit_group = list(set(group_list) ^ set(keep_group))
|
402 |
+
|
403 |
+
tags = input_prompt.split(",") if input_prompt else []
|
404 |
+
people_tags: list[str] = []
|
405 |
+
other_tags: list[str] = []
|
406 |
+
|
407 |
+
group_dict = tag_group_dict
|
408 |
+
for tag in tags:
|
409 |
+
tag = replace_underline(tag)
|
410 |
+
if tag in PEOPLE_TAGS:
|
411 |
+
people_tags.append(tag)
|
412 |
+
elif is_necessary(tag, keep_tags, group_dict):
|
413 |
+
other_tags.append(tag)
|
414 |
+
|
415 |
+
output_prompt = ", ".join(people_tags + other_tags)
|
416 |
+
|
417 |
+
return output_prompt
|
418 |
+
|
419 |
+
|
420 |
+
def sort_taglist(tags: list[str]):
|
421 |
+
if not tags: return []
|
422 |
+
character_tags: list[str] = []
|
423 |
+
series_tags: list[str] = []
|
424 |
+
people_tags: list[str] = []
|
425 |
+
group_list = ['groups', 'body_parts', 'attire', 'posture', 'objects', 'creatures', 'locations', 'disambiguation_pages', 'commonly_misused_tags', 'phrases', 'verbs_and_gerunds', 'subjective', 'nudity', 'sex_objects', 'sex', 'sex_acts', 'image_composition', 'artistic_license', 'text', 'year_tags', 'metatags']
|
426 |
+
group_tags = {}
|
427 |
+
other_tags: list[str] = []
|
428 |
+
rating_tags: list[str] = []
|
429 |
+
|
430 |
+
group_dict = tag_group_dict
|
431 |
+
group_set = set(group_dict.keys())
|
432 |
+
character_set = set(anime_series_dict.keys())
|
433 |
+
series_set = set(anime_series_dict.values())
|
434 |
+
rating_set = set(DANBOORU_TO_E621_RATING_MAP.keys()) | set(DANBOORU_TO_E621_RATING_MAP.values())
|
435 |
+
|
436 |
+
for tag in tags:
|
437 |
+
tag = replace_underline(tag)
|
438 |
+
if tag in PEOPLE_TAGS:
|
439 |
+
people_tags.append(tag)
|
440 |
+
elif tag in rating_set:
|
441 |
+
rating_tags.append(tag)
|
442 |
+
elif tag in group_set:
|
443 |
+
elem = group_dict[tag]
|
444 |
+
group_tags[elem] = group_tags[elem] + [tag] if elem in group_tags else [tag]
|
445 |
+
elif tag in character_set:
|
446 |
+
character_tags.append(tag)
|
447 |
+
elif tag in series_set:
|
448 |
+
series_tags.append(tag)
|
449 |
+
else:
|
450 |
+
other_tags.append(tag)
|
451 |
+
|
452 |
+
output_group_tags: list[str] = []
|
453 |
+
for k in group_list:
|
454 |
+
output_group_tags.extend(group_tags.get(k, []))
|
455 |
+
|
456 |
+
rating_tags = [rating_tags[0]] if rating_tags else []
|
457 |
+
rating_tags = ["explicit, nsfw"] if rating_tags and rating_tags[0] == "explicit" else rating_tags
|
458 |
+
|
459 |
+
output_tags = character_tags + series_tags + people_tags + output_group_tags + other_tags + rating_tags
|
460 |
+
|
461 |
+
return output_tags
|
462 |
+
|
463 |
+
|
464 |
+
def sort_tags(tags: str):
|
465 |
+
if not tags: return ""
|
466 |
+
taglist: list[str] = []
|
467 |
+
for tag in tags.split(","):
|
468 |
+
taglist.append(tag.strip())
|
469 |
+
taglist = list(filter(lambda x: x != "", taglist))
|
470 |
+
return ", ".join(sort_taglist(taglist))
|
471 |
+
|
472 |
+
|
473 |
+
def postprocess_results(results: dict[str, float], general_threshold: float, character_threshold: float):
|
474 |
+
results = {
|
475 |
+
k: v for k, v in sorted(results.items(), key=lambda item: item[1], reverse=True)
|
476 |
+
}
|
477 |
+
|
478 |
+
rating = {}
|
479 |
+
character = {}
|
480 |
+
general = {}
|
481 |
+
|
482 |
+
for k, v in results.items():
|
483 |
+
if k.startswith("rating:"):
|
484 |
+
rating[k.replace("rating:", "")] = v
|
485 |
+
continue
|
486 |
+
elif k.startswith("character:"):
|
487 |
+
character[k.replace("character:", "")] = v
|
488 |
+
continue
|
489 |
+
|
490 |
+
general[k] = v
|
491 |
+
|
492 |
+
character = {k: v for k, v in character.items() if v >= character_threshold}
|
493 |
+
general = {k: v for k, v in general.items() if v >= general_threshold}
|
494 |
+
|
495 |
+
return rating, character, general
|
496 |
+
|
497 |
+
|
498 |
+
def gen_prompt(rating: list[str], character: list[str], general: list[str]):
|
499 |
+
people_tags: list[str] = []
|
500 |
+
other_tags: list[str] = []
|
501 |
+
rating_tag = RATING_MAP[rating[0]]
|
502 |
+
|
503 |
+
for tag in general:
|
504 |
+
if tag in PEOPLE_TAGS:
|
505 |
+
people_tags.append(tag)
|
506 |
+
else:
|
507 |
+
other_tags.append(tag)
|
508 |
+
|
509 |
+
all_tags = people_tags + other_tags
|
510 |
+
|
511 |
+
return ", ".join(all_tags)
|
512 |
+
|
513 |
+
|
514 |
+
@spaces.GPU(duration=30)
|
515 |
+
def predict_tags(image: Image.Image, general_threshold: float = 0.3, character_threshold: float = 0.8):
|
516 |
+
inputs = wd_processor.preprocess(image, return_tensors="pt")
|
517 |
+
|
518 |
+
outputs = wd_model(**inputs.to(wd_model.device, wd_model.dtype))
|
519 |
+
logits = torch.sigmoid(outputs.logits[0]) # take the first logits
|
520 |
+
|
521 |
+
# get probabilities
|
522 |
+
if device != default_device: wd_model.to(device=device)
|
523 |
+
results = {
|
524 |
+
wd_model.config.id2label[i]: float(logit.float()) for i, logit in enumerate(logits)
|
525 |
+
}
|
526 |
+
if device != default_device: wd_model.to(device=default_device)
|
527 |
+
# rating, character, general
|
528 |
+
rating, character, general = postprocess_results(
|
529 |
+
results, general_threshold, character_threshold
|
530 |
+
)
|
531 |
+
prompt = gen_prompt(
|
532 |
+
list(rating.keys()), list(character.keys()), list(general.keys())
|
533 |
+
)
|
534 |
+
output_series_tag = ""
|
535 |
+
output_series_list = character_list_to_series_list(character.keys())
|
536 |
+
if output_series_list:
|
537 |
+
output_series_tag = output_series_list[0]
|
538 |
+
else:
|
539 |
+
output_series_tag = ""
|
540 |
+
return output_series_tag, ", ".join(character.keys()), prompt, gr.update(interactive=True)
|
541 |
+
|
542 |
+
|
543 |
+
def predict_tags_wd(image: Image.Image, input_tags: str, algo: list[str], general_threshold: float = 0.3,
|
544 |
+
character_threshold: float = 0.8, input_series: str = "", input_character: str = ""):
|
545 |
+
if not "Use WD Tagger" in algo and len(algo) != 0:
|
546 |
+
return input_series, input_character, input_tags, gr.update(interactive=True)
|
547 |
+
return predict_tags(image, general_threshold, character_threshold)
|
548 |
+
|
549 |
+
|
550 |
+
def compose_prompt_to_copy(character: str, series: str, general: str):
|
551 |
+
characters = character.split(",") if character else []
|
552 |
+
serieses = series.split(",") if series else []
|
553 |
+
generals = general.split(",") if general else []
|
554 |
+
tags = characters + serieses + generals
|
555 |
+
cprompt = ",".join(tags) if tags else ""
|
556 |
+
return cprompt
|
tagger/utils.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from dartrs.v2 import AspectRatioTag, LengthTag, RatingTag, IdentityTag
|
3 |
+
|
4 |
+
|
5 |
+
V2_ASPECT_RATIO_OPTIONS: list[AspectRatioTag] = [
|
6 |
+
"ultra_wide",
|
7 |
+
"wide",
|
8 |
+
"square",
|
9 |
+
"tall",
|
10 |
+
"ultra_tall",
|
11 |
+
]
|
12 |
+
V2_RATING_OPTIONS: list[RatingTag] = [
|
13 |
+
"sfw",
|
14 |
+
"general",
|
15 |
+
"sensitive",
|
16 |
+
"nsfw",
|
17 |
+
"questionable",
|
18 |
+
"explicit",
|
19 |
+
]
|
20 |
+
V2_LENGTH_OPTIONS: list[LengthTag] = [
|
21 |
+
"very_short",
|
22 |
+
"short",
|
23 |
+
"medium",
|
24 |
+
"long",
|
25 |
+
"very_long",
|
26 |
+
]
|
27 |
+
V2_IDENTITY_OPTIONS: list[IdentityTag] = [
|
28 |
+
"none",
|
29 |
+
"lax",
|
30 |
+
"strict",
|
31 |
+
]
|
32 |
+
|
33 |
+
|
34 |
+
# ref: https://qiita.com/tregu148/items/fccccbbc47d966dd2fc2
|
35 |
+
def gradio_copy_text(_text: None):
|
36 |
+
gr.Info("Copied!")
|
37 |
+
|
38 |
+
|
39 |
+
COPY_ACTION_JS = """\
|
40 |
+
(inputs, _outputs) => {
|
41 |
+
// inputs is the string value of the input_text
|
42 |
+
if (inputs.trim() !== "") {
|
43 |
+
navigator.clipboard.writeText(inputs);
|
44 |
+
}
|
45 |
+
}"""
|
46 |
+
|
47 |
+
|
48 |
+
def gradio_copy_prompt(prompt: str):
|
49 |
+
gr.Info("Copied!")
|
50 |
+
return prompt
|
tagger/v2.py
ADDED
@@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import torch
|
3 |
+
from typing import Callable
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
from dartrs.v2 import (
|
7 |
+
V2Model,
|
8 |
+
MixtralModel,
|
9 |
+
MistralModel,
|
10 |
+
compose_prompt,
|
11 |
+
LengthTag,
|
12 |
+
AspectRatioTag,
|
13 |
+
RatingTag,
|
14 |
+
IdentityTag,
|
15 |
+
)
|
16 |
+
from dartrs.dartrs import DartTokenizer
|
17 |
+
from dartrs.utils import get_generation_config
|
18 |
+
|
19 |
+
|
20 |
+
import gradio as gr
|
21 |
+
from gradio.components import Component
|
22 |
+
|
23 |
+
|
24 |
+
try:
|
25 |
+
from output import UpsamplingOutput
|
26 |
+
except:
|
27 |
+
from .output import UpsamplingOutput
|
28 |
+
|
29 |
+
|
30 |
+
V2_ALL_MODELS = {
|
31 |
+
"dart-v2-moe-sft": {
|
32 |
+
"repo": "p1atdev/dart-v2-moe-sft",
|
33 |
+
"type": "sft",
|
34 |
+
"class": MixtralModel,
|
35 |
+
},
|
36 |
+
"dart-v2-sft": {
|
37 |
+
"repo": "p1atdev/dart-v2-sft",
|
38 |
+
"type": "sft",
|
39 |
+
"class": MistralModel,
|
40 |
+
},
|
41 |
+
}
|
42 |
+
|
43 |
+
|
44 |
+
def prepare_models(model_config: dict):
|
45 |
+
model_name = model_config["repo"]
|
46 |
+
tokenizer = DartTokenizer.from_pretrained(model_name)
|
47 |
+
model = model_config["class"].from_pretrained(model_name)
|
48 |
+
|
49 |
+
return {
|
50 |
+
"tokenizer": tokenizer,
|
51 |
+
"model": model,
|
52 |
+
}
|
53 |
+
|
54 |
+
|
55 |
+
def normalize_tags(tokenizer: DartTokenizer, tags: str):
|
56 |
+
"""Just remove unk tokens."""
|
57 |
+
return ", ".join([tag for tag in tokenizer.tokenize(tags) if tag != "<|unk|>"])
|
58 |
+
|
59 |
+
|
60 |
+
@torch.no_grad()
|
61 |
+
def generate_tags(
|
62 |
+
model: V2Model,
|
63 |
+
tokenizer: DartTokenizer,
|
64 |
+
prompt: str,
|
65 |
+
ban_token_ids: list[int],
|
66 |
+
):
|
67 |
+
output = model.generate(
|
68 |
+
get_generation_config(
|
69 |
+
prompt,
|
70 |
+
tokenizer=tokenizer,
|
71 |
+
temperature=1,
|
72 |
+
top_p=0.9,
|
73 |
+
top_k=100,
|
74 |
+
max_new_tokens=256,
|
75 |
+
ban_token_ids=ban_token_ids,
|
76 |
+
),
|
77 |
+
)
|
78 |
+
|
79 |
+
return output
|
80 |
+
|
81 |
+
|
82 |
+
def _people_tag(noun: str, minimum: int = 1, maximum: int = 5):
|
83 |
+
return (
|
84 |
+
[f"1{noun}"]
|
85 |
+
+ [f"{num}{noun}s" for num in range(minimum + 1, maximum + 1)]
|
86 |
+
+ [f"{maximum+1}+{noun}s"]
|
87 |
+
)
|
88 |
+
|
89 |
+
|
90 |
+
PEOPLE_TAGS = (
|
91 |
+
_people_tag("girl") + _people_tag("boy") + _people_tag("other") + ["no humans"]
|
92 |
+
)
|
93 |
+
|
94 |
+
|
95 |
+
def gen_prompt_text(output: UpsamplingOutput):
|
96 |
+
# separate people tags (e.g. 1girl)
|
97 |
+
people_tags = []
|
98 |
+
other_general_tags = []
|
99 |
+
|
100 |
+
for tag in output.general_tags.split(","):
|
101 |
+
tag = tag.strip()
|
102 |
+
if tag in PEOPLE_TAGS:
|
103 |
+
people_tags.append(tag)
|
104 |
+
else:
|
105 |
+
other_general_tags.append(tag)
|
106 |
+
|
107 |
+
return ", ".join(
|
108 |
+
[
|
109 |
+
part.strip()
|
110 |
+
for part in [
|
111 |
+
*people_tags,
|
112 |
+
output.character_tags,
|
113 |
+
output.copyright_tags,
|
114 |
+
*other_general_tags,
|
115 |
+
output.upsampled_tags,
|
116 |
+
output.rating_tag,
|
117 |
+
]
|
118 |
+
if part.strip() != ""
|
119 |
+
]
|
120 |
+
)
|
121 |
+
|
122 |
+
|
123 |
+
def elapsed_time_format(elapsed_time: float) -> str:
|
124 |
+
return f"Elapsed: {elapsed_time:.2f} seconds"
|
125 |
+
|
126 |
+
|
127 |
+
def parse_upsampling_output(
|
128 |
+
upsampler: Callable[..., UpsamplingOutput],
|
129 |
+
):
|
130 |
+
def _parse_upsampling_output(*args) -> tuple[str, str, dict]:
|
131 |
+
output = upsampler(*args)
|
132 |
+
|
133 |
+
return (
|
134 |
+
gen_prompt_text(output),
|
135 |
+
elapsed_time_format(output.elapsed_time),
|
136 |
+
gr.update(interactive=True),
|
137 |
+
gr.update(interactive=True),
|
138 |
+
)
|
139 |
+
|
140 |
+
return _parse_upsampling_output
|
141 |
+
|
142 |
+
|
143 |
+
class V2UI:
|
144 |
+
model_name: str | None = None
|
145 |
+
model: V2Model
|
146 |
+
tokenizer: DartTokenizer
|
147 |
+
|
148 |
+
input_components: list[Component] = []
|
149 |
+
generate_btn: gr.Button
|
150 |
+
|
151 |
+
def on_generate(
|
152 |
+
self,
|
153 |
+
model_name: str,
|
154 |
+
copyright_tags: str,
|
155 |
+
character_tags: str,
|
156 |
+
general_tags: str,
|
157 |
+
rating_tag: RatingTag,
|
158 |
+
aspect_ratio_tag: AspectRatioTag,
|
159 |
+
length_tag: LengthTag,
|
160 |
+
identity_tag: IdentityTag,
|
161 |
+
ban_tags: str,
|
162 |
+
*args,
|
163 |
+
) -> UpsamplingOutput:
|
164 |
+
if self.model_name is None or self.model_name != model_name:
|
165 |
+
models = prepare_models(V2_ALL_MODELS[model_name])
|
166 |
+
self.model = models["model"]
|
167 |
+
self.tokenizer = models["tokenizer"]
|
168 |
+
self.model_name = model_name
|
169 |
+
|
170 |
+
# normalize tags
|
171 |
+
# copyright_tags = normalize_tags(self.tokenizer, copyright_tags)
|
172 |
+
# character_tags = normalize_tags(self.tokenizer, character_tags)
|
173 |
+
# general_tags = normalize_tags(self.tokenizer, general_tags)
|
174 |
+
|
175 |
+
ban_token_ids = self.tokenizer.encode(ban_tags.strip())
|
176 |
+
|
177 |
+
prompt = compose_prompt(
|
178 |
+
prompt=general_tags,
|
179 |
+
copyright=copyright_tags,
|
180 |
+
character=character_tags,
|
181 |
+
rating=rating_tag,
|
182 |
+
aspect_ratio=aspect_ratio_tag,
|
183 |
+
length=length_tag,
|
184 |
+
identity=identity_tag,
|
185 |
+
)
|
186 |
+
|
187 |
+
start = time.time()
|
188 |
+
upsampled_tags = generate_tags(
|
189 |
+
self.model,
|
190 |
+
self.tokenizer,
|
191 |
+
prompt,
|
192 |
+
ban_token_ids,
|
193 |
+
)
|
194 |
+
elapsed_time = time.time() - start
|
195 |
+
|
196 |
+
return UpsamplingOutput(
|
197 |
+
upsampled_tags=upsampled_tags,
|
198 |
+
copyright_tags=copyright_tags,
|
199 |
+
character_tags=character_tags,
|
200 |
+
general_tags=general_tags,
|
201 |
+
rating_tag=rating_tag,
|
202 |
+
aspect_ratio_tag=aspect_ratio_tag,
|
203 |
+
length_tag=length_tag,
|
204 |
+
identity_tag=identity_tag,
|
205 |
+
elapsed_time=elapsed_time,
|
206 |
+
)
|
207 |
+
|
208 |
+
|
209 |
+
def parse_upsampling_output_simple(upsampler: UpsamplingOutput):
|
210 |
+
return gen_prompt_text(upsampler)
|
211 |
+
|
212 |
+
|
213 |
+
v2 = V2UI()
|
214 |
+
|
215 |
+
|
216 |
+
def v2_upsampling_prompt(model: str = "dart-v2-moe-sft", copyright: str = "", character: str = "",
|
217 |
+
general_tags: str = "", rating: str = "nsfw", aspect_ratio: str = "square",
|
218 |
+
length: str = "very_long", identity: str = "lax", ban_tags: str = "censored"):
|
219 |
+
raw_prompt = parse_upsampling_output_simple(v2.on_generate(model, copyright, character, general_tags,
|
220 |
+
rating, aspect_ratio, length, identity, ban_tags))
|
221 |
+
return raw_prompt
|
222 |
+
|
223 |
+
|
224 |
+
def load_dict_from_csv(filename):
|
225 |
+
dict = {}
|
226 |
+
if not Path(filename).exists():
|
227 |
+
if Path('./tagger/', filename).exists(): filename = str(Path('./tagger/', filename))
|
228 |
+
else: return dict
|
229 |
+
try:
|
230 |
+
with open(filename, 'r', encoding="utf-8") as f:
|
231 |
+
lines = f.readlines()
|
232 |
+
except Exception:
|
233 |
+
print(f"Failed to open dictionary file: {filename}")
|
234 |
+
return dict
|
235 |
+
for line in lines:
|
236 |
+
parts = line.strip().split(',')
|
237 |
+
dict[parts[0]] = parts[1]
|
238 |
+
return dict
|
239 |
+
|
240 |
+
|
241 |
+
anime_series_dict = load_dict_from_csv('character_series_dict.csv')
|
242 |
+
|
243 |
+
|
244 |
+
def select_random_character(series: str, character: str):
|
245 |
+
from random import seed, randrange
|
246 |
+
seed()
|
247 |
+
character_list = list(anime_series_dict.keys())
|
248 |
+
character = character_list[randrange(len(character_list) - 1)]
|
249 |
+
series = anime_series_dict.get(character.split(",")[0].strip(), "")
|
250 |
+
return series, character
|
251 |
+
|
252 |
+
|
253 |
+
def v2_random_prompt(general_tags: str = "", copyright: str = "", character: str = "", rating: str = "nsfw",
|
254 |
+
aspect_ratio: str = "square", length: str = "very_long", identity: str = "lax",
|
255 |
+
ban_tags: str = "censored", model: str = "dart-v2-moe-sft"):
|
256 |
+
if copyright == "" and character == "":
|
257 |
+
copyright, character = select_random_character("", "")
|
258 |
+
raw_prompt = v2_upsampling_prompt(model, copyright, character, general_tags, rating,
|
259 |
+
aspect_ratio, length, identity, ban_tags)
|
260 |
+
return raw_prompt, copyright, character
|