2023-04-18 21:10:06 -04:00
|
|
|
#include "download.h"
|
2023-05-02 20:31:17 -04:00
|
|
|
#include "network.h"
|
2023-06-22 15:44:49 -04:00
|
|
|
#include "modellist.h"
|
2023-04-18 21:10:06 -04:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QDir>
|
2023-04-23 11:28:17 -04:00
|
|
|
#include <QStandardPaths>
|
2023-04-27 13:52:24 -04:00
|
|
|
#include <QSettings>
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-06-27 14:13:42 -04:00
|
|
|
//#define USE_LOCAL_MODELSJSON
|
2023-06-27 13:56:16 -04:00
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
class MyDownload: public Download { };
|
|
|
|
Q_GLOBAL_STATIC(MyDownload, downloadInstance)
|
|
|
|
Download *Download::globalInstance()
|
|
|
|
{
|
|
|
|
return downloadInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
Download::Download()
|
|
|
|
: QObject(nullptr)
|
2023-04-23 19:43:20 -04:00
|
|
|
, m_hashAndSave(new HashAndSaveFile)
|
2023-04-18 21:10:06 -04:00
|
|
|
{
|
2023-04-23 19:43:20 -04:00
|
|
|
connect(this, &Download::requestHashAndSave, m_hashAndSave,
|
|
|
|
&HashAndSaveFile::hashAndSave, Qt::QueuedConnection);
|
|
|
|
connect(m_hashAndSave, &HashAndSaveFile::hashAndSaveFinished, this,
|
|
|
|
&Download::handleHashAndSaveFinished, Qt::QueuedConnection);
|
2023-04-24 17:52:19 -04:00
|
|
|
connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this,
|
|
|
|
&Download::handleSslErrors);
|
2023-06-22 15:44:49 -04:00
|
|
|
connect(ModelList::globalInstance(), &ModelList::localModelsPathChanged, this, &Download::updateModelList);
|
2023-04-18 21:10:06 -04:00
|
|
|
updateModelList();
|
2023-04-28 10:54:05 -04:00
|
|
|
updateReleaseNotes();
|
2023-05-02 18:24:16 -04:00
|
|
|
m_startTime = QDateTime::currentDateTime();
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-27 14:52:40 -04:00
|
|
|
bool operator==(const ModelInfo& lhs, const ModelInfo& rhs) {
|
|
|
|
return lhs.filename == rhs.filename && lhs.md5sum == rhs.md5sum;
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
bool operator==(const ReleaseInfo& lhs, const ReleaseInfo& rhs) {
|
|
|
|
return lhs.version == rhs.version;
|
|
|
|
}
|
|
|
|
|
2023-04-28 20:30:52 -04:00
|
|
|
bool compareVersions(const QString &a, const QString &b) {
|
|
|
|
QStringList aParts = a.split('.');
|
|
|
|
QStringList bParts = b.split('.');
|
|
|
|
|
|
|
|
for (int i = 0; i < std::min(aParts.size(), bParts.size()); ++i) {
|
|
|
|
int aInt = aParts[i].toInt();
|
|
|
|
int bInt = bParts[i].toInt();
|
|
|
|
|
|
|
|
if (aInt > bInt) {
|
|
|
|
return true;
|
|
|
|
} else if (aInt < bInt) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aParts.size() > bParts.size();
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
ReleaseInfo Download::releaseInfo() const
|
|
|
|
{
|
|
|
|
const QString currentVersion = QCoreApplication::applicationVersion();
|
|
|
|
if (m_releaseMap.contains(currentVersion))
|
|
|
|
return m_releaseMap.value(currentVersion);
|
|
|
|
return ReleaseInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Download::hasNewerRelease() const
|
|
|
|
{
|
|
|
|
const QString currentVersion = QCoreApplication::applicationVersion();
|
|
|
|
QList<QString> versions = m_releaseMap.keys();
|
|
|
|
std::sort(versions.begin(), versions.end(), compareVersions);
|
|
|
|
if (versions.isEmpty())
|
|
|
|
return false;
|
|
|
|
return compareVersions(versions.first(), currentVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Download::isFirstStart() const
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
settings.sync();
|
|
|
|
QString lastVersionStarted = settings.value("download/lastVersionStarted").toString();
|
|
|
|
bool first = lastVersionStarted != QCoreApplication::applicationVersion();
|
|
|
|
settings.setValue("download/lastVersionStarted", QCoreApplication::applicationVersion());
|
|
|
|
settings.sync();
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
void Download::updateModelList()
|
|
|
|
{
|
2023-06-27 13:56:16 -04:00
|
|
|
#if defined(USE_LOCAL_MODELSJSON)
|
|
|
|
QUrl jsonUrl("file://" + QDir::homePath() + "/dev/large_language_models/gpt4all/gpt4all-chat/metadata/models.json");
|
|
|
|
#else
|
2023-06-26 16:21:09 -04:00
|
|
|
QUrl jsonUrl("http://gpt4all.io/models/models.json");
|
2023-06-27 13:56:16 -04:00
|
|
|
#endif
|
2023-04-18 21:10:06 -04:00
|
|
|
QNetworkRequest request(jsonUrl);
|
2023-04-24 00:03:01 -04:00
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
2023-04-18 21:10:06 -04:00
|
|
|
QNetworkReply *jsonReply = m_networkManager.get(request);
|
2023-04-28 10:54:05 -04:00
|
|
|
connect(jsonReply, &QNetworkReply::finished, this, &Download::handleModelsJsonDownloadFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::updateReleaseNotes()
|
|
|
|
{
|
|
|
|
QUrl jsonUrl("http://gpt4all.io/meta/release.json");
|
|
|
|
QNetworkRequest request(jsonUrl);
|
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
|
|
|
QNetworkReply *jsonReply = m_networkManager.get(request);
|
|
|
|
connect(jsonReply, &QNetworkReply::finished, this, &Download::handleReleaseJsonDownloadFinished);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadModel(const QString &modelFile)
|
|
|
|
{
|
2023-06-22 15:44:49 -04:00
|
|
|
QFile *tempFile = new QFile(ModelList::globalInstance()->incompleteDownloadPath(modelFile));
|
2023-05-02 18:24:16 -04:00
|
|
|
QDateTime modTime = tempFile->fileTime(QFile::FileModificationTime);
|
|
|
|
bool success = tempFile->open(QIODevice::WriteOnly | QIODevice::Append);
|
2023-04-23 19:43:20 -04:00
|
|
|
qWarning() << "Opening temp file for writing:" << tempFile->fileName();
|
|
|
|
if (!success) {
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Could not open temp file: %1 %2").arg(tempFile->fileName()).arg(modelFile);
|
|
|
|
qWarning() << error;
|
|
|
|
ModelList::globalInstance()->updateData(modelFile, ModelList::DownloadErrorRole, error);
|
2023-04-23 19:43:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-06-22 15:44:49 -04:00
|
|
|
tempFile->flush();
|
2023-05-02 18:24:16 -04:00
|
|
|
size_t incomplete_size = tempFile->size();
|
|
|
|
if (incomplete_size > 0) {
|
2023-06-22 15:44:49 -04:00
|
|
|
bool success = tempFile->seek(incomplete_size);
|
|
|
|
if (!success) {
|
|
|
|
incomplete_size = 0;
|
|
|
|
success = tempFile->seek(incomplete_size);
|
|
|
|
Q_ASSERT(success);
|
2023-05-02 18:24:16 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-23 19:43:20 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
if (!ModelList::globalInstance()->contains(modelFile)) {
|
|
|
|
qWarning() << "ERROR: Could not find file:" << modelFile;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelList::globalInstance()->updateData(modelFile, ModelList::DownloadingRole, true);
|
|
|
|
ModelInfo info = ModelList::globalInstance()->modelInfo(modelFile);
|
2023-06-04 19:02:43 -04:00
|
|
|
QString url = !info.url.isEmpty() ? info.url : "http://gpt4all.io/models/" + modelFile;
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadStarted(modelFile);
|
2023-06-04 19:02:43 -04:00
|
|
|
QNetworkRequest request(url);
|
|
|
|
request.setAttribute(QNetworkRequest::User, modelFile);
|
2023-06-22 15:44:49 -04:00
|
|
|
request.setRawHeader("range", QString("bytes=%1-").arg(tempFile->pos()).toUtf8());
|
2023-04-24 00:05:06 -04:00
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
2023-04-18 21:10:06 -04:00
|
|
|
QNetworkReply *modelReply = m_networkManager.get(request);
|
|
|
|
connect(modelReply, &QNetworkReply::downloadProgress, this, &Download::handleDownloadProgress);
|
|
|
|
connect(modelReply, &QNetworkReply::finished, this, &Download::handleModelDownloadFinished);
|
2023-04-23 19:43:20 -04:00
|
|
|
connect(modelReply, &QNetworkReply::readyRead, this, &Download::handleReadyRead);
|
|
|
|
m_activeDownloads.insert(modelReply, tempFile);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Download::cancelDownload(const QString &modelFile)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_activeDownloads.size(); ++i) {
|
2023-04-23 19:43:20 -04:00
|
|
|
QNetworkReply *modelReply = m_activeDownloads.keys().at(i);
|
2023-04-18 21:10:06 -04:00
|
|
|
QUrl url = modelReply->request().url();
|
|
|
|
if (url.toString().endsWith(modelFile)) {
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadCanceled(modelFile);
|
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
// Disconnect the signals
|
|
|
|
disconnect(modelReply, &QNetworkReply::downloadProgress, this, &Download::handleDownloadProgress);
|
|
|
|
disconnect(modelReply, &QNetworkReply::finished, this, &Download::handleModelDownloadFinished);
|
|
|
|
|
|
|
|
modelReply->abort(); // Abort the download
|
|
|
|
modelReply->deleteLater(); // Schedule the reply for deletion
|
2023-04-23 19:43:20 -04:00
|
|
|
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = m_activeDownloads.value(modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->deleteLater();
|
|
|
|
m_activeDownloads.remove(modelReply);
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
ModelList::globalInstance()->updateData(modelFile, ModelList::DownloadingRole, false);
|
2023-04-18 21:10:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 20:12:15 -04:00
|
|
|
void Download::installModel(const QString &modelFile, const QString &apiKey)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!apiKey.isEmpty());
|
|
|
|
if (apiKey.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Network::globalInstance()->sendInstallModel(modelFile);
|
2023-06-22 15:44:49 -04:00
|
|
|
QString filePath = ModelList::globalInstance()->localModelsPath() + modelFile + ".txt";
|
2023-05-14 20:12:15 -04:00
|
|
|
QFile file(filePath);
|
|
|
|
if (file.open(QIODeviceBase::WriteOnly | QIODeviceBase::Text)) {
|
|
|
|
QTextStream stream(&file);
|
|
|
|
stream << apiKey;
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 09:32:01 -04:00
|
|
|
void Download::removeModel(const QString &modelFile)
|
|
|
|
{
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString filePath = ModelList::globalInstance()->localModelsPath() + modelFile;
|
|
|
|
QFile incompleteFile(ModelList::globalInstance()->incompleteDownloadPath(modelFile));
|
|
|
|
if (incompleteFile.exists()) {
|
|
|
|
incompleteFile.remove();
|
|
|
|
}
|
|
|
|
|
2023-05-16 09:32:01 -04:00
|
|
|
QFile file(filePath);
|
2023-06-22 15:44:49 -04:00
|
|
|
if (file.exists()) {
|
|
|
|
Network::globalInstance()->sendRemoveModel(modelFile);
|
|
|
|
file.remove();
|
2023-05-16 09:32:01 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
ModelList::globalInstance()->updateData(modelFile, ModelList::BytesReceivedRole, 0);
|
|
|
|
ModelList::globalInstance()->updateData(modelFile, ModelList::BytesTotalRole, 0);
|
|
|
|
ModelList::globalInstance()->updateData(modelFile, ModelList::TimestampRole, 0);
|
|
|
|
ModelList::globalInstance()->updateData(modelFile, ModelList::SpeedRole, QString());
|
|
|
|
ModelList::globalInstance()->updateData(modelFile, ModelList::DownloadErrorRole, QString());
|
2023-05-16 09:32:01 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 17:52:19 -04:00
|
|
|
void Download::handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
|
|
|
|
{
|
|
|
|
QUrl url = reply->request().url();
|
2023-06-02 15:46:41 -04:00
|
|
|
for (const auto &e : errors)
|
2023-04-24 17:52:19 -04:00
|
|
|
qWarning() << "ERROR: Received ssl error:" << e.errorString() << "for" << url;
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
void Download::handleModelsJsonDownloadFinished()
|
2023-04-18 21:10:06 -04:00
|
|
|
{
|
|
|
|
QNetworkReply *jsonReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!jsonReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QByteArray jsonData = jsonReply->readAll();
|
|
|
|
jsonReply->deleteLater();
|
2023-04-28 10:54:05 -04:00
|
|
|
parseModelsJsonFile(jsonData);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
void Download::parseModelsJsonFile(const QByteArray &jsonData)
|
2023-04-18 21:10:06 -04:00
|
|
|
{
|
|
|
|
QJsonParseError err;
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsonData, &err);
|
|
|
|
if (err.error != QJsonParseError::NoError) {
|
2023-06-22 15:44:49 -04:00
|
|
|
qWarning() << "ERROR: Couldn't parse: " << jsonData << err.errorString();
|
2023-04-18 21:10:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray jsonArray = document.array();
|
2023-05-09 12:09:49 -04:00
|
|
|
const QString currentVersion = QCoreApplication::applicationVersion();
|
2023-04-18 21:10:06 -04:00
|
|
|
|
|
|
|
for (const QJsonValue &value : jsonArray) {
|
|
|
|
QJsonObject obj = value.toObject();
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
QString modelName = obj["name"].toString();
|
2023-04-18 21:10:06 -04:00
|
|
|
QString modelFilename = obj["filename"].toString();
|
2023-04-20 09:32:51 -04:00
|
|
|
QString modelFilesize = obj["filesize"].toString();
|
2023-05-25 15:22:45 -04:00
|
|
|
QString requiresVersion = obj["requires"].toString();
|
2023-06-16 11:09:33 -04:00
|
|
|
QString deprecatedVersion = obj["deprecated"].toString();
|
2023-06-04 19:02:43 -04:00
|
|
|
QString url = obj["url"].toString();
|
2023-04-18 21:10:06 -04:00
|
|
|
QByteArray modelMd5sum = obj["md5sum"].toString().toLatin1().constData();
|
|
|
|
bool isDefault = obj.contains("isDefault") && obj["isDefault"] == QString("true");
|
2023-06-22 15:44:49 -04:00
|
|
|
bool disableGUI = obj.contains("disableGUI") && obj["disableGUI"] == QString("true");
|
2023-04-27 14:52:40 -04:00
|
|
|
QString description = obj["description"].toString();
|
2023-06-22 15:44:49 -04:00
|
|
|
QString order = obj["order"].toString();
|
|
|
|
int ramrequired = obj["ramrequired"].toString().toInt();
|
|
|
|
QString parameters = obj["parameters"].toString();
|
|
|
|
QString quant = obj["quant"].toString();
|
|
|
|
QString type = obj["type"].toString();
|
2023-04-27 14:52:40 -04:00
|
|
|
|
2023-06-16 11:09:33 -04:00
|
|
|
// If the currentVersion version is strictly less than required version, then continue
|
2023-05-25 15:22:45 -04:00
|
|
|
if (!requiresVersion.isEmpty()
|
|
|
|
&& requiresVersion != currentVersion
|
|
|
|
&& compareVersions(requiresVersion, currentVersion)) {
|
2023-05-09 12:09:49 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-16 11:09:33 -04:00
|
|
|
// If the current version is strictly greater than the deprecated version, then continue
|
|
|
|
if (!deprecatedVersion.isEmpty()
|
|
|
|
&& compareVersions(currentVersion, deprecatedVersion)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
modelFilesize = ModelList::toFileSize(modelFilesize.toULongLong());
|
|
|
|
|
|
|
|
if (!ModelList::globalInstance()->contains(modelFilename))
|
|
|
|
ModelList::globalInstance()->addModel(modelFilename);
|
|
|
|
|
|
|
|
if (!modelName.isEmpty())
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::NameRole, modelName);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::FilesizeRole, modelFilesize);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::Md5sumRole, modelMd5sum);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DefaultRole, isDefault);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DescriptionRole, description);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::RequiresVersionRole, requiresVersion);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DeprecatedVersionRole, deprecatedVersion);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::UrlRole, url);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DisableGUIRole, disableGUI);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::OrderRole, order);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::RamrequiredRole, ramrequired);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::ParametersRole, parameters);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::QuantRole, quant);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::TypeRole, type);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString chatGPTDesc = tr("<ul><li>Requires personal OpenAI API key.</li><li>WARNING: Will send"
|
|
|
|
" your chats to OpenAI!</li><li>Your API key will be stored on disk</li><li>Will only be used"
|
|
|
|
" to communicate with OpenAI</li><li>You can apply for an API key"
|
|
|
|
" <a href=\"https://platform.openai.com/account/api-keys\">here.</a></li>");
|
2023-05-14 20:12:15 -04:00
|
|
|
|
|
|
|
{
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString modelFilename = "chatgpt-gpt-3.5-turbo.txt";
|
|
|
|
if (!ModelList::globalInstance()->contains(modelFilename))
|
|
|
|
ModelList::globalInstance()->addModel(modelFilename);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::NameRole, "ChatGPT-3.5 Turbo");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::FilesizeRole, "minimal");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::ChatGPTRole, true);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DescriptionRole,
|
|
|
|
tr("<strong>OpenAI's ChatGPT model GPT-3.5 Turbo</strong><br>") + chatGPTDesc);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::RequiresVersionRole, "2.4.2");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::OrderRole, "ca");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::RamrequiredRole, 0);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::ParametersRole, "?");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::QuantRole, "NA");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::TypeRole, "GPT");
|
2023-05-14 20:12:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString modelFilename = "chatgpt-gpt-4.txt";
|
|
|
|
if (!ModelList::globalInstance()->contains(modelFilename))
|
|
|
|
ModelList::globalInstance()->addModel(modelFilename);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::NameRole, "ChatGPT-4");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::FilesizeRole, "minimal");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::ChatGPTRole, true);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DescriptionRole,
|
|
|
|
tr("<strong>OpenAI's ChatGPT model GPT-4</strong><br>") + chatGPTDesc);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::RequiresVersionRole, "2.4.2");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::OrderRole, "cb");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::RamrequiredRole, 0);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::ParametersRole, "?");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::QuantRole, "NA");
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::TypeRole, "GPT");
|
2023-05-14 20:12:15 -04:00
|
|
|
}
|
2023-06-26 17:50:11 -04:00
|
|
|
|
|
|
|
if (ModelList::globalInstance()->installedModels()->count()) {
|
|
|
|
const QString firstModel =
|
|
|
|
ModelList::globalInstance()->installedModels()->firstFilename();
|
|
|
|
QSettings settings;
|
|
|
|
settings.sync();
|
|
|
|
settings.setValue("defaultModel", firstModel);
|
|
|
|
settings.sync();
|
|
|
|
}
|
2023-06-26 20:59:46 -04:00
|
|
|
|
|
|
|
ModelList::globalInstance()->updateModelHasNames();
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
void Download::handleReleaseJsonDownloadFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *jsonReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!jsonReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QByteArray jsonData = jsonReply->readAll();
|
|
|
|
jsonReply->deleteLater();
|
|
|
|
parseReleaseJsonFile(jsonData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::parseReleaseJsonFile(const QByteArray &jsonData)
|
|
|
|
{
|
|
|
|
QJsonParseError err;
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsonData, &err);
|
|
|
|
if (err.error != QJsonParseError::NoError) {
|
2023-06-22 15:44:49 -04:00
|
|
|
qWarning() << "ERROR: Couldn't parse: " << jsonData << err.errorString();
|
2023-04-28 10:54:05 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray jsonArray = document.array();
|
|
|
|
|
|
|
|
m_releaseMap.clear();
|
|
|
|
for (const QJsonValue &value : jsonArray) {
|
|
|
|
QJsonObject obj = value.toObject();
|
|
|
|
|
|
|
|
QString version = obj["version"].toString();
|
|
|
|
QString notes = obj["notes"].toString();
|
|
|
|
QString contributors = obj["contributors"].toString();
|
|
|
|
ReleaseInfo releaseInfo;
|
|
|
|
releaseInfo.version = version;
|
|
|
|
releaseInfo.notes = notes;
|
|
|
|
releaseInfo.contributors = contributors;
|
|
|
|
m_releaseMap.insert(version, releaseInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit hasNewerReleaseChanged();
|
|
|
|
emit releaseInfoChanged();
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
void Download::handleErrorOccurred(QNetworkReply::NetworkError code)
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
2023-06-04 19:02:43 -04:00
|
|
|
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Network error occurred attempting to download %1 code: %2 errorString %3")
|
|
|
|
.arg(modelFilename)
|
|
|
|
.arg(code)
|
|
|
|
.arg(modelReply->errorString());
|
|
|
|
qWarning() << error;
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadErrorRole, error);
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadError(modelFilename, (int)code, modelReply->errorString());
|
|
|
|
cancelDownload(modelFilename);
|
|
|
|
}
|
2023-04-28 10:54:05 -04:00
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
void Download::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = m_activeDownloads.value(modelReply);
|
|
|
|
if (!tempFile)
|
|
|
|
return;
|
|
|
|
QString contentRange = modelReply->rawHeader("content-range");
|
|
|
|
if (contentRange.contains("/")) {
|
|
|
|
QString contentTotalSize = contentRange.split("/").last();
|
|
|
|
bytesTotal = contentTotalSize.toLongLong();
|
|
|
|
}
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
|
|
|
const qint64 lastUpdate = ModelList::globalInstance()->data(modelFilename, ModelList::TimestampRole).toLongLong();
|
|
|
|
const qint64 currentUpdate = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
if (currentUpdate - lastUpdate < 1000)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const qint64 lastBytesReceived = ModelList::globalInstance()->data(modelFilename, ModelList::BytesReceivedRole).toLongLong();
|
|
|
|
const qint64 currentBytesReceived = tempFile->pos();
|
|
|
|
|
|
|
|
qint64 timeDifference = currentUpdate - lastUpdate;
|
|
|
|
qint64 bytesDifference = currentBytesReceived - lastBytesReceived;
|
|
|
|
qint64 speed = (bytesDifference / timeDifference) * 1000; // bytes per second
|
|
|
|
QString speedText;
|
|
|
|
if (speed < 1024)
|
|
|
|
speedText = QString::number(static_cast<double>(speed), 'f', 2) + " B/s";
|
|
|
|
else if (speed < 1024 * 1024)
|
|
|
|
speedText = QString::number(static_cast<double>(speed / 1024.0), 'f', 2) + " KB/s";
|
|
|
|
else
|
|
|
|
speedText = QString::number(static_cast<double>(speed / (1024.0 * 1024.0)), 'f', 2) + " MB/s";
|
|
|
|
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::BytesReceivedRole, currentBytesReceived);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::BytesTotalRole, bytesTotal);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::SpeedRole, speedText);
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::TimestampRole, currentUpdate);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
HashAndSaveFile::HashAndSaveFile()
|
|
|
|
: QObject(nullptr)
|
2023-04-18 21:10:06 -04:00
|
|
|
{
|
2023-04-23 19:43:20 -04:00
|
|
|
moveToThread(&m_hashAndSaveThread);
|
|
|
|
m_hashAndSaveThread.setObjectName("hashandsave thread");
|
|
|
|
m_hashAndSaveThread.start();
|
|
|
|
}
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
void HashAndSaveFile::hashAndSave(const QString &expectedHash, const QString &saveFilePath,
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply)
|
2023-04-23 19:43:20 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(!tempFile->isOpen());
|
2023-06-04 19:02:43 -04:00
|
|
|
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
// Reopen the tempFile for hashing
|
2023-05-02 18:24:16 -04:00
|
|
|
if (!tempFile->open(QIODevice::ReadOnly)) {
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Could not open temp file for hashing: %1 %2").arg(tempFile->fileName()).arg(modelFilename);
|
|
|
|
qWarning() << error;
|
|
|
|
emit hashAndSaveFinished(false, error, tempFile, modelReply);
|
2023-04-18 21:10:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Md5);
|
2023-04-23 19:43:20 -04:00
|
|
|
while(!tempFile->atEnd())
|
|
|
|
hash.addData(tempFile->read(16384));
|
|
|
|
if (hash.result().toHex() != expectedHash) {
|
|
|
|
tempFile->close();
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Download error MD5SUM did not match: %1 != %2 for %3").arg(hash.result().toHex()).arg(expectedHash).arg(modelFilename);
|
|
|
|
qWarning() << error;
|
2023-05-02 18:24:16 -04:00
|
|
|
tempFile->remove();
|
2023-06-22 15:44:49 -04:00
|
|
|
emit hashAndSaveFinished(false, error, tempFile, modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file save needs the tempFile closed
|
|
|
|
tempFile->close();
|
|
|
|
|
2023-04-26 20:57:25 -04:00
|
|
|
// Attempt to *move* the verified tempfile into place - this should be atomic
|
|
|
|
// but will only work if the destination is on the same filesystem
|
|
|
|
if (tempFile->rename(saveFilePath)) {
|
2023-06-22 15:44:49 -04:00
|
|
|
emit hashAndSaveFinished(true, QString(), tempFile, modelReply);
|
2023-04-26 20:57:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
// Reopen the tempFile for copying
|
2023-05-02 18:24:16 -04:00
|
|
|
if (!tempFile->open(QIODevice::ReadOnly)) {
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Could not open temp file at finish: %1 %2").arg(tempFile->fileName()).arg(modelFilename);
|
|
|
|
qWarning() << error;
|
|
|
|
emit hashAndSaveFinished(false, error, tempFile, modelReply);
|
2023-04-18 21:10:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the model file to disk
|
2023-04-23 19:43:20 -04:00
|
|
|
QFile file(saveFilePath);
|
2023-04-18 21:10:06 -04:00
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
2023-04-23 19:43:20 -04:00
|
|
|
QByteArray buffer;
|
|
|
|
while (!tempFile->atEnd()) {
|
|
|
|
buffer = tempFile->read(16384);
|
|
|
|
file.write(buffer);
|
|
|
|
}
|
2023-04-18 21:10:06 -04:00
|
|
|
file.close();
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->close();
|
2023-06-22 15:44:49 -04:00
|
|
|
emit hashAndSaveFinished(true, QString(), tempFile, modelReply);
|
2023-04-23 11:28:17 -04:00
|
|
|
} else {
|
|
|
|
QFile::FileError error = file.error();
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString errorString
|
2023-06-26 09:35:29 -04:00
|
|
|
= QString("ERROR: Could not save model to location: %1 failed with code %1").arg(saveFilePath).arg(error);
|
2023-06-22 15:44:49 -04:00
|
|
|
qWarning() << errorString;
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->close();
|
2023-06-22 15:44:49 -04:00
|
|
|
emit hashAndSaveFinished(false, errorString, tempFile, modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::handleModelDownloadFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
2023-06-04 19:02:43 -04:00
|
|
|
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = m_activeDownloads.value(modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
m_activeDownloads.remove(modelReply);
|
|
|
|
|
|
|
|
if (modelReply->error()) {
|
2023-06-26 09:35:29 -04:00
|
|
|
const QString errorString
|
|
|
|
= QString("ERROR: Downloading failed with code %1 \"%2\"").arg(modelReply->error()).arg(modelReply->errorString());
|
|
|
|
qWarning() << errorString;
|
2023-04-23 11:28:17 -04:00
|
|
|
modelReply->deleteLater();
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->deleteLater();
|
2023-06-22 15:44:49 -04:00
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadingRole, false);
|
2023-06-26 09:35:29 -04:00
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadErrorRole, errorString);
|
2023-04-23 11:28:17 -04:00
|
|
|
return;
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
// The hash and save needs the tempFile closed
|
|
|
|
tempFile->close();
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
if (!ModelList::globalInstance()->contains(modelFilename)) {
|
|
|
|
qWarning() << "ERROR: downloading no such file:" << modelFilename;
|
|
|
|
modelReply->deleteLater();
|
|
|
|
tempFile->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
2023-04-23 19:43:20 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
// Notify that we are calculating hash
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::CalcHashRole, true);
|
|
|
|
QByteArray md5sum = ModelList::globalInstance()->modelInfo(modelFilename).md5sum;
|
|
|
|
const QString saveFilePath = ModelList::globalInstance()->localModelsPath() + modelFilename;
|
|
|
|
emit requestHashAndSave(md5sum, saveFilePath, tempFile, modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
void Download::handleHashAndSaveFinished(bool success, const QString &error,
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply)
|
2023-04-23 19:43:20 -04:00
|
|
|
{
|
|
|
|
// The hash and save should send back with tempfile closed
|
|
|
|
Q_ASSERT(!tempFile->isOpen());
|
2023-06-04 19:02:43 -04:00
|
|
|
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadFinished(modelFilename, success);
|
2023-06-22 15:44:49 -04:00
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::CalcHashRole, false);
|
2023-04-23 19:43:20 -04:00
|
|
|
modelReply->deleteLater();
|
|
|
|
tempFile->deleteLater();
|
2023-06-22 15:44:49 -04:00
|
|
|
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadingRole, false);
|
|
|
|
if (!success)
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadErrorRole, error);
|
|
|
|
else
|
|
|
|
ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadErrorRole, QString());
|
2023-04-23 19:43:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Download::handleReadyRead()
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = m_activeDownloads.value(modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
QByteArray buffer;
|
|
|
|
while (!modelReply->atEnd()) {
|
|
|
|
buffer = modelReply->read(16384);
|
|
|
|
tempFile->write(buffer);
|
|
|
|
}
|
2023-05-02 18:24:16 -04:00
|
|
|
tempFile->flush();
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|