Files changed (1) hide show
  1. app.py +423 -422
app.py CHANGED
@@ -1,422 +1,423 @@
1
- import spaces
2
- import gradio as gr
3
- import numpy as np
4
-
5
- # DiffuseCraft
6
- from dc import (infer, _infer, pass_result, get_diffusers_model_list, get_samplers,
7
- get_vaes, enable_model_recom_prompt, enable_diffusers_model_detail,
8
- get_t2i_model_info, get_all_lora_tupled_list, update_loras,
9
- apply_lora_prompt, download_my_lora, search_civitai_lora,
10
- select_civitai_lora, search_civitai_lora_json, extract_exif_data, esrgan_upscale, UPSCALER_KEYS,
11
- preset_quality, preset_styles, process_style_prompt)
12
- # Translator
13
- from llmdolphin import (dolphin_respond_auto, dolphin_parse_simple,
14
- get_llm_formats, get_dolphin_model_format, get_dolphin_models,
15
- get_dolphin_model_info, select_dolphin_model, select_dolphin_format, get_dolphin_sysprompt)
16
- # Tagger
17
- from tagger.v2 import v2_upsampling_prompt, V2_ALL_MODELS
18
- from tagger.utils import (gradio_copy_text, gradio_copy_prompt, COPY_ACTION_JS,
19
- V2_ASPECT_RATIO_OPTIONS, V2_RATING_OPTIONS, V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
20
- from tagger.tagger import (predict_tags_wd, convert_danbooru_to_e621_prompt,
21
- remove_specific_prompt, insert_recom_prompt, compose_prompt_to_copy,
22
- translate_prompt, select_random_character)
23
- from tagger.fl2sd3longcap import predict_tags_fl2_sd3
24
- def description_ui():
25
- gr.Markdown(
26
- """
27
- ## Danbooru Tags Transformer V2 Demo with WD Tagger & SD3 Long Captioner
28
- (Image =>) Prompt => Upsampled longer prompt
29
- - Mod of p1atdev's [Danbooru Tags Transformer V2 Demo](https://huggingface.co/spaces/p1atdev/danbooru-tags-transformer-v2) and [WD Tagger with πŸ€— transformers](https://huggingface.co/spaces/p1atdev/wd-tagger-transformers).
30
- - Models: p1atdev's [wd-swinv2-tagger-v3-hf](https://huggingface.co/p1atdev/wd-swinv2-tagger-v3-hf), [dart-v2-moe-sft](https://huggingface.co/p1atdev/dart-v2-moe-sft), [dart-v2-sft](https://huggingface.co/p1atdev/dart-v2-sft)\
31
- , gokaygokay's [Florence-2-SD3-Captioner](https://huggingface.co/gokaygokay/Florence-2-SD3-Captioner)
32
- """
33
- )
34
-
35
-
36
- MAX_SEED = np.iinfo(np.int32).max
37
- MAX_IMAGE_SIZE = 1216
38
-
39
- css = """
40
- #container { margin: 0 auto; !important; }
41
- #col-container { margin: 0 auto; !important; }
42
- #result { max-width: 520px; max-height: 520px; margin: 0px auto; !important; }
43
- .lora { min-width: 480px; !important; }
44
- #model-info { text-align: center; !important; }
45
- """
46
-
47
- with gr.Blocks(fill_width=True, elem_id="container", css=css, delete_cache=(60, 3600)) as demo:
48
- with gr.Tab("Image Generator"):
49
- with gr.Column(elem_id="col-container"):
50
- with gr.Row():
51
- prompt = gr.Text(label="Prompt", show_label=False, lines=1, max_lines=8, placeholder="Enter your prompt", container=False)
52
-
53
- with gr.Row():
54
- run_button = gr.Button("Run", variant="primary", scale=5)
55
- run_translate_button = gr.Button("Run with LLM Enhance", variant="secondary", scale=3)
56
- auto_trans = gr.Checkbox(label="Auto translate to English", value=False, scale=2)
57
-
58
- result = gr.Image(label="Result", elem_id="result", format="png", show_label=False, interactive=False,
59
- show_download_button=True, show_share_button=False, container=True)
60
-
61
- with gr.Accordion("Advanced Settings", open=False):
62
- with gr.Row():
63
- negative_prompt = gr.Text(label="Negative prompt", lines=1, max_lines=6, placeholder="Enter a negative prompt",
64
- value="(low quality, worst quality:1.2), very displeasing, watermark, signature, ugly")
65
-
66
- with gr.Row():
67
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
68
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
69
-
70
- with gr.Row():
71
- width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 832
72
- height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 1216
73
- guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=30.0, step=0.1, value=7)
74
- num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=100, step=1, value=28)
75
-
76
- with gr.Row():
77
- with gr.Column(scale=4):
78
- model_name = gr.Dropdown(label="Model", info="You can enter a huggingface model repo_id to want to use.",
79
- choices=get_diffusers_model_list(), value=get_diffusers_model_list()[0],
80
- allow_custom_value=True, interactive=True, min_width=320)
81
- model_info = gr.Markdown(elem_id="model-info")
82
- with gr.Column(scale=1):
83
- model_detail = gr.Checkbox(label="Show detail of model in list", value=False)
84
-
85
- with gr.Row():
86
- sampler = gr.Dropdown(label="Sampler", choices=get_samplers(), value="Euler a")
87
- vae_model = gr.Dropdown(label="VAE Model", choices=get_vaes(), value=get_vaes()[0])
88
-
89
- with gr.Accordion("LoRA", open=True, visible=True):
90
- def lora_dropdown(label):
91
- return gr.Dropdown(label=label, choices=get_all_lora_tupled_list(), value="", allow_custom_value=True, elem_classes="lora", min_width=320)
92
-
93
- def lora_scale_slider(label):
94
- return gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label=label)
95
-
96
- def lora_textbox():
97
- return gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
98
-
99
- with gr.Row():
100
- with gr.Column():
101
- with gr.Row():
102
- lora1 = lora_dropdown("LoRA 1")
103
- lora1_wt = lora_scale_slider("LoRA 1: weight")
104
- with gr.Row():
105
- lora1_info = lora_textbox()
106
- lora1_copy = gr.Button(value="Copy example to prompt", visible=False)
107
- lora1_md = gr.Markdown(value="", visible=False)
108
- with gr.Column():
109
- with gr.Row():
110
- lora2 = lora_dropdown("LoRA 2")
111
- lora2_wt = lora_scale_slider("LoRA 2: weight")
112
- with gr.Row():
113
- lora2_info = lora_textbox()
114
- lora2_copy = gr.Button(value="Copy example to prompt", visible=False)
115
- lora2_md = gr.Markdown(value="", visible=False)
116
- with gr.Column():
117
- with gr.Row():
118
- lora3 = lora_dropdown("LoRA 3")
119
- lora3_wt = lora_scale_slider("LoRA 3: weight")
120
- with gr.Row():
121
- lora3_info = lora_textbox()
122
- lora3_copy = gr.Button(value="Copy example to prompt", visible=False)
123
- lora3_md = gr.Markdown(value="", visible=False)
124
- with gr.Column():
125
- with gr.Row():
126
- lora4 = lora_dropdown("LoRA 4")
127
- lora4_wt = lora_scale_slider("LoRA 4: weight")
128
- with gr.Row():
129
- lora4_info = lora_textbox()
130
- lora4_copy = gr.Button(value="Copy example to prompt", visible=False)
131
- lora4_md = gr.Markdown(value="", visible=False)
132
- with gr.Column():
133
- with gr.Row():
134
- lora5 = lora_dropdown("LoRA 5")
135
- lora5_wt = lora_scale_slider("LoRA 5: weight")
136
- with gr.Row():
137
- lora5_info = lora_textbox()
138
- lora5_copy = gr.Button(value="Copy example to prompt", visible=False)
139
- lora5_md = gr.Markdown(value="", visible=False)
140
- with gr.Accordion("From URL", open=True, visible=True):
141
- with gr.Row():
142
- lora_search_civitai_basemodel = gr.CheckboxGroup(label="Search LoRA for", choices=["Pony", "SD 1.5", "SDXL 1.0", "Flux.1 D", "Flux.1 S"], value=["Pony", "SDXL 1.0"])
143
- lora_search_civitai_sort = gr.Radio(label="Sort", choices=["Highest Rated", "Most Downloaded", "Newest"], value="Highest Rated")
144
- lora_search_civitai_period = gr.Radio(label="Period", choices=["AllTime", "Year", "Month", "Week", "Day"], value="AllTime")
145
- with gr.Row():
146
- lora_search_civitai_query = gr.Textbox(label="Query", placeholder="oomuro sakurako...", lines=1)
147
- lora_search_civitai_tag = gr.Textbox(label="Tag", lines=1)
148
- lora_search_civitai_submit = gr.Button("Search on Civitai")
149
- with gr.Row():
150
- lora_search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
151
- lora_search_civitai_json = gr.JSON(value={}, visible=False)
152
- lora_search_civitai_desc = gr.Markdown(value="", visible=False)
153
- lora_download_url = gr.Textbox(label="LoRA URL", placeholder="https://civitai.com/api/download/models/28907", lines=1)
154
- lora_download = gr.Button("Get and set LoRA and apply to prompt")
155
-
156
- with gr.Row():
157
- quality_selector = gr.Radio(label="Quality Tag Presets", interactive=True, choices=list(preset_quality.keys()), value="None", scale=3)
158
- style_selector = gr.Radio(label="Style Presets", interactive=True, choices=list(preset_styles.keys()), value="None", scale=3)
159
- recom_prompt = gr.Checkbox(label="Recommended prompt", value=True, scale=1)
160
-
161
- with gr.Accordion("Translation Settings", open=False):
162
- chatbot = gr.Chatbot(render_markdown=False, visible=False) # component for auto-translation
163
- chat_model = gr.Dropdown(choices=get_dolphin_models(), value=get_dolphin_models()[0][1], allow_custom_value=True, label="Model")
164
- chat_model_info = gr.Markdown(value=get_dolphin_model_info(get_dolphin_models()[0][1]), label="Model info")
165
- chat_format = gr.Dropdown(choices=get_llm_formats(), value=get_dolphin_model_format(get_dolphin_models()[0][1]), label="Message format")
166
- with gr.Row():
167
- chat_tokens = gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max tokens")
168
- chat_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
169
- chat_topp = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
170
- chat_topk = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k")
171
- chat_rp = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
172
- chat_sysmsg = gr.Textbox(value=get_dolphin_sysprompt(), label="System message")
173
-
174
- examples = gr.Examples(
175
- examples = [
176
- ["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"],
177
- ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
178
- ["kafuu chino, 1girl, solo"],
179
- ["1girl"],
180
- ["beautiful sunset"],
181
- ],
182
- inputs=[prompt],
183
- cache_examples=False,
184
- )
185
-
186
- gr.on( #lambda x: None, inputs=None, outputs=result).then(
187
- triggers=[run_button.click, prompt.submit],
188
- fn=infer,
189
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
190
- guidance_scale, num_inference_steps, model_name,
191
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
192
- sampler, vae_model, auto_trans],
193
- outputs=[result],
194
- queue=True,
195
- show_progress="full",
196
- show_api=True,
197
- )
198
-
199
- gr.on( #lambda x: None, inputs=None, outputs=result).then(
200
- triggers=[run_translate_button.click],
201
- fn=_infer, # dummy fn for api
202
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
203
- guidance_scale, num_inference_steps, model_name,
204
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
205
- sampler, vae_model, auto_trans],
206
- outputs=[result],
207
- queue=False,
208
- show_api=True,
209
- api_name="infer_translate",
210
- ).success(
211
- fn=dolphin_respond_auto,
212
- inputs=[prompt, chatbot],
213
- outputs=[chatbot],
214
- queue=True,
215
- show_progress="full",
216
- show_api=False,
217
- ).success(
218
- fn=dolphin_parse_simple,
219
- inputs=[prompt, chatbot],
220
- outputs=[prompt],
221
- queue=False,
222
- show_api=False,
223
- ).success(
224
- fn=infer,
225
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
226
- guidance_scale, num_inference_steps, model_name,
227
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
228
- sampler, vae_model],
229
- outputs=[result],
230
- queue=True,
231
- show_progress="full",
232
- show_api=False,
233
- ).success(lambda: None, None, chatbot, queue=False, show_api=False)\
234
- .success(pass_result, [result], [result], queue=False, show_api=False) # dummy fn for api
235
-
236
- gr.on(
237
- triggers=[lora1.change, lora1_wt.change, lora2.change, lora2_wt.change, lora3.change, lora3_wt.change,
238
- lora4.change, lora4_wt.change, lora5.change, lora5_wt.change],
239
- fn=update_loras,
240
- inputs=[prompt, lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt],
241
- outputs=[prompt, lora1, lora1_wt, lora1_info, lora1_copy, lora1_md,
242
- lora2, lora2_wt, lora2_info, lora2_copy, lora2_md, lora3, lora3_wt, lora3_info, lora3_copy, lora3_md,
243
- lora4, lora4_wt, lora4_info, lora4_copy, lora4_md, lora5, lora5_wt, lora5_info, lora5_copy, lora5_md],
244
- queue=False,
245
- trigger_mode="once",
246
- show_api=False,
247
- )
248
- lora1_copy.click(apply_lora_prompt, [prompt, lora1_info], [prompt], queue=False, show_api=False)
249
- lora2_copy.click(apply_lora_prompt, [prompt, lora2_info], [prompt], queue=False, show_api=False)
250
- lora3_copy.click(apply_lora_prompt, [prompt, lora3_info], [prompt], queue=False, show_api=False)
251
- lora4_copy.click(apply_lora_prompt, [prompt, lora4_info], [prompt], queue=False, show_api=False)
252
- lora5_copy.click(apply_lora_prompt, [prompt, lora5_info], [prompt], queue=False, show_api=False)
253
-
254
- gr.on(
255
- triggers=[lora_search_civitai_submit.click, lora_search_civitai_query.submit, lora_search_civitai_tag.submit],
256
- fn=search_civitai_lora,
257
- inputs=[lora_search_civitai_query, lora_search_civitai_basemodel, lora_search_civitai_sort, lora_search_civitai_period, lora_search_civitai_tag],
258
- outputs=[lora_search_civitai_result, lora_search_civitai_desc, lora_search_civitai_submit, lora_search_civitai_query],
259
- scroll_to_output=True,
260
- queue=True,
261
- show_api=False,
262
- )
263
- lora_search_civitai_json.change(search_civitai_lora_json, [lora_search_civitai_query, lora_search_civitai_basemodel], [lora_search_civitai_json], queue=True, show_api=True) # fn for api
264
- lora_search_civitai_result.change(select_civitai_lora, [lora_search_civitai_result], [lora_download_url, lora_search_civitai_desc], scroll_to_output=True, queue=False, show_api=False)
265
- gr.on(
266
- triggers=[lora_download.click, lora_download_url.submit],
267
- fn=download_my_lora,
268
- inputs=[lora_download_url,lora1, lora2, lora3, lora4, lora5],
269
- outputs=[lora1, lora2, lora3, lora4, lora5],
270
- scroll_to_output=True,
271
- queue=True,
272
- show_api=False,
273
- )
274
-
275
- recom_prompt.change(enable_model_recom_prompt, [recom_prompt], [recom_prompt], queue=False, show_api=False)
276
- gr.on(
277
- triggers=[quality_selector.change, style_selector.change],
278
- fn=process_style_prompt,
279
- inputs=[prompt, negative_prompt, style_selector, quality_selector],
280
- outputs=[prompt, negative_prompt],
281
- queue=False,
282
- trigger_mode="once",
283
- )
284
-
285
- model_detail.change(enable_diffusers_model_detail, [model_detail, model_name], [model_detail, model_name], queue=False, show_api=False)
286
- model_name.change(get_t2i_model_info, [model_name], [model_info], queue=False, show_api=False)
287
-
288
- chat_model.change(select_dolphin_model, [chat_model], [chat_model, chat_format, chat_model_info], queue=True, show_progress="full", show_api=False)\
289
- .success(lambda: None, None, chatbot, queue=False, show_api=False)
290
- chat_format.change(select_dolphin_format, [chat_format], [chat_format], queue=False, show_api=False)\
291
- .success(lambda: None, None, chatbot, queue=False, show_api=False)
292
-
293
- # Tagger
294
- with gr.Tab("Tags Transformer with Tagger"):
295
- with gr.Column():
296
- with gr.Group():
297
- input_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
298
- with gr.Accordion(label="Advanced options", open=False):
299
- general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
300
- character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
301
- input_tag_type = gr.Radio(label="Convert tags to", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
302
- recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
303
- image_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
304
- keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
305
- generate_from_image_btn = gr.Button(value="GENERATE TAGS FROM IMAGE", size="lg", variant="primary")
306
- with gr.Group():
307
- with gr.Row():
308
- input_character = gr.Textbox(label="Character tags", placeholder="hatsune miku")
309
- input_copyright = gr.Textbox(label="Copyright tags", placeholder="vocaloid")
310
- random_character = gr.Button(value="Random character 🎲", size="sm")
311
- input_general = gr.TextArea(label="General tags", lines=4, placeholder="1girl, ...", value="")
312
- input_tags_to_copy = gr.Textbox(value="", visible=False)
313
- with gr.Row():
314
- copy_input_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
315
- copy_prompt_btn_input = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
316
- translate_input_prompt_button = gr.Button(value="Translate prompt to English", size="sm", variant="secondary")
317
- tag_type = gr.Radio(label="Output tag conversion", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="e621", visible=False)
318
- input_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="explicit")
319
- with gr.Accordion(label="Advanced options", open=False):
320
- input_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square")
321
- input_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="very_long")
322
- input_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")
323
- input_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
324
- model_name = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
325
- dummy_np = gr.Textbox(label="Negative prompt", value="", visible=False)
326
- recom_animagine = gr.Textbox(label="Animagine reccomended prompt", value="Animagine", visible=False)
327
- recom_pony = gr.Textbox(label="Pony reccomended prompt", value="Pony", visible=False)
328
- generate_btn = gr.Button(value="GENERATE TAGS", size="lg", variant="primary")
329
- with gr.Row():
330
- with gr.Group():
331
- output_text = gr.TextArea(label="Output tags", interactive=False, show_copy_button=True)
332
- with gr.Row():
333
- copy_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
334
- copy_prompt_btn = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
335
- with gr.Group():
336
- output_text_pony = gr.TextArea(label="Output tags (Pony e621 style)", interactive=False, show_copy_button=True)
337
- with gr.Row():
338
- copy_btn_pony = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
339
- copy_prompt_btn_pony = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
340
-
341
- random_character.click(select_random_character, [input_copyright, input_character], [input_copyright, input_character], queue=False, show_api=False)
342
-
343
- translate_input_prompt_button.click(translate_prompt, [input_general], [input_general], queue=False, show_api=False)
344
- translate_input_prompt_button.click(translate_prompt, [input_character], [input_character], queue=False, show_api=False)
345
- translate_input_prompt_button.click(translate_prompt, [input_copyright], [input_copyright], queue=False, show_api=False)
346
-
347
- generate_from_image_btn.click(
348
- lambda: ("", "", ""), None, [input_copyright, input_character, input_general], queue=False, show_api=False,
349
- ).success(
350
- predict_tags_wd,
351
- [input_image, input_general, image_algorithms, general_threshold, character_threshold],
352
- [input_copyright, input_character, input_general, copy_input_btn],
353
- show_api=False,
354
- ).success(
355
- predict_tags_fl2_sd3, [input_image, input_general, image_algorithms], [input_general], show_api=False,
356
- ).success(
357
- remove_specific_prompt, [input_general, keep_tags], [input_general], queue=False, show_api=False,
358
- ).success(
359
- convert_danbooru_to_e621_prompt, [input_general, input_tag_type], [input_general], queue=False, show_api=False,
360
- ).success(
361
- insert_recom_prompt, [input_general, dummy_np, recom_prompt], [input_general, dummy_np], queue=False, show_api=False,
362
- ).success(lambda: gr.update(interactive=True), None, [copy_prompt_btn_input], queue=False, show_api=False)
363
- copy_input_btn.click(compose_prompt_to_copy, [input_character, input_copyright, input_general], [input_tags_to_copy], show_api=False)\
364
- .success(gradio_copy_text, [input_tags_to_copy], js=COPY_ACTION_JS, show_api=False)
365
- copy_prompt_btn_input.click(compose_prompt_to_copy, inputs=[input_character, input_copyright, input_general], outputs=[input_tags_to_copy], show_api=False)\
366
- .success(gradio_copy_prompt, inputs=[input_tags_to_copy], outputs=[prompt], show_api=False)
367
-
368
- generate_btn.click(
369
- v2_upsampling_prompt,
370
- [model_name, input_copyright, input_character, input_general,
371
- input_rating, input_aspect_ratio, input_length, input_identity, input_ban_tags],
372
- [output_text],
373
- show_api=False,
374
- ).success(
375
- convert_danbooru_to_e621_prompt, [output_text, tag_type], [output_text_pony], queue=False, show_api=False,
376
- ).success(
377
- insert_recom_prompt, [output_text, dummy_np, recom_animagine], [output_text, dummy_np], queue=False, show_api=False,
378
- ).success(
379
- insert_recom_prompt, [output_text_pony, dummy_np, recom_pony], [output_text_pony, dummy_np], queue=False, show_api=False,
380
- ).success(lambda: (gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)),
381
- None, [copy_btn, copy_btn_pony, copy_prompt_btn, copy_prompt_btn_pony], queue=False, show_api=False)
382
- copy_btn.click(gradio_copy_text, [output_text], js=COPY_ACTION_JS, show_api=False)
383
- copy_btn_pony.click(gradio_copy_text, [output_text_pony], js=COPY_ACTION_JS, show_api=False)
384
- copy_prompt_btn.click(gradio_copy_prompt, inputs=[output_text], outputs=[prompt], show_api=False)
385
- copy_prompt_btn_pony.click(gradio_copy_prompt, inputs=[output_text_pony], outputs=[prompt], show_api=False)
386
-
387
- with gr.Tab("PNG Info"):
388
- with gr.Row():
389
- with gr.Column():
390
- image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
391
-
392
- with gr.Column():
393
- result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
394
-
395
- image_metadata.change(
396
- fn=extract_exif_data,
397
- inputs=[image_metadata],
398
- outputs=[result_metadata],
399
- )
400
-
401
- with gr.Tab("Upscaler"):
402
- with gr.Row():
403
- with gr.Column():
404
- image_up_tab = gr.Image(label="Image", type="pil", sources=["upload"])
405
- upscaler_tab = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS[9:], value=UPSCALER_KEYS[11])
406
- upscaler_size_tab = gr.Slider(minimum=1., maximum=4., step=0.1, value=1.1, label="Upscale by")
407
- generate_button_up_tab = gr.Button(value="START UPSCALE", variant="primary")
408
-
409
- with gr.Column():
410
- result_up_tab = gr.Image(label="Result", type="pil", interactive=False, format="png")
411
-
412
- generate_button_up_tab.click(
413
- fn=esrgan_upscale,
414
- inputs=[image_up_tab, upscaler_tab, upscaler_size_tab],
415
- outputs=[result_up_tab],
416
- )
417
-
418
- gr.LoginButton()
419
- gr.DuplicateButton(value="Duplicate Space for private use (This demo does not work on CPU. Requires GPU Space)")
420
-
421
- demo.queue()
422
- demo.launch()
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import numpy as np
4
+
5
+ # DiffuseCraft
6
+ from dc import (infer, _infer, pass_result, get_diffusers_model_list, get_samplers,
7
+ get_vaes, enable_model_recom_prompt, enable_diffusers_model_detail,
8
+ get_t2i_model_info, get_all_lora_tupled_list, update_loras,
9
+ apply_lora_prompt, download_my_lora, search_civitai_lora,
10
+ select_civitai_lora, search_civitai_lora_json, extract_exif_data, esrgan_upscale, UPSCALER_KEYS,
11
+ preset_quality, preset_styles, process_style_prompt)
12
+ # Translator
13
+ from llmdolphin import (dolphin_respond_auto, dolphin_parse_simple,
14
+ get_llm_formats, get_dolphin_model_format, get_dolphin_models,
15
+ get_dolphin_model_info, select_dolphin_model, select_dolphin_format, get_dolphin_sysprompt)
16
+ # Tagger
17
+ from tagger.v2 import v2_upsampling_prompt, V2_ALL_MODELS
18
+ from tagger.utils import (gradio_copy_text, gradio_copy_prompt, COPY_ACTION_JS,
19
+ V2_ASPECT_RATIO_OPTIONS, V2_RATING_OPTIONS, V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
20
+ from tagger.tagger import (predict_tags_wd, convert_danbooru_to_e621_prompt,
21
+ remove_specific_prompt, insert_recom_prompt, compose_prompt_to_copy,
22
+ translate_prompt, select_random_character)
23
+ from tagger.fl2sd3longcap import predict_tags_fl2_sd3
24
+ def description_ui():
25
+ gr.Markdown(
26
+ """
27
+ ## Danbooru Tags Transformer V2 Demo with WD Tagger & SD3 Long Captioner
28
+ (Image =>) Prompt => Upsampled longer prompt
29
+ - Mod of p1atdev's [Danbooru Tags Transformer V2 Demo](https://huggingface.co/spaces/p1atdev/danbooru-tags-transformer-v2) and [WD Tagger with πŸ€— transformers](https://huggingface.co/spaces/p1atdev/wd-tagger-transformers).
30
+ - Models: p1atdev's [wd-swinv2-tagger-v3-hf](https://huggingface.co/p1atdev/wd-swinv2-tagger-v3-hf), [dart-v2-moe-sft](https://huggingface.co/p1atdev/dart-v2-moe-sft), [dart-v2-sft](https://huggingface.co/p1atdev/dart-v2-sft)\
31
+ , gokaygokay's [Florence-2-SD3-Captioner](https://huggingface.co/gokaygokay/Florence-2-SD3-Captioner)
32
+ """
33
+ )
34
+
35
+
36
+ MAX_SEED = np.iinfo(np.int32).max
37
+ MAX_IMAGE_SIZE = 1216
38
+
39
+ css = """
40
+ #container { margin: 0 auto; !important; }
41
+ #col-container { margin: 0 auto; !important; }
42
+ #result { max-width: 520px; max-height: 520px; margin: 0px auto; !important; }
43
+ .lora { min-width: 480px; !important; }
44
+ #model-info { text-align: center; !important; }
45
+ """
46
+
47
+ with gr.Blocks(fill_width=True, elem_id="container", css=css, delete_cache=(60, 3600), theme="hev832/Applio") as demo:
48
+ gr.Markdown("# Votepurchase Multiple Model")
49
+ with gr.Tab("Image Generator"):
50
+ with gr.Column(elem_id="col-container"):
51
+ with gr.Row():
52
+ prompt = gr.Text(label="Prompt", show_label=False, lines=1, max_lines=8, placeholder="Enter your prompt", container=False)
53
+
54
+ with gr.Row():
55
+ run_button = gr.Button("Run", variant="primary", scale=5)
56
+ run_translate_button = gr.Button("Run with LLM Enhance", variant="secondary", scale=3)
57
+ auto_trans = gr.Checkbox(label="Auto translate to English", value=False, scale=2)
58
+
59
+ result = gr.Image(label="Result", elem_id="result", format="png", show_label=False, interactive=False,
60
+ show_download_button=True, show_share_button=False, container=True)
61
+
62
+ with gr.Accordion("Advanced Settings", open=False):
63
+ with gr.Row():
64
+ negative_prompt = gr.Text(label="Negative prompt", lines=1, max_lines=6, placeholder="Enter a negative prompt",
65
+ value="(low quality, worst quality:1.2), very displeasing, watermark, signature, ugly")
66
+
67
+ with gr.Row():
68
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
69
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
70
+
71
+ with gr.Row():
72
+ width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 832
73
+ height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 1216
74
+ guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=30.0, step=0.1, value=7)
75
+ num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=100, step=1, value=28)
76
+
77
+ with gr.Row():
78
+ with gr.Column(scale=4):
79
+ model_name = gr.Dropdown(label="Model", info="You can enter a huggingface model repo_id to want to use.",
80
+ choices=get_diffusers_model_list(), value=get_diffusers_model_list()[0],
81
+ allow_custom_value=True, interactive=True, min_width=320)
82
+ model_info = gr.Markdown(elem_id="model-info")
83
+ with gr.Column(scale=1):
84
+ model_detail = gr.Checkbox(label="Show detail of model in list", value=False)
85
+
86
+ with gr.Row():
87
+ sampler = gr.Dropdown(label="Sampler", choices=get_samplers(), value="Euler a")
88
+ vae_model = gr.Dropdown(label="VAE Model", choices=get_vaes(), value=get_vaes()[0])
89
+
90
+ with gr.Accordion("LoRA", open=True, visible=True):
91
+ def lora_dropdown(label):
92
+ return gr.Dropdown(label=label, choices=get_all_lora_tupled_list(), value="", allow_custom_value=True, elem_classes="lora", min_width=320)
93
+
94
+ def lora_scale_slider(label):
95
+ return gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label=label)
96
+
97
+ def lora_textbox():
98
+ return gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
99
+
100
+ with gr.Row():
101
+ with gr.Column():
102
+ with gr.Row():
103
+ lora1 = lora_dropdown("LoRA 1")
104
+ lora1_wt = lora_scale_slider("LoRA 1: weight")
105
+ with gr.Row():
106
+ lora1_info = lora_textbox()
107
+ lora1_copy = gr.Button(value="Copy example to prompt", visible=False)
108
+ lora1_md = gr.Markdown(value="", visible=False)
109
+ with gr.Column():
110
+ with gr.Row():
111
+ lora2 = lora_dropdown("LoRA 2")
112
+ lora2_wt = lora_scale_slider("LoRA 2: weight")
113
+ with gr.Row():
114
+ lora2_info = lora_textbox()
115
+ lora2_copy = gr.Button(value="Copy example to prompt", visible=False)
116
+ lora2_md = gr.Markdown(value="", visible=False)
117
+ with gr.Column():
118
+ with gr.Row():
119
+ lora3 = lora_dropdown("LoRA 3")
120
+ lora3_wt = lora_scale_slider("LoRA 3: weight")
121
+ with gr.Row():
122
+ lora3_info = lora_textbox()
123
+ lora3_copy = gr.Button(value="Copy example to prompt", visible=False)
124
+ lora3_md = gr.Markdown(value="", visible=False)
125
+ with gr.Column():
126
+ with gr.Row():
127
+ lora4 = lora_dropdown("LoRA 4")
128
+ lora4_wt = lora_scale_slider("LoRA 4: weight")
129
+ with gr.Row():
130
+ lora4_info = lora_textbox()
131
+ lora4_copy = gr.Button(value="Copy example to prompt", visible=False)
132
+ lora4_md = gr.Markdown(value="", visible=False)
133
+ with gr.Column():
134
+ with gr.Row():
135
+ lora5 = lora_dropdown("LoRA 5")
136
+ lora5_wt = lora_scale_slider("LoRA 5: weight")
137
+ with gr.Row():
138
+ lora5_info = lora_textbox()
139
+ lora5_copy = gr.Button(value="Copy example to prompt", visible=False)
140
+ lora5_md = gr.Markdown(value="", visible=False)
141
+ with gr.Accordion("From URL", open=True, visible=True):
142
+ with gr.Row():
143
+ lora_search_civitai_basemodel = gr.CheckboxGroup(label="Search LoRA for", choices=["Pony", "SD 1.5", "SDXL 1.0", "Flux.1 D", "Flux.1 S"], value=["Pony", "SDXL 1.0"])
144
+ lora_search_civitai_sort = gr.Radio(label="Sort", choices=["Highest Rated", "Most Downloaded", "Newest"], value="Highest Rated")
145
+ lora_search_civitai_period = gr.Radio(label="Period", choices=["AllTime", "Year", "Month", "Week", "Day"], value="AllTime")
146
+ with gr.Row():
147
+ lora_search_civitai_query = gr.Textbox(label="Query", placeholder="oomuro sakurako...", lines=1)
148
+ lora_search_civitai_tag = gr.Textbox(label="Tag", lines=1)
149
+ lora_search_civitai_submit = gr.Button("Search on Civitai")
150
+ with gr.Row():
151
+ lora_search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
152
+ lora_search_civitai_json = gr.JSON(value={}, visible=False)
153
+ lora_search_civitai_desc = gr.Markdown(value="", visible=False)
154
+ lora_download_url = gr.Textbox(label="LoRA URL", placeholder="https://civitai.com/api/download/models/28907", lines=1)
155
+ lora_download = gr.Button("Get and set LoRA and apply to prompt")
156
+
157
+ with gr.Row():
158
+ quality_selector = gr.Radio(label="Quality Tag Presets", interactive=True, choices=list(preset_quality.keys()), value="None", scale=3)
159
+ style_selector = gr.Radio(label="Style Presets", interactive=True, choices=list(preset_styles.keys()), value="None", scale=3)
160
+ recom_prompt = gr.Checkbox(label="Recommended prompt", value=True, scale=1)
161
+
162
+ with gr.Accordion("Translation Settings", open=False):
163
+ chatbot = gr.Chatbot(render_markdown=False, visible=False) # component for auto-translation
164
+ chat_model = gr.Dropdown(choices=get_dolphin_models(), value=get_dolphin_models()[0][1], allow_custom_value=True, label="Model")
165
+ chat_model_info = gr.Markdown(value=get_dolphin_model_info(get_dolphin_models()[0][1]), label="Model info")
166
+ chat_format = gr.Dropdown(choices=get_llm_formats(), value=get_dolphin_model_format(get_dolphin_models()[0][1]), label="Message format")
167
+ with gr.Row():
168
+ chat_tokens = gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max tokens")
169
+ chat_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
170
+ chat_topp = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
171
+ chat_topk = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k")
172
+ chat_rp = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
173
+ chat_sysmsg = gr.Textbox(value=get_dolphin_sysprompt(), label="System message")
174
+
175
+ examples = gr.Examples(
176
+ examples = [
177
+ ["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"],
178
+ ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
179
+ ["kafuu chino, 1girl, solo"],
180
+ ["1girl"],
181
+ ["beautiful sunset"],
182
+ ],
183
+ inputs=[prompt],
184
+ cache_examples=False,
185
+ )
186
+
187
+ gr.on( #lambda x: None, inputs=None, outputs=result).then(
188
+ triggers=[run_button.click, prompt.submit],
189
+ fn=infer,
190
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
191
+ guidance_scale, num_inference_steps, model_name,
192
+ lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
193
+ sampler, vae_model, auto_trans],
194
+ outputs=[result],
195
+ queue=True,
196
+ show_progress="full",
197
+ show_api=True,
198
+ )
199
+
200
+ gr.on( #lambda x: None, inputs=None, outputs=result).then(
201
+ triggers=[run_translate_button.click],
202
+ fn=_infer, # dummy fn for api
203
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
204
+ guidance_scale, num_inference_steps, model_name,
205
+ lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
206
+ sampler, vae_model, auto_trans],
207
+ outputs=[result],
208
+ queue=False,
209
+ show_api=True,
210
+ api_name="infer_translate",
211
+ ).success(
212
+ fn=dolphin_respond_auto,
213
+ inputs=[prompt, chatbot],
214
+ outputs=[chatbot],
215
+ queue=True,
216
+ show_progress="full",
217
+ show_api=False,
218
+ ).success(
219
+ fn=dolphin_parse_simple,
220
+ inputs=[prompt, chatbot],
221
+ outputs=[prompt],
222
+ queue=False,
223
+ show_api=False,
224
+ ).success(
225
+ fn=infer,
226
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
227
+ guidance_scale, num_inference_steps, model_name,
228
+ lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
229
+ sampler, vae_model],
230
+ outputs=[result],
231
+ queue=True,
232
+ show_progress="full",
233
+ show_api=False,
234
+ ).success(lambda: None, None, chatbot, queue=False, show_api=False)\
235
+ .success(pass_result, [result], [result], queue=False, show_api=False) # dummy fn for api
236
+
237
+ gr.on(
238
+ triggers=[lora1.change, lora1_wt.change, lora2.change, lora2_wt.change, lora3.change, lora3_wt.change,
239
+ lora4.change, lora4_wt.change, lora5.change, lora5_wt.change],
240
+ fn=update_loras,
241
+ inputs=[prompt, lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt],
242
+ outputs=[prompt, lora1, lora1_wt, lora1_info, lora1_copy, lora1_md,
243
+ lora2, lora2_wt, lora2_info, lora2_copy, lora2_md, lora3, lora3_wt, lora3_info, lora3_copy, lora3_md,
244
+ lora4, lora4_wt, lora4_info, lora4_copy, lora4_md, lora5, lora5_wt, lora5_info, lora5_copy, lora5_md],
245
+ queue=False,
246
+ trigger_mode="once",
247
+ show_api=False,
248
+ )
249
+ lora1_copy.click(apply_lora_prompt, [prompt, lora1_info], [prompt], queue=False, show_api=False)
250
+ lora2_copy.click(apply_lora_prompt, [prompt, lora2_info], [prompt], queue=False, show_api=False)
251
+ lora3_copy.click(apply_lora_prompt, [prompt, lora3_info], [prompt], queue=False, show_api=False)
252
+ lora4_copy.click(apply_lora_prompt, [prompt, lora4_info], [prompt], queue=False, show_api=False)
253
+ lora5_copy.click(apply_lora_prompt, [prompt, lora5_info], [prompt], queue=False, show_api=False)
254
+
255
+ gr.on(
256
+ triggers=[lora_search_civitai_submit.click, lora_search_civitai_query.submit, lora_search_civitai_tag.submit],
257
+ fn=search_civitai_lora,
258
+ inputs=[lora_search_civitai_query, lora_search_civitai_basemodel, lora_search_civitai_sort, lora_search_civitai_period, lora_search_civitai_tag],
259
+ outputs=[lora_search_civitai_result, lora_search_civitai_desc, lora_search_civitai_submit, lora_search_civitai_query],
260
+ scroll_to_output=True,
261
+ queue=True,
262
+ show_api=False,
263
+ )
264
+ lora_search_civitai_json.change(search_civitai_lora_json, [lora_search_civitai_query, lora_search_civitai_basemodel], [lora_search_civitai_json], queue=True, show_api=True) # fn for api
265
+ lora_search_civitai_result.change(select_civitai_lora, [lora_search_civitai_result], [lora_download_url, lora_search_civitai_desc], scroll_to_output=True, queue=False, show_api=False)
266
+ gr.on(
267
+ triggers=[lora_download.click, lora_download_url.submit],
268
+ fn=download_my_lora,
269
+ inputs=[lora_download_url,lora1, lora2, lora3, lora4, lora5],
270
+ outputs=[lora1, lora2, lora3, lora4, lora5],
271
+ scroll_to_output=True,
272
+ queue=True,
273
+ show_api=False,
274
+ )
275
+
276
+ recom_prompt.change(enable_model_recom_prompt, [recom_prompt], [recom_prompt], queue=False, show_api=False)
277
+ gr.on(
278
+ triggers=[quality_selector.change, style_selector.change],
279
+ fn=process_style_prompt,
280
+ inputs=[prompt, negative_prompt, style_selector, quality_selector],
281
+ outputs=[prompt, negative_prompt],
282
+ queue=False,
283
+ trigger_mode="once",
284
+ )
285
+
286
+ model_detail.change(enable_diffusers_model_detail, [model_detail, model_name], [model_detail, model_name], queue=False, show_api=False)
287
+ model_name.change(get_t2i_model_info, [model_name], [model_info], queue=False, show_api=False)
288
+
289
+ chat_model.change(select_dolphin_model, [chat_model], [chat_model, chat_format, chat_model_info], queue=True, show_progress="full", show_api=False)\
290
+ .success(lambda: None, None, chatbot, queue=False, show_api=False)
291
+ chat_format.change(select_dolphin_format, [chat_format], [chat_format], queue=False, show_api=False)\
292
+ .success(lambda: None, None, chatbot, queue=False, show_api=False)
293
+
294
+ # Tagger
295
+ with gr.Tab("Tags Transformer with Tagger"):
296
+ with gr.Column():
297
+ with gr.Group():
298
+ input_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
299
+ with gr.Accordion(label="Advanced options", open=False):
300
+ general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
301
+ character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
302
+ input_tag_type = gr.Radio(label="Convert tags to", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
303
+ recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
304
+ image_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
305
+ keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
306
+ generate_from_image_btn = gr.Button(value="GENERATE TAGS FROM IMAGE", size="lg", variant="primary")
307
+ with gr.Group():
308
+ with gr.Row():
309
+ input_character = gr.Textbox(label="Character tags", placeholder="hatsune miku")
310
+ input_copyright = gr.Textbox(label="Copyright tags", placeholder="vocaloid")
311
+ random_character = gr.Button(value="Random character 🎲", size="sm")
312
+ input_general = gr.TextArea(label="General tags", lines=4, placeholder="1girl, ...", value="")
313
+ input_tags_to_copy = gr.Textbox(value="", visible=False)
314
+ with gr.Row():
315
+ copy_input_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
316
+ copy_prompt_btn_input = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
317
+ translate_input_prompt_button = gr.Button(value="Translate prompt to English", size="sm", variant="secondary")
318
+ tag_type = gr.Radio(label="Output tag conversion", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="e621", visible=False)
319
+ input_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="explicit")
320
+ with gr.Accordion(label="Advanced options", open=False):
321
+ input_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square")
322
+ input_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="very_long")
323
+ input_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")
324
+ input_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
325
+ model_name = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
326
+ dummy_np = gr.Textbox(label="Negative prompt", value="", visible=False)
327
+ recom_animagine = gr.Textbox(label="Animagine reccomended prompt", value="Animagine", visible=False)
328
+ recom_pony = gr.Textbox(label="Pony reccomended prompt", value="Pony", visible=False)
329
+ generate_btn = gr.Button(value="GENERATE TAGS", size="lg", variant="primary")
330
+ with gr.Row():
331
+ with gr.Group():
332
+ output_text = gr.TextArea(label="Output tags", interactive=False, show_copy_button=True)
333
+ with gr.Row():
334
+ copy_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
335
+ copy_prompt_btn = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
336
+ with gr.Group():
337
+ output_text_pony = gr.TextArea(label="Output tags (Pony e621 style)", interactive=False, show_copy_button=True)
338
+ with gr.Row():
339
+ copy_btn_pony = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
340
+ copy_prompt_btn_pony = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
341
+
342
+ random_character.click(select_random_character, [input_copyright, input_character], [input_copyright, input_character], queue=False, show_api=False)
343
+
344
+ translate_input_prompt_button.click(translate_prompt, [input_general], [input_general], queue=False, show_api=False)
345
+ translate_input_prompt_button.click(translate_prompt, [input_character], [input_character], queue=False, show_api=False)
346
+ translate_input_prompt_button.click(translate_prompt, [input_copyright], [input_copyright], queue=False, show_api=False)
347
+
348
+ generate_from_image_btn.click(
349
+ lambda: ("", "", ""), None, [input_copyright, input_character, input_general], queue=False, show_api=False,
350
+ ).success(
351
+ predict_tags_wd,
352
+ [input_image, input_general, image_algorithms, general_threshold, character_threshold],
353
+ [input_copyright, input_character, input_general, copy_input_btn],
354
+ show_api=False,
355
+ ).success(
356
+ predict_tags_fl2_sd3, [input_image, input_general, image_algorithms], [input_general], show_api=False,
357
+ ).success(
358
+ remove_specific_prompt, [input_general, keep_tags], [input_general], queue=False, show_api=False,
359
+ ).success(
360
+ convert_danbooru_to_e621_prompt, [input_general, input_tag_type], [input_general], queue=False, show_api=False,
361
+ ).success(
362
+ insert_recom_prompt, [input_general, dummy_np, recom_prompt], [input_general, dummy_np], queue=False, show_api=False,
363
+ ).success(lambda: gr.update(interactive=True), None, [copy_prompt_btn_input], queue=False, show_api=False)
364
+ copy_input_btn.click(compose_prompt_to_copy, [input_character, input_copyright, input_general], [input_tags_to_copy], show_api=False)\
365
+ .success(gradio_copy_text, [input_tags_to_copy], js=COPY_ACTION_JS, show_api=False)
366
+ copy_prompt_btn_input.click(compose_prompt_to_copy, inputs=[input_character, input_copyright, input_general], outputs=[input_tags_to_copy], show_api=False)\
367
+ .success(gradio_copy_prompt, inputs=[input_tags_to_copy], outputs=[prompt], show_api=False)
368
+
369
+ generate_btn.click(
370
+ v2_upsampling_prompt,
371
+ [model_name, input_copyright, input_character, input_general,
372
+ input_rating, input_aspect_ratio, input_length, input_identity, input_ban_tags],
373
+ [output_text],
374
+ show_api=False,
375
+ ).success(
376
+ convert_danbooru_to_e621_prompt, [output_text, tag_type], [output_text_pony], queue=False, show_api=False,
377
+ ).success(
378
+ insert_recom_prompt, [output_text, dummy_np, recom_animagine], [output_text, dummy_np], queue=False, show_api=False,
379
+ ).success(
380
+ insert_recom_prompt, [output_text_pony, dummy_np, recom_pony], [output_text_pony, dummy_np], queue=False, show_api=False,
381
+ ).success(lambda: (gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)),
382
+ None, [copy_btn, copy_btn_pony, copy_prompt_btn, copy_prompt_btn_pony], queue=False, show_api=False)
383
+ copy_btn.click(gradio_copy_text, [output_text], js=COPY_ACTION_JS, show_api=False)
384
+ copy_btn_pony.click(gradio_copy_text, [output_text_pony], js=COPY_ACTION_JS, show_api=False)
385
+ copy_prompt_btn.click(gradio_copy_prompt, inputs=[output_text], outputs=[prompt], show_api=False)
386
+ copy_prompt_btn_pony.click(gradio_copy_prompt, inputs=[output_text_pony], outputs=[prompt], show_api=False)
387
+
388
+ with gr.Tab("PNG Info"):
389
+ with gr.Row():
390
+ with gr.Column():
391
+ image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
392
+
393
+ with gr.Column():
394
+ result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
395
+
396
+ image_metadata.change(
397
+ fn=extract_exif_data,
398
+ inputs=[image_metadata],
399
+ outputs=[result_metadata],
400
+ )
401
+
402
+ with gr.Tab("Upscaler"):
403
+ with gr.Row():
404
+ with gr.Column():
405
+ image_up_tab = gr.Image(label="Image", type="pil", sources=["upload"])
406
+ upscaler_tab = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS[9:], value=UPSCALER_KEYS[11])
407
+ upscaler_size_tab = gr.Slider(minimum=1., maximum=4., step=0.1, value=1.1, label="Upscale by")
408
+ generate_button_up_tab = gr.Button(value="START UPSCALE", variant="primary")
409
+
410
+ with gr.Column():
411
+ result_up_tab = gr.Image(label="Result", type="pil", interactive=False, format="png")
412
+
413
+ generate_button_up_tab.click(
414
+ fn=esrgan_upscale,
415
+ inputs=[image_up_tab, upscaler_tab, upscaler_size_tab],
416
+ outputs=[result_up_tab],
417
+ )
418
+
419
+ gr.LoginButton()
420
+ gr.DuplicateButton(value="Duplicate Space for private use (This demo does not work on CPU. Requires GPU Space)")
421
+
422
+ demo.queue()
423
+ demo.launch()