|
1 | 1 | import os
|
2 |
| - |
| 2 | +from pathlib import Path |
3 | 3 | import json
|
4 | 4 |
|
5 | 5 | from os.path import exists
|
@@ -70,29 +70,24 @@ def get_abspath(path):
|
70 | 70 |
|
71 | 71 |
|
72 | 72 | def get_model_filenames(folder_path, isLora=False):
|
73 |
| - if not os.path.isdir(folder_path): |
| 73 | + folder_path = Path(folder_path) |
| 74 | + if not folder_path.is_dir(): |
74 | 75 | raise ValueError("Folder path is not a valid directory.")
|
75 | 76 |
|
76 | 77 | filenames = []
|
77 | 78 |
|
78 |
| - for root, dirs, files in os.walk(folder_path): |
79 |
| - relative_path = os.path.relpath(root, folder_path) |
80 |
| - if relative_path == ".": |
81 |
| - relative_path = "" |
82 |
| - for filename in files: |
83 |
| - _, ext = os.path.splitext(filename) |
84 |
| - if ext.lower() in [".pth", ".ckpt", ".bin", ".safetensors"]: |
85 |
| - path = os.path.join(relative_path, filename) |
86 |
| - if isLora: |
87 |
| - txtcheck = path.replace(".safetensors", ".txt") |
88 |
| - if os.path.isfile(f"{folder_path}{txtcheck}"): |
89 |
| - path = path + " 🗒️" |
90 |
| - |
91 |
| - filenames.append(path) |
| 79 | + for path in folder_path.rglob("*"): |
| 80 | + if path.suffix.lower() in [".pth", ".ckpt", ".bin", ".safetensors"]: |
| 81 | + if isLora: |
| 82 | + txtcheck = path.with_suffix(".txt") |
| 83 | + if txtcheck.exists(): |
| 84 | + path = path.with_suffix(f"{path.suffix} 🗒️") |
| 85 | + |
| 86 | + filenames.append(str(path.relative_to(folder_path))) |
92 | 87 |
|
93 | 88 | return sorted(
|
94 | 89 | filenames,
|
95 |
| - key=lambda x: f"0{x.casefold()}" if os.sep in x else f"1{x.casefold()}", |
| 90 | + key=lambda x: f"0{x.casefold()}" if Path(x).suffix in x else f"1{x.casefold()}", |
96 | 91 | )
|
97 | 92 |
|
98 | 93 |
|
|
0 commit comments