1 Star 19 Fork 6

lerry_lca / similarity-model

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

相似度模型服务

对输入的两个句子进行推理预测,获得它们的语义相似度

环境依赖

工具 说明
python 开发语言
paddlepaddle 深度学习框架
flask python web框架
gevent python 并发框架

接口说明

接口介绍

接口名 类型 说明
calculate_similarity post 提供句子相似度计算

参数介绍

参数名 类型 说明
text_list1 list 句子列表1
text_list2 list 句子列表2

返回值介绍

返回值 类型 说明
probs list 相似度列表

例如输入是text_list1=[list1_t1,list1_t2,...]text_list2=[list2_t1,list2_t2,...],则输出为scores=[score1,score2,...]

其中score1list1_t1list2_t1的语义相似度得分,score2list1_t2list2_t2的语义相似度得分...

启动步骤

环境搭建

  1. 深度学习环境

对于paddlepadlle深度学习框架,参考快速安装

或直接使用如下命令安装:

# gpu版
python -m pip install paddlepaddle-gpu==2.0.2.post100 -f https://paddlepaddle.org.cn/whl/mkl/stable.html
# cpu版
python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
  1. 模型下载

该项目使用深度语言模型ERNIE,并基于预训练模型进行微调

所需的预训练模型是ERNIE1.0中文Base模型,点击下载

  1. python web环境

对于python web框架flask和并发框架gevent,使用如下命令安装即可:

pip install flask gevent

配置参数

conf/model_conf.json配置了常见的模型参数,如果这些参数不够满足要求,可以根据实际需求更改,然后重写模型加载前的参数读取方法即可

下面是配置参数的示例:

{
  "ernie": {
    "use_cuda": true,
    "batch_size": 32,
    "init_checkpoint": "data/ernie/pretrained_model/params",
    "ernie_config_path": "data/ernie/pretrained_model/ernie_config.json",
    "vocab_path": "data/ernie/pretrained_model/vocab.txt",
    "save_inference_model_path": "data/ernie/inference_models"
  }
}
  • use_cuda表示是否使用GPU

  • init_checkpoint表示模型要加载的检查点(训练好的模型输出的变量、参数等数据)

  • ernie_config_pathvocab_path是预训练模型的参数和词典

  • save_inference_model_path为模型推理时加载的模型文件。

    如果没有训练,可以直接使用预训练模型进行推理预测,即init_checkpoint的路径改为预训练模型的params即可(如示例)。

启动服务

进入该项目的根目录,然后执行命令

# 不指定参数,使用默认值
python server.py
# 指定参数
python server.py --port 6100 --model ernie
# 可使用`python server.py --help`查看参数说明

其中参数说明如下:

参数名 默认值 说明
port 6100 开放端口
model ernie 指定模型

另附:训练demo

项目根目录下ernie_train.py提供了训练ernie模型的方法,训练集和验证集的格式均为:s1\ts2\tlabel

北京有多少人	北京的人口数量是多少	1
这个空气净化器有用吗?	空气刘海是怎么样的	0
MIT License Copyright (c) 2021 lerry_li Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

可用于部署的语义相似度模型 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lerry-lee/similarity-model.git
git@gitee.com:lerry-lee/similarity-model.git
lerry-lee
similarity-model
similarity-model
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891