File indexing completed on 2024-12-22 04:28:22

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "managermodelvoskspeechtotext.h"
0008 #include "libvoskspeechtotext_debug.h"
0009 #include "speechtotext/speechtotextengineaccessmanager.h"
0010 #include "voskengineutils.h"
0011 #include <KLocalizedString>
0012 #include <QFile>
0013 #include <QJsonArray>
0014 #include <QJsonDocument>
0015 #include <QJsonObject>
0016 #include <QNetworkReply>
0017 #include <QNetworkRequest>
0018 
0019 ManagerModelVoskSpeechToText::ManagerModelVoskSpeechToText(QObject *parent)
0020     : QObject{parent}
0021 {
0022 }
0023 
0024 ManagerModelVoskSpeechToText::~ManagerModelVoskSpeechToText() = default;
0025 
0026 ManagerModelVoskSpeechToText *ManagerModelVoskSpeechToText::self()
0027 {
0028     static ManagerModelVoskSpeechToText s_self;
0029     return &s_self;
0030 }
0031 
0032 void ManagerModelVoskSpeechToText::downloadListModels()
0033 {
0034     const QUrl url = QUrl(VoskEngineUtils::defaultVoskRepository());
0035     // qDebug() << " url " << url;
0036     QNetworkReply *reply = TextSpeechToText::SpeechToTextEngineAccessManager::self()->networkManager()->get(QNetworkRequest(url));
0037 
0038     connect(reply, &QNetworkReply::sslErrors, this, [](const QList<QSslError> &errors) {
0039         qDebug() << "Ssl Error: " << errors;
0040     });
0041     connect(reply, &QNetworkReply::finished, this, [this, reply]() {
0042         const auto readAll = reply->readAll();
0043         // qDebug() << " readAll " << readAll;
0044         parseListModel(QJsonDocument::fromJson(readAll).array());
0045         reply->deleteLater();
0046     });
0047     connect(reply, &QNetworkReply::errorOccurred, this, [this](QNetworkReply::NetworkError error) {
0048         if (error == QNetworkReply::ServiceUnavailableError) {
0049             Q_EMIT errorText(i18n("Error: Engine systems have detected suspicious traffic from your computer network. Please try your request again later."));
0050         } else {
0051             Q_EMIT errorText(i18n("Impossible to access to url: %1", VoskEngineUtils::defaultVoskRepository()));
0052         }
0053     });
0054 }
0055 
0056 void ManagerModelVoskSpeechToText::loadModelList(const QString &fileName)
0057 {
0058     QFile f(fileName);
0059     if (f.open(QIODevice::ReadOnly)) {
0060         const QByteArray content = f.readAll();
0061         f.close();
0062         const QJsonDocument doc = QJsonDocument::fromJson(content);
0063         const QJsonArray fields = doc.array();
0064         parseListModel(fields);
0065     } else {
0066         qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to open " << fileName;
0067     }
0068 }
0069 
0070 void ManagerModelVoskSpeechToText::parseListModel(const QJsonArray &arrays)
0071 {
0072     mSpeechToTextInfos.clear();
0073     for (const QJsonValue &current : arrays) {
0074         if (current.type() == QJsonValue::Object) {
0075             VoskSpeechToTextInfo speechTextInfo;
0076             const QJsonObject translatorObject = current.toObject();
0077             speechTextInfo.parse(translatorObject);
0078             if (speechTextInfo.isValid()) {
0079                 mSpeechToTextInfos.append(std::move(speechTextInfo));
0080             }
0081         } else {
0082             qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << " Problem during parsing" << current;
0083         }
0084     }
0085     qCDebug(LIBVOSKSPEECHTOTEXT_LOG) << " mSpeechToTextInfos.count() " << mSpeechToTextInfos.count();
0086     Q_EMIT downLoadModelListDone();
0087 }
0088 
0089 QVector<VoskSpeechToTextInfo> ManagerModelVoskSpeechToText::speechToTextInfos() const
0090 {
0091     return mSpeechToTextInfos;
0092 }
0093 
0094 void ManagerModelVoskSpeechToText::setSpeechToTextInfos(const QVector<VoskSpeechToTextInfo> &newTranslators)
0095 {
0096     mSpeechToTextInfos = newTranslators;
0097 }
0098 
0099 void ManagerModelVoskSpeechToText::downloadLanguage(const VoskDownloadLanguageJob::DownloadLanguageInfo &info)
0100 {
0101     auto downloadJob = new VoskDownloadLanguageJob(this);
0102     downloadJob->setInfo(info);
0103     const QString url = info.url.toString();
0104     connect(downloadJob, &VoskDownloadLanguageJob::errorText, this, &ManagerModelVoskSpeechToText::errorText);
0105     connect(downloadJob, &VoskDownloadLanguageJob::extractDone, this, &ManagerModelVoskSpeechToText::extractDone);
0106     connect(downloadJob, &VoskDownloadLanguageJob::downloadProgress, this, [this, url](qint64 bytesReceived, qint64 bytesTotal) {
0107         ManagerModelVoskSpeechToText::ProgressInfo info;
0108         info.bytesReceived = bytesReceived;
0109         info.bytesTotal = bytesTotal;
0110         info.languageName = url;
0111         Q_EMIT progress(std::move(info));
0112     });
0113     downloadJob->start();
0114 }
0115 
0116 bool ManagerModelVoskSpeechToText::needDownloadModelList() const
0117 {
0118     return mSpeechToTextInfos.isEmpty();
0119 }
0120 
0121 #include "moc_managermodelvoskspeechtotext.cpp"