avans06 commited on
Commit
eda899c
·
1 Parent(s): bb844f7

Add support for pasting images from the clipboard into the gallery.

Browse files

Add the two tagger models released by deepghs to the available models.

Restore the Examples functionality.

Update Gradio version to 5.12.0

Files changed (3) hide show
  1. README.md +5 -5
  2. app.py +119 -105
  3. requirements.txt +1 -1
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: WaifuDiffusion Tagger
3
  emoji: 💬
4
- colorFrom: blue
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 5.6.0
8
  app_file: app.py
9
- pinned: false
10
  ---
11
 
12
  # Configuration
 
1
  ---
2
+ title: WaifuDiffusion Tagger multiple images
3
  emoji: 💬
4
+ colorFrom: purple
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.12.0
8
  app_file: app.py
9
+ pinned: true
10
  ---
11
 
12
  # Configuration
app.py CHANGED
@@ -33,6 +33,10 @@ CONV_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-convnext-tagger-v2"
33
  CONV2_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-convnextv2-tagger-v2"
34
  VIT_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-vit-tagger-v2"
35
 
 
 
 
 
36
  # Files to download from the repos
37
  MODEL_FILENAME = "model.onnx"
38
  LABEL_FILENAME = "selected_tags.csv"
@@ -300,7 +304,10 @@ def add_images_to_gallery(gallery: list, images):
300
  return gallery
301
 
302
  # Combine the new images with the existing gallery images
303
- gallery.extend(images)
 
 
 
304
  return gallery
305
 
306
  def remove_image_from_gallery(gallery: list, selected_image: str):
@@ -320,115 +327,122 @@ def main():
320
  predictor = Predictor()
321
 
322
  dropdown_list = [
323
- SWINV2_MODEL_DSV3_REPO,
324
  EVA02_LARGE_MODEL_DSV3_REPO,
 
325
  CONV_MODEL_DSV3_REPO,
326
  VIT_MODEL_DSV3_REPO,
327
  VIT_LARGE_MODEL_DSV3_REPO,
 
328
  MOAT_MODEL_DSV2_REPO,
329
  SWIN_MODEL_DSV2_REPO,
330
  CONV_MODEL_DSV2_REPO,
331
  CONV2_MODEL_DSV2_REPO,
332
  VIT_MODEL_DSV2_REPO,
 
 
 
333
  ]
334
 
335
  with gr.Blocks(title=TITLE) as demo:
336
- with gr.Column():
337
- gr.Markdown(
338
- value=f"<h1 style='text-align: center; margin-bottom: 1rem'>{TITLE}</h1>"
339
- )
340
- gr.Markdown(value=DESCRIPTION)
341
- with gr.Row():
 
342
  with gr.Column(variant="panel"):
 
 
 
343
  with gr.Row():
344
- submit = gr.Button(value="Submit", variant="primary", size="lg")
345
- with gr.Row():
346
- gallery = gr.Gallery(columns=5, rows=5, show_share_button=False, interactive=True, height="500px", label="Input")
347
- with gr.Row():
348
- upload_button = gr.UploadButton("Upload Images", file_types=["image"], file_count="multiple", size="sm")
349
  remove_button = gr.Button("Remove Selected Image", size="sm")
350
 
351
- model_repo = gr.Dropdown(
352
- dropdown_list,
353
- value=SWINV2_MODEL_DSV3_REPO,
354
- label="Model",
 
 
 
 
 
 
 
 
 
355
  )
356
- with gr.Row():
357
- general_thresh = gr.Slider(
358
- 0,
359
- 1,
360
- step=args.score_slider_step,
361
- value=args.score_general_threshold,
362
- label="General Tags Threshold",
363
- scale=3,
364
- )
365
- general_mcut_enabled = gr.Checkbox(
366
- value=False,
367
- label="Use MCut threshold",
368
- scale=1,
369
- )
370
- with gr.Row():
371
- character_thresh = gr.Slider(
372
- 0,
373
- 1,
374
- step=args.score_slider_step,
375
- value=args.score_character_threshold,
376
- label="Character Tags Threshold",
377
- scale=3,
378
- )
379
- character_mcut_enabled = gr.Checkbox(
380
- value=False,
381
- label="Use MCut threshold",
382
- scale=1,
383
- )
384
- with gr.Row():
385
- characters_merge_enabled = gr.Checkbox(
386
- value=True,
387
- label="Merge characters into the string output",
388
- scale=1,
389
- )
390
- with gr.Row():
391
- additional_tags_prepend = gr.Text(label="Prepend Additional tags (comma split)")
392
- additional_tags_append = gr.Text(label="Append Additional tags (comma split)")
393
- with gr.Row():
394
- clear = gr.ClearButton(
395
- components=[
396
- gallery,
397
- model_repo,
398
- general_thresh,
399
- general_mcut_enabled,
400
- character_thresh,
401
- character_mcut_enabled,
402
- characters_merge_enabled,
403
- additional_tags_prepend,
404
- additional_tags_append,
405
- ],
406
- variant="secondary",
407
- size="lg",
408
- )
409
- with gr.Column(variant="panel"):
410
- download_file = gr.File(label="Output (Download)")
411
- sorted_general_strings = gr.Textbox(label="Output (string)", show_label=True, show_copy_button=True)
412
- rating = gr.Label(label="Rating")
413
- character_res = gr.Label(label="Output (characters)")
414
- general_res = gr.Label(label="Output (tags)")
415
- clear.add(
416
- [
417
- download_file,
418
- sorted_general_strings,
419
- rating,
420
- character_res,
421
- general_res,
422
- ]
423
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
 
425
- # When the upload button is clicked, add the new images to the gallery
426
- upload_button.upload(add_images_to_gallery, inputs=[gallery, upload_button], outputs=gallery)
427
- # Event to update the selected image when an image is clicked in the gallery
428
- selected_image = gr.Textbox(label="Selected Image", visible=False)
429
- gallery.select(get_selection_from_gallery, inputs=gallery, outputs=[selected_image, sorted_general_strings, rating, character_res, general_res])
430
- # Event to remove a selected image from the gallery
431
- remove_button.click(remove_image_from_gallery, inputs=[gallery, selected_image], outputs=gallery)
432
 
433
  submit.click(
434
  predictor.predict,
@@ -446,20 +460,20 @@ def main():
446
  outputs=[download_file, sorted_general_strings, rating, character_res, general_res],
447
  )
448
 
449
- # gr.Examples(
450
- # [["power.jpg", SWINV2_MODEL_DSV3_REPO, 0.35, False, 0.85, False]],
451
- # inputs=[
452
- # gallery,
453
- # model_repo,
454
- # general_thresh,
455
- # general_mcut_enabled,
456
- # character_thresh,
457
- # character_mcut_enabled,
458
- # characters_merge_enabled,
459
- # additional_tags_prepend,
460
- # additional_tags_append,
461
- # ],
462
- # )
463
 
464
  demo.queue(max_size=10)
465
  demo.launch(inbrowser=True)
 
33
  CONV2_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-convnextv2-tagger-v2"
34
  VIT_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-vit-tagger-v2"
35
 
36
+ # IdolSankaku series of models:
37
+ EVA02_LARGE_MODEL_IS_DSV1_REPO = "deepghs/idolsankaku-eva02-large-tagger-v1"
38
+ SWINV2_MODEL_IS_DSV1_REPO = "deepghs/idolsankaku-swinv2-tagger-v1"
39
+
40
  # Files to download from the repos
41
  MODEL_FILENAME = "model.onnx"
42
  LABEL_FILENAME = "selected_tags.csv"
 
304
  return gallery
305
 
306
  # Combine the new images with the existing gallery images
307
+ if type(images) is str:
308
+ gallery.append(images)
309
+ else:
310
+ gallery.extend(images)
311
  return gallery
312
 
313
  def remove_image_from_gallery(gallery: list, selected_image: str):
 
327
  predictor = Predictor()
328
 
329
  dropdown_list = [
 
330
  EVA02_LARGE_MODEL_DSV3_REPO,
331
+ SWINV2_MODEL_DSV3_REPO,
332
  CONV_MODEL_DSV3_REPO,
333
  VIT_MODEL_DSV3_REPO,
334
  VIT_LARGE_MODEL_DSV3_REPO,
335
+ # ---
336
  MOAT_MODEL_DSV2_REPO,
337
  SWIN_MODEL_DSV2_REPO,
338
  CONV_MODEL_DSV2_REPO,
339
  CONV2_MODEL_DSV2_REPO,
340
  VIT_MODEL_DSV2_REPO,
341
+ # ---
342
+ SWINV2_MODEL_IS_DSV1_REPO,
343
+ EVA02_LARGE_MODEL_IS_DSV1_REPO,
344
  ]
345
 
346
  with gr.Blocks(title=TITLE) as demo:
347
+ gr.Markdown(
348
+ value=f"<h1 style='text-align: center; margin-bottom: 1rem'>{TITLE}</h1>"
349
+ )
350
+ gr.Markdown(value=DESCRIPTION)
351
+ with gr.Row():
352
+ with gr.Column():
353
+ submit = gr.Button(value="Submit", variant="primary", size="lg")
354
  with gr.Column(variant="panel"):
355
+ # Create an Image component for uploading images
356
+ image_input = gr.Image(label="Upload an Image or clicking paste from clipboard button", type="filepath", sources=["upload", "clipboard"], height=150)
357
+ gallery = gr.Gallery(columns=5, rows=5, show_share_button=False, interactive=True, height="500px", label="Gallery that displaying a grid of images")
358
  with gr.Row():
359
+ upload_button = gr.UploadButton("Upload multiple images", file_types=["image"], file_count="multiple", size="sm")
 
 
 
 
360
  remove_button = gr.Button("Remove Selected Image", size="sm")
361
 
362
+ model_repo = gr.Dropdown(
363
+ dropdown_list,
364
+ value=SWINV2_MODEL_DSV3_REPO,
365
+ label="Model",
366
+ )
367
+ with gr.Row():
368
+ general_thresh = gr.Slider(
369
+ 0,
370
+ 1,
371
+ step=args.score_slider_step,
372
+ value=args.score_general_threshold,
373
+ label="General Tags Threshold",
374
+ scale=3,
375
  )
376
+ general_mcut_enabled = gr.Checkbox(
377
+ value=False,
378
+ label="Use MCut threshold",
379
+ scale=1,
380
+ )
381
+ with gr.Row():
382
+ character_thresh = gr.Slider(
383
+ 0,
384
+ 1,
385
+ step=args.score_slider_step,
386
+ value=args.score_character_threshold,
387
+ label="Character Tags Threshold",
388
+ scale=3,
389
+ )
390
+ character_mcut_enabled = gr.Checkbox(
391
+ value=False,
392
+ label="Use MCut threshold",
393
+ scale=1,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  )
395
+ with gr.Row():
396
+ characters_merge_enabled = gr.Checkbox(
397
+ value=True,
398
+ label="Merge characters into the string output",
399
+ scale=1,
400
+ )
401
+ with gr.Row():
402
+ additional_tags_prepend = gr.Text(label="Prepend Additional tags (comma split)")
403
+ additional_tags_append = gr.Text(label="Append Additional tags (comma split)")
404
+ with gr.Row():
405
+ clear = gr.ClearButton(
406
+ components=[
407
+ gallery,
408
+ model_repo,
409
+ general_thresh,
410
+ general_mcut_enabled,
411
+ character_thresh,
412
+ character_mcut_enabled,
413
+ characters_merge_enabled,
414
+ additional_tags_prepend,
415
+ additional_tags_append,
416
+ ],
417
+ variant="secondary",
418
+ size="lg",
419
+ )
420
+ with gr.Column(variant="panel"):
421
+ download_file = gr.File(label="Output (Download)")
422
+ sorted_general_strings = gr.Textbox(label="Output (string)", show_label=True, show_copy_button=True)
423
+ rating = gr.Label(label="Rating")
424
+ character_res = gr.Label(label="Output (characters)")
425
+ general_res = gr.Label(label="Output (tags)")
426
+ clear.add(
427
+ [
428
+ download_file,
429
+ sorted_general_strings,
430
+ rating,
431
+ character_res,
432
+ general_res,
433
+ ]
434
+ )
435
+
436
+ # Define the event listener to add the uploaded image to the gallery
437
+ image_input.change(add_images_to_gallery, inputs=[gallery, image_input], outputs=gallery)
438
 
439
+ # When the upload button is clicked, add the new images to the gallery
440
+ upload_button.upload(add_images_to_gallery, inputs=[gallery, upload_button], outputs=gallery)
441
+ # Event to update the selected image when an image is clicked in the gallery
442
+ selected_image = gr.Textbox(label="Selected Image", visible=False)
443
+ gallery.select(get_selection_from_gallery, inputs=gallery, outputs=[selected_image, sorted_general_strings, rating, character_res, general_res])
444
+ # Event to remove a selected image from the gallery
445
+ remove_button.click(remove_image_from_gallery, inputs=[gallery, selected_image], outputs=gallery)
446
 
447
  submit.click(
448
  predictor.predict,
 
460
  outputs=[download_file, sorted_general_strings, rating, character_res, general_res],
461
  )
462
 
463
+ gr.Examples(
464
+ [["power.jpg", SWINV2_MODEL_DSV3_REPO, 0.35, False, 0.85, False]],
465
+ inputs=[
466
+ image_input,
467
+ model_repo,
468
+ general_thresh,
469
+ general_mcut_enabled,
470
+ character_thresh,
471
+ character_mcut_enabled,
472
+ characters_merge_enabled,
473
+ additional_tags_prepend,
474
+ additional_tags_append,
475
+ ],
476
+ )
477
 
478
  demo.queue(max_size=10)
479
  demo.launch(inbrowser=True)
requirements.txt CHANGED
@@ -2,5 +2,5 @@ pillow>=9.0.0
2
  onnxruntime>=1.12.0
3
  huggingface-hub
4
 
5
- gradio==5.6.0
6
  pandas
 
2
  onnxruntime>=1.12.0
3
  huggingface-hub
4
 
5
+ gradio==5.12.0
6
  pandas