Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions gpt4all-chat/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void Download::downloadModel(const QString &modelFile)
= QString("ERROR: Could not open temp file: %1 %2").arg(tempFile->fileName()).arg(modelFile);
qWarning() << error;
clearRetry(modelFile);
ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadErrorRole, error);
ModelList::globalInstance()->updateDataByFilename(modelFile, {{ ModelList::DownloadErrorRole, error }});
return;
}
tempFile->flush();
Expand All @@ -128,7 +128,7 @@ void Download::downloadModel(const QString &modelFile)
return;
}

ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadingRole, true);
ModelList::globalInstance()->updateDataByFilename(modelFile, {{ ModelList::DownloadingRole, true }});
ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFile);
QString url = !info.url().isEmpty() ? info.url() : "http://gpt4all.io/models/gguf/" + modelFile;
Network::globalInstance()->sendDownloadStarted(modelFile);
Expand Down Expand Up @@ -166,7 +166,7 @@ void Download::cancelDownload(const QString &modelFile)
tempFile->deleteLater();
m_activeDownloads.remove(modelReply);

ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadingRole, false);
ModelList::globalInstance()->updateDataByFilename(modelFile, {{ ModelList::DownloadingRole, false }});
break;
}
}
Expand All @@ -188,7 +188,7 @@ void Download::installModel(const QString &modelFile, const QString &apiKey)
ModelList::globalInstance()->updateModelsFromDirectory();
}

ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::InstalledRole, true);
ModelList::globalInstance()->updateDataByFilename(modelFile, {{ ModelList::InstalledRole, true }});
}

void Download::removeModel(const QString &modelFile)
Expand All @@ -199,20 +199,28 @@ void Download::removeModel(const QString &modelFile)
incompleteFile.remove();
}

bool shouldRemoveInstalled = false;
QFile file(filePath);
if (file.exists()) {
const ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFile);
ModelList::globalInstance()->removeInstalled(info);
shouldRemoveInstalled = info.installed && !info.isClone() && (info.isDiscovered() || info.description() == "" /*indicates sideloaded*/);
if (shouldRemoveInstalled)
ModelList::globalInstance()->removeInstalled(info);
Network::globalInstance()->sendRemoveModel(modelFile);
file.remove();
}

ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::InstalledRole, false);
ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::BytesReceivedRole, 0);
ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::BytesTotalRole, 0);
ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::TimestampRole, 0);
ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::SpeedRole, QString());
ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadErrorRole, QString());
if (!shouldRemoveInstalled) {
QVector<QPair<int, QVariant>> data {
{ ModelList::InstalledRole, false },
{ ModelList::BytesReceivedRole, 0 },
{ ModelList::BytesTotalRole, 0 },
{ ModelList::TimestampRole, 0 },
{ ModelList::SpeedRole, QString() },
{ ModelList::DownloadErrorRole, QString() },
};
ModelList::globalInstance()->updateDataByFilename(modelFile, data);
}
}

void Download::handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
Expand Down Expand Up @@ -313,7 +321,7 @@ void Download::handleErrorOccurred(QNetworkReply::NetworkError code)
.arg(code)
.arg(modelReply->errorString());
qWarning() << error;
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, error);
ModelList::globalInstance()->updateDataByFilename(modelFilename, {{ ModelList::DownloadErrorRole, error }});
Network::globalInstance()->sendDownloadError(modelFilename, (int)code, modelReply->errorString());
cancelDownload(modelFilename);
}
Expand Down Expand Up @@ -352,10 +360,13 @@ void Download::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
else
speedText = QString::number(static_cast<double>(speed / (1024.0 * 1024.0)), 'f', 2) + " MB/s";

ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::BytesReceivedRole, currentBytesReceived);
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::BytesTotalRole, bytesTotal);
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::SpeedRole, speedText);
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::TimestampRole, currentUpdate);
QVector<QPair<int, QVariant>> data {
{ ModelList::BytesReceivedRole, currentBytesReceived },
{ ModelList::BytesTotalRole, bytesTotal },
{ ModelList::SpeedRole, speedText },
{ ModelList::TimestampRole, currentUpdate },
};
ModelList::globalInstance()->updateDataByFilename(modelFilename, data);
}

HashAndSaveFile::HashAndSaveFile()
Expand Down Expand Up @@ -457,8 +468,11 @@ void Download::handleModelDownloadFinished()
modelReply->deleteLater();
tempFile->deleteLater();
if (!hasRetry(modelFilename)) {
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadingRole, false);
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, errorString);
QVector<QPair<int, QVariant>> data {
{ ModelList::DownloadingRole, false },
{ ModelList::DownloadErrorRole, errorString },
};
ModelList::globalInstance()->updateDataByFilename(modelFilename, data);
}
return;
}
Expand All @@ -476,7 +490,7 @@ void Download::handleModelDownloadFinished()
}

// Notify that we are calculating hash
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::CalcHashRole, true);
ModelList::globalInstance()->updateDataByFilename(modelFilename, {{ ModelList::CalcHashRole, true }});
QByteArray hash = ModelList::globalInstance()->modelInfoByFilename(modelFilename).hash;
ModelInfo::HashAlgorithm hashAlgorithm = ModelList::globalInstance()->modelInfoByFilename(modelFilename).hashAlgorithm;
const QString saveFilePath = MySettings::globalInstance()->modelPath() + modelFilename;
Expand All @@ -492,19 +506,25 @@ void Download::handleHashAndSaveFinished(bool success, const QString &error,
Q_ASSERT(!tempFile->isOpen());
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
Network::globalInstance()->sendDownloadFinished(modelFilename, success);
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::CalcHashRole, false);

QVector<QPair<int, QVariant>> data {
{ ModelList::CalcHashRole, false },
{ ModelList::DownloadingRole, false },
};

modelReply->deleteLater();
tempFile->deleteLater();

ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadingRole, false);
if (!success) {
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, error);
data.append({ ModelList::DownloadErrorRole, error });
} else {
data.append({ ModelList::DownloadErrorRole, QString() });
ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFilename);
if (info.isDiscovered())
ModelList::globalInstance()->updateDiscoveredInstalled(info);
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, QString());
}

ModelList::globalInstance()->updateDataByFilename(modelFilename, data);
}

void Download::handleReadyRead()
Expand Down
Loading