KohyaSS/library/resize_lora_gui.py

174 lines
5.0 KiB
Python
Raw Normal View History

2023-02-04 16:55:06 +00:00
import gradio as gr
from easygui import msgbox
import subprocess
import os
from .common_gui import get_saveasfilename_path, get_file_path
2023-03-04 23:56:22 +00:00
PYTHON = 'python3' if os.name == 'posix' else './venv/Scripts/python.exe'
2023-02-04 16:55:06 +00:00
folder_symbol = '\U0001f4c2' # 📂
refresh_symbol = '\U0001f504' # 🔄
save_style_symbol = '\U0001f4be' # 💾
document_symbol = '\U0001F4C4' # 📄
def resize_lora(
2023-02-06 01:07:00 +00:00
model,
new_rank,
save_to,
save_precision,
device,
2023-03-06 17:46:57 +00:00
dynamic_method,
dynamic_param,
verbose,
2023-02-04 16:55:06 +00:00
):
# Check for caption_text_input
if model == '':
msgbox('Invalid model file')
return
# Check if source model exist
if not os.path.isfile(model):
msgbox('The provided model is not a file')
return
2023-03-20 12:47:00 +00:00
2023-03-06 17:46:57 +00:00
if dynamic_method == 'sv_ratio':
if float(dynamic_param) < 2:
2023-03-20 12:47:00 +00:00
msgbox(
f'Dynamic parameter for {dynamic_method} need to be 2 or greater...'
)
2023-03-06 17:46:57 +00:00
return
2023-03-20 12:47:00 +00:00
2023-03-06 17:46:57 +00:00
if dynamic_method == 'sv_fro' or dynamic_method == 'sv_cumulative':
if float(dynamic_param) < 0 or float(dynamic_param) > 1:
2023-03-20 12:47:00 +00:00
msgbox(
f'Dynamic parameter for {dynamic_method} need to be between 0 and 1...'
)
2023-03-06 17:46:57 +00:00
return
# Check if save_to end with one of the defines extension. If not add .safetensors.
if not save_to.endswith(('.pt', '.safetensors')):
save_to += '.safetensors'
2023-02-06 01:07:00 +00:00
2023-02-04 16:55:06 +00:00
if device == '':
device = 'cuda'
run_cmd = f'{PYTHON} "{os.path.join("networks","resize_lora.py")}"'
2023-02-04 16:55:06 +00:00
run_cmd += f' --save_precision {save_precision}'
run_cmd += f' --save_to {save_to}'
run_cmd += f' --model {model}'
run_cmd += f' --new_rank {new_rank}'
run_cmd += f' --device {device}'
2023-03-06 17:46:57 +00:00
if not dynamic_method == 'None':
run_cmd += f' --dynamic_method {dynamic_method}'
run_cmd += f' --dynamic_param {dynamic_param}'
if verbose:
run_cmd += f' --verbose'
2023-02-04 16:55:06 +00:00
print(run_cmd)
# Run the command
2023-03-05 16:43:59 +00:00
if os.name == 'posix':
os.system(run_cmd)
else:
subprocess.run(run_cmd)
2023-02-04 16:55:06 +00:00
###
# Gradio UI
###
def gradio_resize_lora_tab():
with gr.Tab('Resize LoRA'):
2023-02-06 01:07:00 +00:00
gr.Markdown('This utility can resize a LoRA.')
2023-03-06 17:46:57 +00:00
lora_ext = gr.Textbox(value='*.safetensors *.pt', visible=False)
2023-02-04 16:55:06 +00:00
lora_ext_name = gr.Textbox(value='LoRA model types', visible=False)
2023-02-06 01:07:00 +00:00
2023-02-04 16:55:06 +00:00
with gr.Row():
model = gr.Textbox(
label='Source LoRA',
placeholder='Path to the LoRA to resize',
interactive=True,
)
button_lora_a_model_file = gr.Button(
folder_symbol, elem_id='open_folder_small'
)
button_lora_a_model_file.click(
get_file_path,
inputs=[model, lora_ext, lora_ext_name],
outputs=model,
2023-03-04 23:56:22 +00:00
show_progress=False,
2023-02-04 16:55:06 +00:00
)
with gr.Row():
2023-02-06 01:07:00 +00:00
new_rank = gr.Slider(
label='Desired LoRA rank',
minimum=1,
maximum=1024,
step=1,
value=4,
interactive=True,
)
2023-03-06 17:46:57 +00:00
with gr.Row():
dynamic_method = gr.Dropdown(
2023-03-20 12:47:00 +00:00
choices=['None', 'sv_ratio', 'sv_fro', 'sv_cumulative'],
2023-03-06 17:46:57 +00:00
value='sv_fro',
label='Dynamic method',
2023-03-20 12:47:00 +00:00
interactive=True,
2023-03-06 17:46:57 +00:00
)
dynamic_param = gr.Textbox(
label='Dynamic parameter',
value='0.9',
interactive=True,
2023-03-20 12:47:00 +00:00
placeholder='Value for the dynamic method selected.',
2023-03-06 17:46:57 +00:00
)
2023-03-20 12:47:00 +00:00
verbose = gr.Checkbox(label='Verbose', value=False)
2023-02-04 16:55:06 +00:00
with gr.Row():
save_to = gr.Textbox(
label='Save to',
placeholder='path for the LoRA file to save...',
interactive=True,
)
button_save_to = gr.Button(
folder_symbol, elem_id='open_folder_small'
)
button_save_to.click(
2023-02-06 01:07:00 +00:00
get_saveasfilename_path,
inputs=[save_to, lora_ext, lora_ext_name],
outputs=save_to,
2023-03-04 23:56:22 +00:00
show_progress=False,
2023-02-04 16:55:06 +00:00
)
save_precision = gr.Dropdown(
2023-03-02 00:20:05 +00:00
label='Save precision',
2023-02-04 16:55:06 +00:00
choices=['fp16', 'bf16', 'float'],
value='fp16',
interactive=True,
)
2023-03-10 16:44:52 +00:00
device = gr.Dropdown(
2023-02-04 16:55:06 +00:00
label='Device',
2023-03-20 12:47:00 +00:00
choices=[
'cpu',
'cuda',
],
2023-03-06 17:46:57 +00:00
value='cuda',
2023-03-10 16:44:52 +00:00
interactive=True,
2023-02-04 16:55:06 +00:00
)
convert_button = gr.Button('Resize model')
convert_button.click(
resize_lora,
2023-02-06 01:07:00 +00:00
inputs=[
model,
new_rank,
save_to,
save_precision,
device,
2023-03-06 17:46:57 +00:00
dynamic_method,
dynamic_param,
verbose,
2023-02-04 16:55:06 +00:00
],
2023-03-04 23:56:22 +00:00
show_progress=False,
2023-02-04 16:55:06 +00:00
)