John6666 commited on
Commit
2226df1
1 Parent(s): 9bcfafc

Upload app.py

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