From b24ace372bf4f9bc95400843da881f023230a347 Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Tue, 16 May 2023 09:32:01 -0400 Subject: [PATCH] Add ability to remove models. --- gpt4all-chat/download.cpp | 21 ++++++++++ gpt4all-chat/download.h | 1 + gpt4all-chat/network.cpp | 10 +++++ gpt4all-chat/network.h | 1 + gpt4all-chat/qml/ModelDownloaderDialog.qml | 47 +++++++++++++++++----- 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/gpt4all-chat/download.cpp b/gpt4all-chat/download.cpp index 04dc5937..eff55b4f 100644 --- a/gpt4all-chat/download.cpp +++ b/gpt4all-chat/download.cpp @@ -287,6 +287,27 @@ void Download::installModel(const QString &modelFile, const QString &apiKey) } } +void Download::removeModel(const QString &modelFile) +{ + const bool isChatGPT = modelFile.startsWith("chatgpt-"); + const QString filePath = downloadLocalModelsPath() + + (!isChatGPT ? "ggml-" : QString()) + + modelFile + + (!isChatGPT ? ".bin" : ".txt"); + QFile file(filePath); + if (!file.exists()) { + qWarning() << "ERROR: Cannot remove file that does not exist" << filePath; + return; + } + + Network::globalInstance()->sendRemoveModel(modelFile); + ModelInfo info = m_modelMap.value(modelFile); + info.installed = false; + m_modelMap.insert(modelFile, info); + file.remove(); + emit modelListChanged(); +} + void Download::handleSslErrors(QNetworkReply *reply, const QList &errors) { QUrl url = reply->request().url(); diff --git a/gpt4all-chat/download.h b/gpt4all-chat/download.h index 1310d99c..265b8f3e 100644 --- a/gpt4all-chat/download.h +++ b/gpt4all-chat/download.h @@ -91,6 +91,7 @@ public: Q_INVOKABLE void downloadModel(const QString &modelFile); Q_INVOKABLE void cancelDownload(const QString &modelFile); Q_INVOKABLE void installModel(const QString &modelFile, const QString &apiKey); + Q_INVOKABLE void removeModel(const QString &modelFile); Q_INVOKABLE QString defaultLocalModelsPath() const; Q_INVOKABLE QString downloadLocalModelsPath() const; Q_INVOKABLE void setDownloadLocalModelsPath(const QString &modelPath); diff --git a/gpt4all-chat/network.cpp b/gpt4all-chat/network.cpp index 27e9c6ae..75167570 100644 --- a/gpt4all-chat/network.cpp +++ b/gpt4all-chat/network.cpp @@ -253,6 +253,16 @@ void Network::sendInstallModel(const QString &model) sendMixpanelEvent("install_model", QVector{kv}); } +void Network::sendRemoveModel(const QString &model) +{ + if (!m_usageStatsActive) + return; + KeyValue kv; + kv.key = QString("model"); + kv.value = QJsonValue(model); + sendMixpanelEvent("remove_model", QVector{kv}); +} + void Network::sendDownloadStarted(const QString &model) { if (!m_usageStatsActive) diff --git a/gpt4all-chat/network.h b/gpt4all-chat/network.h index 50e32fd4..6cb1b559 100644 --- a/gpt4all-chat/network.h +++ b/gpt4all-chat/network.h @@ -42,6 +42,7 @@ public Q_SLOTS: Q_INVOKABLE void sendModelDownloaderDialog(); Q_INVOKABLE void sendResetContext(int conversationLength); void sendInstallModel(const QString &model); + void sendRemoveModel(const QString &model); void sendDownloadStarted(const QString &model); void sendDownloadCanceled(const QString &model); void sendDownloadError(const QString &model, int code, const QString &errorString); diff --git a/gpt4all-chat/qml/ModelDownloaderDialog.qml b/gpt4all-chat/qml/ModelDownloaderDialog.qml index 1de3f1dd..6a47e6d3 100644 --- a/gpt4all-chat/qml/ModelDownloaderDialog.qml +++ b/gpt4all-chat/qml/ModelDownloaderDialog.qml @@ -209,18 +209,47 @@ Dialog { } } - Label { - id: installedLabel + Item { anchors.top: modelName.top + anchors.topMargin: 15 anchors.right: parent.right - padding: 20 - objectName: "installedLabel" - color: theme.textColor - text: qsTr("Already installed") visible: modelData.installed - Accessible.role: Accessible.Paragraph - Accessible.name: text - Accessible.description: qsTr("Whether the file is already installed on your system") + + Label { + id: installedLabel + anchors.verticalCenter: removeButton.verticalCenter + anchors.right: removeButton.left + anchors.rightMargin: 15 + objectName: "installedLabel" + color: theme.textColor + text: qsTr("Already installed") + Accessible.role: Accessible.Paragraph + Accessible.name: text + Accessible.description: qsTr("Whether the file is already installed on your system") + } + + Button { + id: removeButton + contentItem: Text { + color: theme.textColor + text: "Remove" + } + anchors.right: parent.right + anchors.rightMargin: 20 + background: Rectangle { + opacity: .5 + border.color: theme.backgroundLightest + border.width: 1 + radius: 10 + color: theme.backgroundLight + } + onClicked: { + Download.removeModel(modelData.filename); + } + Accessible.role: Accessible.Button + Accessible.name: qsTr("Remove button") + Accessible.description: qsTr("Remove button to remove model from filesystem") + } } Item {