Transformers
Inference Endpoints
File size: 8,644 Bytes
2d1ecea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
##~ AutoCleaner V3.6 CODE | BY: ANXETY   ~##

import os
import time
import ipywidgets as widgets
from ipywidgets import Label, Button, VBox, HBox
from IPython.display import display, HTML, Javascript


#  ================= DETECT ENV =================
def detect_environment():
    free_plan = (os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / (1024. ** 3) <= 20)
    environments = {
        'COLAB_GPU': ('Google Colab', "/root" if free_plan else "/content"),
        'KAGGLE_URL_BASE': ('Kaggle', "/kaggle/working/content")
    }

    for env_var, (environment, path) in environments.items():
        if env_var in os.environ:
            return environment, path, free_plan

env, root_path, free_plan = detect_environment()
webui_path = f"{root_path}/sdw"
#  ----------------------------------------------


# ==================== CSS ====================
CSS = """
<style>
/* General Styles */

hr {
    border-color: grey;
    background-color: grey;
    opacity: 0.25;
}

.instruction_AC {
    font-family: cursive;
    font-size: 18px;
    color: grey;
    user-select: none;
    cursor: default;
}


/* Container style */

.container_AC {
    position: relative;
    background-color: #232323;
    width: 800px;
    height: auto;
    padding: 15px;
    border-radius: 15px;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.3);
    margin-bottom: 5px;
    overflow: visible;
}

.container_AC::before {
    position: absolute;
    top: 5px;
    right: 10px;
    content: "AutoCleanerV3.6";
    font-weight: bold;
    font-size: 24px;
    color: rgba(0, 0, 0, 0.2);
}

.container_AC::after {
    position: absolute;
    top: 30px;
    right: 10px;
    content: "ANXETY";
    font-weight: bold;
    font-size: 18px;
    color: rgba(0, 0, 0, 0.2);
}

.custom-select-multiple_AC select {
    padding: 10px;
    font-family: cursive;
    border: 1px solid #262626 !important;
    border-radius: 10px;
    color: white;
    background-color: #1c1c1c;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
}

.output_AC {
    padding: 10px;
    height: auto;
    border: 1px solid #262626;
    border-radius: 10px;
    background-color: #1c1c1c;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
    width: auto;
    box-sizing: border-box;
}

.output_message_AC {
    font-family: cursive;
    color: white !important;
    font-size: 14px;
    user-select: none;
    cursor: default
}


.storage_info_AC {
    padding: 5px 20px;
    height: auto;
    border: 1px solid #262626;
    border-radius: 10px;
    background-color: #1c1c1c;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
    width: auto;
    font-family: cursive;
    color: #B2B2B2 !important;
    font-size: 14px;
    user-select: none;
    cursor: default
}


/* Button and storage info layout */
.lower_information_panel_AC {
    display: flex;
    align-items: center;
    justify-content: space-between;
}


/* Button style */

.button_AC {
    width: auto;
    font-family: cursive;
    color: white !important;
    font-size: 14px;
    font-weight: bold;
    height: 35px;
    border-radius: 15px;
    background-image: radial-gradient(circle at top left, purple 10%, violet 90%);
    background-size: 200% 200%;
    background-position: left bottom;
    transition: background 0.5s ease-in-out, transform 0.3s ease;
}

.button_AC:hover {
    cursor: pointer;
    background-size: 200% 200%;
    background-position: right bottom;
    transform: translateY(1px);
}

.button_execute_AC:hover {
    background-image: radial-gradient(circle at top left, purple 10%, #93ac47 90%);
}

.button_clear_AC:hover {
    background-image: radial-gradient(circle at top left, purple 10%, #fc3468 90%);
}

.button_execute_AC:active,
.button_clear_AC:active {
    filter: brightness(0.75);
}

.jupyter-widgets.lm-Widget:focus {
    outline: none;
}


/* Animation of elements */

/* Emergence */
.container_AC {
    animation-name: slideInTopBlur;
    animation-duration: 0.7s;
    animation-fill-mode: forwards;
}

@keyframes slideInTopBlur {
    0% {
        transform: translate3d(0, 50%, 0) scale(0.85) rotate3d(1, 0, 0, -85deg);
        filter: blur(5px) grayscale(1) brightness(0.5);
        opacity: 0;
    }
    100% {
        transform: translate3d(0, 0, 0) scale(1) rotate3d(1, 0, 0, 0deg);
        filter: blur(0) grayscale(0) brightness(1);
        opacity: 1;
    }
}

/* Leaving */
.container_AC.hide {
    animation-name: slideOutTopBlur;
    animation-duration: 0.5s;
    animation-fill-mode: forwards;
}

@keyframes slideOutTopBlur {
    0% {
        transform: translate3d(0, 0, 0) scale(1);
        filter: blur(0) grayscale(0) brightness(1);
        opacity: 1;
    }
    100% {
        transform: translate3d(0, -100%, 0);
        filter: blur(5px) grayscale(1) brightness(0);
        opacity: 0;
    }
}
</style>
"""

display(HTML(CSS))
# ==================== CSS ====================


# ================ AutoCleaner function ================
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/"
}

""" functions """
def clean_directory(directory):
    deleted_files = 0
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith(".txt"):
                continue
            os.remove(os.path.join(root, file))
            if not file.endswith(".yaml"):
                deleted_files += 1
    return deleted_files

def update_memory_info():
    disk_space = psutil.disk_usage(os.getcwd())
    total = disk_space.total / (1024 ** 3)
    used = disk_space.used / (1024 ** 3)
    free = disk_space.free / (1024 ** 3)

    storage_info.value = f'''
    <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>
    '''

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'<p class="output_message_AC">{message}</p>')
            display(message_widget)

    update_memory_info()

def on_clear_button_press(button):
    container.add_class("hide")
    time.sleep(0.5)
    widgets.Widget.close_all()

def generate_messages(deleted_files_dict):
    messages = []
    word_variants = {
        "Images": "Images",
        "Models": "Models",
        "Vae": "Vae",
        "LoRa": "LoRa",
        "ControlNet Models": "ControlNet Models"
    }
    for key, value in deleted_files_dict.items():
        object_word = word_variants.get(key)
        messages.append(f"Deleted {value} {object_word}")
    return messages
# ================ AutoCleaner function ================


# --- storage memory ---
import psutil
disk_space = psutil.disk_usage(os.getcwd())
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('''
<span class="instruction_AC">Use <span style="color: #B2B2B2;">ctrl</span> or <span style="color: #B2B2B2;">shift</span> for multiple selections.</span>
''')
auto_cleaner_widget = widgets.SelectMultiple(options=AutoCleaner_options, layout=widgets.Layout(width='auto')).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'''
<div class="storage_info_AC">Total storage: {total:.2f} GB <span style="color: #555">|</span> Used: {used:.2f} GB <span style="color: #555">|</span> Free: {free:.2f} GB</div>
''')
# ---
buttons = widgets.HBox([execute_button, clear_button])
lower_information_panel = widgets.HBox([buttons, storage_info]).add_class("lower_information_panel_AC")

container = VBox([instruction_label, widgets.HTML('<hr>'), auto_cleaner_widget, output, widgets.HTML('<hr>'), lower_information_panel]).add_class("container_AC")

display(container)