John6666 commited on
Commit
1d13075
1 Parent(s): 40ae154

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +283 -643
app.py CHANGED
@@ -1,659 +1,299 @@
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, save_image_history,
7
- get_vaes, enable_diffusers_model_detail, extract_exif_data, esrgan_upscale, UPSCALER_KEYS,
8
- preset_quality, preset_styles, process_style_prompt, get_all_lora_tupled_list, update_loras, apply_lora_prompt,
9
- download_my_lora, search_civitai_lora, update_civitai_selection, select_civitai_lora, search_civitai_lora_json,
10
- get_t2i_model_info, get_civitai_tag, CIVITAI_SORT, CIVITAI_PERIOD, CIVITAI_BASEMODEL,
11
- SCHEDULE_TYPE_OPTIONS, SCHEDULE_PREDICTION_TYPE_OPTIONS, preprocessor_tab, SDXL_TASK, TASK_MODEL_LIST,
12
- PROMPT_W_OPTIONS, POST_PROCESSING_SAMPLER, IP_ADAPTERS_SD, IP_ADAPTERS_SDXL, DIFFUSERS_CONTROLNET_MODEL,
13
- TASK_AND_PREPROCESSORS, update_task_options, change_preprocessor_choices, get_ti_choices,
14
- update_textual_inversion, set_textual_inversion_prompt, create_mask_now)
15
- # Translator
16
- from llmdolphin import (dolphin_respond_auto, dolphin_parse_simple,
17
- get_llm_formats, get_dolphin_model_format, get_dolphin_models,
18
- get_dolphin_model_info, select_dolphin_model, select_dolphin_format, get_dolphin_sysprompt)
19
- # Tagger
20
- from tagger.v2 import v2_upsampling_prompt, V2_ALL_MODELS
21
- from tagger.utils import (gradio_copy_text, gradio_copy_prompt, COPY_ACTION_JS,
22
- V2_ASPECT_RATIO_OPTIONS, V2_RATING_OPTIONS, V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
23
- from tagger.tagger import (predict_tags_wd, convert_danbooru_to_e621_prompt,
24
- remove_specific_prompt, insert_recom_prompt, compose_prompt_to_copy,
25
- translate_prompt, select_random_character)
26
- from tagger.fl2sd3longcap import predict_tags_fl2_sd3
27
- def description_ui():
28
- gr.Markdown(
29
- """
30
- ## Danbooru Tags Transformer V2 Demo with WD Tagger & SD3 Long Captioner
31
- (Image =>) Prompt => Upsampled longer prompt
32
- - 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).
33
- - 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)\
34
- , gokaygokay's [Florence-2-SD3-Captioner](https://huggingface.co/gokaygokay/Florence-2-SD3-Captioner)
35
- """
36
- )
37
-
38
-
39
- MAX_SEED = np.iinfo(np.int32).max
40
- MAX_IMAGE_SIZE = 4096
41
- MIN_IMAGE_SIZE = 256
42
-
43
- css = """
44
- #container { margin: 0 auto; !important; }
45
- #col-container { margin: 0 auto; !important; }
46
- #result { max-width: 520px; max-height: 520px; margin: 0px auto; !important; }
47
- .lora { min-width: 480px; !important; }
48
- .title { font-size: 3em; align-items: center; text-align: center; }
49
- .info { align-items: center; text-align: center; }
50
- .desc [src$='#float'] { float: right; margin: 20px; }
51
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- with gr.Blocks(fill_width=True, elem_id="container", css=css, delete_cache=(60, 3600)) as demo:
54
- gr.Markdown("# Votepurchase Multiple Model", elem_classes="title")
55
- state = gr.State(value={})
56
- with gr.Tab("Image Generator"):
57
- with gr.Column(elem_id="col-container"):
58
- with gr.Row():
59
- prompt = gr.Text(label="Prompt", show_label=False, lines=1, max_lines=8, placeholder="Enter your prompt", container=False)
60
-
61
- with gr.Row():
62
- run_button = gr.Button("Run", variant="primary", scale=5)
63
- run_translate_button = gr.Button("Run with LLM Enhance", variant="secondary", scale=3)
64
- auto_trans = gr.Checkbox(label="Auto translate to English", value=False, scale=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- result = gr.Image(label="Result", elem_id="result", format="png", type="filepath", show_label=False, interactive=False,
67
- show_download_button=True, show_share_button=False, container=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- with gr.Accordion("History", open=False):
70
- history_files = gr.Files(interactive=False, visible=False)
71
- history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", format="png", interactive=False, show_share_button=False,
72
- show_download_button=True)
73
- history_clear_button = gr.Button(value="Clear History", variant="secondary")
74
- history_clear_button.click(lambda: ([], []), None, [history_gallery, history_files], queue=False, show_api=False)
75
 
76
- with gr.Accordion("Advanced Settings", open=True):
77
- task = gr.Dropdown(label="Task", choices=SDXL_TASK, value=TASK_MODEL_LIST[0])
78
- with gr.Tab("Generation Settings"):
79
- with gr.Row():
80
- negative_prompt = gr.Text(label="Negative prompt", lines=1, max_lines=6, placeholder="Enter a negative prompt", show_copy_button=True,
81
- value="(low quality, worst quality:1.2), very displeasing, watermark, signature, ugly")
82
- with gr.Row():
83
- with gr.Column(scale=4):
84
- model_name = gr.Dropdown(label="Model", info="You can enter a huggingface model repo_id to want to use.",
85
- choices=get_diffusers_model_list(), value=get_diffusers_model_list()[0],
86
- allow_custom_value=True, interactive=True, min_width=320)
87
- model_info = gr.Markdown(elem_classes="info")
88
- with gr.Column(scale=1):
89
- model_detail = gr.Checkbox(label="Show detail of model in list", value=False)
90
- with gr.Accordion("Prompt Settings", open=False):
91
- with gr.Row():
92
- quality_selector = gr.Radio(label="Quality Tag Presets", interactive=True, choices=list(preset_quality.keys()), value="None", scale=3)
93
- style_selector = gr.Radio(label="Style Presets", interactive=True, choices=list(preset_styles.keys()), value="None", scale=3)
94
- recom_prompt = gr.Checkbox(label="Recommended prompt", value=True, scale=1)
95
- prompt_syntax = gr.Dropdown(label="Prompt Syntax", choices=PROMPT_W_OPTIONS, value=PROMPT_W_OPTIONS[1][1])
96
- with gr.Row():
97
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
98
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
99
- gpu_duration = gr.Slider(label="GPU time duration (seconds)", minimum=5, maximum=240, value=59)
100
- with gr.Row():
101
- width = gr.Slider(label="Width", minimum=MIN_IMAGE_SIZE, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 832
102
- height = gr.Slider(label="Height", minimum=MIN_IMAGE_SIZE, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 1216
103
- guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=30.0, step=0.1, value=7)
104
- guidance_rescale = gr.Slider(label="CFG rescale", value=0., step=0.01, minimum=0., maximum=1.5)
105
- with gr.Row():
106
- num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=100, step=1, value=28)
107
- pag_scale = gr.Slider(minimum=0.0, maximum=10.0, step=0.1, value=0.0, label="PAG Scale")
108
- clip_skip = gr.Checkbox(value=True, label="Layer 2 Clip Skip")
109
- free_u = gr.Checkbox(value=False, label="FreeU")
110
- with gr.Row():
111
- sampler = gr.Dropdown(label="Sampler", choices=get_samplers(), value="Euler")
112
- schedule_type = gr.Dropdown(label="Schedule type", choices=SCHEDULE_TYPE_OPTIONS, value=SCHEDULE_TYPE_OPTIONS[0])
113
- schedule_prediction_type = gr.Dropdown(label="Discrete Sampling Type", choices=SCHEDULE_PREDICTION_TYPE_OPTIONS, value=SCHEDULE_PREDICTION_TYPE_OPTIONS[0])
114
- vae_model = gr.Dropdown(label="VAE Model", choices=get_vaes(), value=get_vaes()[0])
115
- with gr.Accordion("Other Settings", open=False):
116
- with gr.Accordion("Textual inversion", open=True):
117
- active_textual_inversion = gr.Checkbox(value=False, label="Active Textual Inversion in prompt")
118
- use_textual_inversion = gr.CheckboxGroup(choices=get_ti_choices(model_name.value) if active_textual_inversion.value else [], value=None, label="Use Textual Invertion in prompt")
119
 
120
- with gr.Tab("LoRA"):
121
- def lora_dropdown(label, visible=True):
122
- return gr.Dropdown(label=label, choices=get_all_lora_tupled_list(), value="", allow_custom_value=True, elem_classes="lora", min_width=320, visible=visible)
123
 
124
- def lora_scale_slider(label, visible=True):
125
- return gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label=label, visible=visible)
126
-
127
- def lora_textbox():
128
- return gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
129
-
130
- with gr.Row():
131
- with gr.Column():
132
- with gr.Row():
133
- lora1 = lora_dropdown("LoRA 1")
134
- lora1_wt = lora_scale_slider("LoRA 1: weight")
135
- with gr.Row():
136
- lora1_info = lora_textbox()
137
- lora1_copy = gr.Button(value="Copy example to prompt", visible=False)
138
- lora1_md = gr.Markdown(value="", visible=False)
139
- with gr.Column():
140
- with gr.Row():
141
- lora2 = lora_dropdown("LoRA 2")
142
- lora2_wt = lora_scale_slider("LoRA 2: weight")
143
- with gr.Row():
144
- lora2_info = lora_textbox()
145
- lora2_copy = gr.Button(value="Copy example to prompt", visible=False)
146
- lora2_md = gr.Markdown(value="", visible=False)
147
- with gr.Column():
148
- with gr.Row():
149
- lora3 = lora_dropdown("LoRA 3")
150
- lora3_wt = lora_scale_slider("LoRA 3: weight")
151
- with gr.Row():
152
- lora3_info = lora_textbox()
153
- lora3_copy = gr.Button(value="Copy example to prompt", visible=False)
154
- lora3_md = gr.Markdown(value="", visible=False)
155
- with gr.Column():
156
- with gr.Row():
157
- lora4 = lora_dropdown("LoRA 4")
158
- lora4_wt = lora_scale_slider("LoRA 4: weight")
159
- with gr.Row():
160
- lora4_info = lora_textbox()
161
- lora4_copy = gr.Button(value="Copy example to prompt", visible=False)
162
- lora4_md = gr.Markdown(value="", visible=False)
163
- with gr.Column():
164
- with gr.Row():
165
- lora5 = lora_dropdown("LoRA 5")
166
- lora5_wt = lora_scale_slider("LoRA 5: weight")
167
- with gr.Row():
168
- lora5_info = lora_textbox()
169
- lora5_copy = gr.Button(value="Copy example to prompt", visible=False)
170
- lora5_md = gr.Markdown(value="", visible=False)
171
- with gr.Column():
172
- with gr.Row():
173
- lora6 = lora_dropdown("LoRA 6", visible=False)
174
- lora6_wt = lora_scale_slider("LoRA 6: weight", visible=False)
175
- with gr.Row():
176
- lora6_info = lora_textbox()
177
- lora6_copy = gr.Button(value="Copy example to prompt", visible=False)
178
- lora6_md = gr.Markdown(value="", visible=False)
179
- with gr.Column():
180
- with gr.Row():
181
- lora7 = lora_dropdown("LoRA 7", visible=False)
182
- lora7_wt = lora_scale_slider("LoRA 7: weight", visible=False)
183
- with gr.Row():
184
- lora7_info = lora_textbox()
185
- lora7_copy = gr.Button(value="Copy example to prompt", visible=False)
186
- lora7_md = gr.Markdown(value="", visible=False)
187
- with gr.Accordion("From URL", open=True, visible=True):
188
- with gr.Row():
189
- lora_search_civitai_basemodel = gr.CheckboxGroup(label="Search LoRA for", choices=CIVITAI_BASEMODEL, value=["Pony", "Illustrious", "SDXL 1.0"])
190
- lora_search_civitai_sort = gr.Radio(label="Sort", choices=CIVITAI_SORT, value="Highest Rated")
191
- lora_search_civitai_period = gr.Radio(label="Period", choices=CIVITAI_PERIOD, value="AllTime")
192
- with gr.Row():
193
- lora_search_civitai_query = gr.Textbox(label="Query", placeholder="oomuro sakurako...", lines=1)
194
- lora_search_civitai_tag = gr.Dropdown(label="Tag", choices=get_civitai_tag(), value=get_civitai_tag()[0], allow_custom_value=True)
195
- lora_search_civitai_user = gr.Textbox(label="Username", lines=1)
196
- lora_search_civitai_submit = gr.Button("Search on Civitai")
197
- with gr.Row():
198
- lora_search_civitai_json = gr.JSON(value={}, visible=False)
199
- lora_search_civitai_desc = gr.Markdown(value="", visible=False, elem_classes="desc")
200
- with gr.Accordion("Select from Gallery", open=False):
201
- lora_search_civitai_gallery = gr.Gallery([], label="Results", allow_preview=False, columns=5, show_share_button=False, interactive=False)
202
- lora_search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
203
- lora_download_url = gr.Textbox(label="LoRA's download URL", placeholder="https://civitai.com/api/download/models/28907", info="It has to be .safetensors files, and you can also download them from Hugging Face.", lines=1)
204
- lora_download = gr.Button("Get and set LoRA and apply to prompt")
205
-
206
- with gr.Tab("ControlNet / Img2img / Inpaint"):
207
- with gr.Row():
208
- #image_control = gr.Image(label="Image ControlNet / Inpaint / Img2img", type="filepath", height=384, sources=["upload", "clipboard", "webcam"], show_share_button=False)
209
- image_control = gr.ImageEditor(label="Image ControlNet / Inpaint / Img2img", type="filepath", sources=["upload", "clipboard", "webcam"], image_mode='RGB',
210
- show_share_button=False, show_fullscreen_button=False, layers=False, canvas_size=(384, 384), width=384, height=512,
211
- brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed", default_size=32), eraser=gr.Eraser(default_size="32"))
212
- image_mask = gr.Image(label="Image Mask", type="filepath", height=384, sources=["upload", "clipboard"], show_share_button=False)
213
- with gr.Row():
214
- strength = gr.Slider(minimum=0.01, maximum=1.0, step=0.01, value=0.55, label="Strength",
215
- info="This option adjusts the level of changes for img2img and inpainting.")
216
- image_resolution = gr.Slider(minimum=64, maximum=2048, step=64, value=1024, label="Image Resolution",
217
- info="The maximum proportional size of the generated image based on the uploaded image.")
218
- with gr.Row():
219
- controlnet_model = gr.Dropdown(label="ControlNet model", choices=DIFFUSERS_CONTROLNET_MODEL, value=DIFFUSERS_CONTROLNET_MODEL[0])
220
- control_net_output_scaling = gr.Slider(minimum=0, maximum=5.0, step=0.1, value=1, label="ControlNet Output Scaling in UNet")
221
- control_net_start_threshold = gr.Slider(minimum=0, maximum=1, step=0.01, value=0, label="ControlNet Start Threshold (%)")
222
- control_net_stop_threshold = gr.Slider(minimum=0, maximum=1, step=0.01, value=1, label="ControlNet Stop Threshold (%)")
223
- with gr.Row():
224
- preprocessor_name = gr.Dropdown(label="Preprocessor Name", choices=TASK_AND_PREPROCESSORS["canny"])
225
- preprocess_resolution = gr.Slider(minimum=64, maximum=2048, step=64, value=512, label="Preprocessor Resolution")
226
- low_threshold = gr.Slider(minimum=1, maximum=255, step=1, value=100, label="'CANNY' low threshold")
227
- high_threshold = gr.Slider(minimum=1, maximum=255, step=1, value=200, label="'CANNY' high threshold")
228
- with gr.Row():
229
- value_threshold = gr.Slider(minimum=1, maximum=2.0, step=0.01, value=0.1, label="'MLSD' Hough value threshold")
230
- distance_threshold = gr.Slider(minimum=1, maximum=20.0, step=0.01, value=0.1, label="'MLSD' Hough distance threshold")
231
- recolor_gamma_correction = gr.Number(minimum=0., maximum=25., value=1., step=0.001, label="'RECOLOR' gamma correction")
232
- tile_blur_sigma = gr.Number(minimum=0, maximum=100, value=9, step=1, label="'TILE' blur sigma")
233
-
234
- with gr.Tab("IP-Adapter"):
235
- IP_MODELS = sorted(list(set(IP_ADAPTERS_SD + IP_ADAPTERS_SDXL)))
236
- MODE_IP_OPTIONS = ["original", "style", "layout", "style+layout"]
237
- with gr.Accordion("IP-Adapter 1", open=True, visible=True):
238
- with gr.Row():
239
- #image_ip1 = gr.Image(label="IP Image", type="filepath", height=384, sources=["upload", "clipboard"], show_share_button=False)
240
- image_ip1 = gr.ImageEditor(label="IP Image", type="filepath", sources=["upload", "clipboard", "webcam"], image_mode='RGB',
241
- show_share_button=False, show_fullscreen_button=False, layers=False, canvas_size=(384, 384), width=384, height=512,
242
- brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed", default_size=32), eraser=gr.Eraser(default_size="32"))
243
- mask_ip1 = gr.Image(label="IP Mask (optional)", type="filepath", height=384, sources=["upload", "clipboard"], show_share_button=False)
244
- with gr.Row():
245
- model_ip1 = gr.Dropdown(value="plus_face", label="Model", choices=IP_MODELS)
246
- mode_ip1 = gr.Dropdown(value="original", label="Mode", choices=MODE_IP_OPTIONS)
247
- scale_ip1 = gr.Slider(minimum=0., maximum=2., step=0.01, value=0.7, label="Scale")
248
- with gr.Accordion("IP-Adapter 2", open=True, visible=True):
249
- with gr.Row():
250
- #image_ip2 = gr.Image(label="IP Image", type="filepath", height=384, sources=["upload", "clipboard"], show_share_button=False)
251
- image_ip2 = gr.ImageEditor(label="IP Image", type="filepath", sources=["upload", "clipboard", "webcam"], image_mode='RGB',
252
- show_share_button=False, show_fullscreen_button=False, layers=False, canvas_size=(384, 384), width=384, height=512,
253
- brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed", default_size=32), eraser=gr.Eraser(default_size="32"))
254
- mask_ip2 = gr.Image(label="IP Mask (optional)", type="filepath", height=384, sources=["upload", "clipboard"], show_share_button=False)
255
  with gr.Row():
256
- model_ip2 = gr.Dropdown(value="base", label="Model", choices=IP_MODELS)
257
- mode_ip2 = gr.Dropdown(value="style", label="Mode", choices=MODE_IP_OPTIONS)
258
- scale_ip2 = gr.Slider(minimum=0., maximum=2., step=0.01, value=0.7, label="Scale")
259
-
260
- with gr.Tab("Inpaint Mask Maker"):
261
  with gr.Row():
262
- with gr.Column():
263
- image_base = gr.ImageEditor(sources=["upload", "clipboard", "webcam"],
264
- brush=gr.Brush(default_size="32", color_mode="fixed", colors=["rgba(0, 0, 0, 1)", "rgba(0, 0, 0, 0.1)", "rgba(255, 255, 255, 0.1)"]),
265
- eraser=gr.Eraser(default_size="32"), show_share_button=False, show_fullscreen_button=False,
266
- canvas_size=(384, 384), width=384, height=512)
267
- invert_mask = gr.Checkbox(value=False, label="Invert mask")
268
- cm_btn = gr.Button("Create mask")
269
- with gr.Column():
270
- img_source = gr.Image(interactive=False, height=384, show_share_button=False)
271
- img_result = gr.Image(label="Mask image", show_label=True, interactive=False, height=384, show_share_button=False)
272
- cm_btn_send = gr.Button("Send to ControlNet / Img2img / Inpaint")
273
- cm_btn_send_ip1 = gr.Button("Send to IP-Adapter 1")
274
- cm_btn_send_ip2 = gr.Button("Send to IP-Adapter 2")
275
- cm_btn.click(create_mask_now, [image_base, invert_mask], [img_source, img_result], show_api=False)
276
- def send_img(img_source, img_result):
277
- return img_source, img_result
278
- cm_btn_send.click(send_img, [img_source, img_result], [image_control, image_mask], queue=False, show_api=False)
279
- cm_btn_send_ip1.click(send_img, [img_source, img_result], [image_ip1, mask_ip1], queue=False, show_api=False)
280
- cm_btn_send_ip2.click(send_img, [img_source, img_result], [image_ip2, mask_ip2], queue=False, show_api=False)
281
-
282
- with gr.Tab("Hires fix / Detailfix"):
283
- with gr.Accordion("Hires fix", open=True):
284
- with gr.Row():
285
- upscaler_model_path = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS, value=UPSCALER_KEYS[0])
286
- upscaler_increases_size = gr.Slider(minimum=1.1, maximum=4., step=0.1, value=1.2, label="Upscale by")
287
- esrgan_tile = gr.Slider(minimum=0, value=0, maximum=500, step=1, label="ESRGAN Tile")
288
- esrgan_tile_overlap = gr.Slider(minimum=1, maximum=200, step=1, value=8, label="ESRGAN Tile Overlap")
289
- with gr.Row():
290
- hires_steps = gr.Slider(minimum=0, value=30, maximum=100, step=1, label="Hires Steps")
291
- hires_denoising_strength = gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.55, label="Hires Denoising Strength")
292
- hires_sampler = gr.Dropdown(label="Hires Sampler", choices=POST_PROCESSING_SAMPLER, value=POST_PROCESSING_SAMPLER[0])
293
- hires_schedule_list = ["Use same schedule type"] + SCHEDULE_TYPE_OPTIONS
294
- hires_schedule_type = gr.Dropdown(label="Hires Schedule type", choices=hires_schedule_list, value=hires_schedule_list[0])
295
- hires_guidance_scale = gr.Slider(minimum=-1., maximum=30., step=0.5, value=-1., label="Hires CFG", info="If the value is -1, the main CFG will be used")
296
- with gr.Row():
297
- hires_prompt = gr.Textbox(label="Hires Prompt", placeholder="Main prompt will be use", lines=3)
298
- hires_negative_prompt = gr.Textbox(label="Hires Negative Prompt", placeholder="Main negative prompt will be use", lines=3)
299
- with gr.Accordion("Detail fix", open=True):
300
- with gr.Row():
301
- # Adetailer Inpaint Only
302
- adetailer_inpaint_only = gr.Checkbox(label="Inpaint only", value=True)
303
- # Adetailer Verbose
304
- adetailer_verbose = gr.Checkbox(label="Verbose", value=False)
305
- # Adetailer Sampler
306
- adetailer_sampler = gr.Dropdown(label="Adetailer sampler:", choices=POST_PROCESSING_SAMPLER, value=POST_PROCESSING_SAMPLER[0])
307
  with gr.Row():
308
- with gr.Accordion("Detailfix A", open=True, visible=True):
309
- # Adetailer A
310
- adetailer_active_a = gr.Checkbox(label="Enable Adetailer A", value=False)
311
- prompt_ad_a = gr.Textbox(label="Main prompt", placeholder="Main prompt will be use", lines=3)
312
- negative_prompt_ad_a = gr.Textbox(label="Negative prompt", placeholder="Main negative prompt will be use", lines=3)
313
- with gr.Row():
314
- strength_ad_a = gr.Number(label="Strength:", value=0.35, step=0.01, minimum=0.01, maximum=1.0)
315
- face_detector_ad_a = gr.Checkbox(label="Face detector", value=False)
316
- person_detector_ad_a = gr.Checkbox(label="Person detector", value=True)
317
- hand_detector_ad_a = gr.Checkbox(label="Hand detector", value=False)
318
- with gr.Row():
319
- mask_dilation_a = gr.Number(label="Mask dilation:", value=4, minimum=1)
320
- mask_blur_a = gr.Number(label="Mask blur:", value=4, minimum=1)
321
- mask_padding_a = gr.Number(label="Mask padding:", value=32, minimum=1)
322
- with gr.Accordion("Detailfix B", open=True, visible=True):
323
- # Adetailer B
324
- adetailer_active_b = gr.Checkbox(label="Enable Adetailer B", value=False)
325
- prompt_ad_b = gr.Textbox(label="Main prompt", placeholder="Main prompt will be use", lines=3)
326
- negative_prompt_ad_b = gr.Textbox(label="Negative prompt", placeholder="Main negative prompt will be use", lines=3)
327
- with gr.Row():
328
- strength_ad_b = gr.Number(label="Strength:", value=0.35, step=0.01, minimum=0.01, maximum=1.0)
329
- face_detector_ad_b = gr.Checkbox(label="Face detector", value=False)
330
- person_detector_ad_b = gr.Checkbox(label="Person detector", value=True)
331
- hand_detector_ad_b = gr.Checkbox(label="Hand detector", value=False)
332
- with gr.Row():
333
- mask_dilation_b = gr.Number(label="Mask dilation:", value=4, minimum=1)
334
- mask_blur_b = gr.Number(label="Mask blur:", value=4, minimum=1)
335
- mask_padding_b = gr.Number(label="Mask padding:", value=32, minimum=1)
336
-
337
- with gr.Tab("Translation Settings"):
338
- chatbot = gr.Chatbot(render_markdown=False, visible=False) # component for auto-translation
339
- chat_model = gr.Dropdown(choices=get_dolphin_models(), value=get_dolphin_models()[0][1], allow_custom_value=True, label="Model")
340
- chat_model_info = gr.Markdown(value=get_dolphin_model_info(get_dolphin_models()[0][1]), label="Model info")
341
- chat_format = gr.Dropdown(choices=get_llm_formats(), value=get_dolphin_model_format(get_dolphin_models()[0][1]), label="Message format")
342
- with gr.Row():
343
- chat_tokens = gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max tokens")
344
- chat_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
345
- chat_topp = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
346
- chat_topk = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k")
347
- chat_rp = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
348
- chat_sysmsg = gr.Textbox(value=get_dolphin_sysprompt(), label="System message")
349
-
350
- examples = gr.Examples(
351
- examples = [
352
- ["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"],
353
- ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
354
- ["kafuu chino, 1girl, solo"],
355
- ["1girl"],
356
- ["beautiful sunset"],
357
- ],
358
- inputs=[prompt],
359
- cache_examples=False,
360
- )
361
-
362
- model_name.change(update_task_options, [model_name, task], [task], queue=False, show_api=False)
363
- task.change(change_preprocessor_choices, [task], [preprocessor_name], queue=False, show_api=False)
364
- active_textual_inversion.change(update_textual_inversion, [active_textual_inversion, model_name], [use_textual_inversion], queue=False, show_api=False)
365
- model_name.change(update_textual_inversion, [active_textual_inversion, model_name], [use_textual_inversion], queue=False, show_api=False)
366
- use_textual_inversion.change(set_textual_inversion_prompt, [use_textual_inversion, prompt, negative_prompt, prompt_syntax], [prompt, negative_prompt])
367
-
368
- gr.on( #lambda x: None, inputs=None, outputs=result).then(
369
- triggers=[run_button.click, prompt.submit],
370
- fn=infer,
371
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
372
- guidance_scale, num_inference_steps, model_name,
373
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt,
374
- lora5, lora5_wt, lora6, lora6_wt, lora7, lora7_wt, task, prompt_syntax,
375
- sampler, vae_model, schedule_type, schedule_prediction_type,
376
- clip_skip, pag_scale, free_u, guidance_rescale,
377
- image_control, image_mask, strength, image_resolution,
378
- controlnet_model, control_net_output_scaling, control_net_start_threshold, control_net_stop_threshold,
379
- preprocessor_name, preprocess_resolution, low_threshold, high_threshold,
380
- value_threshold, distance_threshold, recolor_gamma_correction, tile_blur_sigma,
381
- image_ip1, mask_ip1, model_ip1, mode_ip1, scale_ip1,
382
- image_ip2, mask_ip2, model_ip2, mode_ip2, scale_ip2,
383
- upscaler_model_path, upscaler_increases_size, esrgan_tile, esrgan_tile_overlap, hires_steps, hires_denoising_strength,
384
- hires_sampler, hires_schedule_type, hires_guidance_scale, hires_prompt, hires_negative_prompt,
385
- adetailer_inpaint_only, adetailer_verbose, adetailer_sampler, adetailer_active_a,
386
- prompt_ad_a, negative_prompt_ad_a, strength_ad_a, face_detector_ad_a, person_detector_ad_a, hand_detector_ad_a,
387
- mask_dilation_a, mask_blur_a, mask_padding_a, adetailer_active_b, prompt_ad_b, negative_prompt_ad_b, strength_ad_b,
388
- face_detector_ad_b, person_detector_ad_b, hand_detector_ad_b, mask_dilation_b, mask_blur_b, mask_padding_b,
389
- active_textual_inversion, gpu_duration, auto_trans, recom_prompt],
390
- outputs=[result],
391
- queue=True,
392
- show_progress="full",
393
- show_api=True,
394
- )
395
-
396
- gr.on( #lambda x: None, inputs=None, outputs=result).then(
397
- triggers=[run_translate_button.click],
398
- fn=_infer, # dummy fn for api
399
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
400
- guidance_scale, num_inference_steps, model_name,
401
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt,
402
- lora5, lora5_wt, lora6, lora6_wt, lora7, lora7_wt, task, prompt_syntax,
403
- sampler, vae_model, schedule_type, schedule_prediction_type,
404
- clip_skip, pag_scale, free_u, guidance_rescale,
405
- image_control, image_mask, strength, image_resolution,
406
- controlnet_model, control_net_output_scaling, control_net_start_threshold, control_net_stop_threshold,
407
- preprocessor_name, preprocess_resolution, low_threshold, high_threshold,
408
- value_threshold, distance_threshold, recolor_gamma_correction, tile_blur_sigma,
409
- image_ip1, mask_ip1, model_ip1, mode_ip1, scale_ip1,
410
- image_ip2, mask_ip2, model_ip2, mode_ip2, scale_ip2,
411
- upscaler_model_path, upscaler_increases_size, esrgan_tile, esrgan_tile_overlap, hires_steps, hires_denoising_strength,
412
- hires_sampler, hires_schedule_type, hires_guidance_scale, hires_prompt, hires_negative_prompt,
413
- adetailer_inpaint_only, adetailer_verbose, adetailer_sampler, adetailer_active_a,
414
- prompt_ad_a, negative_prompt_ad_a, strength_ad_a, face_detector_ad_a, person_detector_ad_a, hand_detector_ad_a,
415
- mask_dilation_a, mask_blur_a, mask_padding_a, adetailer_active_b, prompt_ad_b, negative_prompt_ad_b, strength_ad_b,
416
- face_detector_ad_b, person_detector_ad_b, hand_detector_ad_b, mask_dilation_b, mask_blur_b, mask_padding_b,
417
- active_textual_inversion, gpu_duration, auto_trans, recom_prompt],
418
- outputs=[result],
419
- queue=False,
420
- show_api=True,
421
- api_name="infer_translate",
422
- ).success(
423
- fn=dolphin_respond_auto,
424
- inputs=[prompt, chatbot, chat_model, chat_sysmsg, chat_tokens, chat_temperature, chat_topp, chat_topk, chat_rp, state],
425
- outputs=[chatbot, result, prompt],
426
- queue=True,
427
- show_progress="full",
428
- show_api=False,
429
- ).success(
430
- fn=dolphin_parse_simple,
431
- inputs=[prompt, chatbot, state],
432
- outputs=[prompt],
433
- queue=False,
434
- show_api=False,
435
- ).success(
436
- fn=infer,
437
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
438
- guidance_scale, num_inference_steps, model_name,
439
- lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt,
440
- lora5, lora5_wt, lora6, lora6_wt, lora7, lora7_wt, task, prompt_syntax,
441
- sampler, vae_model, schedule_type, schedule_prediction_type,
442
- clip_skip, pag_scale, free_u, guidance_rescale,
443
- image_control, image_mask, strength, image_resolution,
444
- controlnet_model, control_net_output_scaling, control_net_start_threshold, control_net_stop_threshold,
445
- preprocessor_name, preprocess_resolution, low_threshold, high_threshold,
446
- value_threshold, distance_threshold, recolor_gamma_correction, tile_blur_sigma,
447
- image_ip1, mask_ip1, model_ip1, mode_ip1, scale_ip1,
448
- image_ip2, mask_ip2, model_ip2, mode_ip2, scale_ip2,
449
- upscaler_model_path, upscaler_increases_size, esrgan_tile, esrgan_tile_overlap, hires_steps, hires_denoising_strength,
450
- hires_sampler, hires_schedule_type, hires_guidance_scale, hires_prompt, hires_negative_prompt,
451
- adetailer_inpaint_only, adetailer_verbose, adetailer_sampler, adetailer_active_a,
452
- prompt_ad_a, negative_prompt_ad_a, strength_ad_a, face_detector_ad_a, person_detector_ad_a, hand_detector_ad_a,
453
- mask_dilation_a, mask_blur_a, mask_padding_a, adetailer_active_b, prompt_ad_b, negative_prompt_ad_b, strength_ad_b,
454
- face_detector_ad_b, person_detector_ad_b, hand_detector_ad_b, mask_dilation_b, mask_blur_b, mask_padding_b,
455
- active_textual_inversion, gpu_duration, auto_trans, recom_prompt],
456
- outputs=[result],
457
- queue=True,
458
- show_progress="full",
459
- show_api=False,
460
- ).success(lambda: None, None, chatbot, queue=False, show_api=False)\
461
- .success(pass_result, [result], [result], queue=False, show_api=False) # dummy fn for api
462
-
463
- result.change(save_image_history, [result, history_gallery, history_files, model_name], [history_gallery, history_files], queue=False, show_api=False)
464
-
465
- gr.on(
466
- triggers=[lora1.change, lora1_wt.change, lora2.change, lora2_wt.change, lora3.change, lora3_wt.change,
467
- lora4.change, lora4_wt.change, lora5.change, lora5_wt.change, lora6.change, lora6_wt.change, lora7.change, lora7_wt.change, prompt_syntax.change],
468
- fn=update_loras,
469
- inputs=[prompt, prompt_syntax, lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt, lora6, lora6_wt, lora7, lora7_wt],
470
- outputs=[prompt, lora1, lora1_wt, lora1_info, lora1_copy, lora1_md,
471
- lora2, lora2_wt, lora2_info, lora2_copy, lora2_md, lora3, lora3_wt, lora3_info, lora3_copy, lora3_md,
472
- lora4, lora4_wt, lora4_info, lora4_copy, lora4_md, lora5, lora5_wt, lora5_info, lora5_copy, lora5_md,
473
- lora6, lora6_wt, lora6_info, lora6_copy, lora6_md, lora7, lora7_wt, lora7_info, lora7_copy, lora7_md],
474
- queue=False,
475
- trigger_mode="once",
476
- show_api=False,
477
- )
478
- lora1_copy.click(apply_lora_prompt, [prompt, lora1_info], [prompt], queue=False, show_api=False)
479
- lora2_copy.click(apply_lora_prompt, [prompt, lora2_info], [prompt], queue=False, show_api=False)
480
- lora3_copy.click(apply_lora_prompt, [prompt, lora3_info], [prompt], queue=False, show_api=False)
481
- lora4_copy.click(apply_lora_prompt, [prompt, lora4_info], [prompt], queue=False, show_api=False)
482
- lora5_copy.click(apply_lora_prompt, [prompt, lora5_info], [prompt], queue=False, show_api=False)
483
- lora6_copy.click(apply_lora_prompt, [prompt, lora6_info], [prompt], queue=False, show_api=False)
484
- lora7_copy.click(apply_lora_prompt, [prompt, lora7_info], [prompt], queue=False, show_api=False)
485
-
486
- gr.on(
487
- triggers=[lora_search_civitai_submit.click, lora_search_civitai_query.submit],
488
- fn=search_civitai_lora,
489
- inputs=[lora_search_civitai_query, lora_search_civitai_basemodel, lora_search_civitai_sort, lora_search_civitai_period, lora_search_civitai_tag, lora_search_civitai_user, lora_search_civitai_gallery],
490
- outputs=[lora_search_civitai_result, lora_search_civitai_desc, lora_search_civitai_submit, lora_search_civitai_query, lora_search_civitai_gallery],
491
- scroll_to_output=True,
492
- queue=True,
493
- show_api=False,
494
- )
495
- 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
496
- 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)
497
- gr.on(
498
- triggers=[lora_download.click, lora_download_url.submit],
499
- fn=download_my_lora,
500
- inputs=[lora_download_url, lora1, lora2, lora3, lora4, lora5, lora6, lora7],
501
- outputs=[lora1, lora2, lora3, lora4, lora5, lora6, lora7],
502
- scroll_to_output=True,
503
- queue=True,
504
- show_api=False,
505
- )
506
- lora_search_civitai_gallery.select(update_civitai_selection, None, [lora_search_civitai_result], queue=False, show_api=False)
507
-
508
- #recom_prompt.change(enable_model_recom_prompt, [recom_prompt], [recom_prompt], queue=False, show_api=False)
509
- gr.on(
510
- triggers=[quality_selector.change, style_selector.change],
511
- fn=process_style_prompt,
512
- inputs=[prompt, negative_prompt, style_selector, quality_selector],
513
- outputs=[prompt, negative_prompt],
514
- queue=False,
515
- trigger_mode="once",
516
- show_api=False,
517
- )
518
-
519
- model_detail.change(enable_diffusers_model_detail, [model_detail, model_name, state], [model_detail, model_name, state], queue=False, show_api=False)
520
- model_name.change(get_t2i_model_info, [model_name], [model_info], queue=False, show_api=False)
521
-
522
- chat_model.change(select_dolphin_model, [chat_model, state], [chat_model, chat_format, chat_model_info, state], queue=True, show_progress="full", show_api=False)\
523
- .success(lambda: None, None, chatbot, queue=False, show_api=False)
524
- chat_format.change(select_dolphin_format, [chat_format, state], [chat_format, state], queue=False, show_api=False)\
525
- .success(lambda: None, None, chatbot, queue=False, show_api=False)
526
-
527
- # Tagger
528
- with gr.Tab("Tags Transformer with Tagger"):
529
- with gr.Column():
530
- with gr.Group():
531
- input_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
532
- with gr.Accordion(label="Advanced options", open=False):
533
- general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
534
- character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
535
- input_tag_type = gr.Radio(label="Convert tags to", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
536
- recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
537
- image_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
538
- keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
539
- generate_from_image_btn = gr.Button(value="GENERATE TAGS FROM IMAGE", size="lg", variant="primary")
540
- with gr.Group():
541
- with gr.Row():
542
- input_character = gr.Textbox(label="Character tags", placeholder="hatsune miku")
543
- input_copyright = gr.Textbox(label="Copyright tags", placeholder="vocaloid")
544
- random_character = gr.Button(value="Random character 🎲", size="sm")
545
- input_general = gr.TextArea(label="General tags", lines=4, placeholder="1girl, ...", value="")
546
- input_tags_to_copy = gr.Textbox(value="", visible=False)
547
  with gr.Row():
548
- copy_input_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
549
- copy_prompt_btn_input = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
550
- translate_input_prompt_button = gr.Button(value="Translate prompt to English", size="sm", variant="secondary")
551
- tag_type = gr.Radio(label="Output tag conversion", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="e621", visible=False)
552
- input_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="explicit")
553
- with gr.Accordion(label="Advanced options", open=False):
554
- input_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square")
555
- input_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="very_long")
556
- 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")
557
- input_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
558
- model_name = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
559
- dummy_np = gr.Textbox(label="Negative prompt", value="", visible=False)
560
- recom_animagine = gr.Textbox(label="Animagine reccomended prompt", value="Animagine", visible=False)
561
- recom_pony = gr.Textbox(label="Pony reccomended prompt", value="Pony", visible=False)
562
- generate_btn = gr.Button(value="GENERATE TAGS", size="lg", variant="primary")
563
- with gr.Row():
564
  with gr.Group():
565
- output_text = gr.TextArea(label="Output tags", interactive=False, show_copy_button=True)
566
  with gr.Row():
567
- copy_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
568
- copy_prompt_btn = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
569
- with gr.Group():
570
- output_text_pony = gr.TextArea(label="Output tags (Pony e621 style)", interactive=False, show_copy_button=True)
571
  with gr.Row():
572
- copy_btn_pony = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
573
- copy_prompt_btn_pony = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
574
-
575
- random_character.click(select_random_character, [input_copyright, input_character], [input_copyright, input_character], queue=False, show_api=False)
576
-
577
- translate_input_prompt_button.click(translate_prompt, [input_general], [input_general], queue=False, show_api=False)
578
- translate_input_prompt_button.click(translate_prompt, [input_character], [input_character], queue=False, show_api=False)
579
- translate_input_prompt_button.click(translate_prompt, [input_copyright], [input_copyright], queue=False, show_api=False)
580
-
581
- generate_from_image_btn.click(
582
- lambda: ("", "", ""), None, [input_copyright, input_character, input_general], queue=False, show_api=False,
583
- ).success(
584
- predict_tags_wd,
585
- [input_image, input_general, image_algorithms, general_threshold, character_threshold],
586
- [input_copyright, input_character, input_general, copy_input_btn],
587
- show_api=False,
588
- ).success(
589
- predict_tags_fl2_sd3, [input_image, input_general, image_algorithms], [input_general], show_api=False,
590
- ).success(
591
- remove_specific_prompt, [input_general, keep_tags], [input_general], queue=False, show_api=False,
592
- ).success(
593
- convert_danbooru_to_e621_prompt, [input_general, input_tag_type], [input_general], queue=False, show_api=False,
594
- ).success(
595
- insert_recom_prompt, [input_general, dummy_np, recom_prompt], [input_general, dummy_np], queue=False, show_api=False,
596
- ).success(lambda: gr.update(interactive=True), None, [copy_prompt_btn_input], queue=False, show_api=False)
597
- copy_input_btn.click(compose_prompt_to_copy, [input_character, input_copyright, input_general], [input_tags_to_copy], show_api=False)\
598
- .success(gradio_copy_text, [input_tags_to_copy], js=COPY_ACTION_JS, show_api=False)
599
- copy_prompt_btn_input.click(compose_prompt_to_copy, inputs=[input_character, input_copyright, input_general], outputs=[input_tags_to_copy], show_api=False)\
600
- .success(gradio_copy_prompt, inputs=[input_tags_to_copy], outputs=[prompt], show_api=False)
601
-
602
- generate_btn.click(
603
- v2_upsampling_prompt,
604
- [model_name, input_copyright, input_character, input_general,
605
- input_rating, input_aspect_ratio, input_length, input_identity, input_ban_tags],
606
- [output_text],
607
- show_api=False,
608
- ).success(
609
- convert_danbooru_to_e621_prompt, [output_text, tag_type], [output_text_pony], queue=False, show_api=False,
610
- ).success(
611
- insert_recom_prompt, [output_text, dummy_np, recom_animagine], [output_text, dummy_np], queue=False, show_api=False,
612
- ).success(
613
- insert_recom_prompt, [output_text_pony, dummy_np, recom_pony], [output_text_pony, dummy_np], queue=False, show_api=False,
614
- ).success(lambda: (gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)),
615
- None, [copy_btn, copy_btn_pony, copy_prompt_btn, copy_prompt_btn_pony], queue=False, show_api=False)
616
- copy_btn.click(gradio_copy_text, [output_text], js=COPY_ACTION_JS, show_api=False)
617
- copy_btn_pony.click(gradio_copy_text, [output_text_pony], js=COPY_ACTION_JS, show_api=False)
618
- copy_prompt_btn.click(gradio_copy_prompt, inputs=[output_text], outputs=[prompt], show_api=False)
619
- copy_prompt_btn_pony.click(gradio_copy_prompt, inputs=[output_text_pony], outputs=[prompt], show_api=False)
620
-
621
- with gr.Tab("PNG Info"):
622
- with gr.Row():
623
- with gr.Column():
624
- image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
625
-
626
- with gr.Column():
627
- result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
628
-
629
- image_metadata.change(
630
- fn=extract_exif_data,
631
- inputs=[image_metadata],
632
- outputs=[result_metadata],
633
- )
634
-
635
- with gr.Tab("Upscaler"):
636
- with gr.Row():
637
- with gr.Column():
638
- image_up_tab = gr.Image(label="Image", type="pil", sources=["upload"])
639
- upscaler_tab = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS[9:], value=UPSCALER_KEYS[11])
640
- upscaler_size_tab = gr.Slider(minimum=1., maximum=4., step=0.1, value=1.1, label="Upscale by")
641
- generate_button_up_tab = gr.Button(value="START UPSCALE", variant="primary")
642
-
643
- with gr.Column():
644
- result_up_tab = gr.Image(label="Result", type="pil", interactive=False, format="png")
645
-
646
- generate_button_up_tab.click(
647
- fn=esrgan_upscale,
648
- inputs=[image_up_tab, upscaler_tab, upscaler_size_tab],
649
- outputs=[result_up_tab],
650
- )
651
-
652
- with gr.Tab("Preprocessor", render=True):
653
- preprocessor_tab()
654
-
655
- gr.LoginButton()
656
- gr.DuplicateButton(value="Duplicate Space for private use (This demo does not work on CPU. Requires GPU Space)")
657
 
658
  demo.queue()
659
- demo.launch(show_error=True, debug=True)
 
 
1
  import gradio as gr
2
+ import requests
3
+ from pathlib import Path
4
+ import re
5
+ import os
6
+ import tempfile
7
+ import shutil
8
+ import urllib
9
+ from huggingface_hub import whoami, HfApi, hf_hub_download, RepoCard
10
+ from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
11
+ from gradio_huggingfacehub_search import HuggingfaceHubSearch
12
+
13
+ ENDPOINT = "https://huggingface.co"
14
+ # ENDPOINT = "http://localhost:5564"
15
+
16
+ REPO_TYPES = ["model", "dataset", "space"]
17
+ HF_REPO = os.environ.get("HF_REPO") if os.environ.get("HF_REPO") else "" # set your default repo
18
+ HF_REPO_PREFIX = os.environ.get("HF_REPO_PREFIX") if os.environ.get("HF_REPO_PREFIX") else "" # set your default repo prefix
19
+ HF_REPO_SUFFIX = os.environ.get("HF_REPO_SUFFIX") if os.environ.get("HF_REPO_SUFFIX") else "" # set your default repo suffix
20
+ HF_USER = os.environ.get("HF_USER") if os.environ.get("HF_USER") else "" # set your username
21
+ REGEX_HF_REPO = r'^[\w_\-\.]+/[\w_\-\.]+$'
22
+
23
+ def remove_repo_tags(repo_id: str, tags: list[str], repo_type: str, hf_token: str):
24
+ try:
25
+ card = RepoCard.load(repo_id, repo_type=repo_type, token=hf_token)
26
+ orig_content = card.content
27
+ for tag in tags:
28
+ if 'tags' in card.data and tag in card.data['tags']: card.data['tags'].remove(tag)
29
+ if card.content == orig_content: return
30
+ card.push_to_hub(repo_id=repo_id, repo_type=repo_type, token=hf_token)
31
+ except Exception as e:
32
+ print(f"Failed to remove tags from repocard. {e}")
33
+
34
+ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, auto_dir, remove_tag, oauth_token: gr.OAuthToken | None, progress=gr.Progress(track_tqdm=True)):
35
+ hf_token = oauth_token.token
36
+ api = HfApi(token=hf_token)
37
+ try:
38
+ if not repo_type in REPO_TYPES:
39
+ raise ValueError("need to select valid repo type")
40
+ _ = whoami(oauth_token.token)
41
+ # ^ this will throw if token is invalid
42
+ except Exception as e:
43
+ raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
44
+
45
+ try:
46
+ if re.fullmatch(REGEX_HF_REPO, source_repo): target = ""
47
+ else:
48
+ source_repo, target = re.findall(r'^(?:http.+\.co/)?(?:datasets)?(?:spaces)?([\w_\-\.]+/[\w_\-\.]+)/?(?:blob/main/)?(?:resolve/main/)?(.+)?$', source_repo)[0]
49
+ target = urllib.parse.unquote(target.removesuffix("/"))
50
+
51
+ if re.fullmatch(REGEX_HF_REPO, dst_repo): subfolder = ""
52
+ else:
53
+ dst_repo, subfolder = re.findall(r'^([\w_\-\.]+/[\w_\-\.]+)/?(.+)?$', dst_repo)[0]
54
+ subfolder = subfolder.removesuffix("/")
55
+ if auto_dir: subfolder = source_repo
56
+
57
+ if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token): raise gr.Error(f"Repo already exists {dst_repo}")
58
+
59
+ if overwrite or subfolder:
60
+ temp_dir = tempfile.mkdtemp()
61
+ api.create_repo(repo_id=dst_repo, repo_type=repo_type, private=private, exist_ok=True, token=hf_token)
62
+ for path in api.list_repo_files(repo_id=source_repo, repo_type=repo_type, token=hf_token):
63
+ if target and target not in path: continue
64
+ file = hf_hub_download(repo_id=source_repo, filename=path, repo_type=repo_type, local_dir=temp_dir, token=hf_token)
65
+ if not Path(file).exists(): continue
66
+ if Path(file).is_dir(): # unused for now
67
+ api.upload_folder(repo_id=dst_repo, folder_path=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path, repo_type=repo_type, token=hf_token)
68
+ elif Path(file).is_file():
69
+ api.upload_file(repo_id=dst_repo, path_or_fileobj=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path, repo_type=repo_type, token=hf_token)
70
+ if Path(file).exists(): Path(file).unlink()
71
+ if repo_type == "dataset": repo_url = f"https://huggingface.co/datasets/{dst_repo}"
72
+ elif repo_type == "space": repo_url = f"https://huggingface.co/spaces/{dst_repo}"
73
+ else: repo_url = f"https://huggingface.co/{dst_repo}"
74
+ shutil.rmtree(temp_dir)
75
+ else:
76
+ r = requests.post(
77
+ f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
78
+ headers=build_hf_headers(token=oauth_token.token),
79
+ json={"repository": dst_repo, "private": private},
80
+ )
81
+ hf_raise_for_status(r)
82
+
83
+ repo_url = r.json().get("url")
84
+
85
+ if remove_tag: remove_repo_tags(dst_repo, ["not-for-all-audiences"], repo_type, hf_token)
86
+
87
+ return (
88
+ f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
89
+ "sp.jpg",
90
+ )
91
 
92
+ except Exception as e:
93
+ print(e)
94
+ raise gr.Error(f"Error occured: {e}")
95
+
96
+ def parse_repos(s):
97
+ repo_pattern = r'[^\w_\-\.]?([\w_\-\.]+/[\w_\-\.]+)[^\w_\-\.]?'
98
+ try:
99
+ s = re.sub("https?://[\\w/:%#\\$&\\?\\(\\)~\\.=\\+\\-]+", "", s)
100
+ repos = re.findall(repo_pattern, s)
101
+ return list(repos)
102
+ except Exception:
103
+ return []
104
+
105
+ def duplicate_m2o(source_repos_str, dst_repo, repo_type, private, overwrite, oauth_token: gr.OAuthToken | None, progress=gr.Progress(track_tqdm=True)):
106
+ hf_token = oauth_token.token
107
+ api = HfApi(token=hf_token)
108
+ try:
109
+ if not repo_type in REPO_TYPES:
110
+ raise ValueError("need to select valid repo type")
111
+ _ = whoami(oauth_token.token)
112
+ # ^ this will throw if token is invalid
113
+ except Exception as e:
114
+ raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
115
+
116
+ try:
117
+ if re.fullmatch(REGEX_HF_REPO, dst_repo): subfolder_prefix = ""
118
+ else:
119
+ dst_repo, subfolder_prefix = re.findall(r'^([\w_\-\.]+/[\w_\-\.]+)/?(.+)?$', dst_repo)[0]
120
+ subfolder_prefix = subfolder.removesuffix("/")
121
+ if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token): raise gr.Error(f"Repo already exists {dst_repo}")
122
+ source_repos = parse_repos(source_repos_str)
123
+ for source_repo in source_repos:
124
+ if re.fullmatch(REGEX_HF_REPO, source_repo): target = ""
125
+ else:
126
+ source_repo, target = re.findall(r'^(?:http.+\.co/)?(?:datasets)?(?:spaces)?([\w_\-\.]+/[\w_\-\.]+)/?(?:blob/main/)?(?:resolve/main/)?(.+)?$', source_repo)[0]
127
+ target = urllib.parse.unquote(target.removesuffix("/"))
128
+
129
+ subfolder = subfolder_prefix + "/" + source_repo if subfolder_prefix else source_repo
130
+
131
+ temp_dir = tempfile.mkdtemp()
132
+ api.create_repo(repo_id=dst_repo, repo_type=repo_type, private=private, exist_ok=True, token=hf_token)
133
+ for path in api.list_repo_files(repo_id=source_repo, repo_type=repo_type, token=hf_token):
134
+ if target and target not in path: continue
135
+ file = hf_hub_download(repo_id=source_repo, filename=path, repo_type=repo_type, local_dir=temp_dir, token=hf_token)
136
+ if not Path(file).exists(): continue
137
+ if Path(file).is_dir(): # unused for now
138
+ api.upload_folder(repo_id=dst_repo, folder_path=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path, repo_type=repo_type, token=hf_token)
139
+ elif Path(file).is_file():
140
+ api.upload_file(repo_id=dst_repo, path_or_fileobj=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path, repo_type=repo_type, token=hf_token)
141
+ if Path(file).exists(): Path(file).unlink()
142
+ if repo_type == "dataset": repo_url = f"https://huggingface.co/datasets/{dst_repo}"
143
+ elif repo_type == "space": repo_url = f"https://huggingface.co/spaces/{dst_repo}"
144
+ else: repo_url = f"https://huggingface.co/{dst_repo}"
145
+ shutil.rmtree(temp_dir)
146
+
147
+ return (
148
+ f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
149
+ "sp.jpg",
150
+ )
151
 
152
+ except Exception as e:
153
+ print(e)
154
+ raise gr.Error(f"Error occured: {e}")
155
+
156
+ def duplicate_m2m(source_repos_str, hf_user, repo_type, private, overwrite, remove_tag, repo_prefix, repo_suffix, oauth_token: gr.OAuthToken | None, progress=gr.Progress(track_tqdm=True)):
157
+ hf_token = oauth_token.token
158
+ api = HfApi(token=hf_token)
159
+ try:
160
+ if not repo_type in REPO_TYPES:
161
+ raise ValueError("need to select valid repo type")
162
+ _ = whoami(oauth_token.token)
163
+ # ^ this will throw if token is invalid
164
+ except Exception as e:
165
+ raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
166
+
167
+ try:
168
+ source_repos = parse_repos(source_repos_str)
169
+ repo_url_result = 'Find your repo '
170
+ for source_repo in source_repos:
171
+ if not re.fullmatch(REGEX_HF_REPO, source_repo) or not api.repo_exists(repo_id=source_repo, repo_type=repo_type, token=hf_token): continue
172
+ dst_repo = hf_user + "/" + repo_prefix + source_repo.split("/")[-1] + repo_suffix
173
+ if not re.fullmatch(REGEX_HF_REPO, dst_repo): continue
174
+ if not overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token):
175
+ gr.Info(f"Repo already exists {dst_repo}")
176
+ continue
177
+
178
+ r = requests.post(
179
+ f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
180
+ headers=build_hf_headers(token=oauth_token.token),
181
+ json={"repository": dst_repo, "private": private},
182
+ )
183
+ hf_raise_for_status(r)
184
+
185
+ repo_url = r.json().get("url")
186
+ repo_url_result += f'<a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">{dst_repo}</a><br>\n'
187
+
188
+ if remove_tag: remove_repo_tags(dst_repo, ["not-for-all-audiences"], repo_type, hf_token)
189
+
190
+ return (
191
+ repo_url_result,
192
+ "sp.jpg",
193
+ )
194
 
195
+ except Exception as e:
196
+ print(e)
197
+ raise gr.Error(f"Error occured: {e}")
 
 
 
198
 
199
+ def add_repo_text(repo_id: str, source_repos: str):
200
+ return source_repos + "\n" + repo_id if source_repos else repo_id
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
+ def swap_visibilty(profile: gr.OAuthProfile | None):
203
+ return gr.update(elem_classes=["main_ui_logged_in"]) if profile else gr.update(elem_classes=["main_ui_logged_out"])
 
204
 
205
+ css = '''
206
+ .main_ui_logged_out{opacity: 0.3; pointer-events: none}
207
+ .title {text-align: center; align-items: center}
208
+ '''
209
+ with gr.Blocks(css=css) as demo:
210
+ gr.LoginButton()
211
+ with gr.Column(elem_classes="main_ui_logged_out") as main_ui:
212
+ gr.Markdown("# Duplicate your repo!", elem_classes="title")
213
+ gr.Markdown("Duplicate a Hugging Face repository! This Space is a an experimental demo.")
214
+ with gr.Tab("One to One"):
215
+ with gr.Row():
216
+ with gr.Column():
217
+ search = HuggingfaceHubSearch(
218
+ label="source_repo",
219
+ placeholder="Source repository (e.g. osanseviero/src)",
220
+ search_type=["model", "dataset", "space"],
221
+ sumbit_on_select=False,
222
+ )
223
+ with gr.Group():
224
+ dst_repo = gr.Textbox(label="dst_repo", placeholder="Destination repository (e.g. osanseviero/dst)", value=HF_REPO)
225
+ repo_type = gr.Dropdown(label="repo_type", choices=REPO_TYPES, value="model")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  with gr.Row():
227
+ is_private = gr.Checkbox(label="Make new repo private?", value=True)
228
+ is_overwrite = gr.Checkbox(label="Overwrite existing repo?", value=True)
229
+ is_subdir = gr.Checkbox(label="Create subdirectories automatically?", value=True)
230
+ is_remtag = gr.Checkbox(label="Remove NFAA tag?", value=True)
 
231
  with gr.Row():
232
+ submit_button = gr.Button("Submit", variant="primary")
233
+ clear_button = gr.Button("Clear", variant="secondary")
234
+ with gr.Column():
235
+ output_md = gr.Markdown(label="output")
236
+ output_image = gr.Image(show_label=False)
237
+ with gr.Tab("Multi to One"):
238
+ with gr.Row():
239
+ with gr.Column():
240
+ m2o_search = HuggingfaceHubSearch(
241
+ label="source_repo",
242
+ placeholder="Source repository (e.g. osanseviero/src)",
243
+ search_type=["model", "dataset", "space"],
244
+ sumbit_on_select=True,
245
+ )
246
+ m2o_source_repos = gr.Textbox(label="source_repos", placeholder="Source repositories (e.g. osanseviero/src)\n...", value="", lines=10)
247
+ with gr.Group():
248
+ m2o_dst_repo = gr.Textbox(label="dst_repo", placeholder="Destination repository (e.g. osanseviero/dst)", value=HF_REPO)
249
+ m2o_repo_type = gr.Dropdown(label="repo_type", choices=REPO_TYPES, value="model")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  with gr.Row():
251
+ m2o_is_private = gr.Checkbox(label="Make new repo private?", value=True)
252
+ m2o_is_overwrite = gr.Checkbox(label="Overwrite existing repo?", value=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  with gr.Row():
254
+ m2o_submit_button = gr.Button("Submit", variant="primary")
255
+ m2o_clear_button = gr.Button("Clear", variant="secondary")
256
+ with gr.Column():
257
+ m2o_output_md = gr.Markdown(label="output")
258
+ m2o_output_image = gr.Image(show_label=False)
259
+ with gr.Tab("Multi to Multi"):
260
+ with gr.Row():
261
+ with gr.Column():
262
+ m2m_search = HuggingfaceHubSearch(
263
+ label="source_repo",
264
+ placeholder="Source repository (e.g. osanseviero/src)",
265
+ search_type=["model", "dataset", "space"],
266
+ sumbit_on_select=True,
267
+ )
268
+ m2m_source_repos = gr.Textbox(label="source_repos", placeholder="Source repositories (e.g. osanseviero/src)\n...", value="", lines=10)
 
269
  with gr.Group():
 
270
  with gr.Row():
271
+ m2m_user = gr.Textbox(label="hf_user", placeholder="Your HF username", value=HF_USER)
272
+ m2m_prefix = gr.Textbox(label="repo_prefix", value=HF_REPO_PREFIX)
273
+ m2m_suffix = gr.Textbox(label="repo_suffix", value=HF_REPO_SUFFIX)
274
+ m2m_repo_type = gr.Dropdown(label="repo_type", choices=REPO_TYPES, value="model")
275
  with gr.Row():
276
+ m2m_is_private = gr.Checkbox(label="Make new repo private?", value=True)
277
+ m2m_is_overwrite = gr.Checkbox(label="Overwrite existing repo?", value=False)
278
+ m2m_is_remtag = gr.Checkbox(label="Remove NFAA tag?", value=True)
279
+ with gr.Row():
280
+ m2m_submit_button = gr.Button("Submit", variant="primary")
281
+ m2m_clear_button = gr.Button("Clear", variant="secondary")
282
+ with gr.Column():
283
+ m2m_output_md = gr.Markdown(label="output")
284
+ m2m_output_image = gr.Image(show_label=False)
285
+ demo.load(fn=swap_visibilty, outputs=main_ui)
286
+ submit_button.click(duplicate, [search, dst_repo, repo_type, is_private, is_overwrite, is_subdir, is_remtag], [output_md, output_image])
287
+ clear_button.click(lambda: ("", HF_REPO, "model", True, True, True, True), None, [search, dst_repo, repo_type, is_private, is_overwrite, is_subdir, is_remtag], queue=False)
288
+ m2o_search.submit(add_repo_text, [m2o_search, m2o_source_repos], [m2o_source_repos], queue=False)
289
+ m2o_submit_button.click(duplicate_m2o, [m2o_source_repos, m2o_dst_repo, m2o_repo_type, m2o_is_private, m2o_is_overwrite], [m2o_output_md, m2o_output_image])
290
+ m2o_clear_button.click(lambda: ("", HF_REPO, "model", True, True, ""), None,
291
+ [m2o_search, m2o_dst_repo, m2o_repo_type, m2o_is_private, m2o_is_overwrite, m2o_source_repos], queue=False)
292
+ m2m_search.submit(add_repo_text, [m2m_search, m2m_source_repos], [m2m_source_repos], queue=False)
293
+ m2m_submit_button.click(duplicate_m2m, [m2m_source_repos, m2m_user, m2m_repo_type, m2m_is_private, m2m_is_overwrite, m2m_is_remtag, m2m_prefix, m2m_suffix],
294
+ [m2m_output_md, m2m_output_image])
295
+ m2m_clear_button.click(lambda: ("", HF_USER, "model", True, False, True, "", HF_REPO_PREFIX, HF_REPO_SUFFIX), None,
296
+ [m2m_search, m2m_user, m2m_repo_type, m2m_is_private, m2m_is_overwrite, m2m_is_remtag, m2m_source_repos, m2m_prefix, m2m_suffix], queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
  demo.queue()
299
+ demo.launch()