#!/usr/bin/env python # coding: utf-8 # In[ ]: ##~ AutoCleaner V3.5 CODE | BY: ANXETY ~## import os import time import ipywidgets as widgets from ipywidgets import Label, Button, VBox, HBox from IPython.display import display, HTML # ================= DETECT ENV ================= def detect_environment(): environments = { 'COLAB_GPU': ('Google Colab', "/content"), 'KAGGLE_URL_BASE': ('Kaggle', "/kaggle/working/content"), 'SAGEMAKER_INTERNAL_IMAGE_URI': ('SageMaker Studio Lab', "/home/studio-lab-user/content") } for env_var, (environment, path) in environments.items(): if env_var in os.environ: return environment, path env, root_path = detect_environment() webui_path = f"{root_path}/sdw" # ---------------------------------------------- directories = { "Images": f"{webui_path}/outputs", "Models": f"{webui_path}/models/Stable-diffusion/", "Vae": f"{webui_path}/models/VAE/", "LoRa": f"{webui_path}/models/Lora/", "ControlNet Models": f"{webui_path}/models/ControlNet/" } # ==================== CSS ==================== CSS = """ """ display(HTML(CSS)) # ==================== CSS ==================== # ================ AutoCleaner function ================ def clean_directory(directory): deleted_files = 0 for root, dirs, files in os.walk(directory): for file in files: if not file.endswith((".txt", ".yaml")): file_path = os.path.join(root, file) os.remove(file_path) deleted_files += 1 return deleted_files def get_word_variant(n, variants): unit = abs(n) % 10 tens = abs(n) % 100 if tens in range(11, 15): return variants[2] elif unit == 1: return variants[0] elif unit in range(2, 5): return variants[1] return variants[2] def on_execute_button_press(button): selected_cleaners = auto_cleaner_widget.value deleted_files_dict = {} for option in selected_cleaners: if option in directories: deleted_files_dict[option] = clean_directory(directories[option]) output.clear_output() with output: for message in generate_messages(deleted_files_dict): message_widget = HTML(f'
') display(message_widget) def on_clear_button_press(button): container.add_class("hide") time.sleep(0.3) widgets.Widget.close_all() def generate_messages(deleted_files_dict): messages = [] word_variants = { "Images": ["Image", "Images", "Images"], "Models": ["Model", "Models", "Models"], "Vae": ["VAE", "VAE", "VAE"], "LoRa": ["LoRa", "LoRa", "LoRa"], "ControlNet Models": ["ControlNet Model", "ControlNet Models", "ControlNet Models"] } deleted_word_variants = ["Deleted", "Deleted", "Deleted"] for key, value in deleted_files_dict.items(): word_variant = word_variants.get(key, ["", "", ""]) deleted_word = get_word_variant(value, deleted_word_variants) object_word = get_word_variant(value, word_variant) messages.append(f"{deleted_word} {value} {object_word}") return messages # ================ AutoCleaner function ================ # --- storage memory --- import psutil directory = os.getcwd() disk_space = psutil.disk_usage(directory) total = disk_space.total / (1024 ** 3) used = disk_space.used / (1024 ** 3) free = disk_space.free / (1024 ** 3) # UI Code AutoCleaner_options = AutoCleaner_options = list(directories.keys()) instruction_label = widgets.HTML(''' Use ctrl or shift for multiple selections. ''') auto_cleaner_widget = widgets.SelectMultiple(options=AutoCleaner_options, layout=widgets.Layout(width='100%')).add_class("custom-select-multiple_AC") output = widgets.Output().add_class("output_AC") # --- execute_button = Button(description='Execute Cleaning').add_class("button_execute_AC").add_class("button_AC") execute_button.on_click(on_execute_button_press) clear_button = Button(description='Hide Widget').add_class("button_clear_AC").add_class("button_AC") clear_button.on_click(on_clear_button_press) # --- storage_info = widgets.HTML(f'''