-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannaplugin.cpp
239 lines (215 loc) · 6.94 KB
/
annaplugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <QDebug>
#include <QTextStream>
#include <functional>
#include <future>
#include <chrono>
#include "annaplugin.h"
#include "annacfgdialog.h"
using namespace std;
AnnaPlugin::AnnaPlugin() :
QObject(),
MillaGenericPlugin()
{
qDebug() << "[ANNA] Plugin instance created";
DefaultConfig();
}
AnnaPlugin::~AnnaPlugin()
{
qDebug() << "[ANNA] Plugin destroyed";
}
bool AnnaPlugin::init()
{
qDebug() << "[ANNA] Init OK";
return true;
}
bool AnnaPlugin::finalize()
{
qDebug() << "[ANNA] Finalizing...";
if (brain) {
delete brain;
brain = nullptr;
}
qDebug() << "[ANNA] Finalized";
return true;
}
bool AnnaPlugin::showUI()
{
qDebug() << "[ANNA] Showing interface...";
AnnaCfgDialog dlg;
dlg.loadConfig(&config);
if (!dlg.exec()) return false;
//set some config options
dlg.updateConfig(&config);
qDebug() << "[ANNA] Config updated";
//and save them
if (!config_cb) return true;
QVariant r;
r = config_cb("save_key_value","ANNA_fmodel="+QString(config.params.model));
r = config_cb("save_key_value","ANNA_fvision="+QString::fromStdString(cfg_extra.vision_file));
r = config_cb("save_key_value","ANNA_prefai="+QString::fromStdString(cfg_extra.ai_prefix));
r = config_cb("save_key_value","ANNA_prefusr="+QString::fromStdString(cfg_extra.usr_prefix));
r = config_cb("save_key_value","ANNA_prompt="+QString(config.params.prompt));
//FIXME: check results??
qDebug() << "[ANNA] Config saved";
return true;
}
void AnnaPlugin::setConfigCB(PlugConfCB cb)
{
config_cb = cb;
if (!cb) return;
//pre-load config (if exists)
string tmp;
QVariant r;
r = config_cb("load_key_value","ANNA_fmodel");
if (r.canConvert<QString>()) {
tmp = r.value<QString>().toStdString();
if (!tmp.empty()) strncpy(config.params.model,tmp.c_str(),sizeof(config.params.model)-1);
}
r = config_cb("load_key_value","ANNA_fvision");
if (r.canConvert<QString>() && !r.value<QString>().isEmpty())
cfg_extra.vision_file = r.value<QString>().toStdString();
r = config_cb("load_key_value","ANNA_prefai");
if (r.canConvert<QString>() && !r.value<QString>().isEmpty())
cfg_extra.ai_prefix = r.value<QString>().toStdString();
r = config_cb("load_key_value","ANNA_prefusr");
if (r.canConvert<QString>() && !r.value<QString>().isEmpty())
cfg_extra.usr_prefix = r.value<QString>().toStdString();
r = config_cb("load_key_value","ANNA_prompt");
if (r.canConvert<QString>()) {
tmp = r.value<QString>().toStdString();
if (!tmp.empty()) strncpy(config.params.prompt,tmp.c_str(),sizeof(config.params.prompt)-1);
}
qDebug() << "[ANNA] Config preloaded";
}
QVariant AnnaPlugin::getParam(QString key)
{
qDebug() << "[ANNA] requested parameter " << key;
if (key == "show_ui") {
return true;
} else if (key == "use_config_cb") {
return true;
}
return QVariant();
}
bool AnnaPlugin::setParam(QString key, QVariant val)
{
qDebug() << "[ANNA] parameter " << key << " received";
if (key == "process_started") {
if (val.canConvert(QMetaType::Bool) && !val.toBool()) {
qDebug() << "[ANNA] Stopping plugin by UI request, deleting brain";
if (brain) {
delete brain;
brain = nullptr;
}
}
return true;
}
return false;
}
QVariant AnnaPlugin::action(QVariant in)
{
//sanity check
if (!config.params.model[0] || !config.params.prompt[0] || cfg_extra.vision_file.empty())
return QVariant();
//create or reset brain first
if (brain) {
brain->Reset();
qDebug() << "[ANNA] Brain reset";
} else {
brain = new AnnaBrain(&config);
if (brain->getState() == ANNA_ERROR) {
qDebug() << "[ANNA] ERROR: " << QString::fromStdString(brain->getError());
delete brain;
brain = nullptr;
return QVariant();
}
qDebug() << "[ANNA] Brain created";
}
//process prompt first
brain->setInput(config.params.prompt);
if (!Generate(true)) {
qDebug() << "[ANNA] Failed to process the prompt";
return QVariant();
}
qDebug() << "[ANNA] Prompt processed";
//encode image
QPixmap img;
if (in.canConvert<QPixmap>()) img = in.value<QPixmap>();
if (img.isNull()) {
qDebug() << "[ANNA] No image is given or unable to read input image";
return QVariant();
}
img.save(AP_IMAGE_TEMP);
brain->setClipModelFile(cfg_extra.vision_file);
if (!brain->EmbedImage(AP_IMAGE_TEMP)) {
qDebug() << "[ANNA] Unable to embed image: " << QString::fromStdString(brain->getError());
return QVariant();
}
qDebug() << "[ANNA] Image encoded";
//get the result
brain->setInput(cfg_extra.usr_prefix);
brain->setPrefix(cfg_extra.ai_prefix);
if (Generate(false)) {
QString out = QString::fromStdString(brain->getOutput());
qDebug() << "[ANNA] Result: " << out;
return out;
} else
qDebug() << "[ANNA] Failed to generate";
return QVariant();
}
void AnnaPlugin::DefaultConfig()
{
config.convert_eos_to_nl = true;
config.nl_to_turnover = false;
config.verbose_level = 2; //FIXME: DEBUG ONLY!!!
config.user = &cfg_extra;
gpt_params* p = &config.params;
p->seed = 0;
p->n_threads = thread::hardware_concurrency();
if (p->n_threads < 1) p->n_threads = 1;
p->n_predict = -1;
p->n_ctx = AP_DEFAULT_CONTEXT;
p->n_batch = AP_DEFAULT_BATCH;
p->n_gpu_layers = 32; //FIXME: DEBUG ONLY!!!
p->model[0] = 0;
p->prompt[0] = 0;
p->sparams.temp = AP_DEFAULT_TEMP;
}
bool AnnaPlugin::Generate(bool no_sample)
{
while (brain) {
//detach actual processing into separate thread
auto rhnd = async(launch::async,[&]() -> auto {
return brain->Processing(no_sample);
});
//while calling wait callback
double dummy = 0;
while (rhnd.wait_for(AP_WAITCB_PERIOD) != future_status::ready) {
if (progress_cb) progress_cb(dummy);
dummy += AP_WAITCB_INC;
if (dummy >= 100.f) dummy = 0;
}
if (progress_cb) progress_cb(100.f);
//finally we can acquire the request result
AnnaState s = rhnd.get();
//and decide how to continue
switch (s) {
case ANNA_TURNOVER:
return true;
case ANNA_READY:
if (no_sample) return true;
//qDebug() << QString::fromStdString(brain->getOutput());
//fall-thru
case ANNA_PROCESSING:
//nothing to do, waiting
break;
case ANNA_ERROR:
qDebug() << "[ANNA] Error: " << QString::fromStdString(brain->getError());
return false;
default:
qDebug() << "[ANNA] Wrong brain state: " << QString::fromStdString(AnnaBrain::StateToStr(s));
return false;
}
}
return true;
}