Skip to content

Commit

Permalink
Update code to last version of gradio 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaltais committed Feb 10, 2024
1 parent 1a0d8ec commit 7167b33
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 61 deletions.
1 change: 1 addition & 0 deletions kohya_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def UI(**kwargs):
launch_kwargs["inbrowser"] = inbrowser
if share:
launch_kwargs["share"] = share
launch_kwargs["debug"] = True
interface.launch(**launch_kwargs)


Expand Down
12 changes: 6 additions & 6 deletions library/class_advanced_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ def __init__(self, headless=False, finetuning: bool = False, training_type: str
def noise_offset_type_change(noise_offset_type):
if noise_offset_type == 'Original':
return (
gr.Group.update(visible=True),
gr.Group.update(visible=False),
gr.Group(visible=True),
gr.Group(visible=False),
)
else:
return (
gr.Group.update(visible=False),
gr.Group.update(visible=True),
gr.Group(visible=False),
gr.Group(visible=True),
)

with gr.Row(visible=not finetuning):
Expand Down Expand Up @@ -88,9 +88,9 @@ def full_options_update(full_fp16, full_bf16):
full_bf16_active = False
if full_bf16:
full_fp16_active = False
return gr.Checkbox.update(
return gr.Checkbox(
interactive=full_fp16_active,
), gr.Checkbox.update(interactive=full_bf16_active)
), gr.Checkbox(interactive=full_bf16_active)

self.keep_tokens = gr.Slider(
label='Keep n tokens', value='0', minimum=0, maximum=32, step=1
Expand Down
6 changes: 3 additions & 3 deletions library/class_basic_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ def __init__(

def update_learning_rate_te(sdxl_checkbox, finetuning, dreambooth):
return (
gr.Number.update(visible=(not sdxl_checkbox and (finetuning or dreambooth))),
gr.Number.update(visible=(sdxl_checkbox and (finetuning or dreambooth))),
gr.Number.update(visible=(sdxl_checkbox and (finetuning or dreambooth))),
gr.Number(visible=(not sdxl_checkbox and (finetuning or dreambooth))),
gr.Number(visible=(sdxl_checkbox and (finetuning or dreambooth))),
gr.Number(visible=(sdxl_checkbox and (finetuning or dreambooth))),
)

self.sdxl_checkbox.change(
Expand Down
2 changes: 1 addition & 1 deletion library/class_sdxl_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
)

self.sdxl_checkbox.change(
lambda sdxl_checkbox: gr.Accordion.update(visible=sdxl_checkbox),
lambda sdxl_checkbox: gr.Accordion(visible=sdxl_checkbox),
inputs=[self.sdxl_checkbox],
outputs=[self.sdxl_row],
)
64 changes: 32 additions & 32 deletions library/common_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ def color_aug_changed(color_aug):
msgbox(
'Disabling "Cache latent" because "Color augmentation" has been selected...'
)
return gr.Checkbox.update(value=False, interactive=False)
return gr.Checkbox(value=False, interactive=False)
else:
return gr.Checkbox.update(value=True, interactive=True)
return gr.Checkbox(value=True, interactive=True)


def save_inference_file(output_dir, v2, v_parameterization, output_name):
Expand Down Expand Up @@ -454,14 +454,14 @@ def set_pretrained_model_name_or_path_input(
# Check if the given model_list is in the list of SDXL models
if str(model_list) in SDXL_MODELS:
log.info("SDXL model selected. Setting sdxl parameters")
v2 = gr.Checkbox.update(value=False, visible=False)
v_parameterization = gr.Checkbox.update(value=False, visible=False)
sdxl = gr.Checkbox.update(value=True, visible=False)
pretrained_model_name_or_path = gr.Textbox.update(
v2 = gr.Checkbox(value=False, visible=False)
v_parameterization = gr.Checkbox(value=False, visible=False)
sdxl = gr.Checkbox(value=True, visible=False)
pretrained_model_name_or_path = gr.Textbox(
value=str(model_list), visible=False
)
pretrained_model_name_or_path_file = gr.Button.update(visible=False)
pretrained_model_name_or_path_folder = gr.Button.update(visible=False)
pretrained_model_name_or_path_file = gr.Button(visible=False)
pretrained_model_name_or_path_folder = gr.Button(visible=False)
return (
model_list,
pretrained_model_name_or_path,
Expand All @@ -475,14 +475,14 @@ def set_pretrained_model_name_or_path_input(
# Check if the given model_list is in the list of V2 base models
if str(model_list) in V2_BASE_MODELS:
log.info("SD v2 base model selected. Setting --v2 parameter")
v2 = gr.Checkbox.update(value=True, visible=False)
v_parameterization = gr.Checkbox.update(value=False, visible=False)
sdxl = gr.Checkbox.update(value=False, visible=False)
pretrained_model_name_or_path = gr.Textbox.update(
v2 = gr.Checkbox(value=True, visible=False)
v_parameterization = gr.Checkbox(value=False, visible=False)
sdxl = gr.Checkbox(value=False, visible=False)
pretrained_model_name_or_path = gr.Textbox(
value=str(model_list), visible=False
)
pretrained_model_name_or_path_file = gr.Button.update(visible=False)
pretrained_model_name_or_path_folder = gr.Button.update(visible=False)
pretrained_model_name_or_path_file = gr.Button(visible=False)
pretrained_model_name_or_path_folder = gr.Button(visible=False)
return (
model_list,
pretrained_model_name_or_path,
Expand All @@ -498,14 +498,14 @@ def set_pretrained_model_name_or_path_input(
log.info(
"SD v2 model selected. Setting --v2 and --v_parameterization parameters"
)
v2 = gr.Checkbox.update(value=True, visible=False)
v_parameterization = gr.Checkbox.update(value=True, visible=False)
sdxl = gr.Checkbox.update(value=False, visible=False)
pretrained_model_name_or_path = gr.Textbox.update(
v2 = gr.Checkbox(value=True, visible=False)
v_parameterization = gr.Checkbox(value=True, visible=False)
sdxl = gr.Checkbox(value=False, visible=False)
pretrained_model_name_or_path = gr.Textbox(
value=str(model_list), visible=False
)
pretrained_model_name_or_path_file = gr.Button.update(visible=False)
pretrained_model_name_or_path_folder = gr.Button.update(visible=False)
pretrained_model_name_or_path_file = gr.Button(visible=False)
pretrained_model_name_or_path_folder = gr.Button(visible=False)
return (
model_list,
pretrained_model_name_or_path,
Expand All @@ -519,14 +519,14 @@ def set_pretrained_model_name_or_path_input(
# Check if the given model_list is in the list of V1 models
if str(model_list) in V1_MODELS:
log.info(f"{model_list} model selected.")
v2 = gr.Checkbox.update(value=False, visible=False)
v_parameterization = gr.Checkbox.update(value=False, visible=False)
sdxl = gr.Checkbox.update(value=False, visible=False)
pretrained_model_name_or_path = gr.Textbox.update(
v2 = gr.Checkbox(value=False, visible=False)
v_parameterization = gr.Checkbox(value=False, visible=False)
sdxl = gr.Checkbox(value=False, visible=False)
pretrained_model_name_or_path = gr.Textbox(
value=str(model_list), visible=False
)
pretrained_model_name_or_path_file = gr.Button.update(visible=False)
pretrained_model_name_or_path_folder = gr.Button.update(visible=False)
pretrained_model_name_or_path_file = gr.Button(visible=False)
pretrained_model_name_or_path_folder = gr.Button(visible=False)
return (
model_list,
pretrained_model_name_or_path,
Expand All @@ -539,12 +539,12 @@ def set_pretrained_model_name_or_path_input(

# Check if the model_list is set to 'custom'
if model_list == "custom":
v2 = gr.Checkbox.update(visible=True)
v_parameterization = gr.Checkbox.update(visible=True)
sdxl = gr.Checkbox.update(visible=True)
pretrained_model_name_or_path = gr.Textbox.update(visible=True)
pretrained_model_name_or_path_file = gr.Button.update(visible=True)
pretrained_model_name_or_path_folder = gr.Button.update(visible=True)
v2 = gr.Checkbox(visible=True)
v_parameterization = gr.Checkbox(visible=True)
sdxl = gr.Checkbox(visible=True)
pretrained_model_name_or_path = gr.Textbox(visible=True)
pretrained_model_name_or_path_file = gr.Button(visible=True)
pretrained_model_name_or_path_folder = gr.Button(visible=True)
return (
model_list,
pretrained_model_name_or_path,
Expand Down
2 changes: 1 addition & 1 deletion library/extract_lora_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def extract_lora(

def gradio_extract_lora_tab(headless=False):
def change_sdxl(sdxl):
return gr.update(visible=sdxl), gr.update(visible=sdxl)
return gr(visible=sdxl), gr(visible=sdxl)



Expand Down
10 changes: 5 additions & 5 deletions library/extract_lycoris_locon_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ def extract_lycoris_locon(
# def update_mode(mode):
# # 'fixed', 'threshold','ratio','quantile'
# if mode == 'fixed':
# return gr.Row.update(visible=True), gr.Row.update(visible=False), gr.Row.update(visible=False), gr.Row.update(visible=False)
# return gr.Row(visible=True), gr.Row(visible=False), gr.Row(visible=False), gr.Row(visible=False)
# if mode == 'threshold':
# return gr.Row.update(visible=False), gr.Row.update(visible=True), gr.Row.update(visible=False), gr.Row.update(visible=False)
# return gr.Row(visible=False), gr.Row(visible=True), gr.Row(visible=False), gr.Row(visible=False)
# if mode == 'ratio':
# return gr.Row.update(visible=False), gr.Row.update(visible=False), gr.Row.update(visible=True), gr.Row.update(visible=False)
# return gr.Row(visible=False), gr.Row(visible=False), gr.Row(visible=True), gr.Row(visible=False)
# if mode == 'threshold':
# return gr.Row.update(visible=False), gr.Row.update(visible=False), gr.Row.update(visible=False), gr.Row.update(visible=True)
# return gr.Row(visible=False), gr.Row(visible=False), gr.Row(visible=False), gr.Row(visible=True)


def update_mode(mode):
Expand All @@ -123,7 +123,7 @@ def update_mode(mode):
# Iterate through the possible modes
for m in modes:
# Add a visibility update for each mode, setting it to True if the input mode matches the current mode in the loop
updates.append(gr.Row.update(visible=(mode == m)))
updates.append(gr.Row(visible=(mode == m)))

# Return the visibility updates as a tuple
return tuple(updates)
Expand Down
10 changes: 5 additions & 5 deletions library/manual_caption_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _get_tag_checkbox_updates(caption, quick_tags, quick_tags_set):
t for t in caption_tags_have if t not in quick_tags_set
]
caption_tags_all = quick_tags + caption_tags_unique
return gr.CheckboxGroup.update(
return gr.CheckboxGroup(
choices=caption_tags_all, value=caption_tags_have
)

Expand Down Expand Up @@ -118,7 +118,7 @@ def import_tags_from_captions(
"""

def empty_return():
return gr.Text.update()
return gr.Text()

# Check for images_dir
if not images_dir:
Expand Down Expand Up @@ -247,7 +247,7 @@ def update_images(
tag_checkboxes = _get_tag_checkbox_updates(
caption, quick_tags, quick_tags_set
)
rows.append(gr.Row.update(visible=show_row))
rows.append(gr.Row(visible=show_row))
image_paths.append(image_path)
captions.append(caption)
tag_checkbox_groups.append(tag_checkboxes)
Expand All @@ -258,7 +258,7 @@ def update_images(
+ image_paths
+ captions
+ tag_checkbox_groups
+ [gr.Row.update(visible=True), gr.Row.update(visible=True)]
+ [gr.Row(visible=True), gr.Row(visible=True)]
)


Expand Down Expand Up @@ -473,7 +473,7 @@ def render_pagination():
# Save buttons visibility
# (on auto-save on/off)
auto_save.change(
lambda auto_save: [gr.Button.update(visible=not auto_save)]
lambda auto_save: [gr.Button(visible=not auto_save)]
* IMAGES_TO_SHOW,
inputs=auto_save,
outputs=save_buttons,
Expand Down
19 changes: 12 additions & 7 deletions lora_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,16 @@ def open_configuration(

# Check if we are "applying" a preset or a config
if apply_preset:
log.info(f"Applying preset {training_preset}...")
file_path = f"./presets/lora/{training_preset}.json"
if training_preset != "none":
log.info(f"Applying preset {training_preset}...")
file_path = f"./presets/lora/{training_preset}.json"
else:
# If not applying a preset, set the `training_preset` field to an empty string
# Find the index of the `training_preset` parameter using the `index()` method
training_preset_index = parameters.index(("training_preset", training_preset))

# Update the value of `training_preset` by directly assigning an empty string value
parameters[training_preset_index] = ("training_preset", "")
parameters[training_preset_index] = ("training_preset", "none")

original_file_path = file_path

Expand Down Expand Up @@ -414,9 +415,9 @@ def open_configuration(
"LyCORIS/LoCon",
"LyCORIS/GLoRA",
}:
values.append(gr.Row.update(visible=True))
values.append(gr.Row(visible=True))
else:
values.append(gr.Row.update(visible=False))
values.append(gr.Row(visible=False))

return tuple(values)

Expand Down Expand Up @@ -1074,6 +1075,9 @@ def lora_tab(

def list_presets(path):
json_files = []

# Insert an empty string at the beginning
json_files.insert(0, "none")

for file in os.listdir(path):
if file.endswith(".json"):
Expand All @@ -1092,6 +1096,7 @@ def list_presets(path):
label="Presets",
choices=list_presets("./presets/lora"),
elem_id="myDropdown",
value="none"
)

with gr.Tab("Basic", elem_id="basic_tab"):
Expand Down Expand Up @@ -1129,7 +1134,7 @@ def list_presets(path):
interactive=True
# info="https://github.com/KohakuBlueleaf/LyCORIS/blob/0006e2ffa05a48d8818112d9f70da74c0cd30b99/docs/Preset.md"
)
with gr.Box():
with gr.Group():
with gr.Row():
lora_network_weights = gr.Textbox(
label="LoRA network weights",
Expand Down Expand Up @@ -1673,7 +1678,7 @@ def update_LoRA_settings(
for attr, settings in lora_settings_config.items():
update_params = settings["update_params"]

results.append(settings["gr_type"].update(**update_params))
results.append(settings["gr_type"](**update_params))

return tuple(results)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ easygui==0.98.3
einops==0.6.1
fairscale==0.4.13
ftfy==6.1.1
gradio==3.36.1
gradio==3.50.2
huggingface-hub==0.20.1
# for loading Diffusers' SDXL
invisible-watermark==0.2.0
Expand Down

0 comments on commit 7167b33

Please sign in to comment.