sd15_checkboxes = [widgets.Checkbox(value=False, description=key) for key in sd15_models.keys()]
xl_checkboxes = [widgets.Checkbox(value=False, description=key) for key in xl_models.keys()]
"Select All" checkboxes
select_all_sd15 = widgets.Checkbox(description="Select All SD15", value=False)
select_all_xl = widgets.Checkbox(description="Select All XL", value=False)
Function to update the selection state of all checkboxes based on "Select All" state
def update_selection(checkboxes, select_all_checkbox):
for cb in checkboxes:
cb.value = select_all_checkbox.value
Attach the update_selection function to the "Select All" checkboxes
def download_models(b):
target_directory = "~/stable-diffusion-webui/extensions/sd-webui-controlnet/models"
for checkbox in checkbox_container.children[1:]: # Skip the "Select All" checkbox
if checkbox.value: # If the checkbox is checked
model_name = checkbox.description
model_files = sd15_models.get(model_name) or xl_models.get(model_name)
for file_info in model_files:
if isinstance(file_info, tuple):
url, filename = file_info
filepath = os.path.join(os.path.expanduser(target_directory), filename)
else:
url = file_info
filename = url.split('/')[-1]
filepath = os.path.join(os.path.expanduser(target_directory), filename)
# Check if the file already exists
if not os.path.isfile(filepath):
# If the file does not exist, download it
if isinstance(file_info, tuple):
!wget {url} -O {filepath}
else:
!wget {url} -P {target_directory}
else:
print(f"File {filename} already exists. Skipping download.")
You've created a comprehensive interface for selecting and downloading models for both SD15 and XL versions using IPyWidgets. Here's an overview and some minor improvements for your script:
Improvements
Data Structures Improvement:
Ensure consistency in URL structures for easier management.
Prefer a consistent data structure for model URLs; all could use tuples with explicit filenames to avoid ambiguity.
Better Handling of Directories:
Ensure the target directory exists before downloading.
Feedback and Progress Indication:
It might be useful to provide feedback to the user while downloading files.
Here's the improved and cleaned-up version of your script:
import os
import ipywidgets as widgets
from IPython.display import display
# Define the models and their URLs with explicit filenames
sd15_models = {
"ControlNet++ Canny": [("https://huggingface.co/huchenlei/ControlNet_plus_plus_collection_fp16/resolve/main/controlnet%2B%2B_canny_sd15_fp16.safetensors", "controlnet++_canny_sd15_fp16.safetensors")],
"ControlNet++ Depth": [("https://huggingface.co/huchenlei/ControlNet_plus_plus_collection_fp16/resolve/main/controlnet%2B%2B_depth_sd15_fp16.safetensors", "controlnet++_depth_sd15_fp16.safetensors")],
# Add other SD15 models...
}
xl_models = {
"SAI XL Canny": [("https://huggingface.co/lllyasviel/sd_control_collection/resolve/main/sai_xl_canny_256lora.safetensors", "sai_xl_canny_256lora.safetensors")],
"SAI XL Depth": [("https://huggingface.co/lllyasviel/sd_control_collection/resolve/main/sai_xl_depth_256lora.safetensors", "sai_xl_depth_256lora.safetensors")],
# Add other XL models...
}
# Create checkboxes for each model category
sd15_checkboxes = [widgets.Checkbox(value=False, description=key) for key in sd15_models.keys()]
xl_checkboxes = [widgets.Checkbox(value=False, description=key) for key in xl_models.keys()]
# "Select All" checkboxes
select_all_sd15 = widgets.Checkbox(description="Select All SD15", value=False)
select_all_xl = widgets.Checkbox(description="Select All XL", value=False)
# Update selection function
def update_selection(checkboxes, select_all_checkbox):
for cb in checkboxes:
cb.value = select_all_checkbox.value
# Attach the update_selection function
def attach_select_all(select_all_checkbox, checkboxes):
def on_select_all_change(change):
update_selection(checkboxes, select_all_checkbox)
select_all_checkbox.observe(on_select_all_change, names='value')
attach_select_all(select_all_sd15, sd15_checkboxes)
attach_select_all(select_all_xl, xl_checkboxes)
# Display checkboxes based on model type
checkbox_container = widgets.VBox(children=[])
def display_checkboxes(model_type):
if model_type == 'sd15':
checkbox_container.children = [select_all_sd15] + sd15_checkboxes
elif model_type == 'xl':
checkbox_container.children = [select_all_xl] + xl_checkboxes
# Buttons for model type selection
sd15_button = widgets.Button(description="SD15 Models")
xl_button = widgets.Button(description="XL Models")
# Button click events
sd15_button.on_click(lambda b: display_checkboxes('sd15'))
xl_button.on_click(lambda b: display_checkboxes('xl'))
# Display buttons
buttons_container = widgets.HBox(children=[sd15_button, xl_button])
display(buttons_container)
display(checkbox_container)
# Download selected models function
def download_models(b):
target_directory = os.path.expanduser("~/stable-diffusion-webui/extensions/sd-webui-controlnet/models")
os.makedirs(target_directory, exist_ok=True)
for checkbox in checkbox_container.children[1:]: # Skip the "Select All" checkbox
if checkbox.value:
model_name = checkbox.description
model_files = sd15_models.get(model_name) or xl_models.get(model_name)
for url, filename in model_files:
filepath = os.path.join(target_directory, filename)
if not os.path.isfile(filepath): # Check if file exists
print(f"Downloading {filename}...")
!wget {url} -O {filepath}
else:
print(f"File {filename} already exists. Skipping download.")
# Button to start the download
download_button = widgets.Button(description="Download Selected Models")
download_button.on_click(download_models)
display(download_button)
Summary of Changes:
Consistent URL Tuples: Explicitly associating filenames with URLs for clarity and consistency.
Directory Handling: Ensuring the target directory exists before downloading files.
Feedback: Giving feedback upon downloading or skipping files.
These changes enhance the script's robustness and user experience when downloading models.