KohyaSS/library/basic_caption_gui.py

140 lines
4.4 KiB
Python
Raw Normal View History

2022-12-16 16:04:06 +00:00
import gradio as gr
2022-12-16 18:16:23 +00:00
from easygui import msgbox
2022-12-16 16:04:06 +00:00
import subprocess
from .common_gui import get_folder_path, add_pre_postfix, find_replace
2022-12-16 16:04:06 +00:00
2022-12-17 01:26:26 +00:00
def caption_images(
caption_text_input,
images_dir_input,
overwrite_input,
caption_file_ext,
prefix,
postfix, find, replace
2022-12-17 01:26:26 +00:00
):
2022-12-16 16:04:06 +00:00
# Check for images_dir_input
2022-12-17 01:26:26 +00:00
if images_dir_input == '':
msgbox('Image folder is missing...')
2022-12-16 16:04:06 +00:00
return
if caption_file_ext == '':
msgbox('Please provide an extension for the caption files.')
return
2022-12-17 01:26:26 +00:00
if not caption_text_input == '':
print(
f'Captioning files in {images_dir_input} with {caption_text_input}...'
)
run_cmd = f'python "tools/caption.py"'
run_cmd += f' --caption_text="{caption_text_input}"'
if overwrite_input:
run_cmd += f' --overwrite'
if caption_file_ext != '':
run_cmd += f' --caption_file_ext="{caption_file_ext}"'
run_cmd += f' "{images_dir_input}"'
2022-12-17 01:26:26 +00:00
print(run_cmd)
2022-12-17 01:26:26 +00:00
# Run the command
subprocess.run(run_cmd)
if overwrite_input:
if not prefix=='' or not postfix=='':
# Add prefix and postfix
add_pre_postfix(
folder=images_dir_input,
caption_file_ext=caption_file_ext,
prefix=prefix,
postfix=postfix,
)
if not find=='':
find_replace(
folder=images_dir_input,
caption_file_ext=caption_file_ext,
find=find,
replace=replace,
)
else:
if not prefix == '' or not postfix == '':
msgbox(
'Could not modify caption files with requested change because the "Overwrite existing captions in folder" option is not selected...'
)
2022-12-17 01:26:26 +00:00
print('...captioning done')
2022-12-17 01:26:26 +00:00
2022-12-16 16:04:06 +00:00
###
# Gradio UI
###
2022-12-17 01:26:26 +00:00
def gradio_basic_caption_gui_tab():
with gr.Tab('Basic Captioning'):
2022-12-16 16:04:06 +00:00
gr.Markdown(
'This utility will allow the creation of simple caption files for each images in a folder.'
2022-12-16 16:04:06 +00:00
)
with gr.Row():
images_dir_input = gr.Textbox(
label='Image folder to caption',
placeholder='Directory containing the images to caption',
interactive=True,
)
button_images_dir_input = gr.Button(
'📂', elem_id='open_folder_small'
)
button_images_dir_input.click(
get_folder_path, outputs=images_dir_input
)
caption_file_ext = gr.Textbox(
label='Caption file extension',
placeholder='Extention for caption file. eg: .caption, .txt',
value='.txt',
interactive=True,
)
overwrite_input = gr.Checkbox(
label='Overwrite existing captions in folder',
interactive=True,
value=False,
)
with gr.Row():
prefix = gr.Textbox(
2023-01-01 19:14:58 +00:00
label='Prefix to add to caption',
placeholder='(Optional)',
interactive=True,
)
2022-12-16 16:04:06 +00:00
caption_text_input = gr.Textbox(
2022-12-17 01:26:26 +00:00
label='Caption text',
placeholder='Eg: , by some artist. Leave empty if you just want to add pre or postfix',
2022-12-16 16:04:06 +00:00
interactive=True,
2022-12-17 01:26:26 +00:00
)
postfix = gr.Textbox(
2023-01-01 19:14:58 +00:00
label='Postfix to add to caption',
placeholder='(Optional)',
interactive=True,
)
with gr.Row():
find = gr.Textbox(
label='Find text',
placeholder='Eg: , by some artist. Leave empty if you just want to add pre or postfix',
2022-12-16 16:04:06 +00:00
interactive=True,
)
replace = gr.Textbox(
label='Replacement text',
placeholder='Eg: , by some artist. Leave empty if you just want to replace with nothing',
2022-12-16 16:04:06 +00:00
interactive=True,
)
2022-12-17 01:26:26 +00:00
caption_button = gr.Button('Caption images')
caption_button.click(
caption_images,
inputs=[
caption_text_input,
images_dir_input,
overwrite_input,
caption_file_ext,
prefix,
postfix,
find, replace
2022-12-17 01:26:26 +00:00
],
)