Skip to content

Commit

Permalink
Fix settings translations (#2690)
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 Jul 19, 2024
1 parent 56d5a23 commit d7f7c36
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 62 deletions.
40 changes: 6 additions & 34 deletions gpt4all-chat/modellist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,45 +526,17 @@ QString ModelList::incompleteDownloadPath(const QString &modelFile)
return MySettings::globalInstance()->modelPath() + "incomplete-" + modelFile;
}

const QList<ModelInfo> ModelList::exportModelList() const
const QList<ModelInfo> ModelList::selectableModelList() const
{
// FIXME: This needs to be kept in sync with m_selectableModels so should probably be merged
QMutexLocker locker(&m_mutex);
QList<ModelInfo> infos;
for (ModelInfo *info : m_models)
if (info->installed)
if (info->installed && !info->isEmbeddingModel)
infos.append(*info);
return infos;
}

const QList<QString> ModelList::userDefaultModelList() const
{
QMutexLocker locker(&m_mutex);

const QString userDefaultModelName = MySettings::globalInstance()->userDefaultModel();
QList<QString> models;
bool foundUserDefault = false;
for (ModelInfo *info : m_models) {

// Only installed chat models are suitable as a default
if (!info->installed || info->isEmbeddingModel)
continue;

if (info->id() == userDefaultModelName) {
foundUserDefault = true;
models.prepend(info->name());
} else {
models.append(info->name());
}
}

const QString defaultId = "Application default";
if (foundUserDefault)
models.append(defaultId);
else
models.prepend(defaultId);
return models;
}

ModelInfo ModelList::defaultModelInfo() const
{
QMutexLocker locker(&m_mutex);
Expand Down Expand Up @@ -681,7 +653,7 @@ void ModelList::addModel(const QString &id)
m_mutex.unlock();
endInsertRows();

emit userDefaultModelListChanged();
emit selectableModelListChanged();
}

void ModelList::changeId(const QString &oldId, const QString &newId)
Expand Down Expand Up @@ -1029,7 +1001,7 @@ void ModelList::updateData(const QString &id, const QVector<QPair<int, QVariant>
m_installedModels->invalidate();
m_downloadableModels->invalidate();

emit userDefaultModelListChanged();
emit selectableModelListChanged();
}

void ModelList::resortModel()
Expand Down Expand Up @@ -1169,7 +1141,7 @@ void ModelList::removeInternal(const ModelInfo &model)
delete info;
}
endRemoveRows();
emit userDefaultModelListChanged();
emit selectableModelListChanged();
MySettings::globalInstance()->eraseModel(model);
}

Expand Down
7 changes: 3 additions & 4 deletions gpt4all-chat/modellist.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class ModelList : public QAbstractListModel
Q_PROPERTY(InstalledModels* installedModels READ installedModels NOTIFY installedModelsChanged)
Q_PROPERTY(InstalledModels* selectableModels READ selectableModels NOTIFY selectableModelsChanged)
Q_PROPERTY(DownloadableModels* downloadableModels READ downloadableModels NOTIFY downloadableModelsChanged)
Q_PROPERTY(QList<QString> userDefaultModelList READ userDefaultModelList NOTIFY userDefaultModelListChanged)
Q_PROPERTY(QList<ModelInfo> selectableModelList READ selectableModelList NOTIFY selectableModelListChanged)
Q_PROPERTY(bool asyncModelRequestOngoing READ asyncModelRequestOngoing NOTIFY asyncModelRequestOngoingChanged)
Q_PROPERTY(int discoverLimit READ discoverLimit WRITE setDiscoverLimit NOTIFY discoverLimitChanged)
Q_PROPERTY(int discoverSortDirection READ discoverSortDirection WRITE setDiscoverSortDirection NOTIFY discoverSortDirectionChanged)
Expand Down Expand Up @@ -408,8 +408,7 @@ class ModelList : public QAbstractListModel
void addModel(const QString &id);
void changeId(const QString &oldId, const QString &newId);

const QList<ModelInfo> exportModelList() const;
const QList<QString> userDefaultModelList() const;
const QList<ModelInfo> selectableModelList() const;

InstalledModels *installedModels() const { return m_installedModels; }
InstalledModels *selectableModels() const { return m_selectableModels; }
Expand Down Expand Up @@ -452,7 +451,7 @@ class ModelList : public QAbstractListModel
void installedModelsChanged();
void selectableModelsChanged();
void downloadableModelsChanged();
void userDefaultModelListChanged();
void selectableModelListChanged();
void asyncModelRequestOngoingChanged();
void discoverLimitChanged();
void discoverSortDirectionChanged();
Expand Down
8 changes: 4 additions & 4 deletions gpt4all-chat/mysettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static const bool forceMetal = false;
static const bool networkIsActive = false;
static const bool networkUsageStatsActive = false;
static const QString device = "Auto";
static const QString languageAndLocale = "Default";
static const QString languageAndLocale = "System Locale";

} // namespace defaults

Expand Down Expand Up @@ -91,7 +91,7 @@ static QString defaultLocalModelsPath()

static QStringList getDevices(bool skipKompute = false)
{
QStringList deviceList { "Auto" };
QStringList deviceList;
#if defined(Q_OS_MAC) && defined(__aarch64__)
deviceList << "Metal";
#else
Expand Down Expand Up @@ -121,7 +121,7 @@ static QString getUiLanguage(const QString directory, const QString fileName)

static QStringList getUiLanguages(const QString &modelPath)
{
QStringList languageList( { QObject::tr("Default") } );
QStringList languageList;

// Add the language translations from model path files first which is used by translation developers
// to load translations in progress without having to rebuild all of GPT4All from source
Expand Down Expand Up @@ -627,7 +627,7 @@ void MySettings::setLanguageAndLocale(const QString &bcp47Name)
// to either the default which is the system locale or the one explicitly set by the user previously.
QLocale locale;
const QString l = languageAndLocale();
if (l == "Default")
if (l == "System Locale")
locale = QLocale::system();
else
locale = QLocale(l);
Expand Down
95 changes: 78 additions & 17 deletions gpt4all-chat/qml/ApplicationSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,41 @@ MySettingsTab {
Layout.maximumWidth: 200
Layout.fillWidth: false
Layout.alignment: Qt.AlignRight
model: MySettings.uiLanguages
Accessible.name: fontLabel.text
Accessible.description: fontLabel.helpText
model: ListModel {
Component.onCompleted: {
for (var i = 0; i < MySettings.uiLanguages.length; ++i)
append({"text": MySettings.uiLanguages[i]});
languageBox.updateModel();
}
ListElement { text: qsTr("System Locale") }
}

Accessible.name: languageLabel.text
Accessible.description: languageLabel.helpText
function updateModel() {
languageBox.currentIndex = languageBox.indexOfValue(MySettings.languageAndLocale);
// This usage of 'System Locale' should not be translated
// FIXME: Make this refer to a string literal variable accessed by both QML and C++
if (MySettings.languageAndLocale === "System Locale")
languageBox.currentIndex = 0
else
languageBox.currentIndex = languageBox.indexOfValue(MySettings.languageAndLocale);
}
Component.onCompleted: {
languageBox.updateModel()
}
onActivated: {
MySettings.languageAndLocale = languageBox.currentText
// This usage of 'System Locale' should not be translated
// FIXME: Make this refer to a string literal variable accessed by both QML and C++
if (languageBox.currentIndex === 0)
MySettings.languageAndLocale = "System Locale";
else
MySettings.languageAndLocale = languageBox.currentText;
}
}
MySettingsLabel {
id: deviceLabel
text: qsTr("Device")
helpText: qsTr('The compute device used for text generation. "Auto" uses Vulkan or Metal.')
helpText: qsTr('The compute device used for text generation.')
Layout.row: 5
Layout.column: 0
}
Expand All @@ -207,11 +225,24 @@ MySettingsTab {
Layout.maximumWidth: 400
Layout.fillWidth: false
Layout.alignment: Qt.AlignRight
model: MySettings.deviceList
model: ListModel {
Component.onCompleted: {
for (var i = 0; i < MySettings.deviceList.length; ++i)
append({"text": MySettings.deviceList[i]});
deviceBox.updateModel();
}
ListElement { text: qsTr("Application default") }
}

Accessible.name: deviceLabel.text
Accessible.description: deviceLabel.helpText
function updateModel() {
deviceBox.currentIndex = deviceBox.indexOfValue(MySettings.device);
// This usage of 'Auto' should not be translated
// FIXME: Make this refer to a string literal variable accessed by both QML and C++
if (MySettings.device === "Auto")
deviceBox.currentIndex = 0
else
deviceBox.currentIndex = deviceBox.indexOfValue(MySettings.device);
}
Component.onCompleted: {
deviceBox.updateModel();
Expand All @@ -223,7 +254,12 @@ MySettingsTab {
}
}
onActivated: {
MySettings.device = deviceBox.currentText;
// This usage of 'Auto' should not be translated
// FIXME: Make this refer to a string literal variable accessed by both QML and C++
if (deviceBox.currentIndex === 0)
MySettings.device = "Auto";
else
MySettings.device = deviceBox.currentText;
}
}
MySettingsLabel {
Expand All @@ -234,29 +270,54 @@ MySettingsTab {
Layout.column: 0
}
MyComboBox {
id: comboBox
id: defaultModelBox
Layout.row: 6
Layout.column: 2
Layout.minimumWidth: 400
Layout.maximumWidth: 400
Layout.alignment: Qt.AlignRight
model: ModelList.userDefaultModelList
model: ListModel {
id: defaultModelBoxModel
Component.onCompleted: {
defaultModelBox.rebuildModel()
}
}
Accessible.name: defaultModelLabel.text
Accessible.description: defaultModelLabel.helpText
function rebuildModel() {
defaultModelBoxModel.clear();
defaultModelBoxModel.append({"text": qsTr("Application default")});
for (var i = 0; i < ModelList.selectableModelList.length; ++i)
defaultModelBoxModel.append({"text": ModelList.selectableModelList[i].name});
defaultModelBox.updateModel();
}
function updateModel() {
comboBox.currentIndex = comboBox.indexOfValue(MySettings.userDefaultModel);
// This usage of 'Application default' should not be translated
// FIXME: Make this refer to a string literal variable accessed by both QML and C++
if (MySettings.userDefaultModel === "Application default")
defaultModelBox.currentIndex = 0
else
defaultModelBox.currentIndex = defaultModelBox.indexOfValue(MySettings.userDefaultModel);
}
Component.onCompleted: {
comboBox.updateModel()
onActivated: {
// This usage of 'Application default' should not be translated
// FIXME: Make this refer to a string literal variable accessed by both QML and C++
if (defaultModelBox.currentIndex === 0)
MySettings.userDefaultModel = "Application default";
else
MySettings.userDefaultModel = defaultModelBox.currentText;
}
Connections {
target: MySettings
function onUserDefaultModelChanged() {
comboBox.updateModel()
defaultModelBox.updateModel()
}
}
onActivated: {
MySettings.userDefaultModel = comboBox.currentText
Connections {
target: ModelList
function onSelectableModelListChanged() {
defaultModelBox.rebuildModel()
}
}
}
MySettingsLabel {
Expand Down
9 changes: 6 additions & 3 deletions gpt4all-chat/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ void Server::start()
if (!MySettings::globalInstance()->serverChat())
return QHttpServerResponse(QHttpServerResponder::StatusCode::Unauthorized);

const QList<ModelInfo> modelList = ModelList::globalInstance()->exportModelList();
const QList<ModelInfo> modelList = ModelList::globalInstance()->selectableModelList();
QJsonObject root;
root.insert("object", "list");
QJsonArray data;
for (const ModelInfo &info : modelList) {
Q_ASSERT(info.installed);
if (!info.installed)
continue;
data.append(modelToJson(info));
Expand All @@ -116,9 +117,10 @@ void Server::start()
if (!MySettings::globalInstance()->serverChat())
return QHttpServerResponse(QHttpServerResponder::StatusCode::Unauthorized);

const QList<ModelInfo> modelList = ModelList::globalInstance()->exportModelList();
const QList<ModelInfo> modelList = ModelList::globalInstance()->selectableModelList();
QJsonObject object;
for (const ModelInfo &info : modelList) {
Q_ASSERT(info.installed);
if (!info.installed)
continue;

Expand Down Expand Up @@ -232,8 +234,9 @@ QHttpServerResponse Server::handleCompletionRequest(const QHttpServerRequest &re

const QString modelRequested = body["model"].toString();
ModelInfo modelInfo = ModelList::globalInstance()->defaultModelInfo();
const QList<ModelInfo> modelList = ModelList::globalInstance()->exportModelList();
const QList<ModelInfo> modelList = ModelList::globalInstance()->selectableModelList();
for (const ModelInfo &info : modelList) {
Q_ASSERT(info.installed);
if (!info.installed)
continue;
if (modelRequested == info.name() || modelRequested == info.filename()) {
Expand Down

0 comments on commit d7f7c36

Please sign in to comment.