Fix issue with spaces in python path

pull/2074/head
Bernard Maltais 2024-03-12 07:12:08 -04:00
parent 8d30431d1e
commit 9a3a16143a
16 changed files with 58 additions and 26 deletions

View File

@ -38,7 +38,7 @@ def caption_images(
log.info(f'Captioning files in {images_dir} with {caption_text}...')
# Build the command to run caption.py
run_cmd = fr'{PYTHON} "{scriptdir}/tools/caption.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/tools/caption.py"'
run_cmd += f' --caption_text="{caption_text}"'
# Add optional flags to the command

View File

@ -37,7 +37,7 @@ def caption_images(
log.info(f"Captioning files in {train_data_dir}...")
# Construct the command to run
run_cmd = rf'{PYTHON} "{scriptdir}/sd-scripts/finetune/make_captions.py"'
run_cmd = rf'"{PYTHON}" "{scriptdir}/sd-scripts/finetune/make_captions.py"'
run_cmd += f' --batch_size="{int(batch_size)}"'
run_cmd += f' --num_beams="{int(num_beams)}"'
run_cmd += f' --top_p="{top_p}"'

View File

@ -28,7 +28,7 @@ def convert_lcm(
lora_scale,
model_type
):
run_cmd = fr'{PYTHON} "{scriptdir}/tools/lcm_convert.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/tools/lcm_convert.py"'
# Check if source model exist
if not os.path.isfile(model_path):

View File

@ -49,7 +49,7 @@ def convert_model(
msgbox('The provided target folder does not exist')
return
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/tools/convert_diffusers20_original_sd.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/tools/convert_diffusers20_original_sd.py"'
v1_models = [
'runwayml/stable-diffusion-v1-5',

View File

@ -51,7 +51,7 @@ def extract_dylora(
save_to = f"{path}_tmp{ext}"
run_cmd = (
fr'{PYTHON} "{scriptdir}/sd-scripts/networks/extract_lora_from_dylora.py"'
fr'"{PYTHON}" "{scriptdir}/sd-scripts/networks/extract_lora_from_dylora.py"'
)
run_cmd += fr' --save_to "{save_to}"'
run_cmd += fr' --model "{model}"'

View File

@ -74,7 +74,7 @@ def extract_lora(
return
run_cmd = (
fr'{PYTHON} "{scriptdir}/sd-scripts/networks/extract_lora_from_models.py"'
fr'"{PYTHON}" "{scriptdir}/sd-scripts/networks/extract_lora_from_models.py"'
)
run_cmd += f' --load_precision {load_precision}'
run_cmd += f' --save_precision {save_precision}'

View File

@ -74,7 +74,7 @@ def extract_lycoris_locon(
path, ext = os.path.splitext(output_name)
output_name = f"{path}_tmp{ext}"
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/tools/lycoris_locon_extract.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/tools/lycoris_locon_extract.py"'
if is_sdxl:
run_cmd += f" --is_sdxl"
if is_v2:

View File

@ -453,7 +453,7 @@ def train_model(
# create caption json file
if generate_caption_database:
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/finetune/merge_captions_to_metadata.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/finetune/merge_captions_to_metadata.py"'
if caption_extension == "":
run_cmd += f' --caption_extension=".caption"'
else:
@ -474,7 +474,7 @@ def train_model(
# create images buckets
if generate_image_buckets:
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/finetune/prepare_buckets_latents.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/finetune/prepare_buckets_latents.py"'
run_cmd += fr' "{image_folder}"'
run_cmd += fr' "{train_dir}/{caption_metadata_filename}"'
run_cmd += fr' "{train_dir}/{latent_metadata_filename}"'

View File

@ -33,7 +33,7 @@ def caption_images(
return
log.info(f'GIT captioning files in {train_data_dir}...')
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/finetune/make_captions_by_git.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/finetune/make_captions_by_git.py"'
if not model_id == '':
run_cmd += f' --model_id="{model_id}"'
run_cmd += f' --batch_size="{int(batch_size)}"'

View File

@ -32,7 +32,7 @@ def group_images(
log.info(f'Grouping images in {input_folder}...')
run_cmd = fr'{PYTHON} "{scriptdir}/tools/group_images.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/tools/group_images.py"'
run_cmd += f' "{input_folder}"'
run_cmd += f' "{output_folder}"'
run_cmd += f' {(group_size)}'

View File

@ -385,10 +385,10 @@ class GradioMergeLoRaTab:
return
if not sdxl_model:
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/networks/merge_lora.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/networks/merge_lora.py"'
else:
run_cmd = (
fr'{PYTHON} "{scriptdir}/sd-scripts/networks/sdxl_merge_lora.py"'
fr'"{PYTHON}" "{scriptdir}/sd-scripts/networks/sdxl_merge_lora.py"'
)
if sd_model:
run_cmd += fr' --sd_model "{sd_model}"'

View File

@ -36,7 +36,7 @@ def merge_lycoris(
):
log.info('Merge model...')
run_cmd = fr'{PYTHON} "{scriptdir}/tools/merge_lycoris.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/tools/merge_lycoris.py"'
run_cmd += fr' "{base_model}"'
run_cmd += fr' "{lycoris_model}"'
run_cmd += fr' "{output_name}"'

View File

@ -59,7 +59,7 @@ def resize_lora(
if device == '':
device = 'cuda'
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/networks/resize_lora.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/networks/resize_lora.py"'
run_cmd += f' --save_precision {save_precision}'
run_cmd += fr' --save_to "{save_to}"'
run_cmd += fr' --model "{model}"'

View File

@ -53,7 +53,7 @@ def svd_merge_lora(
ratio_c /= total_ratio
ratio_d /= total_ratio
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/networks/svd_merge_lora.py"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/networks/svd_merge_lora.py"'
run_cmd += f' --save_precision {save_precision}'
run_cmd += f' --precision {precision}'
run_cmd += fr' --save_to "{save_to}"'

View File

@ -38,7 +38,7 @@ def verify_lora(
msgbox('The provided model A is not a file')
return
run_cmd = fr'{PYTHON} "{scriptdir}/sd-scripts/networks/check_lora_weights.py" "{lora_model}"'
run_cmd = fr'"{PYTHON}" "{scriptdir}/sd-scripts/networks/check_lora_weights.py" "{lora_model}"'
log.info(run_cmd)

View File

@ -423,36 +423,58 @@ def pip(arg: str, ignore: bool = False, quiet: bool = False, show_stdout: bool =
return txt
import re
import pkg_resources
import logging
log = logging.getLogger(__name__)
def installed(package, friendly: str = None):
#
# This function was adapted from code written by vladimandic: https://github.com/vladmandic/automatic/commits/master
#
"""
Checks if the specified package(s) are installed with the correct version.
This function can handle package specifications with or without version constraints,
and can also filter out command-line options and URLs when a 'friendly' string is provided.
# Remove brackets and their contents from the line using regular expressions
# e.g., diffusers[torch]==0.10.2 becomes diffusers==0.10.2
Parameters:
- package: A string that specifies one or more packages with optional version constraints.
- friendly: An optional string used to provide a cleaner version of the package string
that excludes command-line options and URLs.
Returns:
- True if all specified packages are installed with the correct versions, False otherwise.
Note:
This function was adapted from code written by vladimandic.
"""
# Remove any optional features specified in brackets (e.g., "package[option]==version" becomes "package==version")
package = re.sub(r'\[.*?\]', '', package)
try:
if friendly:
# If a 'friendly' version of the package string is provided, split it into components
pkgs = friendly.split()
# Exclude command-line options and URLs like "--index-url https://download.pytorch.org/whl/cu118"
# Filter out command-line options and URLs from the package specification
pkgs = [
p
for p in package.split()
if not p.startswith('--') and "://" not in p # Exclude command-line options and URLs
if not p.startswith('--') and "://" not in p
]
else:
# Split the package string into components, excluding '-' and '=' prefixed items
pkgs = [
p
for p in package.split()
if not p.startswith('-') and not p.startswith('=')
]
# For each package component, extract the package name, excluding any URLs
pkgs = [
p.split('/')[-1] for p in pkgs
] # get only package name if installing from URL
]
for pkg in pkgs:
# Parse the package name and version based on the version specifier used
if '>=' in pkg:
pkg_name, pkg_version = [x.strip() for x in pkg.split('>=')]
elif '==' in pkg:
@ -460,35 +482,45 @@ def installed(package, friendly: str = None):
else:
pkg_name, pkg_version = pkg.strip(), None
# Attempt to find the installed package by its name
spec = pkg_resources.working_set.by_key.get(pkg_name, None)
if spec is None:
# Try again with lowercase name
spec = pkg_resources.working_set.by_key.get(pkg_name.lower(), None)
if spec is None:
# Try replacing underscores with dashes
spec = pkg_resources.working_set.by_key.get(pkg_name.replace('_', '-'), None)
if spec is not None:
# Package is found, check version
version = pkg_resources.get_distribution(pkg_name).version
log.debug(f'Package version found: {pkg_name} {version}')
if pkg_version is not None:
# Verify if the installed version meets the specified constraints
if '>=' in pkg:
ok = version >= pkg_version
else:
ok = version == pkg_version
if not ok:
# Version mismatch, log warning and return False
log.warning(f'Package wrong version: {pkg_name} {version} required {pkg_version}')
return False
else:
# Package not found, log debug message and return False
log.debug(f'Package version not found: {pkg_name}')
return False
# All specified packages are installed with the correct versions
return True
except ModuleNotFoundError:
# One or more packages are not installed, log debug message and return False
log.debug(f'Package not installed: {pkgs}')
return False
# install package using pip if not already installed
def install(
#