From f36a2874ebe1c3f41c2600c9333e9edc252f95f5 Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Sat, 9 Mar 2024 10:03:31 -0500 Subject: [PATCH] Clean up settings properly for removed models and also when user manually deletes. Signed-off-by: Adam Treat --- gpt4all-chat/download.cpp | 1 + gpt4all-chat/modellist.cpp | 37 ++++++++++++++++++------------------- gpt4all-chat/modellist.h | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/gpt4all-chat/download.cpp b/gpt4all-chat/download.cpp index bd870119..da23eaf2 100644 --- a/gpt4all-chat/download.cpp +++ b/gpt4all-chat/download.cpp @@ -203,6 +203,7 @@ void Download::removeModel(const QString &modelFile) QFile file(filePath); if (file.exists()) { const ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFile); + MySettings::globalInstance()->eraseModel(info); shouldRemoveInstalled = info.installed && !info.isClone() && (info.isDiscovered() || info.description() == "" /*indicates sideloaded*/); if (shouldRemoveInstalled) ModelList::globalInstance()->removeInstalled(info); diff --git a/gpt4all-chat/modellist.cpp b/gpt4all-chat/modellist.cpp index da950bfb..718f9212 100644 --- a/gpt4all-chat/modellist.cpp +++ b/gpt4all-chat/modellist.cpp @@ -1137,27 +1137,18 @@ QString ModelList::uniqueModelName(const ModelInfo &model) const return baseName; } -QString ModelList::modelDirPath(const QString &modelName, bool isOnline) +bool ModelList::modelExists(const QString &modelFilename) const { - QVector possibleFilePaths; - if (isOnline) - possibleFilePaths << "/" + modelName + ".txt"; - else { - possibleFilePaths << "/ggml-" + modelName + ".bin"; - possibleFilePaths << "/" + modelName + ".bin"; - } - for (const QString &modelFilename : possibleFilePaths) { - QString appPath = QCoreApplication::applicationDirPath() + modelFilename; - QFileInfo infoAppPath(appPath); - if (infoAppPath.exists()) - return QCoreApplication::applicationDirPath(); + QString appPath = QCoreApplication::applicationDirPath() + modelFilename; + QFileInfo infoAppPath(appPath); + if (infoAppPath.exists()) + return true; - QString downloadPath = MySettings::globalInstance()->modelPath() + modelFilename; - QFileInfo infoLocalPath(downloadPath); - if (infoLocalPath.exists()) - return MySettings::globalInstance()->modelPath(); - } - return QString(); + QString downloadPath = MySettings::globalInstance()->modelPath() + modelFilename; + QFileInfo infoLocalPath(downloadPath); + if (infoLocalPath.exists()) + return true; + return false; } void ModelList::updateModelsFromDirectory() @@ -1569,6 +1560,14 @@ void ModelList::updateModelsFromSettings() if (contains(id)) continue; + // If we can't find the corresponding file, then delete it from settings as this reflects a + // stale model. The file could have been deleted manually by the user for instance. + if (!settings.contains(g + "/filename") || !modelExists(settings.value(g + "/filename").toString())) { + settings.remove(g); + settings.sync(); + continue; + } + addModel(id); QVector> data; diff --git a/gpt4all-chat/modellist.h b/gpt4all-chat/modellist.h index 2268179d..0529afbc 100644 --- a/gpt4all-chat/modellist.h +++ b/gpt4all-chat/modellist.h @@ -462,7 +462,7 @@ private Q_SLOTS: private: void removeInternal(const ModelInfo &model); void clearDiscoveredModels(); - QString modelDirPath(const QString &modelName, bool isOnline); + bool modelExists(const QString &fileName) const; int indexForModel(ModelInfo *model); QVariant dataInternal(const ModelInfo *info, int role) const; static bool lessThan(const ModelInfo* a, const ModelInfo* b, DiscoverSort s, int d);