John6666 commited on
Commit
1632b07
β€’
1 Parent(s): db97375

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +90 -368
app.py CHANGED
@@ -1,373 +1,95 @@
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,
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")
55
- run_translate_button = gr.Button("Translate")
56
-
57
- result = gr.Image(label="Result", elem_id="result", format="png", show_label=False, interactive=False,
58
- show_download_button=True, show_share_button=False, container=True)
59
-
60
- with gr.Accordion("Advanced Settings", open=False):
61
- with gr.Row():
62
- negative_prompt = gr.Text(label="Negative prompt", lines=1, max_lines=6, placeholder="Enter a negative prompt",
63
- value="(low quality, worst quality:1.2), very displeasing, watermark, signature, ugly")
64
-
65
- with gr.Row():
66
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
67
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
68
-
69
- with gr.Row():
70
- width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 832
71
- height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 1216
72
- guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=30.0, step=0.1, value=7)
73
- num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=100, step=1, value=28)
74
-
75
  with gr.Row():
76
- with gr.Column(scale=4):
77
- model_name = gr.Dropdown(label="Model", info="You can enter a huggingface model repo_id to want to use.",
78
- choices=get_diffusers_model_list(), value=get_diffusers_model_list()[0],
79
- allow_custom_value=True, interactive=True, min_width=320)
80
- model_info = gr.Markdown(elem_id="model-info")
81
- with gr.Column(scale=1):
82
- model_detail = gr.Checkbox(label="Show detail of model in list", value=False)
83
-
 
 
 
 
84
  with gr.Row():
85
- sampler = gr.Dropdown(label="Sampler", choices=get_samplers(), value="Euler a")
86
- vae_model = gr.Dropdown(label="VAE Model", choices=get_vaes(), value=get_vaes()[0])
87
-
88
- with gr.Accordion("LoRA", open=True, visible=True):
89
- with gr.Row():
90
- with gr.Column():
91
- with gr.Row():
92
- lora1 = gr.Dropdown(label="LoRA 1", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True, elem_classes="lora", min_width=320)
93
- lora1_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 1: weight")
94
- with gr.Row():
95
- lora1_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
96
- lora1_copy = gr.Button(value="Copy example to prompt", visible=False)
97
- lora1_md = gr.Markdown(value="", visible=False)
98
- with gr.Column():
99
- with gr.Row():
100
- lora2 = gr.Dropdown(label="LoRA 2", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True, elem_classes="lora", min_width=320)
101
- lora2_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 2: weight")
102
- with gr.Row():
103
- lora2_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
104
- lora2_copy = gr.Button(value="Copy example to prompt", visible=False)
105
- lora2_md = gr.Markdown(value="", visible=False)
106
- with gr.Column():
107
- with gr.Row():
108
- lora3 = gr.Dropdown(label="LoRA 3", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True, elem_classes="lora", min_width=320)
109
- lora3_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 3: weight")
110
- with gr.Row():
111
- lora3_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
112
- lora3_copy = gr.Button(value="Copy example to prompt", visible=False)
113
- lora3_md = gr.Markdown(value="", visible=False)
114
- with gr.Column():
115
- with gr.Row():
116
- lora4 = gr.Dropdown(label="LoRA 4", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True, elem_classes="lora", min_width=320)
117
- lora4_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 4: weight")
118
- with gr.Row():
119
- lora4_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
120
- lora4_copy = gr.Button(value="Copy example to prompt", visible=False)
121
- lora4_md = gr.Markdown(value="", visible=False)
122
- with gr.Column():
123
- with gr.Row():
124
- lora5 = gr.Dropdown(label="LoRA 5", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True, elem_classes="lora", min_width=320)
125
- lora5_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 5: weight")
126
- with gr.Row():
127
- lora5_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
128
- lora5_copy = gr.Button(value="Copy example to prompt", visible=False)
129
- lora5_md = gr.Markdown(value="", visible=False)
130
- with gr.Accordion("From URL", open=True, visible=True):
131
- with gr.Row():
132
- lora_search_civitai_query = gr.Textbox(label="Query", placeholder="oomuro sakurako...", lines=1)
133
- lora_search_civitai_basemodel = gr.CheckboxGroup(label="Search LoRA for", choices=["Pony", "SD 1.5", "SDXL 1.0"], value=["Pony", "SDXL 1.0"])
134
- lora_search_civitai_submit = gr.Button("Search on Civitai")
135
- with gr.Row():
136
- lora_search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
137
- lora_search_civitai_json = gr.JSON(value={}, visible=False)
138
- lora_search_civitai_desc = gr.Markdown(value="", visible=False)
139
- lora_download_url = gr.Textbox(label="URL", placeholder="http://...my_lora_url.safetensors", lines=1)
140
- lora_download = gr.Button("Get and set LoRA and apply to prompt")
141
-
142
- with gr.Row():
143
- quality_selector = gr.Radio(label="Quality Tag Presets", interactive=True, choices=list(preset_quality.keys()), value="None", scale=3)
144
- style_selector = gr.Radio(label="Style Presets", interactive=True, choices=list(preset_styles.keys()), value="None", scale=3)
145
- recom_prompt = gr.Checkbox(label="Recommended prompt", value=True, scale=1)
146
-
147
- with gr.Accordion("Translation Settings", open=False):
148
- chatbot = gr.Chatbot(likeable=False, render_markdown=False, visible=False) # component for auto-translation
149
- chat_model = gr.Dropdown(choices=get_dolphin_models(), value=get_dolphin_models()[0][1], allow_custom_value=True, label="Model")
150
- chat_model_info = gr.Markdown(value=get_dolphin_model_info(get_dolphin_models()[0][1]), label="Model info")
151
- chat_format = gr.Dropdown(choices=get_llm_formats(), value=get_dolphin_model_format(get_dolphin_models()[0][1]), label="Message format")
152
- with gr.Row():
153
- chat_tokens = gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max tokens")
154
- chat_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
155
- chat_topp = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
156
- chat_topk = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k")
157
- chat_rp = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
158
- chat_sysmsg = gr.Textbox(value=get_dolphin_sysprompt(), label="System message")
159
-
160
- examples = gr.Examples(
161
- examples = [
162
- ["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"],
163
- ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
164
- ["kafuu chino, 1girl, solo"],
165
- ["1girl"],
166
- ["beautiful sunset"],
167
- ],
168
- inputs=[prompt],
169
- )
170
-
171
- gr.on( #lambda x: None, inputs=None, outputs=result).then(
172
- triggers=[run_button.click, prompt.submit],
173
- fn=infer,
174
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
175
- guidance_scale, num_inference_steps, model_name,
176
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
177
- sampler, vae_model],
178
- outputs=[result],
179
- queue=True,
180
- show_progress="full",
181
- show_api=True,
182
- )
183
-
184
- gr.on( #lambda x: None, inputs=None, outputs=result).then(
185
- triggers=[run_translate_button.click],
186
- fn=_infer, # dummy fn for api
187
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
188
- guidance_scale, num_inference_steps, model_name,
189
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
190
- sampler, vae_model],
191
- outputs=[result],
192
- queue=False,
193
- show_api=True,
194
- api_name="infer_translate",
195
- ).success(
196
- fn=dolphin_respond_auto,
197
- inputs=[prompt, chatbot],
198
- outputs=[chatbot],
199
- queue=True,
200
- show_progress="full",
201
- show_api=False,
202
- ).success(
203
- fn=dolphin_parse_simple,
204
- inputs=[prompt, chatbot],
205
- outputs=[prompt],
206
- queue=False,
207
- show_api=False,
208
- ).success(
209
- fn=infer,
210
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
211
- guidance_scale, num_inference_steps, model_name,
212
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
213
- sampler, vae_model],
214
- outputs=[result],
215
- queue=True,
216
- show_progress="full",
217
- show_api=False,
218
- ).success(lambda: None, None, chatbot, queue=False, show_api=False)\
219
- .success(pass_result, [result], [result], queue=False, show_api=False) # dummy fn for api
220
-
221
- gr.on(
222
- triggers=[lora1.change, lora1_wt.change, lora2.change, lora2_wt.change, lora3.change, lora3_wt.change,
223
- lora4.change, lora4_wt.change, lora5.change, lora5_wt.change],
224
- fn=update_loras,
225
- inputs=[prompt, lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt],
226
- outputs=[prompt, lora1, lora1_wt, lora1_info, lora1_copy, lora1_md,
227
- lora2, lora2_wt, lora2_info, lora2_copy, lora2_md, lora3, lora3_wt, lora3_info, lora3_copy, lora3_md,
228
- lora4, lora4_wt, lora4_info, lora4_copy, lora4_md, lora5, lora5_wt, lora5_info, lora5_copy, lora5_md],
229
- queue=False,
230
- trigger_mode="once",
231
- show_api=False,
232
- )
233
- lora1_copy.click(apply_lora_prompt, [prompt, lora1_info], [prompt], queue=False, show_api=False)
234
- lora2_copy.click(apply_lora_prompt, [prompt, lora2_info], [prompt], queue=False, show_api=False)
235
- lora3_copy.click(apply_lora_prompt, [prompt, lora3_info], [prompt], queue=False, show_api=False)
236
- lora4_copy.click(apply_lora_prompt, [prompt, lora4_info], [prompt], queue=False, show_api=False)
237
- lora5_copy.click(apply_lora_prompt, [prompt, lora5_info], [prompt], queue=False, show_api=False)
238
-
239
- gr.on(
240
- triggers=[lora_search_civitai_submit.click, lora_search_civitai_query.submit],
241
- fn=search_civitai_lora,
242
- inputs=[lora_search_civitai_query, lora_search_civitai_basemodel],
243
- outputs=[lora_search_civitai_result, lora_search_civitai_desc, lora_search_civitai_submit, lora_search_civitai_query],
244
- scroll_to_output=True,
245
- queue=True,
246
- show_api=False,
247
- )
248
- 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
249
- 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)
250
- gr.on(
251
- triggers=[lora_download.click, lora_download_url.submit],
252
- fn=download_my_lora,
253
- inputs=[lora_download_url,lora1, lora2, lora3, lora4, lora5],
254
- outputs=[lora1, lora2, lora3, lora4, lora5],
255
- scroll_to_output=True,
256
- queue=True,
257
- show_api=False,
258
- )
259
-
260
- recom_prompt.change(enable_model_recom_prompt, [recom_prompt], [recom_prompt], queue=False, show_api=False)
261
- gr.on(
262
- triggers=[quality_selector.change, style_selector.change],
263
- fn=process_style_prompt,
264
- inputs=[prompt, negative_prompt, style_selector, quality_selector],
265
- outputs=[prompt, negative_prompt],
266
- queue=False,
267
- trigger_mode="once",
268
- )
269
-
270
- model_detail.change(enable_diffusers_model_detail, [model_detail, model_name], [model_detail, model_name], queue=False, show_api=False)
271
- model_name.change(get_t2i_model_info, [model_name], [model_info], queue=False, show_api=False)
272
-
273
- chat_model.change(select_dolphin_model, [chat_model], [chat_model, chat_format, chat_model_info], queue=True, show_progress="full", show_api=False)\
274
- .success(lambda: None, None, chatbot, queue=False, show_api=False)
275
- chat_format.change(select_dolphin_format, [chat_format], [chat_format], queue=False, show_api=False)\
276
- .success(lambda: None, None, chatbot, queue=False, show_api=False)
277
-
278
- # Tagger
279
- with gr.Tab("Tags Transformer with Tagger"):
280
- with gr.Column():
281
- with gr.Group():
282
- input_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
283
- with gr.Accordion(label="Advanced options", open=False):
284
- general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
285
- character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
286
- input_tag_type = gr.Radio(label="Convert tags to", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
287
- recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
288
- image_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
289
- keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
290
- generate_from_image_btn = gr.Button(value="GENERATE TAGS FROM IMAGE", size="lg", variant="primary")
291
- with gr.Group():
292
- with gr.Row():
293
- input_character = gr.Textbox(label="Character tags", placeholder="hatsune miku")
294
- input_copyright = gr.Textbox(label="Copyright tags", placeholder="vocaloid")
295
- random_character = gr.Button(value="Random character 🎲", size="sm")
296
- input_general = gr.TextArea(label="General tags", lines=4, placeholder="1girl, ...", value="")
297
- input_tags_to_copy = gr.Textbox(value="", visible=False)
298
- with gr.Row():
299
- copy_input_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
300
- copy_prompt_btn_input = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
301
- translate_input_prompt_button = gr.Button(value="Translate prompt to English", size="sm", variant="secondary")
302
- tag_type = gr.Radio(label="Output tag conversion", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="e621", visible=False)
303
- input_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="explicit")
304
- with gr.Accordion(label="Advanced options", open=False):
305
- input_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square")
306
- input_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="very_long")
307
- 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")
308
- input_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
309
- model_name = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
310
- dummy_np = gr.Textbox(label="Negative prompt", value="", visible=False)
311
- recom_animagine = gr.Textbox(label="Animagine reccomended prompt", value="Animagine", visible=False)
312
- recom_pony = gr.Textbox(label="Pony reccomended prompt", value="Pony", visible=False)
313
- generate_btn = gr.Button(value="GENERATE TAGS", size="lg", variant="primary")
314
- with gr.Row():
315
- with gr.Group():
316
- output_text = gr.TextArea(label="Output tags", interactive=False, show_copy_button=True)
317
- with gr.Row():
318
- copy_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
319
- copy_prompt_btn = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
320
- with gr.Group():
321
- output_text_pony = gr.TextArea(label="Output tags (Pony e621 style)", interactive=False, show_copy_button=True)
322
- with gr.Row():
323
- copy_btn_pony = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
324
- copy_prompt_btn_pony = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
325
-
326
- random_character.click(select_random_character, [input_copyright, input_character], [input_copyright, input_character], queue=False, show_api=False)
327
-
328
- translate_input_prompt_button.click(translate_prompt, [input_general], [input_general], queue=False, show_api=False)
329
- translate_input_prompt_button.click(translate_prompt, [input_character], [input_character], queue=False, show_api=False)
330
- translate_input_prompt_button.click(translate_prompt, [input_copyright], [input_copyright], queue=False, show_api=False)
331
-
332
- generate_from_image_btn.click(
333
- lambda: ("", "", ""), None, [input_copyright, input_character, input_general], queue=False, show_api=False,
334
- ).success(
335
- predict_tags_wd,
336
- [input_image, input_general, image_algorithms, general_threshold, character_threshold],
337
- [input_copyright, input_character, input_general, copy_input_btn],
338
- show_api=False,
339
- ).success(
340
- predict_tags_fl2_sd3, [input_image, input_general, image_algorithms], [input_general], show_api=False,
341
- ).success(
342
- remove_specific_prompt, [input_general, keep_tags], [input_general], queue=False, show_api=False,
343
- ).success(
344
- convert_danbooru_to_e621_prompt, [input_general, input_tag_type], [input_general], queue=False, show_api=False,
345
- ).success(
346
- insert_recom_prompt, [input_general, dummy_np, recom_prompt], [input_general, dummy_np], queue=False, show_api=False,
347
- ).success(lambda: gr.update(interactive=True), None, [copy_prompt_btn_input], queue=False, show_api=False)
348
- copy_input_btn.click(compose_prompt_to_copy, [input_character, input_copyright, input_general], [input_tags_to_copy], show_api=False)\
349
- .success(gradio_copy_text, [input_tags_to_copy], js=COPY_ACTION_JS, show_api=False)
350
- copy_prompt_btn_input.click(compose_prompt_to_copy, inputs=[input_character, input_copyright, input_general], outputs=[input_tags_to_copy], show_api=False)\
351
- .success(gradio_copy_prompt, inputs=[input_tags_to_copy], outputs=[prompt], show_api=False)
352
-
353
- generate_btn.click(
354
- v2_upsampling_prompt,
355
- [model_name, input_copyright, input_character, input_general,
356
- input_rating, input_aspect_ratio, input_length, input_identity, input_ban_tags],
357
- [output_text],
358
- show_api=False,
359
- ).success(
360
- convert_danbooru_to_e621_prompt, [output_text, tag_type], [output_text_pony], queue=False, show_api=False,
361
- ).success(
362
- insert_recom_prompt, [output_text, dummy_np, recom_animagine], [output_text, dummy_np], queue=False, show_api=False,
363
- ).success(
364
- insert_recom_prompt, [output_text_pony, dummy_np, recom_pony], [output_text_pony, dummy_np], queue=False, show_api=False,
365
- ).success(lambda: (gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)),
366
- None, [copy_btn, copy_btn_pony, copy_prompt_btn, copy_prompt_btn_pony], queue=False, show_api=False)
367
- copy_btn.click(gradio_copy_text, [output_text], js=COPY_ACTION_JS, show_api=False)
368
- copy_btn_pony.click(gradio_copy_text, [output_text_pony], js=COPY_ACTION_JS, show_api=False)
369
- copy_prompt_btn.click(gradio_copy_prompt, inputs=[output_text], outputs=[prompt], show_api=False)
370
- copy_prompt_btn_pony.click(gradio_copy_prompt, inputs=[output_text_pony], outputs=[prompt], show_api=False)
371
-
372
- demo.queue()
373
- demo.launch()
 
1
  import spaces
2
  import gradio as gr
3
+ from utils import gradio_copy_text, COPY_ACTION_JS
4
+ from tagger import convert_danbooru_to_e621_prompt, insert_recom_prompt
5
+ from genimage import generate_image
6
+ from llmdolphin import (get_llm_formats, get_dolphin_model_format,
7
+ get_dolphin_models, get_dolphin_model_info, select_dolphin_model,
8
+ select_dolphin_format, add_dolphin_models, get_dolphin_sysprompt,
9
+ get_dolphin_sysprompt_mode, select_dolphin_sysprompt, get_dolphin_languages,
10
+ select_dolphin_language, dolphin_respond, dolphin_parse)
11
+
12
+
13
+ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css="", delete_cache=(60, 3600)) as app:
14
+ gr.Markdown("""# Natural Text to SD Prompt Translator With LLM alpha
15
+ Text in natural language (English, Japanese, ...) => Prompt
16
+ """)
17
+ with gr.Column():
18
+ with gr.Group():
19
+ chatbot = gr.Chatbot(likeable=False, show_copy_button=True, show_share_button=False, layout="bubble", container=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  with gr.Row():
21
+ chat_msg = gr.Textbox(show_label=False, placeholder="Input text in English, Japanese, or any other languages and press Enter or click Send.", scale=4)
22
+ chat_submit = gr.Button("Send", scale=1)
23
+ chat_clear = gr.Button("Clear", scale=1)
24
+ with gr.Accordion("Additional inputs", open=False):
25
+ chat_format = gr.Dropdown(choices=get_llm_formats(), value=get_dolphin_model_format(get_dolphin_models()[0][1]), label="Message format")
26
+ chat_sysmsg = gr.Textbox(value=get_dolphin_sysprompt(), label="System message")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  with gr.Row():
28
+ chat_tokens = gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max tokens")
29
+ chat_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
30
+ chat_topp = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
31
+ chat_topk = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k")
32
+ chat_rp = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
33
+ with gr.Accordion("Add models", open=False):
34
+ chat_add_text = gr.Textbox(label="URL or Repo ID", placeholder="https://huggingface.co/mradermacher/MagnumChronos-i1-GGUF/blob/main/MagnumChronos.i1-Q4_K_M.gguf", lines=1)
35
+ chat_add_format = gr.Dropdown(choices=get_llm_formats(), value=get_llm_formats()[0], label="Message format")
36
+ chat_add_submit = gr.Button("Update lists of models")
37
+ with gr.Accordion("Modes", open=True):
38
+ chat_model = gr.Dropdown(choices=get_dolphin_models(), value=get_dolphin_models()[0][1], allow_custom_value=True, label="Model")
39
+ chat_model_info = gr.Markdown(value=get_dolphin_model_info(get_dolphin_models()[0][1]), label="Model info")
40
  with gr.Row():
41
+ chat_mode = gr.Dropdown(choices=get_dolphin_sysprompt_mode(), value=get_dolphin_sysprompt_mode()[0], allow_custom_value=False, label="Mode")
42
+ chat_lang = gr.Dropdown(choices=get_dolphin_languages(), value="English", allow_custom_value=True, label="Output language")
43
+ with gr.Row():
44
+ with gr.Group():
45
+ output_text = gr.TextArea(label="Output tags", interactive=False, show_copy_button=True)
46
+ copy_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
47
+ with gr.Group():
48
+ output_text_pony = gr.TextArea(label="Output tags (Pony e621 style)", interactive=False, show_copy_button=True)
49
+ copy_btn_pony = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
50
+ with gr.Accordion(label="Advanced options", open=False, visible=False):
51
+ tag_type = gr.Radio(label="Output tag conversion", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="e621", visible=False)
52
+ dummy_np = gr.Textbox(label="Negative prompt", value="", visible=False)
53
+ dummy_np_pony = gr.Textbox(label="Negative prompt", value="", visible=False)
54
+ recom_animagine = gr.Textbox(label="Animagine reccomended prompt", value="Animagine", visible=False)
55
+ recom_pony = gr.Textbox(label="Pony reccomended prompt", value="Pony", visible=False)
56
+ generate_image_btn = gr.Button(value="GENERATE IMAGE", size="lg", variant="primary")
57
+ result_image = gr.Gallery(label="Generated images", columns=1, object_fit="contain", container=True, preview=True, show_label=False, show_share_button=False, show_download_button=True, interactive=False, visible=True, format="png")
58
+
59
+ gr.on(
60
+ triggers=[chat_msg.submit, chat_submit.click],
61
+ fn=dolphin_respond,
62
+ inputs=[chat_msg, chatbot, chat_model, chat_sysmsg, chat_tokens, chat_temperature, chat_topp, chat_topk, chat_rp],
63
+ outputs=[chatbot],
64
+ queue=True,
65
+ show_progress="full",
66
+ trigger_mode="once",
67
+ ).success(dolphin_parse, [chatbot], [output_text, copy_btn, copy_btn_pony]).success(
68
+ convert_danbooru_to_e621_prompt, [output_text, tag_type], [output_text_pony], queue=False,
69
+ ).success(insert_recom_prompt, [output_text, dummy_np, recom_animagine], [output_text, dummy_np], queue=False,
70
+ ).success(insert_recom_prompt, [output_text_pony, dummy_np_pony, recom_pony], [output_text_pony, dummy_np_pony], queue=False)
71
+ chat_clear.click(lambda: None, None, chatbot, queue=False)
72
+ chat_model.change(select_dolphin_model, [chat_model], [chat_model, chat_format, chat_model_info], queue=True, show_progress="full")\
73
+ .success(lambda: None, None, chatbot, queue=False)
74
+ chat_format.change(select_dolphin_format, [chat_format], [chat_format], queue=False)\
75
+ .success(lambda: None, None, chatbot, queue=False)
76
+ chat_mode.change(select_dolphin_sysprompt, [chat_mode], [chat_sysmsg], queue=False)
77
+ chat_lang.change(select_dolphin_language, [chat_lang], [chat_sysmsg], queue=False)
78
+ gr.on(
79
+ triggers=[chat_add_text.submit, chat_add_submit.click],
80
+ fn=add_dolphin_models,
81
+ inputs=[chat_add_text, chat_add_format],
82
+ outputs=[chat_model],
83
+ queue=False,
84
+ trigger_mode="once",
85
+ )
86
+
87
+ copy_btn.click(gradio_copy_text, [output_text], js=COPY_ACTION_JS)
88
+ copy_btn_pony.click(gradio_copy_text, [output_text_pony], js=COPY_ACTION_JS)
89
+
90
+ generate_image_btn.click(generate_image, [output_text, dummy_np], [result_image], show_progress="full")
91
+
92
+
93
+ if __name__ == "__main__":
94
+ app.queue()
95
+ app.launch()