1
0
Fork 0
mirror of https://gitlab.com/niansa/anyproc.git synced 2025-03-06 20:49:24 +01:00

Added translation cache

This commit is contained in:
niansa 2023-04-18 17:22:03 +02:00
parent 47cc4411df
commit a528d0ba20

View file

@ -3,7 +3,9 @@
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <string_view>
#include <utility>
#include <justlm.hpp>
@ -142,6 +144,7 @@ public:
class Translator : PyEval {
LM::Inference::Savestate sv;
std::unordered_map<std::string_view, std::string> cache;
static inline LM::Inference::Params get_params() {
auto p = get_recommended_params();
@ -190,10 +193,26 @@ public:
}
std::string translate(std::string_view text, std::string_view language, const std::function<bool (float)> &on_append_tick = nullptr, const std::function<bool (const char *generated)>& on_generation_tick = nullptr) {
// Cache lookup
auto res = std::find_if(cache.begin(), cache.end(), [text] (const auto& o) {
return o.first == text;
});
if (res != cache.end()) {
return res->second;
}
// Restore savestate
if (sv.is_valid()) restore_savestate(sv);
else create_savestate(sv, on_append_tick);
// Run inference
auto fres = unescape(begin().expression(translation_exprgen(escape(text), escape(language)))
.run(on_append_tick, on_generation_tick));
// Add to cache
cache[text] = fres;
// Return final result;
return fres;
}
};