File size: 18,075 Bytes
bb7f1f4 | 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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | import html
import json
import os
from typing import List
import scripts.mo.ui_format as ui_format
from scripts.mo.data.storage import map_record_to_dict
from scripts.mo.environment import env
from scripts.mo.models import Record, ModelType
from scripts.mo.utils import get_best_preview_url
_NO_PREVIEW_DARK = 'file=extensions/sd-model-organizer/pic/no-preview-dark-blue.png'
_NO_PREVIEW_LIGHT = 'file=extensions/sd-model-organizer/pic/no-preview-light.png'
def alert_danger(value) -> str:
if isinstance(value, list):
text = "<br>".join([html.escape(s) for s in value])
else:
text = html.escape(value)
return f'<div class="mo-alert mo-alert-danger">{text}</div>'
def alert_primary(value) -> str:
if isinstance(value, list):
text = "<br>".join([html.escape(s) for s in value])
else:
text = html.escape(value)
return f'<div class="mo-alert mo-alert-primary">{text}</div>'
def alert_success(value) -> str:
if isinstance(value, list):
text = "<br>".join([html.escape(s) for s in value])
else:
text = html.escape(value)
return f'<div class="mo-alert mo-alert-success">{text}</div>'
def alert_warning(value) -> str:
if isinstance(value, list):
text = "<br>".join([html.escape(s) for s in value])
else:
text = html.escape(value)
return f'<div class="mo-alert mo-alert-warning">{text}</div>'
def _limit_description(text):
if text and len(text) > 600:
return text[:600] + '...'
else:
return text
def _limit_card_name(text):
if text and len(text) > 150:
return text[:150] + '...'
else:
return text
def _model_type_css_class(model_type: ModelType) -> str:
if model_type == ModelType.CHECKPOINT:
css_class = 'mo-badge-checkpoint'
elif model_type == ModelType.VAE:
css_class = 'mo-badge-vae'
elif model_type == ModelType.LORA:
css_class = 'mo-badge-lora'
elif model_type == ModelType.HYPER_NETWORK:
css_class = 'mo-badge-hyper-network'
elif model_type == ModelType.LYCORIS:
css_class = 'mo-badge-lycoris'
elif model_type == ModelType.EMBEDDING:
css_class = 'mo-badge-embedding'
elif model_type == ModelType.OTHER:
css_class = 'mo-badge-other'
else:
raise ValueError(f'Unhandled model_type value: {model_type}')
return css_class
def _model_card_type_css_class(model_type: ModelType) -> str:
if model_type == ModelType.CHECKPOINT:
css_class = 'mo-card-checkpoint'
elif model_type == ModelType.VAE:
css_class = 'mo-card-vae'
elif model_type == ModelType.LORA:
css_class = 'mo-card-lora'
elif model_type == ModelType.HYPER_NETWORK:
css_class = 'mo-card-hyper-network'
elif model_type == ModelType.LYCORIS:
css_class = 'mo-card-lycoris'
elif model_type == ModelType.EMBEDDING:
css_class = 'mo-card-embedding'
elif model_type == ModelType.OTHER:
css_class = 'mo-card-other'
else:
raise ValueError(f'Unhandled model_type value: {model_type}')
return css_class
def _no_preview_image_url() -> str:
if env.theme() == 'dark':
return _NO_PREVIEW_DARK
else:
return _NO_PREVIEW_LIGHT
def records_table(records: List) -> str:
table_html = '<div id="organizer_record_table" class="mo-container">'
table_html += '<div class="mo-row mo-row-header">'
table_html += '<div class="mo-col mo-col-preview"><span class="mo-text-header">Preview</span></div>'
table_html += '<div class="mo-col mo-col-type"><span class="mo-text-header">Type</span></div>'
table_html += '<div class="mo-col mo-col-name"><span class="mo-text-header">Name</span></div>'
table_html += '<div class="mo-col mo-col-description"><span class="mo-text-header">Description</span></div>'
table_html += '<div class="mo-col mo-col-actions"><span class="mo-text-header">Actions</span></div>'
table_html += '</div>'
nsfw_blur = env.nsfw_blur()
for record in records:
contains_nsfw = any('nsfw' in group.lower() for group in record.groups) and nsfw_blur
name = html.escape(record.name)
type_ = record.model_type.value
preview_url = get_best_preview_url(record)
description = _limit_description(record.description)
# Add row
table_html += '<div class="mo-row">'
# Add preview URL column
table_html += '<div class="mo-col mo-col-preview">'
isLocalFileRecord = record.is_local_file_record()
img = f'<img class="mo-preview-image" src="{preview_url}" ' \
f'alt="Preview image"' \
f' onerror="this.onerror=null; this.src=\'{_no_preview_image_url()}\';"/'
if not isLocalFileRecord:
img += f'onclick="fillPrompt({record.id_})"'
img += '>'
table_html += img
table_html += '</div>'
# Add type column
type_badge_class = _model_type_css_class(record.model_type)
table_html += f'<div class="mo-col mo-col-type"><span class="mo-badge {type_badge_class}">{type_}</span></div>'
# Add name column
table_html += f'<div class="mo-col mo-col-name">'
table_html += f'<button class="mo-button-name" onclick="navigateDetails(\'{record.id_}\', event)">{name}</button>'
table_html += '</div>'
# Add description column
table_html += f'<div class="mo-col mo-col-description">'
table_html += f'<span class="mo-text-description">{html.escape(description)}</span>'
table_html += '</div>'
# Add actions column
table_html += '<div class="mo-col mo-col-actions ">'
if record.is_local_file_record():
json_record = html.escape(json.dumps(map_record_to_dict(record)))
table_html += '<button type="button" class="mo-btn mo-btn-success" ' \
f'onclick="navigateEditPrefilled(\'{json_record}\', event)">Add</button><br>'
table_html += '<button type="button" class="mo-btn mo-btn-danger" ' \
f'onclick="navigateRemove(\'{record.location}\', event)">Remove</button><br>'
else:
table_html += '<button type="button" class="mo-btn mo-btn-success" ' \
f'onclick="navigateDetails(\'{record.id_}\', event)">Details</button><br>'
if record.is_download_possible():
table_html += '<button type="button" class="mo-btn mo-btn-primary" ' \
f'onclick="navigateDownloadRecord(\'{record.id_}\', event)">Download</button><br>'
table_html += '<button type="button" class="mo-btn mo-btn-warning" ' \
f'onclick="navigateEdit(\'{record.id_}\', event)">Edit</button><br>'
table_html += '<button type="button" class="mo-btn mo-btn-danger" ' \
f'onclick="navigateRemove(\'{record.id_}\', event)">Remove</button><br>'
table_html += '</div>'
# Close row
table_html += '</div>'
# Close table
table_html += '</div>'
return table_html
def _create_content_text(text: str) -> str:
return f'<span class="mo-text-content">{text}</span>'
def _create_content_model_type(model_type: ModelType) -> str:
return f'<span class="mo-badge {_model_type_css_class(model_type)} mo-details-badge">{model_type.value}</span>'
def _create_content_hash(value: str) -> str:
return f'<samp class="mo-text-content">{value}</samp>'
def _create_content_link(link: str) -> str:
return f'<a class="mo-nav-link" target="_blank" href="{link}">{link}</a>'
def _create_groups(groups: List) -> str:
groups_html = ''
for group in groups:
groups_html += f'<span class="mo-badge mo-badge-group" onclick="">{html.escape(group)}</span>'
return groups_html
def _create_top_fields_dict(record: Record) -> dict:
result = {
'Name': _create_content_text(html.escape(record.name)),
'Type': _create_content_model_type(record.model_type)
}
if record.location and os.path.exists(record.location):
size = os.path.getsize(record.location)
result['Size'] = _create_content_hash(ui_format.format_bytes(size))
if record.sha256_hash:
result['SHA256'] = _create_content_hash(record.sha256_hash)
if record.location and os.path.exists(record.location):
result['Location'] = _create_content_hash(record.location)
if record.url:
result['Model page'] = _create_content_link(record.url)
if record.groups:
result['Groups'] = _create_groups(record.groups)
if record.download_url:
result['Download URL'] = _create_content_link(record.download_url)
if record.download_path:
result['Download path'] = _create_content_text(record.download_path)
if record.download_filename:
result['Download filename'] = _create_content_text(record.download_filename)
if record.subdir:
result['Subdir'] = _create_content_text(record.subdir)
if record.weight != 1:
result['Prompt Weight']= _create_content_text(str(record.weight))
return result
def _details_field_row(title: str, field: str, is_even: bool) -> str:
highlight = 'mo-details-row-even' if is_even else 'mo-details-row-odd'
content = f'<div class="mo-details-row {highlight}">'
content += '<div class="mo-details-sub-col mo-details-sub-col-header">'
content += f'<span class="mo-text-header">{title}:</span>'
content += '</div>'
content += '<div class="mo-details-sub-col">'
content += field
content += '</div>'
content += '</div>'
return content
def _details_top(record: Record) -> str:
preview_url = get_best_preview_url(record)
content = '<div class="mo-details-row">'
# Preview image
content += '<div class="mo-details-col mo-details-col-preview">'
content += f'<img class="mo-details-image" src="{preview_url}" alt="Preview Image" ' \
f'onerror="this.onerror=null; this.src=\'{_no_preview_image_url()}\';"/>'
content += '</div>'
# Details column
content += '<div class="mo-details-col">'
content += '<div class="mo-details-sub-container">'
fields = _create_top_fields_dict(record)
counter = 0
for key, value in fields.items():
content += _details_field_row(key, value, counter % 2 == 0)
counter += 1
# End Top Details column
content += '</div>'
content += '</div>'
content += '</div>'
if record.positive_prompts or record.negative_prompts:
content += '<div class="mo-details-row mo-details-row-padding">'
content += '<div class="mo-details-col">'
content += '<div class="mo-details-row">'
content += '<div class="mo-details-sub-container">'
content += '<div class="mo-details-sub-row">'
if record.positive_prompts:
content += '<div class="mo-details-sub-col mo-details-row-positive">'
content += '<span class="mo-text-header mo-text-positive-header">Positive Prompts:</span>'
content += '</div>'
if record.negative_prompts:
content += '<div class="mo-details-sub-col mo-details-row-negative">'
content += '<span class="mo-text-header mo-text-negative-header">Negative Prompts</span>'
content += '</div>'
content += '</div>'
content += '<div class="mo-details-sub-row">'
if record.positive_prompts:
content += '<div class="mo-details-sub-col mo-details-row-positive">'
content += '<span class="mo-text-content mo-text-positive">'
content += html.escape(record.positive_prompts)
content += '</span>'
content += '</div>'
if record.negative_prompts:
content += '<div class="mo-details-sub-col mo-details-row-negative">'
content += '<span class="mo-text-content mo-text-negative">'
content += html.escape(record.negative_prompts)
content += '</span>'
content += '</div>'
content += '</div>'
content += '</div>'
content += '</div>'
content += '</div>'
content += '</div>'
content += '</div>'
return content
def record_details(record: Record) -> str:
content = '<div class="mo-details-container">'
content += _details_top(record)
content += '</div>'
return content
def records_cards(records: List) -> str:
content = '<div id="organizer_record_card_grid" class="mo-card-grid">'
nsfw_blur = env.nsfw_blur()
for record in records:
contains_nsfw = any('nsfw' in group.lower() for group in record.groups) and nsfw_blur
isLocalFileRecord = record.is_local_file_record()
# Taken from extra networks cards.
cardStr = f'<div class="mo-card {_model_card_type_css_class(record.model_type)} {"blur" if contains_nsfw else ""}"'
if not isLocalFileRecord:
cardStr += f'onclick="fillPrompt({record.id_})"'
cardStr += '>'
content += cardStr
preview_url = get_best_preview_url(record)
content += f'<img src="{preview_url}" alt="Preview Image" ' \
f'onerror="this.onerror=null; this.src=\'{_no_preview_image_url()}\';"/>'
content += f'<div class="mo-card-blur-overlay-bottom">{html.escape(_limit_card_name(record.name))}</div>'
content += '<div class="mo-card-content-top">'
content += f'<div class="mo-card-text-left"><span class="mo-badge {_model_type_css_class(record.model_type)}"' \
f'>{record.model_type.value}</span></div>'
content += '</div>'
content += '<div class="mo-card-hover">'
content += '<div class="mo-card-hover-buttons">'
if isLocalFileRecord:
json_record = html.escape(json.dumps(map_record_to_dict(record)))
content += '<button type="button" class="mo-btn mo-btn-success" ' \
f'onclick="navigateEditPrefilled(\'{json_record}\', event)">Add</button><br>'
location = record.location.replace("\\", "\\\\")
content += '<button type="button" class="mo-btn mo-btn-danger" ' \
f'onclick="navigateRemove(\'{location}\', event)">Remove</button><br>'
else:
content += '<button type="button" class="mo-btn mo-btn-success" ' \
f'onclick="navigateDetails(\'{record.id_}\', event)">Details</button><br>'
if record.is_download_possible():
content += '<button type="button" class="mo-btn mo-btn-primary" ' \
f'onclick="navigateDownloadRecord(\'{record.id_}\', event)">Download</button><br>'
content += '<button type="button" class="mo-btn mo-btn-warning" ' \
f'onclick="navigateEdit(\'{record.id_}\', event)">Edit</button><br>'
content += '<button type="button" class="mo-btn mo-btn-danger" ' \
f'onclick="navigateRemove(\'{record.id_}\', event)">Remove</button><br>'
content += '</div>'
content += '</div>'
content += '</div>'
content += '</div>'
return content
def _downloads_header(record_id, title) -> str:
content = '<div class="mo-downloads-header">'
content += f'<h2 style="margin: 0;" id="title-{record_id}">{html.escape(title)}</h2>'
content += f'<p style="margin: 0; white-space: nowrap;" id="status-{record_id}">Pending</p>'
content += '</div>'
return content
def _download_url(record_id, url: str, is_preview: bool) -> str:
preview = '-preview' if is_preview else ''
hint = 'Preview URL' if is_preview else 'Model URL'
content = f'<p style="margin-top: 2rem; display: block; overflow-wrap: anywhere;" id="url{preview}-{record_id}">' \
f'[{hint}]: {url}</p>'
return content
def _download_info(record_id, is_preview: bool) -> str:
preview = '-preview' if is_preview else ''
content = f'<div class="mo-download-info" id="info-bar{preview}-{record_id}" style="display: none !important">'
content += f'<p style="margin: 0;" id="progress-info-left{preview}-{record_id}"></p>'
content += f'<p style="margin: 0;" id="progress-info-center{preview}-{record_id}"></p>'
content += f'<p style="margin: 0;" id="progress-info-right{preview}-{record_id}"></p>'
content += '</div>'
return content
def _download_progress_bar(record_id, is_preview: bool) -> str:
preview = '-preview' if is_preview else ''
content = '<div class="mo-progress" style="height: 1.2rem; margin-top: 1rem; display: none" ' \
f'id="progress{preview}-{record_id}">'
content += '<div class="mo-progress-bar" role="progressbar" style="width: 0"'
content += 'aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"'
content += f'id="progress-bar{preview}-{record_id}">0%</div>'
content += '</div>'
return content
def download_cards(records: List, token) -> str:
content = f'<div class="mo-downloads-container" token="{token}">'
counter = 0
for record in records:
id_ = record.id_
card_margin_top = '0' if counter == 0 else '2rem'
content += f'<div class="mo-downloads-card mo-alert-secondary" id="download-card-{id_}" ' \
f'style="margin-top: {card_margin_top}">'
content += _downloads_header(id_, record.name)
content += _download_url(id_, record.download_url, False)
content += _download_info(id_, False)
content += _download_progress_bar(id_, False)
if record.preview_url and env.download_preview():
content += _download_url(id_, record.preview_url, True)
content += _download_info(id_, True)
content += _download_progress_bar(id_, True)
content += f'<div id="result-box-{id_}" style="margin-top: 1rem">'
content += '</div>'
content += '</div>'
counter += 1
content += '</div>'
return content
|