File size: 6,773 Bytes
1c907cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7e1de
 
1c907cb
 
b4e8d6d
1c907cb
 
 
 
ee7e1de
 
 
 
1c907cb
 
 
 
 
 
 
 
 
 
 
 
ee7e1de
 
 
 
 
1c907cb
 
 
b4e8d6d
1c907cb
 
 
 
 
 
 
 
b4e8d6d
1c907cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e8d6d
1c907cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e8d6d
1c907cb
b4e8d6d
1c907cb
 
 
 
 
 
 
 
 
b4e8d6d
1c907cb
 
 
 
 
2c89886
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "JKTCrY9LU7Oq"
      },
      "outputs": [],
      "source": [
        "##~ AutoCleaner V3.7 CODE | BY: ANXETY   ~##\n",
        "\n",
        "from directory_setup import models_dir, vaes_dir, control_dir, loras_dir, output_dir\n",
        "\n",
        "import os\n",
        "import time\n",
        "from ipywidgets import widgets\n",
        "from IPython.display import display, HTML\n",
        "\n",
        "\n",
        "# Setup Env\n",
        "env = os.getenv('ENV_NAME')\n",
        "root_path = os.getenv('ROOT_PATH')\n",
        "webui_path = os.getenv('WEBUI_PATH')\n",
        "free_plan = os.getenv('FREE_PLAN')\n",
        "\n",
        "\n",
        "# ==================== CSS ====================\n",
        "# Main CSS\n",
        "css_file_path = f\"{root_path}/CSS/auto_cleaner.css\"\n",
        "with open(css_file_path , \"r\") as f:\n",
        "    CSS_AC = f.read()\n",
        "display(HTML(f\"<style>{CSS_AC}</style>\"))\n",
        "\n",
        "\n",
        "# ================ AutoCleaner function ================\n",
        "directories = {\n",
        "    \"Изображения\": output_dir,\n",
        "    \"Модели\": models_dir,\n",
        "    \"Vae\": vaes_dir,\n",
        "    \"LoRa\": loras_dir,\n",
        "    \"ControlNet Модели\": control_dir\n",
        "}\n",
        "\n",
        "\"\"\" functions \"\"\"\n",
        "def clean_directory(directory, directory_type):\n",
        "    deleted_files = 0\n",
        "\n",
        "    for root, dirs, files in os.walk(directory):\n",
        "        for file in files:\n",
        "            file_path = os.path.join(root, file)\n",
        "\n",
        "            if file.endswith(\".txt\"):\n",
        "                continue\n",
        "            if file.endswith((\".safetensors\", \".pt\")) or directory_type == \"Images\":\n",
        "                deleted_files += 1\n",
        "\n",
        "            os.remove(file_path)\n",
        "    return deleted_files\n",
        "\n",
        "def update_memory_info():\n",
        "    disk_space = psutil.disk_usage(os.getcwd())\n",
        "    total = disk_space.total / (1024 ** 3)\n",
        "    used = disk_space.used / (1024 ** 3)\n",
        "    free = disk_space.free / (1024 ** 3)\n",
        "\n",
        "    storage_info.value = f'''\n",
        "    <div class=\"storage_info_AC\">Всего: {total:.2f} GB <span style=\"color: #555\">|</span> Используется: {used:.2f} GB <span style=\"color: #555\">|</span> Свободно: {free:.2f} GB</div>\n",
        "    '''\n",
        "\n",
        "def on_execute_button_press(button):\n",
        "    selected_cleaners = auto_cleaner_widget.value\n",
        "    deleted_files_dict = {}\n",
        "\n",
        "    for option in selected_cleaners:\n",
        "        if option in directories:\n",
        "            deleted_files_dict[option] = clean_directory(directories[option], option)\n",
        "\n",
        "    output.clear_output()\n",
        "\n",
        "    with output:\n",
        "        for message in generate_messages(deleted_files_dict):\n",
        "            message_widget = HTML(f'<p class=\"output_message_AC\">{message}</p>')\n",
        "            display(message_widget)\n",
        "\n",
        "    update_memory_info()\n",
        "\n",
        "def on_clear_button_press(button):\n",
        "    container.add_class(\"hide\")\n",
        "    time.sleep(0.5)\n",
        "    widgets.Widget.close_all()\n",
        "\n",
        "def generate_messages(deleted_files_dict):\n",
        "    messages = []\n",
        "    word_variants = {\n",
        "        \"Изображения\": \"Изображений\",\n",
        "        \"Модели\": \"Моделей\",\n",
        "        \"Vae\": \"Vae\",\n",
        "        \"LoRa\": \"LoRa\",\n",
        "        \"ControlNet Модели\": \"ControlNet Моделей\"\n",
        "    }\n",
        "    for key, value in deleted_files_dict.items():\n",
        "        object_word = word_variants.get(key)\n",
        "        messages.append(f\"Удалено {value} {object_word}\")\n",
        "    return messages\n",
        "\n",
        "\n",
        "# --- storage memory ---\n",
        "import psutil\n",
        "disk_space = psutil.disk_usage(os.getcwd())\n",
        "total = disk_space.total / (1024 ** 3)\n",
        "used = disk_space.used / (1024 ** 3)\n",
        "free = disk_space.free / (1024 ** 3)\n",
        "\n",
        "\n",
        "# ================ Widgets ================\n",
        "# UI Code\n",
        "AutoCleaner_options = AutoCleaner_options = list(directories.keys())\n",
        "instruction_label = widgets.HTML('''\n",
        "<span class=\"instruction_AC\">Используйте <span style=\"color: #B2B2B2;\">ctrl</span> или <span style=\"color: #B2B2B2;\">shift</span> для множественного выбора.</span>\n",
        "''')\n",
        "auto_cleaner_widget = widgets.SelectMultiple(options=AutoCleaner_options, layout=widgets.Layout(width=\"auto\")).add_class(\"custom-select-multiple_AC\")\n",
        "output = widgets.Output().add_class(\"output_AC\")\n",
        "\n",
        "execute_button = widgets.Button(description='Выполнить Очистку').add_class(\"button_execute_AC\").add_class(\"button_AC\")\n",
        "execute_button.on_click(on_execute_button_press)\n",
        "clear_button = widgets.Button(description='Скрыть Виджет').add_class(\"button_clear_AC\").add_class(\"button_AC\")\n",
        "clear_button.on_click(on_clear_button_press)\n",
        "\n",
        "storage_info = widgets.HTML(f'''\n",
        "<div class=\"storage_info_AC\">Всего: {total:.2f} GB <span style=\"color: #555\">|</span> Используется: {used:.2f} GB <span style=\"color: #555\">|</span> Свободно: {free:.2f} GB</div>\n",
        "''')\n",
        "\n",
        "buttons = widgets.HBox([execute_button, clear_button])\n",
        "lower_information_panel = widgets.HBox([buttons, storage_info]).add_class(\"lower_information_panel_AC\")\n",
        "\n",
        "container = widgets.VBox([instruction_label, widgets.HTML('<hr>'), auto_cleaner_widget, output, widgets.HTML('<hr>'), lower_information_panel]).add_class(\"container_AC\")\n",
        "\n",
        "display(container)"
      ]
    }
  ]
}