From 62fe173b779502ec556ae30e2e20cbb2b4fe3f76 Mon Sep 17 00:00:00 2001 From: niansa Date: Sun, 30 Apr 2023 00:52:12 +0200 Subject: [PATCH] Made status texts configurable --- example_config.txt | 1 + example_texts.txt | 7 ++++++ explained_config.txt | 3 +++ main.cpp | 54 ++++++++++++++++++++++++++++++++++++-------- 4 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 example_texts.txt diff --git a/example_config.txt b/example_config.txt index 0562bf5..d415193 100644 --- a/example_config.txt +++ b/example_config.txt @@ -2,6 +2,7 @@ token MTA0MDYxMTQzNjUwNzk1OTMyNw.Gl_iMU.jVVM3bRqBJVi8ORVpWHquOivlASGJpRySt8qFg # The following parameters are set to their defaults here and can be ommited models_dir models +texts_file none language EN threads_only true live_edit false diff --git a/example_texts.txt b/example_texts.txt new file mode 100644 index 0000000..a5214ae --- /dev/null +++ b/example_texts.txt @@ -0,0 +1,7 @@ +# The following parameters are set to their defaults here and can be ommited +please_wait Please wait... +thread_create_fail Error: I couldn't create a thread here. Do I have enough permissions? +model_missing Error: The model that was used in this thread could no longer be found. +timeout Error: Timeout + +translated false diff --git a/explained_config.txt b/explained_config.txt index d5d9c4d..7f3faec 100644 --- a/explained_config.txt +++ b/explained_config.txt @@ -5,6 +5,9 @@ token MTA0MDYxMTQzNjUwNzk1OTMyNw.Gl_iMU.jVVM3bRqBJVi8ORVpWHquOivlASGJpRySt8qFg # Directory the models are located in. For example, see example_models/ models_dir models +# File containing status texts. For example, see example_texts.txt +texts_file none + # Language everything is translated to (will be disabled if set to "EN" anyways) language EN diff --git a/main.cpp b/main.cpp index 6216369..8beaacf 100644 --- a/main.cpp +++ b/main.cpp @@ -69,6 +69,13 @@ public: const ModelConfig *model_config; bool instruct_mode = false; }; + struct Texts { + std::string please_wait = "Please wait...", + thread_create_fail = "Error: I couldn't create a thread here. Do I have enough permissions?", + model_missing = "Error: The model that was used in this thread could no longer be found.", + timeout = "Error: Timeout"; + bool translated = false; + }; struct Configuration { std::string token, language = "EN", @@ -76,7 +83,8 @@ public: translation_model = "none", prompt_file = "none", instruct_prompt_file = "none", - models_dir = "models"; + models_dir = "models", + texts_file = "none"; unsigned ctx_size = 1012, pool_size = 2, timeout = 120, @@ -97,13 +105,7 @@ private: const Configuration& config; const std::unordered_map& model_configs; - struct Texts { - std::string please_wait = "Please wait...", - thread_create_fail = "Error: I couldn't create a thread here. Do I have enough permissions?", - model_missing = "Error: The model that was used in this thread could no longer be found.", - timeout = "Error: Timeout"; - bool translated = false; - } texts; + Texts texts; inline static bool show_console_progress(float progress) { @@ -787,6 +789,8 @@ int main(int argc, char **argv) { cfg.instruct_prompt_file = std::move(value); } else if (key == "models_dir") { cfg.models_dir = std::move(value); + } else if (key == "texts_file") { + cfg.texts_file = std::move(value); } else if (key == "pool_size") { cfg.pool_size = std::stoi(value); } else if (key == "threads") { @@ -817,6 +821,38 @@ int main(int argc, char **argv) { } } + // Parse texts_file + Bot::Texts texts; + if (cfg.texts_file != "none") { + std::ifstream textsf(cfg.texts_file); + if (!textsf) { + std::cerr << "Error: Failed to open texts file: " << cfg.texts_file << std::endl; + exit(-1); + } + for (std::string key; textsf >> key;) { + // Read value + std::string value; + std::getline(textsf, value); + // Erase all leading spaces + while (!value.empty() && (value[0] == ' ' || value[0] == '\t')) value.erase(0, 1); + // Check key and ignore comment lines + if (key == "model_missing") { + texts.model_missing = std::move(value); + } else if (key == "please_wait") { + texts.please_wait = std::move(value); + } else if (key == "thread_create_fail") { + texts.thread_create_fail = std::move(value); + } else if (key == "timeout") { + texts.timeout = std::move(value); + } else if (key == "translated") { + texts.translated = parse_bool(value); + } else if (!key.empty() && key[0] != '#') { + std::cerr << "Error: Failed to parse texts file: Unknown key: " << key << std::endl; + exit(-3); + } + } + } + // Parse model configurations std::unordered_map models; std::filesystem::path models_dir(cfg.models_dir); @@ -929,7 +965,7 @@ int main(int argc, char **argv) { // Construct and configure bot Bot bot(cfg, models); - // Set signal handlers on Linux + // Set signal handlers if available # ifdef sa_sigaction struct sigaction sigact; static Bot& bot_st = bot;