Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions FastDeploy.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ if (ENABLE_TEXT)
find_library(FASTER_TOKENIZER_LIB core_tokenizers ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/faster_tokenizer/lib NO_DEFAULT_PATH)
list(APPEND FASTDEPLOY_LIBS ${FASTER_TOKENIZER_LIB})
list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/faster_tokenizer/include)
# TODO (zhoushunjie): Will remove it later.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要升级FasterTokenizer的目录结构。

list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/faster_tokenizer/include/faster_tokenizer)
list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/faster_tokenizer/third_party/include)
endif()

Expand Down
182 changes: 0 additions & 182 deletions examples/text/information_extraction/ernie/cpp/infer.cc

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)

include_directories(${FASTDEPLOY_INCS})

add_executable(infer_ernie_demo ${PROJECT_SOURCE_DIR}/infer.cc)
target_link_libraries(infer_ernie_demo ${FASTDEPLOY_LIBS})
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc ${PROJECT_SOURCE_DIR}/uie.cc)
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
47 changes: 47 additions & 0 deletions examples/text/uie/cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 通用信息抽取 UIE C++部署示例

本目录下提供`infer.cc`快速完成[UIE模型](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/model_zoo/uie)在CPU/GPU的示例。

在部署前,需确认以下两个步骤

- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../docs/quick_start/requirements.md)
- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../docs/compile/prebuilt_libraries.md)

以Linux上uie-base模型推理为例,在本目录执行如下命令即可完成编译测试。

```
# UIE目前还未发布,当前需开发者自行编译FastDeploy,通过如下脚本编译得到部署库fastdeploy-linux-x64-dev
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy
mkdir build && cd build
cmake .. -DENABLE_ORT_BACKEND=ON \
-DENABLE_VISION=ON \
-DENABLE_PADDLE_BACKEND=ON \
-DENABLE_TEXT=ON \
-DWITH_GPU=ON \
-DCMAKE_INSTALL_PREFIX=${PWD}/fastdeploy-linux-x64-gpu-dev

make -j8
make install

# 编译模型examples代码(SDK中包含了examples代码)
cd ../examples/text/uie/cpp
mkdir build
cd build
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/../../../../../build/fastdeploy-linux-x64-gpu-dev
make -j

# 下载uie-base模型以及词表
wget https://bj.bcebos.com/fastdeploy/models/uie/uie-base.tgz
tar -xvfz uie-base.tgz


# CPU 推理
./infer_demo uie-base 0

# GPU 推理
./infer_demo uie-base 1
```

## 模型获取
UIE 模型介绍可以参考https://github.com/PaddlePaddle/PaddleNLP/tree/develop/model_zoo/uie 。其中,在完成训练后,需要将训练后的模型导出成推理模型。该步骤可参考该文档完成导出:https://github.com/PaddlePaddle/PaddleNLP/tree/develop/model_zoo/uie#%E6%A8%A1%E5%9E%8B%E9%83%A8%E7%BD%B2 。
115 changes: 115 additions & 0 deletions examples/text/uie/cpp/infer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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 <iostream>
#include <sstream>

#include "fastdeploy/function/reduce.h"
#include "fastdeploy/function/softmax.h"
#include "fastdeploy/text.h"
#include "faster_tokenizer/tokenizers/ernie_faster_tokenizer.h"
#include "uie.h"

using namespace paddlenlp;

#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endif

int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout << "Usage: infer_demo path/to/model run_option, "
"e.g ./infer_demo uie-base 0"
<< std::endl;
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run "
"with gpu."
<< std::endl;
return -1;
}
auto option = fastdeploy::RuntimeOption();
if (std::atoi(argv[2]) == 0) {
option.UseCpu();
} else {
option.UseGpu();
}
std::string model_dir(argv[1]);
std::string model_path = model_dir + sep + "inference.pdmodel";
std::string param_path = model_dir + sep + "inference.pdiparams";
std::string vocab_path = model_dir + sep + "vocab.txt";

auto predictor = UIEModel(model_path, param_path, vocab_path, 0.5, 128,
{"时间", "选手", "赛事名称"}, option);
fastdeploy::FDINFO << "After init predictor" << std::endl;
std::vector<std::unordered_map<std::string, std::vector<UIEResult>>> results;
// Named Entity Recognition
predictor.Predict({"2月8日上午北京冬奥会自由式滑雪女子大跳台决赛中中国选手谷"
"爱凌以188.25分获得金牌!"},
&results);
std::cout << results << std::endl;
results.clear();

// Relation Extraction
predictor.SetSchema({{"竞赛名称",
{SchemaNode("主办方"), SchemaNode("承办方"),
SchemaNode("已举办次数")}}});
predictor.Predict(
{"2022语言与智能技术竞赛由中国中文信息学会和中国计算机学会联合主办,百度"
"公司、中国中文信息学会评测工作委员会和中国计算机学会自然语言处理专委会"
"承办,已连续举办4届,成为全球最热门的中文NLP赛事之一。"},
&results);
std::cout << results << std::endl;
results.clear();

// Event Extraction
predictor.SetSchema({{"地震触发词",
{SchemaNode("地震强度"), SchemaNode("时间"),
SchemaNode("震中位置"), SchemaNode("震源深度")}}});
predictor.Predict(
{"中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24."
"34度,东经99.98度)发生3.5级地震,震源深度10千米。"},
&results);
std::cout << results << std::endl;
results.clear();

// Opinion Extraction
predictor.SetSchema(
{{"评价维度",
{SchemaNode("观点词"), SchemaNode("情感倾向[正向,负向]")}}});
predictor.Predict(
{"店面干净,很清静,服务员服务热情,性价比很高,发现收银台有排队"},
&results);
std::cout << results << std::endl;
results.clear();

// Sequence classification
predictor.SetSchema({"情感倾向[正向,负向]"});
predictor.Predict({"这个产品用起来真的很流畅,我非常喜欢"}, &results);
std::cout << results << std::endl;
results.clear();

// Cross task extraction

predictor.SetSchema({{"法院", {}},
{"原告", {SchemaNode("委托代理人")}},
{"被告", {SchemaNode("委托代理人")}}});
predictor.Predict({"北京市海淀区人民法院\n民事判决书\n(199x)"
"建初字第xxx号\n原告:张三。\n委托代理人李四,北京市 "
"A律师事务所律师。\n被告:B公司,法定代表人王五,开发公司"
"总经理。\n委托代理人赵六,北京市 C律师事务所律师。"},
&results);
std::cout << results << std::endl;
results.clear();
return 0;
}
Loading