@@ -87,6 +87,37 @@ def save_caption(caption, caption_ext, image_file, images_dir):
87
87
return gr .Markdown (visible = False )
88
88
89
89
90
+ def delete_images_and_caption (
91
+ image_file , control_images_dir , target_images_dir , caption_ext
92
+ ):
93
+ if not image_file :
94
+ return gr .Markdown (visible = False )
95
+
96
+ # Delete control image
97
+ control_image_path = os .path .join (control_images_dir , image_file )
98
+ if os .path .exists (control_image_path ):
99
+ os .remove (control_image_path )
100
+ log .info (f"Deleted control image: { control_image_path } " )
101
+
102
+ # Delete target image
103
+ target_image_path = os .path .join (target_images_dir , image_file )
104
+ if os .path .exists (target_image_path ):
105
+ os .remove (target_image_path )
106
+ log .info (f"Deleted target image: { target_image_path } " )
107
+
108
+ # Delete caption file
109
+ caption_path = _get_caption_path (
110
+ image_file , target_images_dir , caption_ext
111
+ )
112
+ if caption_path and os .path .exists (caption_path ):
113
+ os .remove (caption_path )
114
+ log .info (f"Deleted caption file: { caption_path } " )
115
+
116
+ return gr .Markdown (
117
+ f"🗑️ Deleted files for `{ image_file } `" , visible = True
118
+ )
119
+
120
+
90
121
def update_quick_tags (quick_tags_text , * image_caption_texts ):
91
122
quick_tags , quick_tags_set = _get_quick_tags (quick_tags_text )
92
123
return [
@@ -374,7 +405,7 @@ def render_pagination_with_logic(page, max_page):
374
405
375
406
pagination_row1 , page_count1 = render_pagination_with_logic (page , max_page )
376
407
377
- image_rows , image_files , target_image_images , control_image_images , image_caption_texts , image_tag_checks , save_buttons = [], [], [], [], [], [], []
408
+ image_rows , image_files , target_image_images , control_image_images , image_caption_texts , image_tag_checks , save_buttons , delete_buttons = [], [], [], [], [], [], [], []
378
409
for i in range (IMAGES_TO_SHOW ):
379
410
with gr .Row (visible = False ) as row :
380
411
image_file = gr .Text (visible = False )
@@ -387,13 +418,42 @@ def render_pagination_with_logic(page, max_page):
387
418
tag_checkboxes = gr .CheckboxGroup ([], label = "Tags" , interactive = True )
388
419
with gr .Column (min_width = 40 ):
389
420
save_button = gr .Button ("💾" , elem_id = "save_button" , visible = False )
421
+ delete_button = gr .Button ("🗑️" , elem_id = "delete_button" )
390
422
391
423
image_rows .append (row ); image_files .append (image_file ); control_image_images .append (control_image_image ); target_image_images .append (target_image_image )
392
- image_caption_texts .append (image_caption_text ); image_tag_checks .append (tag_checkboxes ); save_buttons .append (save_button )
424
+ image_caption_texts .append (image_caption_text ); image_tag_checks .append (tag_checkboxes ); save_buttons .append (save_button ); delete_buttons . append ( delete_button )
393
425
394
426
image_caption_text .input (update_image_caption , inputs = [quick_tags_text , image_caption_text , image_file , loaded_images_dir , caption_ext , auto_save ], outputs = tag_checkboxes )
395
427
tag_checkboxes .input (update_image_tags , inputs = [quick_tags_text , tag_checkboxes , image_file , loaded_images_dir , caption_ext , auto_save ], outputs = [image_caption_text ])
396
428
save_button .click (save_caption , inputs = [image_caption_text , caption_ext , image_file , loaded_images_dir ], outputs = info_box )
429
+ delete_button .click (
430
+ delete_images_and_caption ,
431
+ inputs = [
432
+ image_file ,
433
+ loaded_control_images_dir ,
434
+ loaded_images_dir ,
435
+ caption_ext ,
436
+ ],
437
+ outputs = info_box ,
438
+ ).then (
439
+ load_images ,
440
+ inputs = [
441
+ loaded_images_dir ,
442
+ loaded_control_images_dir ,
443
+ caption_ext ,
444
+ quick_tags_text ,
445
+ ignore_load_tags_word_count ,
446
+ ],
447
+ outputs = [
448
+ image_files_state ,
449
+ loaded_images_dir ,
450
+ loaded_control_images_dir ,
451
+ page ,
452
+ max_page ,
453
+ info_box ,
454
+ quick_tags_text ,
455
+ ],
456
+ )
397
457
398
458
pagination_row2 , page_count2 = render_pagination_with_logic (page , max_page )
399
459
0 commit comments