KTextAddons

managermodelvoskspeechtotext.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "managermodelvoskspeechtotext.h"
8#include "libvoskspeechtotext_debug.h"
9#include "speechtotext/speechtotextengineaccessmanager.h"
10#include "voskengineutils.h"
11#include <KLocalizedString>
12#include <QFile>
13#include <QJsonArray>
14#include <QJsonDocument>
15#include <QJsonObject>
16#include <QNetworkReply>
17#include <QNetworkRequest>
18
19ManagerModelVoskSpeechToText::ManagerModelVoskSpeechToText(QObject *parent)
20 : QObject{parent}
21{
22}
23
24ManagerModelVoskSpeechToText::~ManagerModelVoskSpeechToText() = default;
25
26ManagerModelVoskSpeechToText *ManagerModelVoskSpeechToText::self()
27{
28 static ManagerModelVoskSpeechToText s_self;
29 return &s_self;
30}
31
32void ManagerModelVoskSpeechToText::downloadListModels()
33{
34 const QUrl url = QUrl(VoskEngineUtils::defaultVoskRepository());
35 // qDebug() << " url " << url;
36 QNetworkReply *reply = TextSpeechToText::SpeechToTextEngineAccessManager::self()->networkManager()->get(QNetworkRequest(url));
37
38 connect(reply, &QNetworkReply::sslErrors, this, [](const QList<QSslError> &errors) {
39 qDebug() << "Ssl Error: " << errors;
40 });
41 connect(reply, &QNetworkReply::finished, this, [this, reply]() {
42 const auto readAll = reply->readAll();
43 // qDebug() << " readAll " << readAll;
44 parseListModel(QJsonDocument::fromJson(readAll).array());
45 reply->deleteLater();
46 });
49 Q_EMIT errorText(i18n("Error: Engine systems have detected suspicious traffic from your computer network. Please try your request again later."));
50 } else {
51 Q_EMIT errorText(i18n("Impossible to access to url: %1", VoskEngineUtils::defaultVoskRepository()));
52 }
53 });
54}
55
56void ManagerModelVoskSpeechToText::loadModelList(const QString &fileName)
57{
58 QFile f(fileName);
59 if (f.open(QIODevice::ReadOnly)) {
60 const QByteArray content = f.readAll();
61 f.close();
62 const QJsonDocument doc = QJsonDocument::fromJson(content);
63 const QJsonArray fields = doc.array();
64 parseListModel(fields);
65 } else {
66 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to open " << fileName;
67 }
68}
69
70void ManagerModelVoskSpeechToText::parseListModel(const QJsonArray &arrays)
71{
72 mSpeechToTextInfos.clear();
73 for (const QJsonValue &current : arrays) {
74 if (current.type() == QJsonValue::Object) {
75 VoskSpeechToTextInfo speechTextInfo;
76 const QJsonObject translatorObject = current.toObject();
77 speechTextInfo.parse(translatorObject);
78 if (speechTextInfo.isValid()) {
79 mSpeechToTextInfos.append(std::move(speechTextInfo));
80 }
81 } else {
82 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << " Problem during parsing" << current;
83 }
84 }
85 qCDebug(LIBVOSKSPEECHTOTEXT_LOG) << " mSpeechToTextInfos.count() " << mSpeechToTextInfos.count();
86 Q_EMIT downLoadModelListDone();
87}
88
89QVector<VoskSpeechToTextInfo> ManagerModelVoskSpeechToText::speechToTextInfos() const
90{
91 return mSpeechToTextInfos;
92}
93
94void ManagerModelVoskSpeechToText::setSpeechToTextInfos(const QVector<VoskSpeechToTextInfo> &newTranslators)
95{
96 mSpeechToTextInfos = newTranslators;
97}
98
99void ManagerModelVoskSpeechToText::downloadLanguage(const VoskDownloadLanguageJob::DownloadLanguageInfo &info)
100{
101 auto downloadJob = new VoskDownloadLanguageJob(this);
102 downloadJob->setInfo(info);
103 const QString url = info.url.toString();
104 connect(downloadJob, &VoskDownloadLanguageJob::errorText, this, &ManagerModelVoskSpeechToText::errorText);
105 connect(downloadJob, &VoskDownloadLanguageJob::extractDone, this, &ManagerModelVoskSpeechToText::extractDone);
106 connect(downloadJob, &VoskDownloadLanguageJob::downloadProgress, this, [this, url](qint64 bytesReceived, qint64 bytesTotal) {
107 ManagerModelVoskSpeechToText::ProgressInfo info;
108 info.bytesReceived = bytesReceived;
109 info.bytesTotal = bytesTotal;
110 info.languageName = url;
111 Q_EMIT progress(std::move(info));
112 });
113 downloadJob->start();
114}
115
116bool ManagerModelVoskSpeechToText::needDownloadModelList() const
117{
118 return mSpeechToTextInfos.isEmpty();
119}
120
121#include "moc_managermodelvoskspeechtotext.cpp"
QString i18n(const char *text, const TYPE &arg...)
QByteArray readAll()
QJsonArray array() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QNetworkReply * get(const QNetworkRequest &request)
void errorOccurred(QNetworkReply::NetworkError code)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.