Added model size sort.

This commit is contained in:
Christian Bastian
2024-04-14 00:40:14 -04:00
parent f0e7b11b6d
commit 04f44927d7
2 changed files with 24 additions and 7 deletions

View File

@@ -497,6 +497,7 @@ async def get_model_list(request):
break
abs_path = os.path.join(cwd, model)
stats = pathlib.Path(abs_path).stat()
sizeBytes = stats.st_size
model_modified = stats.st_mtime_ns
model_created = stats.st_ctime_ns
if use_safetensor_thumbnail and image is None and model_ext == ".safetensors":
@@ -513,12 +514,21 @@ async def get_model_list(request):
image = model + image_ext
image_modified = model_modified
rel_path = "" if cwd == model_base_path else os.path.relpath(cwd, model_base_path)
info = (model, image, base_path_index, rel_path, model_modified, model_created, image_modified)
info = (
model,
image,
base_path_index,
rel_path,
model_modified,
model_created,
image_modified,
sizeBytes,
)
file_infos.append(info)
file_infos.sort(key=lambda tup: tup[4], reverse=True) # TODO: remove sort; sorted on client
#file_infos.sort(key=lambda tup: tup[4], reverse=True) # TODO: remove sort; sorted on client
model_items = []
for model, image, base_path_index, rel_path, model_modified, model_created, image_modified in file_infos:
for model, image, base_path_index, rel_path, model_modified, model_created, image_modified, sizeBytes in file_infos:
item = {
"name": model,
"path": "/" + os.path.join(model_type, str(base_path_index), rel_path, model).replace(os.path.sep, "/"), # relative logical path
@@ -527,6 +537,7 @@ async def get_model_list(request):
"dateCreated": model_created,
#"dateLastUsed": "", # TODO: track server-side, send increment client-side
#"countUsed": 0, # TODO: track server-side, send increment client-side
"sizeBytes": sizeBytes,
}
if image is not None:
raw_post = os.path.join(model_type, str(base_path_index), rel_path, image)

View File

@@ -1273,6 +1273,7 @@ class DirectoryDropdown {
const MODEL_SORT_DATE_CREATED = "dateCreated";
const MODEL_SORT_DATE_MODIFIED = "dateModified";
const MODEL_SORT_SIZE_BYTES = "sizeBytes";
const MODEL_SORT_DATE_NAME = "name";
class ModelGrid {
@@ -1354,6 +1355,9 @@ class ModelGrid {
case MODEL_SORT_DATE_CREATED:
compareFn = (a, b) => { return b[MODEL_SORT_DATE_CREATED] - a[MODEL_SORT_DATE_CREATED]; };
break;
case MODEL_SORT_SIZE_BYTES:
compareFn = (a, b) => { return b[MODEL_SORT_SIZE_BYTES] - a[MODEL_SORT_SIZE_BYTES]; };
break;
default:
console.warn("Invalid filter sort value: '" + sortBy + "'");
return list;
@@ -2977,12 +2981,14 @@ class ModelTab {
onchange: () => updateModelGrid(),
},
[
$el("option", { value: MODEL_SORT_DATE_CREATED }, ["Created (newest to oldest)"]),
$el("option", { value: "-" + MODEL_SORT_DATE_CREATED }, ["Created (oldest to newest)"]),
$el("option", { value: MODEL_SORT_DATE_MODIFIED }, ["Modified (newest to oldest)"]),
$el("option", { value: "-" + MODEL_SORT_DATE_MODIFIED }, ["Modified (oldest to newest)"]),
$el("option", { value: MODEL_SORT_DATE_CREATED }, ["Created (newest first)"]),
$el("option", { value: "-" + MODEL_SORT_DATE_CREATED }, ["Created (oldest first)"]),
$el("option", { value: MODEL_SORT_DATE_MODIFIED }, ["Modified (newest first)"]),
$el("option", { value: "-" + MODEL_SORT_DATE_MODIFIED }, ["Modified (oldest first)"]),
$el("option", { value: MODEL_SORT_DATE_NAME }, ["Name (A-Z)"]),
$el("option", { value: "-" + MODEL_SORT_DATE_NAME }, ["Name (Z-A)"]),
$el("option", { value: MODEL_SORT_SIZE_BYTES }, ["Size (largest first)"]),
$el("option", { value: "-" + MODEL_SORT_SIZE_BYTES }, ["Size (smallest first)"]),
],
),
]),