Enhance model caching and exclusion functionality: update cache version, add excluded models to cache data, and ensure cache is saved to disk after model exclusion and deletion.

This commit is contained in:
Will Miao
2025-06-04 18:38:45 +08:00
parent 641fa8a3d9
commit 9fc2fb4d17
4 changed files with 27 additions and 2 deletions

View File

@@ -22,7 +22,8 @@ logger = logging.getLogger(__name__)
# Version history: # Version history:
# 1 - Initial version # 1 - Initial version
# 2 - Added duplicate_filenames and duplicate_hashes tracking # 2 - Added duplicate_filenames and duplicate_hashes tracking
CACHE_VERSION = 2 # 3 - Added _excluded_models list to cache
CACHE_VERSION = 3
class ModelScanner: class ModelScanner:
"""Base service for scanning and managing model files""" """Base service for scanning and managing model files"""
@@ -115,7 +116,8 @@ class ModelScanner:
"duplicate_filenames": self._hash_index._duplicate_filenames "duplicate_filenames": self._hash_index._duplicate_filenames
}, },
"tags_count": self._tags_count, "tags_count": self._tags_count,
"dirs_last_modified": self._get_dirs_last_modified() "dirs_last_modified": self._get_dirs_last_modified(),
"excluded_models": self._excluded_models # Add excluded_models to cache data
} }
# Preprocess data to handle large integers # Preprocess data to handle large integers
@@ -217,6 +219,9 @@ class ModelScanner:
# Load tags count # Load tags count
self._tags_count = cache_data.get("tags_count", {}) self._tags_count = cache_data.get("tags_count", {})
# Load excluded models
self._excluded_models = cache_data.get("excluded_models", [])
# Resort the cache # Resort the cache
await self._cache.resort() await self._cache.resort()

View File

@@ -304,6 +304,8 @@ class ModelRouteUtils:
if hasattr(scanner, '_hash_index') and scanner._hash_index: if hasattr(scanner, '_hash_index') and scanner._hash_index:
scanner._hash_index.remove_by_path(file_path) scanner._hash_index.remove_by_path(file_path)
await scanner._save_cache_to_disk()
return web.json_response({ return web.json_response({
'success': True, 'success': True,
'deleted_files': deleted_files 'deleted_files': deleted_files
@@ -485,6 +487,8 @@ class ModelRouteUtils:
# Add to excluded models list # Add to excluded models list
scanner._excluded_models.append(file_path) scanner._excluded_models.append(file_path)
await scanner._save_cache_to_disk()
return web.json_response({ return web.json_response({
'success': True, 'success': True,
'message': f"Model {os.path.basename(file_path)} excluded" 'message': f"Model {os.path.basename(file_path)} excluded"

View File

@@ -434,6 +434,8 @@ export function replaceModelPreview(filePath, modelType = 'lora') {
// Delete a model (generic) // Delete a model (generic)
export async function deleteModel(filePath, modelType = 'lora') { export async function deleteModel(filePath, modelType = 'lora') {
try { try {
state.loadingManager.showSimpleLoading(`Deleting ${modelType}...`);
const endpoint = modelType === 'checkpoint' const endpoint = modelType === 'checkpoint'
? '/api/checkpoints/delete' ? '/api/checkpoints/delete'
: '/api/delete_model'; : '/api/delete_model';
@@ -475,6 +477,8 @@ export async function deleteModel(filePath, modelType = 'lora') {
console.error(`Error deleting ${modelType}:`, error); console.error(`Error deleting ${modelType}:`, error);
showToast(`Failed to delete ${modelType}: ${error.message}`, 'error'); showToast(`Failed to delete ${modelType}: ${error.message}`, 'error');
return false; return false;
} finally {
state.loadingManager.hide();
} }
} }
@@ -662,6 +666,8 @@ export async function refreshSingleModelMetadata(filePath, modelType = 'lora') {
// Generic function to exclude a model // Generic function to exclude a model
export async function excludeModel(filePath, modelType = 'lora') { export async function excludeModel(filePath, modelType = 'lora') {
try { try {
state.loadingManager.showSimpleLoading(`Excluding ${modelType}...`);
const endpoint = modelType === 'checkpoint' const endpoint = modelType === 'checkpoint'
? '/api/checkpoints/exclude' ? '/api/checkpoints/exclude'
: '/api/loras/exclude'; : '/api/loras/exclude';
@@ -703,6 +709,8 @@ export async function excludeModel(filePath, modelType = 'lora') {
console.error(`Error excluding ${modelType}:`, error); console.error(`Error excluding ${modelType}:`, error);
showToast(`Failed to exclude ${modelType}: ${error.message}`, 'error'); showToast(`Failed to exclude ${modelType}: ${error.message}`, 'error');
return false; return false;
} finally {
state.loadingManager.hide();
} }
} }

View File

@@ -37,6 +37,10 @@ export async function confirmDelete() {
} }
closeDeleteModal(); closeDeleteModal();
if (window.modelDuplicatesManager) {
window.modelDuplicatesManager.updateDuplicatesBadgeAfterRefresh();
}
} catch (error) { } catch (error) {
console.error('Error deleting model:', error); console.error('Error deleting model:', error);
alert(`Error deleting model: ${error}`); alert(`Error deleting model: ${error}`);
@@ -86,6 +90,10 @@ export async function confirmExclude() {
} }
closeExcludeModal(); closeExcludeModal();
if (window.modelDuplicatesManager) {
window.modelDuplicatesManager.updateDuplicatesBadgeAfterRefresh();
}
} catch (error) { } catch (error) {
console.error('Error excluding model:', error); console.error('Error excluding model:', error);
} }