-
Notifications
You must be signed in to change notification settings - Fork 442
/
uie_pybind.cc
89 lines (82 loc) · 4.09 KB
/
uie_pybind.cc
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
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/pybind/main.h"
namespace py = pybind11;
namespace fastdeploy {
void BindUIE(pybind11::module& m) {
py::class_<text::SchemaNode>(m, "SchemaNode")
.def(py::init<>())
.def(py::init<std::string, std::vector<text::SchemaNode>>(),
py::arg("name"), py::arg("children"))
.def_readwrite("name", &text::SchemaNode::name_)
.def_readwrite("prefix", &text::SchemaNode::prefix_)
.def_readwrite("relations", &text::SchemaNode::relations_)
.def_readwrite("children", &text::SchemaNode::children_);
py::enum_<text::SchemaLanguage>(m, "SchemaLanguage", py::arithmetic(),
"The language of schema.")
.value("ZH", text::SchemaLanguage::ZH)
.value("EN", text::SchemaLanguage::EN);
py::class_<text::UIEModel, FastDeployModel>(m, "UIEModel")
.def(py::init<std::string, std::string, std::string, float, size_t,
std::vector<std::string>, int, RuntimeOption, ModelFormat,
text::SchemaLanguage>(),
py::arg("model_file"), py::arg("params_file"), py::arg("vocab_file"),
py::arg("position_prob"), py::arg("max_length"), py::arg("schema"),
py::arg("batch_size"),
py::arg("custom_option") = fastdeploy::RuntimeOption(),
py::arg("model_format") = fastdeploy::ModelFormat::PADDLE,
py::arg("schema_language") = text::SchemaLanguage::ZH)
.def(py::init<std::string, std::string, std::string, float, size_t,
std::vector<text::SchemaNode>, int, RuntimeOption,
ModelFormat, text::SchemaLanguage>(),
py::arg("model_file"), py::arg("params_file"), py::arg("vocab_file"),
py::arg("position_prob"), py::arg("max_length"), py::arg("schema"),
py::arg("batch_size"),
py::arg("custom_option") = fastdeploy::RuntimeOption(),
py::arg("model_format") = fastdeploy::ModelFormat::PADDLE,
py::arg("schema_language") = text::SchemaLanguage::ZH)
.def(py::init<std::string, std::string, std::string, float, size_t,
text::SchemaNode, int, RuntimeOption, ModelFormat,
text::SchemaLanguage>(),
py::arg("model_file"), py::arg("params_file"), py::arg("vocab_file"),
py::arg("position_prob"), py::arg("max_length"), py::arg("schema"),
py::arg("batch_size"),
py::arg("custom_option") = fastdeploy::RuntimeOption(),
py::arg("model_format") = fastdeploy::ModelFormat::PADDLE,
py::arg("schema_language") = text::SchemaLanguage::ZH)
.def("set_schema",
static_cast<void (text::UIEModel::*)(
const std::vector<std::string>&)>(&text::UIEModel::SetSchema),
py::arg("schema"))
.def("set_schema",
static_cast<void (text::UIEModel::*)(
const std::vector<text::SchemaNode>&)>(
&text::UIEModel::SetSchema),
py::arg("schema"))
.def("set_schema",
static_cast<void (text::UIEModel::*)(const text::SchemaNode&)>(
&text::UIEModel::SetSchema),
py::arg("schema"))
.def(
"predict",
[](text::UIEModel& self, const std::vector<std::string>& texts) {
std::vector<
std::unordered_map<std::string, std::vector<text::UIEResult>>>
results;
self.Predict(texts, &results);
return results;
},
py::arg("text"));
}
} // namespace fastdeploy