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

Initial commit

This commit is contained in:
niansa 2023-04-16 21:08:29 +02:00
commit 132bd657f2
7 changed files with 313 additions and 0 deletions

74
.gitignore vendored Normal file
View file

@ -0,0 +1,74 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
CMakeLists.txt.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "libjustlm"]
path = libjustlm
url = https://gitlab.com/niansa/libjustlm.git

21
CMakeLists.txt Normal file
View file

@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.5)
project(anyproc LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(libjustlm)
add_library(anyproc INTERFACE)
target_include_directories(anyproc INTERFACE include/)
target_link_libraries(anyproc INTERFACE libjustlm)
add_executable(dictionary dictionary.cpp)
target_link_libraries(dictionary PUBLIC anyproc)
add_executable(translator translator.cpp)
target_link_libraries(translator PUBLIC anyproc)
install(TARGETS anyproc
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

33
dictionary.cpp Normal file
View file

@ -0,0 +1,33 @@
#include "anyproc.hpp"
#include <iostream>
#include <string>
#include <justlm.hpp>
int main() {
const auto progress_indicator = [](float progress) {
std::cout << unsigned(progress) << "% \r" << std::flush;
return true;
};
const auto result_printer = [](const char *token) {
std::cout << token << std::flush;
return true;
};
Dictionary dict("13B-ggml-model-quant.bin");
for (;;) {
std::string what;
std::cout << "What: " << std::flush;
std::getline(std::cin, what);
for (;;) {
std::string word;
std::cout << "\rWord: " << std::flush;
std::getline(std::cin, word);
if (word.empty()) break;
dict.lookup(word, what, progress_indicator, result_printer);
}
}
}

148
include/anyproc.hpp Normal file
View file

@ -0,0 +1,148 @@
#ifndef ANYPROC_HPP
#define ANYPROC_HPP
#include <iostream>
#include <string>
#include <string_view>
#include <justlm.hpp>
class PyEval : LM::Inference {
std::string buffer = "Python 3.9.2 (default, Feb 28 2021, 17:03:44)\n"
"[GCC 10.2.1 20210110] on linux\n"
"Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n"
">>> print(\"Hello world!\")\n"
"Hello world!\n";
public:
static inline const LM::Inference::Params get_recommended_params() {
LM::Inference::Params p;
p.use_mlock = false;
p.repeat_penalty = 1.2f;
p.n_repeat_last = 64;
p.temp = 0.4f;
p.top_k = 1;
p.top_p = 0.f;
return p;
}
PyEval(const std::string& weights_path, const Params& p = get_recommended_params())
: LM::Inference(weights_path, p) {}
auto& begin() {
buffer += ">>> ";
return *this;
}
auto& load_module(std::string_view name) {
buffer += "import ";
buffer += name;
return *this;
}
auto& load_module(std::string_view name, std::string_view alias) {
buffer += "import ";
buffer += name;
buffer += " as ";
buffer += alias;
return *this;
}
auto& expression(std::string_view expression) {
buffer += expression;
return *this;
}
auto run(const std::function<bool (float)> &on_append_tick = nullptr, const std::function<bool (const char *generated)>& on_generation_tick = nullptr) {
buffer += "\n";
append(buffer, on_append_tick);
buffer.clear();
return LM::Inference::run("\n", on_generation_tick);
}
void example(std::string_view response) {
buffer += "\n";
buffer += response;
if (!response.empty()) buffer += "\n";
buffer += ">>>";
}
};
class Dictionary : PyEval {
static inline LM::Inference::Params get_params() {
auto p = get_recommended_params();
p.top_k = 5;
p.top_p = 0.2f;
p.temp = 0.2f;
return p;
}
static inline auto word_lookup_exprgen(const std::string& word) {
return "dict.word_lookup(\""+word+"\")";
}
public:
Dictionary(const std::string& weights_path) : PyEval(weights_path, get_params()) {
begin()
.load_module("huge_dictionary", "dict")
.example("");
begin()
.expression(word_lookup_exprgen("Treehouse")+".description")
.example("'A small house, especially one for children to play in, built or placed up in the branches of a tree.'");
begin()
.expression(word_lookup_exprgen("Python")+".description")
.example("'Any of several Old World boa constrictors of the subfamily Pythoninae, often growing to a length of more than 20 feet (6 meters): the Indian python, Python molurus, is endangered.'");
begin()
.expression(word_lookup_exprgen("C")+".description")
.example("'The 3rd letter of the alphabet.'");
begin()
.expression(word_lookup_exprgen("Appletree")+".syllables")
.example("['Ap', 'ple', 'tree']");
}
std::string lookup(const std::string& word, const std::string& what, const std::function<bool (float)> &on_append_tick = nullptr, const std::function<bool (const char *generated)>& on_generation_tick = nullptr) {
return begin().expression(word_lookup_exprgen(word)+'.'+what)
.run(on_append_tick, on_generation_tick);
}
};
class Translator : PyEval {
static inline LM::Inference::Params get_params() {
auto p = get_recommended_params();
p.top_k = 5;
p.top_p = 0.2f;
p.temp = 0.2f;
return p;
}
static inline auto translation_exprgen(const std::string& word, const std::string& language) {
return "translator.translate(\""+word+"\", \""+language+"\")";
}
public:
Translator(const std::string& weights_path) : PyEval(weights_path, get_params()) {
begin()
.load_module("google_translate", "translator")
.example("");
begin()
.expression(translation_exprgen("Treehouse", "DE"))
.example("'Baumhaus'");
begin()
.expression(translation_exprgen("Mir fiel ein Ball auf den Fuss", "EN"))
.example("'A ball fell onto my foot'");
begin()
.expression(translation_exprgen("How long until school starts?", "IT"))
.example("'Quanto manca all'inizio della scuola?'");
begin()
.expression(translation_exprgen("Bitte wann kommst du Online?? <@1052568298895712277> <:Windel:1092530435776581804>", "EN"))
.example("'Please when are you coming online?? <@1052568298895712277> <:Windel:1092530435776581804>'");
begin()
.expression(translation_exprgen("Please DO NOT announce to the server when you are going to go masturbate. This has been a reoccurring issue, and I'm not sure why some people have such under developed social skills that they think that a server full of mostly male strangers would need to know that. No one is going to be impressed and give you a high five (especially considering where that hand has been). I don't want to add this to the rules, since it would be embarrassing for new users to see that we have a problem with this, but it is going to be enforced as a rule from now on.", "DE"))
.example("Bitte kündige auf dem Server NICHT an, wann du masturbieren gehen wirs. Dies ist ein wiederkehrendes Problem, und ich bin mir nicht sicher, warum einige Leute so unterentwickelte soziale Fähigkeiten haben, dass sie denken, dass ein Server voller meist männlicher Fremder das wissen muss. Niemand wird beeindruckt sein und dir ein High Five geben (besonders wenn man bedenkt, wo diese Hand schon war). Ich möchte dies nicht zu den Regeln hinzufügen, da es für neue Benutzer peinlich wäre, zu sehen, dass wir ein Problem damit haben, aber es wird von nun an als Regel durchgesetzt werden.");
}
std::string translate(const std::string& text, const std::string& language, const std::function<bool (float)> &on_append_tick = nullptr, const std::function<bool (const char *generated)>& on_generation_tick = nullptr) {
return begin().expression(translation_exprgen(text, language))
.run(on_append_tick, on_generation_tick);
}
};
#endif // ANYPROC_HPP

1
libjustlm Submodule

@ -0,0 +1 @@
Subproject commit 2d97e7b2bd0db84823687f8bd61a94e583f8a792

33
translator.cpp Normal file
View file

@ -0,0 +1,33 @@
#include "anyproc.hpp"
#include <iostream>
#include <string>
#include <justlm.hpp>
int main() {
const auto progress_indicator = [](float progress) {
std::cout << unsigned(progress) << "% \r" << std::flush;
return true;
};
const auto result_printer = [](const char *token) {
std::cout << token << std::flush;
return true;
};
Translator translator("13B-ggml-model-quant.bin");
for (;;) {
std::string language;
std::cout << "Language: " << std::flush;
std::getline(std::cin, language);
for (;;) {
std::string text;
std::cout << "\rText: " << std::flush;
std::getline(std::cin, text);
if (text.empty()) break;
translator.translate(text, language, progress_indicator, result_printer);
}
}
}