Update configuration class

pull/2111/head^2
bmaltais 2024-03-16 19:03:45 -04:00
parent 6150a0f056
commit 0e4582cd52
6 changed files with 72 additions and 36 deletions

View File

@ -1,60 +1,96 @@
import gradio as gr
import os
from .common_gui import list_files
from .common_gui import list_files, scriptdir, create_refresh_button
class ConfigurationFile:
def __init__(self, headless=False, output_dir: gr.Dropdown = None):
from .common_gui import create_refresh_button
"""
A class to handle configuration file operations in the GUI.
"""
def __init__(self, headless: bool = False, config_dir: str = None):
"""
Initialize the ConfigurationFile class.
Parameters:
- headless (bool): Whether to run in headless mode.
- config_dir (str): The directory for configuration files.
"""
self.headless = headless
self.output_dir = None
def update_configs(output_dir):
self.output_dir = output_dir
return gr.Dropdown(choices=[""] + list(list_files(output_dir, exts=[".json"], all=True)))
# Sets the directory for storing configuration files, defaults to a 'presets' folder within the script directory.
self.current_config_dir = (
config_dir if config_dir is not None else os.path.join(scriptdir, "presets")
)
def list_configs(path):
self.output_dir = path
return list(list_files(path, exts=[".json"], all=True))
# Initialize the GUI components for configuration.
self.create_config_gui()
def list_config_dir(self, path: str) -> list:
"""
List directories in the data directory.
Parameters:
- path (str): The path to list directories from.
Returns:
- list: A list of directories.
"""
self.current_config_dir = path
# Lists all .json files in the current configuration directory, used for populating dropdown choices.
return list(list_files(self.current_config_dir, exts=[".json"], all=True))
def create_config_gui(self) -> None:
"""
Create the GUI for configuration file operations.
"""
# Starts a new group in the GUI for better layout organization.
with gr.Group():
# Creates a row within the group to align elements horizontally.
with gr.Row():
# Dropdown for selecting or entering the name of a configuration file.
self.config_file_name = gr.Dropdown(
label='Load/Save Config file',
choices=[""] + list_configs(self.output_dir),
label="Load/Save Config file",
choices=[""] + self.list_config_dir(self.current_config_dir),
value="",
interactive=True,
allow_custom_value=True,
)
create_refresh_button(self.config_file_name, lambda: None, lambda: {"choices": list_configs(self.output_dir)}, "open_folder_small")
# Button to refresh the list of configuration files in the dropdown.
create_refresh_button(
self.config_file_name,
lambda: None, # Placeholder for potential future functionality.
lambda: {
"choices": [""] + self.list_config_dir(self.current_config_dir)
},
"open_folder_small",
)
# Buttons for opening, saving, and loading configuration files, displayed conditionally based on headless mode.
self.button_open_config = gr.Button(
'📂',
elem_id='open_folder_small',
elem_classes=['tool'],
"📂",
elem_id="open_folder_small",
elem_classes=["tool"],
visible=(not self.headless),
)
self.button_save_config = gr.Button(
'💾',
elem_id='open_folder_small',
elem_classes=['tool'],
"💾",
elem_id="open_folder_small",
elem_classes=["tool"],
)
self.button_load_config = gr.Button(
'↩️ ',
elem_id='open_folder_small',
elem_classes=['tool'],
"↩️ ",
elem_id="open_folder_small",
elem_classes=["tool"],
)
# Handler for change events on the configuration file dropdown, allowing dynamic update of choices.
self.config_file_name.change(
fn=lambda path: gr.Dropdown(choices=[""] + list_configs(path)),
fn=lambda path: gr.Dropdown(choices=[""] + self.list_config_dir(path)),
inputs=self.config_file_name,
outputs=self.config_file_name,
show_progress=False,
)
output_dir.change(
fn=update_configs,
inputs=output_dir,
outputs=self.config_file_name,
)

View File

@ -110,7 +110,7 @@ class Folders:
allow_custom_value=True,
)
# Refresh button for output directory
create_refresh_button(self.output_dir, lambda: None, lambda: {"choices": self.list_output_dirs(self.current_output_dir)}, "open_folder_small")
create_refresh_button(self.output_dir, lambda: None, lambda: {"choices": [""] + self.list_output_dirs(self.current_output_dir)}, "open_folder_small")
# Output directory button
self.output_dir_folder = gr.Button(
'📂', elem_id='open_folder_small', elem_classes=["tool"], visible=(not self.headless)
@ -131,7 +131,7 @@ class Folders:
allow_custom_value=True,
)
# Refresh button for regularisation directory
create_refresh_button(self.reg_data_dir, lambda: None, lambda: {"choices": self.list_data_dirs(self.current_data_dir)}, "open_folder_small")
create_refresh_button(self.reg_data_dir, lambda: None, lambda: {"choices": [""] + self.list_data_dirs(self.current_data_dir)}, "open_folder_small")
# Regularisation directory button
self.reg_data_dir_folder = gr.Button(
'📂', elem_id='open_folder_small', elem_classes=["tool"], visible=(not self.headless)
@ -152,7 +152,7 @@ class Folders:
allow_custom_value=True,
)
# Refresh button for logging directory
create_refresh_button(self.logging_dir, lambda: None, lambda: {"choices": self.list_logging_dirs(self.current_logging_dir)}, "open_folder_small")
create_refresh_button(self.logging_dir, lambda: None, lambda: {"choices": [""] + self.list_logging_dirs(self.current_logging_dir)}, "open_folder_small")
# Logging directory button
self.logging_dir_folder = gr.Button(
'📂', elem_id='open_folder_small', elem_classes=["tool"], visible=(not self.headless)

View File

@ -747,7 +747,7 @@ def dreambooth_tab(
# Setup Configuration Files Gradio
with gr.Accordion("Configuration", open=False):
config = ConfigurationFile(headless=headless, output_dir=folders.output_dir)
config = ConfigurationFile(headless=headless)
with gr.Column(), gr.Group():
with gr.Row():

View File

@ -825,7 +825,7 @@ def finetune_tab(headless=False):
# Setup Configuration Files Gradio
with gr.Accordion("Configuration", open=False):
config = ConfigurationFile(headless=headless, output_dir=train_dir)
config = ConfigurationFile(headless=headless)
with gr.Column(), gr.Group():

View File

@ -1768,7 +1768,7 @@ def lora_tab(
# Setup Configuration Files Gradio
with gr.Accordion('Configuration', open=False):
config = ConfigurationFile(headless=headless, output_dir=folders.output_dir)
config = ConfigurationFile(headless=headless)
with gr.Column(), gr.Group():

View File

@ -788,7 +788,7 @@ def ti_tab(
# Setup Configuration Files Gradio
with gr.Accordion("Configuration", open=False):
config = ConfigurationFile(headless=headless, output_dir=folders.output_dir)
config = ConfigurationFile(headless=headless)
with gr.Column(), gr.Group():