Skip to content

Commit

Permalink
Model discovery.
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Treat <treat.adam@gmail.com>
  • Loading branch information
manyoso committed Mar 5, 2024
1 parent f2b4809 commit 83c76be
Show file tree
Hide file tree
Showing 9 changed files with 1,360 additions and 264 deletions.
32 changes: 22 additions & 10 deletions gpt4all-chat/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Download::downloadModel(const QString &modelFile)

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;
QString url = !info.url().isEmpty() ? info.url() : "http://gpt4all.io/models/gguf/" + modelFile;
Network::globalInstance()->sendDownloadStarted(modelFile);
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::User, modelFile);
Expand Down Expand Up @@ -201,6 +201,8 @@ void Download::removeModel(const QString &modelFile)

QFile file(filePath);
if (file.exists()) {
const ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFile);
ModelList::globalInstance()->removeInstalled(info);
Network::globalInstance()->sendRemoveModel(modelFile);
file.remove();
}
Expand Down Expand Up @@ -364,8 +366,8 @@ HashAndSaveFile::HashAndSaveFile()
m_hashAndSaveThread.start();
}

void HashAndSaveFile::hashAndSave(const QString &expectedHash, const QString &saveFilePath,
QFile *tempFile, QNetworkReply *modelReply)
void HashAndSaveFile::hashAndSave(const QString &expectedHash, QCryptographicHash::Algorithm a,
const QString &saveFilePath, QFile *tempFile, QNetworkReply *modelReply)
{
Q_ASSERT(!tempFile->isOpen());
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
Expand All @@ -379,13 +381,16 @@ void HashAndSaveFile::hashAndSave(const QString &expectedHash, const QString &sa
return;
}

QCryptographicHash hash(QCryptographicHash::Md5);
QCryptographicHash hash(a);
while(!tempFile->atEnd())
hash.addData(tempFile->read(16384));
if (hash.result().toHex() != expectedHash) {
if (hash.result().toHex() != expectedHash.toLatin1()) {
tempFile->close();
const QString error
= QString("ERROR: Download error MD5SUM did not match: %1 != %2 for %3").arg(hash.result().toHex()).arg(expectedHash).arg(modelFilename);
= QString("ERROR: Download error hash did not match: %1 != %2 for %3")
.arg(hash.result().toHex())
.arg(expectedHash.toLatin1())
.arg(modelFilename);
qWarning() << error;
tempFile->remove();
emit hashAndSaveFinished(false, error, tempFile, modelReply);
Expand Down Expand Up @@ -472,9 +477,12 @@ void Download::handleModelDownloadFinished()

// Notify that we are calculating hash
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::CalcHashRole, true);
QByteArray md5sum = ModelList::globalInstance()->modelInfoByFilename(modelFilename).md5sum;
QByteArray hash = ModelList::globalInstance()->modelInfoByFilename(modelFilename).hash;
ModelInfo::HashAlgorithm hashAlgorithm = ModelList::globalInstance()->modelInfoByFilename(modelFilename).hashAlgorithm;
const QString saveFilePath = MySettings::globalInstance()->modelPath() + modelFilename;
emit requestHashAndSave(md5sum, saveFilePath, tempFile, modelReply);
emit requestHashAndSave(hash,
(hashAlgorithm == ModelInfo::Md5 ? QCryptographicHash::Md5 : QCryptographicHash::Sha256),
saveFilePath, tempFile, modelReply);
}

void Download::handleHashAndSaveFinished(bool success, const QString &error,
Expand All @@ -489,10 +497,14 @@ void Download::handleHashAndSaveFinished(bool success, const QString &error,
tempFile->deleteLater();

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

void Download::handleReadyRead()
Expand Down
4 changes: 2 additions & 2 deletions gpt4all-chat/download.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HashAndSaveFile : public QObject
HashAndSaveFile();

public Q_SLOTS:
void hashAndSave(const QString &hash, const QString &saveFilePath,
void hashAndSave(const QString &hash, QCryptographicHash::Algorithm a, const QString &saveFilePath,
QFile *tempFile, QNetworkReply *modelReply);

Q_SIGNALS:
Expand Down Expand Up @@ -72,7 +72,7 @@ private Q_SLOTS:
Q_SIGNALS:
void releaseInfoChanged();
void hasNewerReleaseChanged();
void requestHashAndSave(const QString &hash, const QString &saveFilePath,
void requestHashAndSave(const QString &hash, QCryptographicHash::Algorithm a, const QString &saveFilePath,
QFile *tempFile, QNetworkReply *modelReply);

private:
Expand Down

0 comments on commit 83c76be

Please sign in to comment.