1、參考
https://zhuanlan.zhihu.com/p/50582974
https://github.com/hanxiao/bert-asservice/blob/master/client/README.md
2择示、下載bert中文詞向量
地址:https://github.com/google-research/bert#pre-trained-models
中文向量鏈接:https://storage.googleapis.com/bert_models/2018_11_03/chinese_L-12_H-768_A-12.zip
3、提供服務
3.1 將上述壓縮文件解壓
3.2 構(gòu)建環(huán)境
安裝依賴環(huán)境:
pip install numpy
pip install -U bert-serving-server[http]
pip install bert-serving-client
pip install tensorflow>=1.10.0
4、 提供本地/遠程服務
4.1 本地直接調(diào)用:
from bert_serving.client import BertClient
bc = BertClient()
bc.encode(['我 喜歡 你們', '我 喜 歡 你 們'])
4.2 遠程請求服務
post服務:
curl -X POST http://**.*.*.68:8125/encode -H 'content-type: application/json' -d '{"id": 123,"texts": ["hello world"], "is_tokenized": false}'
返回結(jié)果:
{
"id":123,
"result":[[-0.00980051327496767,0.05821939557790756,-0.06836936622858047,
-0.4723478853702545,0.48761454224586487,-1.4105712175369263,
...
...
,-0.10073700547218323,-0.17246723175048828]],
"status":200
}
4.3榛丢、在一個GPU服務器(**.*.*.68)上部署bert服務,在另外一臺cpu服務器(**.*.*.67)調(diào)用這個服務:
step1: 調(diào)用前先在(**.*.*.68)上安裝client:
pip install bert-serving-client
step2: 調(diào)用服務demo
# on another CPU machine
from bert_serving.client import BertClient
bc = BertClient(ip='xx.xx.xx.xx') # ip address of the GPU machine
bc.encode(['First do it', 'then do it right', 'then do it better'])
5挺庞、 模型不需要分詞晰赞,距離如下,發(fā)現(xiàn)這集中情況得到的編碼向量是一樣的
image.png