Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2382 some incorrect usage related to the recent shell=false issue #2417

Merged
4 changes: 1 addition & 3 deletions kohya_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ def UI(**kwargs):
reg_data_dir_input=reg_data_dir_input,
output_dir_input=output_dir_input,
logging_dir_input=logging_dir_input,
enable_copy_info_button=True,
headless=headless,
config=config,
use_shell_flag=use_shell_flag,
)
with gr.Tab("LoRA"):
_ = LoRATools(headless=headless, use_shell_flag=use_shell_flag)
_ = LoRATools(headless=headless)
with gr.Tab("About"):
gr.Markdown(f"kohya_ss GUI release {release}")
with gr.Tab("README"):
Expand Down
39 changes: 20 additions & 19 deletions kohya_gui/basic_caption_gui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import gradio as gr
from easygui import msgbox
import subprocess
from .common_gui import (
get_folder_path,
Expand Down Expand Up @@ -28,7 +27,6 @@ def caption_images(
postfix: str,
find_text: str,
replace_text: str,
use_shell: bool = False,
):
"""
Captions images in a given directory with a given caption text.
Expand All @@ -48,47 +46,50 @@ def caption_images(
"""
# Check if images_dir is provided
if not images_dir:
msgbox(
log.info(
"Image folder is missing. Please provide the directory containing the images to caption."
)
return

# Check if caption_ext is provided
if not caption_ext:
msgbox("Please provide an extension for the caption files.")
log.info("Please provide an extension for the caption files.")
return

# Log the captioning process
if caption_text:
log.info(f"Captioning files in {images_dir} with {caption_text}...")

# Build the command to run caption.py
run_cmd = rf'"{PYTHON}" "{scriptdir}/tools/caption.py"'
run_cmd += f' --caption_text="{caption_text}"'
run_cmd = [
rf"{PYTHON}",
rf"{scriptdir}/tools/caption.py",
"--caption_text",
caption_text,
]

# Add optional flags to the command
if overwrite:
run_cmd += f" --overwrite"
run_cmd.append("--overwrite")
if caption_ext:
run_cmd += f' --caption_file_ext="{caption_ext}"'
run_cmd.append("--caption_file_ext")
run_cmd.append(caption_ext)

run_cmd += f' "{images_dir}"'
run_cmd.append(rf"{images_dir}")

# Log the command
log.info(run_cmd)
# Reconstruct the safe command string for display
command_to_run = " ".join(run_cmd)
log.info(f"Executing command: {command_to_run}")

# Set the environment variable for the Python path
env = os.environ.copy()
env["PYTHONPATH"] = (
f"{scriptdir}{os.pathsep}{scriptdir}/sd-scripts{os.pathsep}{env.get('PYTHONPATH', '')}"
rf"{scriptdir}{os.pathsep}{scriptdir}/sd-scripts{os.pathsep}{env.get('PYTHONPATH', '')}"
)
env["TF_ENABLE_ONEDNN_OPTS"] = "0"

log.info(f"Executing command: {run_cmd} with shell={use_shell}")

# Run the command in the sd-scripts folder context
subprocess.run(run_cmd, env=env, shell=use_shell)

subprocess.run(run_cmd, env=env, shell=False)

# Check if overwrite option is enabled
if overwrite:
Expand All @@ -111,7 +112,7 @@ def caption_images(
else:
# Show a message if modification is not possible without overwrite option enabled
if prefix or postfix:
msgbox(
log.info(
'Could not modify caption files with requested change because the "Overwrite existing captions in folder" option is not selected.'
)

Expand All @@ -120,7 +121,7 @@ def caption_images(


# Gradio UI
def gradio_basic_caption_gui_tab(headless=False, default_images_dir=None, use_shell: bool = False):
def gradio_basic_caption_gui_tab(headless=False, default_images_dir=None):
"""
Creates a Gradio tab for basic image captioning.

Expand Down Expand Up @@ -200,6 +201,7 @@ def list_images_dirs(path):
choices=[".cap", ".caption", ".txt"],
value=".txt",
interactive=True,
allow_custom_value=True,
)
# Checkbox to overwrite existing captions
overwrite = gr.Checkbox(
Expand Down Expand Up @@ -258,7 +260,6 @@ def list_images_dirs(path):
postfix,
find_text,
replace_text,
gr.Checkbox(value=use_shell, visible=False),
],
show_progress=False,
)
Expand Down
41 changes: 20 additions & 21 deletions kohya_gui/blip_caption_gui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import gradio as gr
from easygui import msgbox
import subprocess
import os
import sys
Expand All @@ -23,7 +22,6 @@ def caption_images(
beam_search: bool,
prefix: str = "",
postfix: str = "",
use_shell: bool = False,
) -> None:
"""
Automatically generates captions for images in the specified directory using the BLIP model.
Expand All @@ -47,59 +45,60 @@ def caption_images(
"""
# Check if the image folder is provided
if not train_data_dir:
msgbox("Image folder is missing...")
log.info("Image folder is missing...")
return

# Check if the caption file extension is provided
if not caption_file_ext:
msgbox("Please provide an extension for the caption files.")
log.info("Please provide an extension for the caption files.")
return

log.info(f"Captioning files in {train_data_dir}...")

# Construct the command to run make_captions.py
run_cmd = [fr'"{PYTHON}"', fr'"{scriptdir}/sd-scripts/finetune/make_captions.py"']
run_cmd = [rf"{PYTHON}", rf"{scriptdir}/sd-scripts/finetune/make_captions.py"]

# Add required arguments
run_cmd.append('--batch_size')
run_cmd.append("--batch_size")
run_cmd.append(str(batch_size))
run_cmd.append('--num_beams')
run_cmd.append("--num_beams")
run_cmd.append(str(num_beams))
run_cmd.append('--top_p')
run_cmd.append("--top_p")
run_cmd.append(str(top_p))
run_cmd.append('--max_length')
run_cmd.append("--max_length")
run_cmd.append(str(max_length))
run_cmd.append('--min_length')
run_cmd.append("--min_length")
run_cmd.append(str(min_length))

# Add optional flags to the command
if beam_search:
run_cmd.append("--beam_search")
if caption_file_ext:
run_cmd.append('--caption_extension')
run_cmd.append("--caption_extension")
run_cmd.append(caption_file_ext)

# Add the directory containing the training data
run_cmd.append(fr'"{train_data_dir}"')
run_cmd.append(rf"{train_data_dir}")

# Add URL for caption model weights
run_cmd.append('--caption_weights')
run_cmd.append("https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_large_caption.pth")
run_cmd.append("--caption_weights")
run_cmd.append(
rf"https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_large_caption.pth"
)

# Set up the environment
env = os.environ.copy()
env["PYTHONPATH"] = (
f"{scriptdir}{os.pathsep}{scriptdir}/sd-scripts{os.pathsep}{env.get('PYTHONPATH', '')}"
rf"{scriptdir}{os.pathsep}{scriptdir}/sd-scripts{os.pathsep}{env.get('PYTHONPATH', '')}"
)
env["TF_ENABLE_ONEDNN_OPTS"] = "0"

# Reconstruct the safe command string for display
command_to_run = " ".join(run_cmd)
log.info(f"Executing command: {command_to_run} with shell={use_shell}")

# Run the command in the sd-scripts folder context
subprocess.run(command_to_run, env=env, shell=use_shell, cwd=f"{scriptdir}/sd-scripts")
log.info(f"Executing command: {command_to_run}")

# Run the command in the sd-scripts folder context
subprocess.run(run_cmd, env=env, shell=False, cwd=rf"{scriptdir}/sd-scripts")

# Add prefix and postfix
add_pre_postfix(
Expand All @@ -117,7 +116,7 @@ def caption_images(
###


def gradio_blip_caption_gui_tab(headless=False, default_train_dir=None, use_shell: bool = False):
def gradio_blip_caption_gui_tab(headless=False, default_train_dir=None):
from .common_gui import create_refresh_button

default_train_dir = (
Expand Down Expand Up @@ -167,6 +166,7 @@ def list_train_dirs(path):
choices=[".cap", ".caption", ".txt"],
value=".txt",
interactive=True,
allow_custom_value=True,
)

prefix = gr.Textbox(
Expand Down Expand Up @@ -207,7 +207,6 @@ def list_train_dirs(path):
beam_search,
prefix,
postfix,
gr.Checkbox(value=use_shell, visible=False),
],
show_progress=False,
)
Expand Down
6 changes: 3 additions & 3 deletions kohya_gui/class_command_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, headless: bool = False):
"Stop training", visible=self.process is not None or headless, variant="stop"
)

def execute_command(self, run_cmd: str, use_shell: bool = False, **kwargs):
def execute_command(self, run_cmd: str, **kwargs):
"""
Execute a command if no other command is currently running.

Expand All @@ -44,10 +44,10 @@ def execute_command(self, run_cmd: str, use_shell: bool = False, **kwargs):

# Reconstruct the safe command string for display
command_to_run = " ".join(run_cmd)
log.info(f"Executing command: {command_to_run} with shell={use_shell}")
log.info(f"Executing command: {command_to_run}")

# Execute the command securely
self.process = subprocess.Popen(command_to_run, **kwargs, shell=use_shell)
self.process = subprocess.Popen(run_cmd, **kwargs)
log.info("Command executed.")

def kill_command(self):
Expand Down
15 changes: 7 additions & 8 deletions kohya_gui/class_lora_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class LoRATools:
def __init__(
self,
headless: bool = False,
use_shell_flag: bool = False,
):
gr.Markdown("This section provide various LoRA tools...")
gradio_extract_dylora_tab(headless=headless, use_shell=use_shell_flag)
gradio_convert_lcm_tab(headless=headless, use_shell=use_shell_flag)
gradio_extract_lora_tab(headless=headless, use_shell=use_shell_flag)
gradio_extract_lycoris_locon_tab(headless=headless, use_shell=use_shell_flag)
gradio_merge_lora_tab = GradioMergeLoRaTab(use_shell=use_shell_flag)
gradio_merge_lycoris_tab(headless=headless, use_shell=use_shell_flag)
gradio_svd_merge_lora_tab(headless=headless, use_shell=use_shell_flag)
gradio_extract_dylora_tab(headless=headless)
gradio_convert_lcm_tab(headless=headless)
gradio_extract_lora_tab(headless=headless)
gradio_extract_lycoris_locon_tab(headless=headless)
gradio_merge_lora_tab = GradioMergeLoRaTab()
gradio_merge_lycoris_tab(headless=headless)
gradio_svd_merge_lora_tab(headless=headless)
gradio_resize_lora_tab(headless=headless)
gradio_verify_lora_tab(headless=headless)
5 changes: 0 additions & 5 deletions kohya_gui/common_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,11 +752,6 @@ def add_pre_postfix(
postfix (str, optional): Postfix to add to the content of the caption files.
caption_file_ext (str, optional): Extension of the caption files.
"""
# Enforce that the provided extension is one of .caption, .cap, .txt
if caption_file_ext not in (".caption", ".cap", ".txt"):
log.error("Invalid caption file extension. Must be on of .caption, .cap, .txt")
return

# If neither prefix nor postfix is provided, return early
if prefix == "" and postfix == "":
return
Expand Down
32 changes: 17 additions & 15 deletions kohya_gui/convert_lcm_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ def convert_lcm(
model_path,
lora_scale,
model_type,
use_shell: bool = False,
):
run_cmd = rf'"{PYTHON}" "{scriptdir}/tools/lcm_convert.py"'

# Check if source model exist
if not os.path.isfile(model_path):
log.error("The provided DyLoRA model is not a file")
Expand All @@ -48,35 +45,41 @@ def convert_lcm(
save_to = f"{path}_lcm{ext}"

# Construct the command to run the script
run_cmd += f" --lora-scale {lora_scale}"
run_cmd += f' --model "{model_path}"'
run_cmd += f' --name "{name}"'
run_cmd = [
rf"{PYTHON}",
rf"{scriptdir}/tools/lcm_convert.py",
"--lora-scale",
str(lora_scale),
"--model",
rf"{model_path}",
"--name",
str(name),
]

if model_type == "SDXL":
run_cmd += f" --sdxl"
run_cmd.append("--sdxl")
if model_type == "SSD-1B":
run_cmd += f" --ssd-1b"
run_cmd.append("--ssd-1b")

# Set up the environment
env = os.environ.copy()
env["PYTHONPATH"] = (
f"{scriptdir}{os.pathsep}{scriptdir}/sd-scripts{os.pathsep}{env.get('PYTHONPATH', '')}"
fr"{scriptdir}{os.pathsep}{scriptdir}/sd-scripts{os.pathsep}{env.get('PYTHONPATH', '')}"
)
env["TF_ENABLE_ONEDNN_OPTS"] = "0"

# Reconstruct the safe command string for display
log.info(f"Executing command: {run_cmd} with shell={use_shell}")
command_to_run = " ".join(run_cmd)
log.info(f"Executing command: {command_to_run}")

# Run the command in the sd-scripts folder context
subprocess.run(
run_cmd, env=env, shell=use_shell
)
subprocess.run(run_cmd, env=env, shell=False)

# Return a success message
log.info("Done extracting...")


def gradio_convert_lcm_tab(headless=False, use_shell: bool = False):
def gradio_convert_lcm_tab(headless=False):
current_model_dir = os.path.join(scriptdir, "outputs")
current_save_dir = os.path.join(scriptdir, "outputs")

Expand Down Expand Up @@ -186,7 +189,6 @@ def list_save_to(path):
model_path,
lora_scale,
model_type,
gr.Checkbox(value=use_shell, visible=False),
],
show_progress=False,
)
Loading