2022-12-19 14:22:52 +00:00
|
|
|
from tkinter import filedialog, Tk
|
2022-12-19 16:43:29 +00:00
|
|
|
import os
|
2023-01-02 03:43:44 +00:00
|
|
|
import gradio as gr
|
|
|
|
from easygui import msgbox
|
2023-01-09 16:48:57 +00:00
|
|
|
import shutil
|
2022-12-19 14:22:52 +00:00
|
|
|
|
2023-01-06 23:25:55 +00:00
|
|
|
def get_dir_and_file(file_path):
|
|
|
|
dir_path, file_name = os.path.split(file_path)
|
|
|
|
return (dir_path, file_name)
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2023-01-09 00:12:01 +00:00
|
|
|
def has_ext_files(directory, extension):
|
|
|
|
# Iterate through all the files in the directory
|
|
|
|
for file in os.listdir(directory):
|
|
|
|
# If the file name ends with extension, return True
|
|
|
|
if file.endswith(extension):
|
|
|
|
return True
|
|
|
|
# If no extension files were found, return False
|
|
|
|
return False
|
|
|
|
|
2023-01-06 23:25:55 +00:00
|
|
|
def get_file_path(file_path='', defaultextension='.json', extension_name='Config files'):
|
2022-12-19 14:22:52 +00:00
|
|
|
current_file_path = file_path
|
|
|
|
# print(f'current file path: {current_file_path}')
|
2023-01-06 23:25:55 +00:00
|
|
|
|
|
|
|
initial_dir, initial_file = get_dir_and_file(file_path)
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
2022-12-20 02:50:05 +00:00
|
|
|
file_path = filedialog.askopenfilename(
|
2023-01-06 23:25:55 +00:00
|
|
|
filetypes=((f'{extension_name}', f'{defaultextension}'), ('All files', '*')),
|
|
|
|
defaultextension=defaultextension, initialfile=initial_file, initialdir=initial_dir
|
2022-12-20 02:50:05 +00:00
|
|
|
)
|
2022-12-19 14:22:52 +00:00
|
|
|
root.destroy()
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
if file_path == '':
|
|
|
|
file_path = current_file_path
|
2022-12-16 18:16:23 +00:00
|
|
|
|
|
|
|
return file_path
|
|
|
|
|
2022-12-29 19:00:02 +00:00
|
|
|
def get_any_file_path(file_path=''):
|
|
|
|
current_file_path = file_path
|
|
|
|
# print(f'current file path: {current_file_path}')
|
2023-01-06 23:25:55 +00:00
|
|
|
|
|
|
|
initial_dir, initial_file = get_dir_and_file(file_path)
|
2022-12-29 19:00:02 +00:00
|
|
|
|
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
2023-01-06 23:25:55 +00:00
|
|
|
file_path = filedialog.askopenfilename(initialdir=initial_dir,
|
|
|
|
initialfile=initial_file,)
|
2022-12-29 19:00:02 +00:00
|
|
|
root.destroy()
|
|
|
|
|
|
|
|
if file_path == '':
|
|
|
|
file_path = current_file_path
|
|
|
|
|
|
|
|
return file_path
|
|
|
|
|
2022-12-17 01:26:26 +00:00
|
|
|
|
2022-12-17 14:51:30 +00:00
|
|
|
def remove_doublequote(file_path):
|
|
|
|
if file_path != None:
|
|
|
|
file_path = file_path.replace('"', '')
|
2022-12-16 18:16:23 +00:00
|
|
|
|
2022-12-17 01:26:26 +00:00
|
|
|
return file_path
|
2022-12-19 14:22:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_folder_path(folder_path=''):
|
|
|
|
current_folder_path = folder_path
|
2023-01-06 23:25:55 +00:00
|
|
|
|
|
|
|
initial_dir, initial_file = get_dir_and_file(folder_path)
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
2023-01-06 23:25:55 +00:00
|
|
|
folder_path = filedialog.askdirectory(initialdir=initial_dir)
|
2022-12-19 14:22:52 +00:00
|
|
|
root.destroy()
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
if folder_path == '':
|
|
|
|
folder_path = current_folder_path
|
|
|
|
|
|
|
|
return folder_path
|
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2023-01-06 23:25:55 +00:00
|
|
|
def get_saveasfile_path(file_path='', defaultextension='.json', extension_name='Config files'):
|
2022-12-19 14:22:52 +00:00
|
|
|
current_file_path = file_path
|
|
|
|
# print(f'current file path: {current_file_path}')
|
2023-01-06 23:25:55 +00:00
|
|
|
|
|
|
|
initial_dir, initial_file = get_dir_and_file(file_path)
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
2022-12-20 02:50:05 +00:00
|
|
|
save_file_path = filedialog.asksaveasfile(
|
2023-01-06 23:25:55 +00:00
|
|
|
filetypes=((f'{extension_name}', f'{defaultextension}'), ('All files', '*')),
|
2022-12-20 02:50:05 +00:00
|
|
|
defaultextension=defaultextension,
|
2023-01-06 23:25:55 +00:00
|
|
|
initialdir=initial_dir,
|
|
|
|
initialfile=initial_file,
|
2022-12-20 02:50:05 +00:00
|
|
|
)
|
2022-12-19 14:22:52 +00:00
|
|
|
root.destroy()
|
2022-12-20 02:50:05 +00:00
|
|
|
|
|
|
|
# print(save_file_path)
|
|
|
|
|
|
|
|
if save_file_path == None:
|
2022-12-19 14:22:52 +00:00
|
|
|
file_path = current_file_path
|
2022-12-19 14:47:35 +00:00
|
|
|
else:
|
|
|
|
print(save_file_path.name)
|
|
|
|
file_path = save_file_path.name
|
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
# print(file_path)
|
2022-12-19 14:22:52 +00:00
|
|
|
|
2022-12-19 16:43:29 +00:00
|
|
|
return file_path
|
|
|
|
|
2023-01-06 23:25:55 +00:00
|
|
|
def get_saveasfilename_path(file_path='', extensions='*', extension_name='Config files'):
|
|
|
|
current_file_path = file_path
|
|
|
|
# print(f'current file path: {current_file_path}')
|
|
|
|
|
|
|
|
initial_dir, initial_file = get_dir_and_file(file_path)
|
|
|
|
|
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
|
|
|
save_file_path = filedialog.asksaveasfilename(filetypes=((f'{extension_name}', f'{extensions}'), ('All files', '*')),
|
|
|
|
defaultextension=extensions,
|
|
|
|
initialdir=initial_dir,
|
|
|
|
initialfile=initial_file,
|
|
|
|
)
|
|
|
|
root.destroy()
|
|
|
|
|
|
|
|
if save_file_path == '':
|
|
|
|
file_path = current_file_path
|
|
|
|
else:
|
|
|
|
# print(save_file_path)
|
|
|
|
file_path = save_file_path
|
|
|
|
|
|
|
|
return file_path
|
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
|
|
|
|
def add_pre_postfix(
|
|
|
|
folder='', prefix='', postfix='', caption_file_ext='.caption'
|
|
|
|
):
|
2023-01-09 00:12:01 +00:00
|
|
|
if not has_ext_files(folder, caption_file_ext):
|
|
|
|
msgbox(f'No files with extension {caption_file_ext} were found in {folder}...')
|
|
|
|
return
|
|
|
|
|
2022-12-22 16:51:34 +00:00
|
|
|
if prefix == '' and postfix == '':
|
|
|
|
return
|
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
files = [f for f in os.listdir(folder) if f.endswith(caption_file_ext)]
|
2022-12-19 16:43:29 +00:00
|
|
|
if not prefix == '':
|
|
|
|
prefix = f'{prefix} '
|
|
|
|
if not postfix == '':
|
|
|
|
postfix = f' {postfix}'
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 16:43:29 +00:00
|
|
|
for file in files:
|
|
|
|
with open(os.path.join(folder, file), 'r+') as f:
|
|
|
|
content = f.read()
|
|
|
|
content = content.rstrip()
|
2022-12-20 02:50:05 +00:00
|
|
|
f.seek(0, 0)
|
2022-12-19 16:43:29 +00:00
|
|
|
f.write(f'{prefix}{content}{postfix}')
|
2022-12-20 02:50:05 +00:00
|
|
|
f.close()
|
2023-01-09 00:12:01 +00:00
|
|
|
|
|
|
|
def find_replace(
|
|
|
|
folder='', caption_file_ext='.caption', find='', replace=''
|
|
|
|
):
|
|
|
|
print('Running caption find/replace')
|
|
|
|
if not has_ext_files(folder, caption_file_ext):
|
|
|
|
msgbox(f'No files with extension {caption_file_ext} were found in {folder}...')
|
|
|
|
return
|
|
|
|
|
|
|
|
if find == '':
|
|
|
|
return
|
|
|
|
|
|
|
|
files = [f for f in os.listdir(folder) if f.endswith(caption_file_ext)]
|
|
|
|
for file in files:
|
|
|
|
with open(os.path.join(folder, file), 'r') as f:
|
|
|
|
content = f.read()
|
|
|
|
f.close
|
|
|
|
content = content.replace(find, replace)
|
|
|
|
with open(os.path.join(folder, file), 'w') as f:
|
|
|
|
f.write(content)
|
|
|
|
f.close()
|
2023-01-02 03:43:44 +00:00
|
|
|
|
|
|
|
def color_aug_changed(color_aug):
|
|
|
|
if color_aug:
|
|
|
|
msgbox('Disabling "Cache latent" because "Color augmentation" has been selected...')
|
|
|
|
return gr.Checkbox.update(value=False, interactive=False)
|
|
|
|
else:
|
2023-01-09 16:48:57 +00:00
|
|
|
return gr.Checkbox.update(value=True, interactive=True)
|
|
|
|
|
|
|
|
def save_inference_file(output_dir, v2, v_parameterization, output_name):
|
|
|
|
# List all files in the directory
|
|
|
|
files = os.listdir(output_dir)
|
|
|
|
|
|
|
|
# Iterate over the list of files
|
|
|
|
for file in files:
|
|
|
|
# Check if the file starts with the value of output_name
|
|
|
|
if file.startswith(output_name):
|
|
|
|
# Check if it is a file or a directory
|
|
|
|
if os.path.isfile(os.path.join(output_dir, file)):
|
|
|
|
# Split the file name and extension
|
|
|
|
file_name, ext = os.path.splitext(file)
|
|
|
|
|
|
|
|
# Copy the v2-inference-v.yaml file to the current file, with a .yaml extension
|
|
|
|
if v2 and v_parameterization:
|
|
|
|
print(f'Saving v2-inference-v.yaml as {output_dir}/{file_name}.yaml')
|
|
|
|
shutil.copy(
|
|
|
|
f'./v2_inference/v2-inference-v.yaml',
|
|
|
|
f'{output_dir}/{file_name}.yaml',
|
|
|
|
)
|
|
|
|
elif v2:
|
|
|
|
print(f'Saving v2-inference.yaml as {output_dir}/{file_name}.yaml')
|
|
|
|
shutil.copy(
|
|
|
|
f'./v2_inference/v2-inference.yaml',
|
|
|
|
f'{output_dir}/{file_name}.yaml',
|
|
|
|
)
|
|
|
|
|
|
|
|
def set_pretrained_model_name_or_path_input(value, v2, v_parameterization):
|
|
|
|
# define a list of substrings to search for
|
|
|
|
substrings_v2 = [
|
|
|
|
'stabilityai/stable-diffusion-2-1-base',
|
|
|
|
'stabilityai/stable-diffusion-2-base',
|
|
|
|
]
|
|
|
|
|
|
|
|
# check if $v2 and $v_parameterization are empty and if $pretrained_model_name_or_path contains any of the substrings in the v2 list
|
|
|
|
if str(value) in substrings_v2:
|
|
|
|
print('SD v2 model detected. Setting --v2 parameter')
|
|
|
|
v2 = True
|
|
|
|
v_parameterization = False
|
|
|
|
|
|
|
|
return value, v2, v_parameterization
|
|
|
|
|
|
|
|
# define a list of substrings to search for v-objective
|
|
|
|
substrings_v_parameterization = [
|
|
|
|
'stabilityai/stable-diffusion-2-1',
|
|
|
|
'stabilityai/stable-diffusion-2',
|
|
|
|
]
|
|
|
|
|
|
|
|
# check if $v2 and $v_parameterization are empty and if $pretrained_model_name_or_path contains any of the substrings in the v_parameterization list
|
|
|
|
if str(value) in substrings_v_parameterization:
|
|
|
|
print(
|
|
|
|
'SD v2 v_parameterization detected. Setting --v2 parameter and --v_parameterization'
|
|
|
|
)
|
|
|
|
v2 = True
|
|
|
|
v_parameterization = True
|
|
|
|
|
|
|
|
return value, v2, v_parameterization
|
|
|
|
|
|
|
|
# define a list of substrings to v1.x
|
|
|
|
substrings_v1_model = [
|
|
|
|
'CompVis/stable-diffusion-v1-4',
|
|
|
|
'runwayml/stable-diffusion-v1-5',
|
|
|
|
]
|
|
|
|
|
|
|
|
if str(value) in substrings_v1_model:
|
|
|
|
v2 = False
|
|
|
|
v_parameterization = False
|
|
|
|
|
|
|
|
return value, v2, v_parameterization
|
|
|
|
|
|
|
|
if value == 'custom':
|
|
|
|
value = ''
|
|
|
|
v2 = False
|
|
|
|
v_parameterization = False
|
|
|
|
|
|
|
|
return value, v2, v_parameterization
|