Skip to content

Commit 87c9314

Browse files
feat: Add delete button to kontext manual captioning UI
This commit adds the ability to delete a pair of images and their corresponding caption file from the kontext manual captioning tool. A "Delete" button has been added to the UI for each image pair. When clicked, this button triggers the deletion of the control image, the target image, and the caption file. The UI is then refreshed to reflect the changes.
1 parent f0d3e3f commit 87c9314

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

kohya_gui/kontext_manual_caption_gui.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,37 @@ def save_caption(caption, caption_ext, image_file, images_dir):
8787
return gr.Markdown(visible=False)
8888

8989

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+
90121
def update_quick_tags(quick_tags_text, *image_caption_texts):
91122
quick_tags, quick_tags_set = _get_quick_tags(quick_tags_text)
92123
return [
@@ -374,7 +405,7 @@ def render_pagination_with_logic(page, max_page):
374405

375406
pagination_row1, page_count1 = render_pagination_with_logic(page, max_page)
376407

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 = [], [], [], [], [], [], [], []
378409
for i in range(IMAGES_TO_SHOW):
379410
with gr.Row(visible=False) as row:
380411
image_file = gr.Text(visible=False)
@@ -387,13 +418,42 @@ def render_pagination_with_logic(page, max_page):
387418
tag_checkboxes = gr.CheckboxGroup([], label="Tags", interactive=True)
388419
with gr.Column(min_width=40):
389420
save_button = gr.Button("💾", elem_id="save_button", visible=False)
421+
delete_button = gr.Button("🗑️", elem_id="delete_button")
390422

391423
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)
393425

394426
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)
395427
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])
396428
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+
)
397457

398458
pagination_row2, page_count2 = render_pagination_with_logic(page, max_page)
399459

0 commit comments

Comments
 (0)