uie_pybind.cc 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/pybind/main.h"
  15. namespace py = pybind11;
  16. namespace ultra_infer {
  17. void BindUIE(pybind11::module &m) {
  18. py::class_<text::SchemaNode>(m, "SchemaNode")
  19. .def(py::init<>())
  20. .def(py::init<std::string, std::vector<text::SchemaNode>>(),
  21. py::arg("name"), py::arg("children"))
  22. .def_readwrite("name", &text::SchemaNode::name_)
  23. .def_readwrite("prefix", &text::SchemaNode::prefix_)
  24. .def_readwrite("relations", &text::SchemaNode::relations_)
  25. .def_readwrite("children", &text::SchemaNode::children_);
  26. py::enum_<text::SchemaLanguage>(m, "SchemaLanguage", py::arithmetic(),
  27. "The language of schema.")
  28. .value("ZH", text::SchemaLanguage::ZH)
  29. .value("EN", text::SchemaLanguage::EN);
  30. py::class_<text::UIEModel, UltraInferModel>(m, "UIEModel")
  31. .def(py::init<std::string, std::string, std::string, float, size_t,
  32. std::vector<std::string>, int, RuntimeOption, ModelFormat,
  33. text::SchemaLanguage>(),
  34. py::arg("model_file"), py::arg("params_file"), py::arg("vocab_file"),
  35. py::arg("position_prob"), py::arg("max_length"), py::arg("schema"),
  36. py::arg("batch_size"),
  37. py::arg("custom_option") = ultra_infer::RuntimeOption(),
  38. py::arg("model_format") = ultra_infer::ModelFormat::PADDLE,
  39. py::arg("schema_language") = text::SchemaLanguage::ZH)
  40. .def(py::init<std::string, std::string, std::string, float, size_t,
  41. std::vector<text::SchemaNode>, int, RuntimeOption,
  42. ModelFormat, text::SchemaLanguage>(),
  43. py::arg("model_file"), py::arg("params_file"), py::arg("vocab_file"),
  44. py::arg("position_prob"), py::arg("max_length"), py::arg("schema"),
  45. py::arg("batch_size"),
  46. py::arg("custom_option") = ultra_infer::RuntimeOption(),
  47. py::arg("model_format") = ultra_infer::ModelFormat::PADDLE,
  48. py::arg("schema_language") = text::SchemaLanguage::ZH)
  49. .def(py::init<std::string, std::string, std::string, float, size_t,
  50. text::SchemaNode, int, RuntimeOption, ModelFormat,
  51. text::SchemaLanguage>(),
  52. py::arg("model_file"), py::arg("params_file"), py::arg("vocab_file"),
  53. py::arg("position_prob"), py::arg("max_length"), py::arg("schema"),
  54. py::arg("batch_size"),
  55. py::arg("custom_option") = ultra_infer::RuntimeOption(),
  56. py::arg("model_format") = ultra_infer::ModelFormat::PADDLE,
  57. py::arg("schema_language") = text::SchemaLanguage::ZH)
  58. .def("set_schema",
  59. static_cast<void (text::UIEModel::*)(
  60. const std::vector<std::string> &)>(&text::UIEModel::SetSchema),
  61. py::arg("schema"))
  62. .def("set_schema",
  63. static_cast<void (text::UIEModel::*)(
  64. const std::vector<text::SchemaNode> &)>(
  65. &text::UIEModel::SetSchema),
  66. py::arg("schema"))
  67. .def("set_schema",
  68. static_cast<void (text::UIEModel::*)(const text::SchemaNode &)>(
  69. &text::UIEModel::SetSchema),
  70. py::arg("schema"))
  71. .def(
  72. "predict",
  73. [](text::UIEModel &self, const std::vector<std::string> &texts) {
  74. std::vector<
  75. std::unordered_map<std::string, std::vector<text::UIEResult>>>
  76. results;
  77. self.Predict(texts, &results);
  78. return results;
  79. },
  80. py::arg("text"));
  81. }
  82. } // namespace ultra_infer