feat(translate): add translation presets and custom instructions

This commit is contained in:
tri committed 2026-11-07 13:28:32 +07:00
1 parent cebce0915f
commit 18ba92a6e3
9 files changed
+172 -8

No files matched your search

+16 -2
View File
@@ -2,6 +2,7 @@
#include "apikeystore.h"
#include "qtpotatosettings.h"
#include "translationpresets.h"
#include <KLocalizedString>
@@ -34,7 +35,7 @@ constexpr int MaxAttempts = 3;
// depend on each other.
constexpr int ParallelRequests = 3;
// Bump when the prompt changes so old cached translations are not reused.
constexpr auto CacheVersion = "llm-v1";
constexpr auto CacheVersion = "llm-v2";
// "Vietnamese" -> "vi", so the file ends in ".vi.srt" like other players expect.
QString languageSuffix(const QString &language)
@@ -112,6 +113,15 @@ QStringList TranslationService::availableModels() const
return m_availableModels;
}
QVariantList TranslationService::translationPresets() const
{
QVariantList result;
for (const auto &preset : TranslationPresets::all()) {
result.append(QVariantMap{{u"id"_s, preset.id}, {u"name"_s, preset.name}, {u"guidance"_s, preset.guidance}});
}
return result;
}
bool TranslationService::isFetchingModels() const
{
return m_fetchingModels;
@@ -160,6 +170,8 @@ QCoro::Task<std::pair<LlmClient::Config, QString>> TranslationService::loadConfi
{},
settings->model().trimmed(),
settings->targetLanguage().trimmed(),
settings->translationPreset().trimmed(),
settings->customInstructions(),
};
if (!config.baseUrl.isValid() || config.baseUrl.scheme().isEmpty() || config.baseUrl.host().isEmpty()) {
@@ -235,11 +247,13 @@ QCoro::Task<> TranslationService::run(QUrl videoUrl, QVariantMap track)
co_return;
}
// Same subtitles, model and language give the same file, so it is translated once.
// Same subtitles, model, language, and prompt settings give the same file, so it is translated once.
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(CacheVersion);
hash.addData(config.model.toUtf8());
hash.addData(config.targetLanguage.toUtf8());
hash.addData(config.translationPreset.toUtf8());
hash.addData(config.customInstructions.toUtf8());
hash.addData(Subtitles::toSrt(cues).toUtf8());
const QString outputPath = cachePath(videoUrl, QString::fromLatin1(hash.result().toHex().left(12)), config.targetLanguage);