-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.cpp
30 lines (28 loc) · 1.17 KB
/
binding.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
#include "clip_tokenizer.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/complex.h>
PYBIND11_MODULE(clip_tokenizer_py, m) {
pybind11::class_<CLIPTokenizer>(m, "CLIPTokenizer")
.def(pybind11::init<const std::string&>())
.def("tokenize", &CLIPTokenizer::tokenize);
pybind11::class_<TokenizerResult>(m, "TokenizerResult")
.def(pybind11::init<>())
.def_readwrite("tokens", &TokenizerResult::tokens)
.def_readwrite("attention_mask", &TokenizerResult::attention_mask)
.def("__repr__", [](const TokenizerResult &a) {
std::string tokens;
for (auto& token : a.tokens[0]) {
tokens += std::to_string(token) + ", ";
}
tokens.pop_back();
tokens.pop_back();
std::string attention_mask;
for (auto& mask : a.attention_mask[0]) {
attention_mask += std::to_string(mask) + ", ";
}
attention_mask.pop_back();
attention_mask.pop_back();
return "<clip_tokenizer_py.TokenizerResult tokens=[" + tokens + "] attention_mask=[" + attention_mask + "]>";
});
}