【AI实战】BERT 文本分类模型自动化部署之 dockerfile

【AI实战】BERT 文本分类模型自动化部署之 dockerfile

  • BERT
  • BERT 文本分类模型
    • 基于中文预训练bert的文本分类模型
    • 针对多分类模型的loss函数
      • 样本不均衡时
      • 多标签分类时
  • dockerfile
    • 编写 dockerfile
    • build镜像
    • 运行docker
    • 测试服务
  • 参考

本文主要介绍:

  1. 基于BERT的文本分类模型,样本不均衡的多分类loss函数的写法
  2. dockerfile自动构建docker镜像,服务部署

BERT

BERT 的全称为 Bidirectional Encoder Representation from Transformers,是一个预训练的语言表征模型。它强调了不再像以往一样采用传统的单向语言模型或者把两个单向语言模型进行浅层拼接的方法进行预训练,而是采用新的masked language model(MLM),以致能生成深度的双向语言表征。

BERT 文本分类模型

基于中文预训练bert的文本分类模型

基本架构:bert_model + dropout + 全连接层
代码:

class BERTClass(torch.nn.Module):def __init__(self, num_class):super(BERTClass, self).__init__()self.bert_model = BertModel.from_pretrained('bert-base-chinese', return_dict=True)self.dropout = torch.nn.Dropout(0.3)self.linear = torch.nn.Linear(768, num_class)def forward(self, input_ids, attn_mask, token_type_ids):output = self.bert_model(input_ids, attention_mask=attn_mask, token_type_ids=token_type_ids)output_dropout = self.dropout(output.pooler_output)output = self.linear(output_dropout)return output

针对多分类模型的loss函数

样本不均衡时

代码:

class MultiClassFocalLossWithAlpha(nn.Module):def __init__(self, alpha, gamma=2, reduction='mean'):""":param alpha: alpha=[0.2, 0.3, 0.5] 权重系数列表,三分类中第0类权重0.2,第1类权重0.3,第2类权重0.5:param gamma: 困难样本挖掘的gamma:param reduction:"""super(MultiClassFocalLossWithAlpha, self).__init__()self.alpha = alpha#torch.tensor(alpha)self.gamma = gammaself.reduction = reductiondef forward(self, pred, target_src):target = torch.argmax(target_src, axis = 1)alpha = self.alpha[target]  log_softmax = torch.log_softmax(pred, dim=1)logpt = torch.gather(log_softmax, dim=1, index=target.view(-1, 1))logpt = logpt.view(-1)ce_loss = -logpt pt = torch.exp(logpt) f_loss = alpha * (1 - pt) ** self.gamma * ce_loss if self.reduction == "mean":return torch.mean(f_loss)if self.reduction == "sum":return torch.sum(f_loss)return f_lossdef focal_loss(outputs, targets):import jsonwith open('./output/loss_weight.json') as f:data = f.read()loss_weight = json.loads(data)class_weight = torch.tensor(loss_weight['class_weight'])class_weight = class_weight.to(torch.device(device))loss = MultiClassFocalLossWithAlpha(alpha=class_weight)return loss.forward(outputs, targets)

多标签分类时

代码:

# BCEWithLogitsLoss combines a Sigmoid layer and the BCELoss in one single class. 
# This version is more numerically stable than using a plain Sigmoid followed 
# by a BCELoss as, by combining the operations into one layer, 
# we take advantage of the log-sum-exp trick for numerical stability.
def loss_fn(outputs, targets):import jsonwith open('./output/loss_weight.json') as f:data = f.read()loss_weight = json.loads(data)class_weight = torch.tensor(loss_weight['class_weight'])pos_weight = torch.tensor(loss_weight['pos_weight'])class_weight = class_weight.to(torch.device(device))pos_weight = pos_weight.to(torch.device(device))loss_fn2 = torch.nn.BCEWithLogitsLoss(weight=class_weight, pos_weight=pos_weight)outputs = outputs.float()targets = targets.float()loss = loss_fn2(outputs, targets)return loss

dockerfile

Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。

编写 dockerfile

以python3.9来构建 bert 模型运行环境的镜像,基于 torch 的 CPU 版本

Dockerfile:

FROM ludotech/python3.9-poetry:latestADD bert_model.tar /homeRUN ["pip", "--no-cache-dir", "install", "-r", "/home/bert_model/requirements.txt", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple"]RUN chmod -x /home/bert_model/run.sh
CMD bash /home/bert_model/run.sh

其中:

  • bert_model.tar 为完整的代码、模型、数据等
  • requirements.txt 包含完整的依赖库
  • run.sh 中为启动模型的脚步,可以支持多进程启动

requirements.txt :

urllib3==1.26.16
charset-normalizer==3.2.0
Flask==2.3.2
gensim==4.3.1
h5py==3.9.0
huggingface-hub==0.16.4
importlib-metadata==6.8.0
importlib-resources==6.0.0
ipython==8.14.0
jieba==0.42.1
joblib==1.3.1
matplotlib==3.7.2
matplotlib-inline==0.1.6
nltk==3.8.1
numpy==1.23.0
packaging==23.1
pandas==2.0.3
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==10.0.0
platformdirs==3.10.0
prompt-toolkit==3.0.39
protobuf==4.23.4
psutil==5.9.5
pyparsing==3.0.9
python-dateutil==2.8.2
pytorch-pretrained-bert==0.6.2
pytz==2023.3
PyYAML==6.0
pyzmq==25.1.0
safetensors==0.3.1
scikit-learn==1.3.0
scipy==1.11.1
sentencepiece==0.1.99
tensorboardX==2.6.2
threadpoolctl==3.2.0
tokenizers==0.12.1
torch==1.10.1
tqdm==4.65.0
transformers==4.30.2

run.sh(我启动了 2 个服务进程):

cd /home/bert_model
python deploy.py &cd /home/bert_model
python train_srv.py &touch /home/a
tail -f /home/a

build镜像

  • 文件准备:

    $ ls
    build.sh  Dockerfile  bert_model.tar 
    

    其中:
    build.sh:

    docker build -t  bert_model:v1.
    
  • 执行build

    sh build.sh
    

    如果是正常,则会输出如下:

    $ sh build.sh  
    Sending build context to Docker daemon  822.3MB
    Step 1/5 : FROM ludotech/python3.9-poetry:latest---> bbbc285de928
    Step 2/5 : ADD bert_model:v1.tar /home---> fb481b25dbfd
    Step 3/5 : RUN ["pip", "--no-cache-dir", "install", "-r", "/home/bert_model:v1/requirements.txt", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple"]---> Running in 122bad5d6b64
    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
    Collecting charset-normalizer==3.2.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f9/0d/514be8597d7a96243e5467a37d337b9399cec117a513fcf9328405d911c0/charset_normalizer-3.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (202 kB)
    Collecting Flask==2.3.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fa/1a/f191d32818e5cd985bdd3f47a6e4f525e2db1ce5e8150045ca0c31813686/Flask-2.3.2-py3-none-any.whl (96 kB)
    Collecting gensim==4.3.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/79/93/bb490709bb24004d3d4c20005e19939ef1e1ee62ed7698e85c186745b01d/gensim-4.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (26.5 MB)
    Collecting h5py==3.9.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4f/79/8e6e05bc4954ebdb8b9c587f780a11f28790585798bd15a8e4870cfc02bc/h5py-3.9.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB)
    Collecting huggingface-hub==0.16.4Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/c4/adcbe9a696c135578cabcbdd7331332daad4d49b7c43688bc2d36b3a47d2/huggingface_hub-0.16.4-py3-none-any.whl (268 kB)
    Collecting importlib-metadata==6.8.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/cc/37/db7ba97e676af155f5fcb1a35466f446eadc9104e25b83366e8088c9c926/importlib_metadata-6.8.0-py3-none-any.whl (22 kB)
    Collecting importlib-resources==6.0.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/29/d1/bed03eca30aa05aaf6e0873de091f9385c48705c4a607c2dfe3edbe543e8/importlib_resources-6.0.0-py3-none-any.whl (31 kB)
    Collecting ipython==8.14.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/52/d1/f70cdafba20030cbc1412d7a7d6a89c5035071835cc50e47fc5ed8da553c/ipython-8.14.0-py3-none-any.whl (798 kB)
    Collecting jieba==0.42.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c6/cb/18eeb235f833b726522d7ebed54f2278ce28ba9438e3135ab0278d9792a2/jieba-0.42.1.tar.gz (19.2 MB)
    Collecting joblib==1.3.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/08/9dcdaa5aac4634e4c23af26d92121f7ce445c630efa0d3037881ae2407fb/joblib-1.3.1-py3-none-any.whl (301 kB)
    Collecting matplotlib==3.7.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/47/b9/6c0daa9b953a80b4e6933bf6a11a2d0633f257e84ee5995c5fd35de564c9/matplotlib-3.7.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.6 MB)
    Collecting matplotlib-inline==0.1.6Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f2/51/c34d7a1d528efaae3d8ddb18ef45a41f284eacf9e514523b191b7d0872cc/matplotlib_inline-0.1.6-py3-none-any.whl (9.4 kB)
    Collecting nltk==3.8.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a6/0a/0d20d2c0f16be91b9fa32a77b76c60f9baf6eba419e5ef5deca17af9c582/nltk-3.8.1-py3-none-any.whl (1.5 MB)
    Collecting numpy==1.23.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/da/0e/496e529f440f528273f6847e14d7b132b0556a824fc2af36e8afd8e6a020/numpy-1.23.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.1 MB)
    Collecting packaging==23.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ab/c3/57f0601a2d4fe15de7a553c00adbc901425661bf048f2a22dfc500caf121/packaging-23.1-py3-none-any.whl (48 kB)
    Collecting pandas==2.0.3Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9e/0d/91a9fd2c202f2b1d97a38ab591890f86480ecbb596cbc56d035f6f23fdcc/pandas-2.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.4 MB)
    Collecting parso==0.8.3Downloading https://pypi.tuna.tsinghua.edu.cn/packages/05/63/8011bd08a4111858f79d2b09aad86638490d62fbf881c44e434a6dfca87b/parso-0.8.3-py2.py3-none-any.whl (100 kB)
    Collecting pexpect==4.8.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/39/7b/88dbb785881c28a102619d46423cb853b46dbccc70d3ac362d99773a78ce/pexpect-4.8.0-py2.py3-none-any.whl (59 kB)
    Collecting pickleshare==0.7.5Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9a/41/220f49aaea88bc6fa6cba8d05ecf24676326156c23b991e80b3f2fc24c77/pickleshare-0.7.5-py2.py3-none-any.whl (6.9 kB)
    Collecting Pillow==10.0.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/50/e5/0d484d1ac71b934638f91b7156203ba5bf3eb12f596b616a68a85c123808/Pillow-10.0.0-cp39-cp39-manylinux_2_28_x86_64.whl (3.4 MB)
    Collecting platformdirs==3.10.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/14/51/fe5a0d6ea589f0d4a1b97824fb518962ad48b27cd346dcdfa2405187997a/platformdirs-3.10.0-py3-none-any.whl (17 kB)
    Collecting prompt-toolkit==3.0.39Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a9/b4/ba77c84edf499877317225d7b7bc047a81f7c2eed9628eeb6bab0ac2e6c9/prompt_toolkit-3.0.39-py3-none-any.whl (385 kB)
    Collecting protobuf==4.23.4Downloading https://pypi.tuna.tsinghua.edu.cn/packages/01/cb/445b3e465abdb8042a41957dc8f60c54620dc7540dbcf9b458a921531ca2/protobuf-4.23.4-cp37-abi3-manylinux2014_x86_64.whl (304 kB)
    Collecting psutil==5.9.5Downloading https://pypi.tuna.tsinghua.edu.cn/packages/af/4d/389441079ecef400e2551a3933224885a7bde6b8a4810091d628cdd75afe/psutil-5.9.5-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (282 kB)
    Collecting pyparsing==3.0.9Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6c/10/a7d0fa5baea8fe7b50f448ab742f26f52b80bfca85ac2be9d35cdd9a3246/pyparsing-3.0.9-py3-none-any.whl (98 kB)
    Collecting python-dateutil==2.8.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
    Collecting pytorch-pretrained-bert==0.6.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d7/e0/c08d5553b89973d9a240605b9c12404bcf8227590de62bae27acbcfe076b/pytorch_pretrained_bert-0.6.2-py3-none-any.whl (123 kB)
    Collecting pytz==2023.3Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/99/ad6bd37e748257dd70d6f85d916cafe79c0b0f5e2e95b11f7fbc82bf3110/pytz-2023.3-py2.py3-none-any.whl (502 kB)
    Collecting PyYAML==6.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/12/fc/a4d5a7554e0067677823f7265cb3ae22aed8a238560b5133b58cda252dad/PyYAML-6.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (661 kB)
    Collecting pyzmq==25.1.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/94/4b/1093172b73984b568d9f1a72bcd61793822fab40aa571f5d6ed9db6234cb/pyzmq-25.1.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.1 MB)
    Collecting safetensors==0.3.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/60/c7/1911e04710666eb79ca3311a4e91b669419a1f23c2b2619005165104368c/safetensors-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)
    Collecting scikit-learn==1.3.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d4/61/966d3238f6cbcbb13350d31bd0accfc5efdf9e349cd2a42d9761b8b67a18/scikit_learn-1.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.9 MB)
    Collecting scipy==1.11.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/08/25/035fe07fc32c5a8b314f882faa9d4817223fa5faf524d3fedcf17a4b9d22/scipy-1.11.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (36.5 MB)
    Collecting sentencepiece==0.1.99Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6b/22/4157918b2112d47014fb1e79b0dd6d5a141b8d1b049bae695d405150ebaf/sentencepiece-0.1.99-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)
    Collecting tensorboardX==2.6.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/44/7b/eee50dcadcee4c674353ca207fdcd53a5b1f382021af1ed1797f9c0c45d2/tensorboardX-2.6.2-py2.py3-none-any.whl (101 kB)
    Collecting threadpoolctl==3.2.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/81/12/fd4dea011af9d69e1cad05c75f3f7202cdcbeac9b712eea58ca779a72865/threadpoolctl-3.2.0-py3-none-any.whl (15 kB)
    Collecting tokenizers==0.12.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/60/fc/3da9736965bf6edd96e8b098984c9f4559c4a1cc5be563436cd228ad1e69/tokenizers-0.12.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (6.6 MB)
    Collecting torch==1.10.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2c/c8/dcef19018d2fe730ecacf47650d3d6e8d6fe545f02fbdbde0174e0279f02/torch-1.10.1-cp39-cp39-manylinux1_x86_64.whl (881.9 MB)
    Collecting tqdm==4.65.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e6/02/a2cff6306177ae6bc73bc0665065de51dfb3b9db7373e122e2735faf0d97/tqdm-4.65.0-py3-none-any.whl (77 kB)
    Collecting transformers==4.30.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5b/0b/e45d26ccd28568013523e04f325432ea88a442b4e3020b757cf4361f0120/transformers-4.30.2-py3-none-any.whl (7.2 MB)
    Collecting urllib3==1.26.16Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c5/05/c214b32d21c0b465506f95c4f28ccbcba15022e000b043b72b3df7728471/urllib3-1.26.16-py2.py3-none-any.whl (143 kB)
    Collecting blinker>=1.6.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0d/f1/5f39e771cd730d347539bb74c6d496737b9d5f0a53bc9fdbf3e170f1ee48/blinker-1.6.2-py3-none-any.whl (13 kB)
    Collecting click>=8.1.3Downloading https://pypi.tuna.tsinghua.edu.cn/packages/1a/70/e63223f8116931d365993d4a6b7ef653a4d920b41d03de7c59499962821f/click-8.1.6-py3-none-any.whl (97 kB)
    Collecting contourpy>=1.0.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/38/6f/5382bdff9dda60cb17cef6dfa2bad3e6edacffd5c2243e282e851c63f721/contourpy-1.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (300 kB)
    Collecting cycler>=0.10Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5c/f9/695d6bedebd747e5eb0fe8fad57b72fdf25411273a39791cde838d5a8f51/cycler-0.11.0-py3-none-any.whl (6.4 kB)
    Collecting fonttools>=4.22.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/91/0e/8303b815e3bcc211a2da3e4427748cb963247594837dceb051e28d4e4b66/fonttools-4.42.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB)
    Collecting itsdangerous>=2.1.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/68/5f/447e04e828f47465eeab35b5d408b7ebaaaee207f48b7136c5a7267a30ae/itsdangerous-2.1.2-py3-none-any.whl (15 kB)
    Collecting jedi>=0.16Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8e/46/7e3ae3aa2dcfcffc5138c6cef5448523218658411c84a2000bf75c8d3ec1/jedi-0.19.0-py2.py3-none-any.whl (1.6 MB)
    Collecting Jinja2>=3.1.2Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bc/c3/f068337a370801f372f2f8f6bad74a5c140f6fda3d9de154052708dd3c65/Jinja2-3.1.2-py3-none-any.whl (133 kB)
    Collecting kiwisolver>=1.0.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a4/36/c414d75be311ce97ef7248edcc4fc05afae2998641bf6b592d43a9dee581/kiwisolver-1.4.4-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.6 MB)
    Collecting MarkupSafe>=2.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/de/63/cb7e71984e9159ec5f45b5e81e896c8bdd0e45fe3fc6ce02ab497f0d790e/MarkupSafe-2.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
    Collecting ptyprocess>=0.5Downloading https://pypi.tuna.tsinghua.edu.cn/packages/22/a6/858897256d0deac81a172289110f31629fc4cee19b6f01283303e18c8db3/ptyprocess-0.7.0-py2.py3-none-any.whl (13 kB)
    Collecting pygments>=2.4.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/43/88/29adf0b44ba6ac85045e63734ae0997d3c58d8b1a91c914d240828d0d73d/Pygments-2.16.1-py3-none-any.whl (1.2 MB)
    Collecting regex>=2021.8.3Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c0/f4/278e305e02245937579a7952b8a3205116b4d2480a3c03fa11e599b773d6/regex-2023.8.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (771 kB)
    Collecting six>=1.5Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl (11 kB)
    Collecting smart-open>=1.8.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/47/80/c2d1bdd36c6b64ae566d9a29724291510e4f3796ce99639d3c2999286284/smart_open-6.3.0-py3-none-any.whl (56 kB)
    Collecting traitlets>=5Downloading https://pypi.tuna.tsinghua.edu.cn/packages/77/75/c28e9ef7abec2b7e9ff35aea3e0be6c1aceaf7873c26c95ae1f0d594de71/traitlets-5.9.0-py3-none-any.whl (117 kB)
    Collecting typing-extensions>=3.7.4.3Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ec/6b/63cc3df74987c36fe26157ee12e09e8f9db4de771e0f3404263117e75b95/typing_extensions-4.7.1-py3-none-any.whl (33 kB)
    Collecting tzdata>=2022.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d5/fb/a79efcab32b8a1f1ddca7f35109a50e4a80d42ac1c9187ab46522b2407d7/tzdata-2023.3-py2.py3-none-any.whl (341 kB)
    Collecting Werkzeug>=2.3.3Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9b/59/a7c32e3d8d0e546a206e0552a2c04444544f15c1da4a01df8938d20c6ffc/werkzeug-2.3.7-py3-none-any.whl (242 kB)
    Collecting zipp>=0.5Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8c/08/d3006317aefe25ea79d3b76c9650afabaf6d63d1c8443b236e7405447503/zipp-3.16.2-py3-none-any.whl (7.2 kB)
    Collecting backcallDownloading https://pypi.tuna.tsinghua.edu.cn/packages/4c/1c/ff6546b6c12603d8dd1070aa3c3d273ad4c07f5771689a7b69a550e8c951/backcall-0.2.0-py2.py3-none-any.whl (11 kB)
    Collecting boto3Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ec/9a/c0837684f1ab666add90e639944575f7325301d59d19f72b6acf6c850b78/boto3-1.28.27-py3-none-any.whl (135 kB)
    Collecting botocore<1.32.0,>=1.31.27Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e8/82/33a94da51ac24033fe83400fd08f5a54dfca5179761e0d3c8935bce538d9/botocore-1.31.27-py3-none-any.whl (11.1 MB)
    Collecting jmespath<2.0.0,>=0.7.1Downloading https://pypi.tuna.tsinghua.edu.cn/packages/31/b4/b9b800c45527aadd64d5b442f9b932b00648617eb5d63d2c7a6587b7cafc/jmespath-1.0.1-py3-none-any.whl (20 kB)
    Collecting s3transfer<0.7.0,>=0.6.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d9/17/a3b666f5ef9543cfd3c661d39d1e193abb9649d0cfbbfee3cf3b51d5af02/s3transfer-0.6.2-py3-none-any.whl (79 kB)
    Collecting decoratorDownloading https://pypi.tuna.tsinghua.edu.cn/packages/d5/50/83c593b07763e1161326b3b8c6686f0f4b0f24d5526546bee538c89837d6/decorator-5.1.1-py3-none-any.whl (9.1 kB)
    Collecting filelockDownloading https://pypi.tuna.tsinghua.edu.cn/packages/00/45/ec3407adf6f6b5bf867a4462b2b0af27597a26bd3cd6e2534cb6ab029938/filelock-3.12.2-py3-none-any.whl (10 kB)
    Collecting fsspecDownloading https://pypi.tuna.tsinghua.edu.cn/packages/e3/bd/4c0a4619494188a9db5d77e2100ab7d544a42e76b2447869d8e124e981d8/fsspec-2023.6.0-py3-none-any.whl (163 kB)
    Collecting requestsDownloading https://pypi.tuna.tsinghua.edu.cn/packages/70/8e/0e2d847013cb52cd35b38c009bb167a1a26b2ce6cd6965bf26b47bc0bf44/requests-2.31.0-py3-none-any.whl (62 kB)
    Collecting certifi>=2017.4.17Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4c/dd/2234eab22353ffc7d94e8d13177aaa050113286e93e7b40eae01fbf7c3d9/certifi-2023.7.22-py3-none-any.whl (158 kB)
    Collecting idna<4,>=2.5Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fc/34/3030de6f1370931b9dbb4dad48f6ab1015ab1d32447850b9fc94e60097be/idna-3.4-py3-none-any.whl (61 kB)
    Collecting stack-dataDownloading https://pypi.tuna.tsinghua.edu.cn/packages/6a/81/aa96c25c27f78cdc444fec27d80f4c05194c591465e491a1358d8a035bc1/stack_data-0.6.2-py3-none-any.whl (24 kB)
    Collecting asttokens>=2.1.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f3/e1/64679d9d0759db5b182222c81ff322c2fe2c31e156a59afd6e9208c960e5/asttokens-2.2.1-py2.py3-none-any.whl (26 kB)
    Collecting executing>=1.2.0Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/3c/bc3819dd8b1a1588c9215a87271b6178cc5498acaa83885211f5d4d9e693/executing-1.2.0-py2.py3-none-any.whl (24 kB)
    Collecting pure-evalDownloading https://pypi.tuna.tsinghua.edu.cn/packages/2b/27/77f9d5684e6bce929f5cfe18d6cfbe5133013c06cb2fbf5933670e60761d/pure_eval-0.2.2-py3-none-any.whl (11 kB)
    Collecting wcwidthDownloading https://pypi.tuna.tsinghua.edu.cn/packages/20/f4/c0584a25144ce20bfcf1aecd041768b8c762c1eb0aa77502a3f0baa83f11/wcwidth-0.2.6-py2.py3-none-any.whl (29 kB)
    Building wheels for collected packages: jiebaBuilding wheel for jieba (setup.py): startedBuilding wheel for jieba (setup.py): finished with status 'done'Created wheel for jieba: filename=jieba-0.42.1-py3-none-any.whl size=19314477 sha256=57e3819b1f7805dacd361648a3cb7f041a401078f47663d2f5d58f4523b2c1c7Stored in directory: /tmp/pip-ephem-wheel-cache-oey5ckcm/wheels/1a/76/68/b6d79c4db704bb18d54f6a73ab551185f4711f9730c0c15d97
    Successfully built jieba
    Installing collected packages: six, urllib3, python-dateutil, jmespath, idna, charset-normalizer, certifi, botocore, zipp, wcwidth, typing-extensions, traitlets, tqdm, s3transfer, requests, PyYAML, pure-eval, ptyprocess, parso, packaging, numpy, MarkupSafe, fsspec, filelock, executing, asttokens, Werkzeug, tzdata, torch, tokenizers, threadpoolctl, stack-data, smart-open, scipy, safetensors, regex, pytz, pyparsing, pygments, protobuf, prompt-toolkit, Pillow, pickleshare, pexpect, matplotlib-inline, kiwisolver, joblib, Jinja2, jedi, itsdangerous, importlib-resources, importlib-metadata, huggingface-hub, fonttools, decorator, cycler, contourpy, click, boto3, blinker, backcall, transformers, tensorboardX, sentencepiece, scikit-learn, pyzmq, pytorch-pretrained-bert, psutil, platformdirs, pandas, nltk, matplotlib, jieba, ipython, h5py, gensim, Flask
    Successfully installed Flask-2.3.2 Jinja2-3.1.2 MarkupSafe-2.1.3 Pillow-10.0.0 PyYAML-6.0 Werkzeug-2.3.7 asttokens-2.2.1 backcall-0.2.0 blinker-1.6.2 boto3-1.28.27 botocore-1.31.27 certifi-2023.7.22 charset-normalizer-3.2.0 click-8.1.6 contourpy-1.1.0 cycler-0.11.0 decorator-5.1.1 executing-1.2.0 filelock-3.12.2 fonttools-4.42.0 fsspec-2023.6.0 gensim-4.3.1 h5py-3.9.0 huggingface-hub-0.16.4 idna-3.4 importlib-metadata-6.8.0 importlib-resources-6.0.0 ipython-8.14.0 itsdangerous-2.1.2 jedi-0.19.0 jieba-0.42.1 jmespath-1.0.1 joblib-1.3.1 kiwisolver-1.4.4 matplotlib-3.7.2 matplotlib-inline-0.1.6 nltk-3.8.1 numpy-1.23.0 packaging-23.1 pandas-2.0.3 parso-0.8.3 pexpect-4.8.0 pickleshare-0.7.5 platformdirs-3.10.0 prompt-toolkit-3.0.39 protobuf-4.23.4 psutil-5.9.5 ptyprocess-0.7.0 pure-eval-0.2.2 pygments-2.16.1 pyparsing-3.0.9 python-dateutil-2.8.2 pytorch-pretrained-bert-0.6.2 pytz-2023.3 pyzmq-25.1.0 regex-2023.8.8 requests-2.31.0 s3transfer-0.6.2 safetensors-0.3.1 scikit-learn-1.3.0 scipy-1.11.1 sentencepiece-0.1.99 six-1.16.0 smart-open-6.3.0 stack-data-0.6.2 tensorboardX-2.6.2 threadpoolctl-3.2.0 tokenizers-0.12.1 torch-1.10.1 tqdm-4.65.0 traitlets-5.9.0 transformers-4.30.2 typing-extensions-4.7.1 tzdata-2023.3 urllib3-1.26.16 wcwidth-0.2.6 zipp-3.16.2
    WARNING: You are using pip version 20.3.3; however, version 23.2.1 is available.
    You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
    Removing intermediate container 122bad5d6b64---> 532901e8e45d
    Step 4/5 : RUN chmod -x /home/bert_model:v1/run.sh---> Running in 57624b20b496
    Removing intermediate container 57624b20b496---> 4b68e0e4fcd5
    Step 5/5 : CMD bash /home/bert_model:v1/run.sh---> Running in 1deeb0da3ee4
    Removing intermediate container 1deeb0da3ee4---> 1309aef51499
    Successfully built 1309aef51499
    Successfully tagged bert_model:v1
    d22928152eb3cfc123ea321cc25a75980d88f7dfee90a762781892c71c54c13
    

运行docker

  • 创建 run_docker.sh
docker run -it -d \--name bert_model\-v /bee/xx/:/home/xx/ \-e TZ='Asia/Shanghai' \-p 12928:12345 \-p 12929:12346 \--shm-size 16G \bert_model:v1
  • 启动服务
sh run_docker.sh

测试服务

使用curl测试服务是否正常:
我的服务是post:

curl -H "Content-Type:application/json" -X POST -d '{"text": "xxxxxx"}' http://localhost:12928/xxx

测试响应时间:

time curl -H "Content-Type:application/json" -X POST -d '{"text": "xxxxxx"}' http://localhost:12928/xxx

返回:

{"code": 10000, "label": ["aaa"]}
real    0m0.157s
user    0m0.010s
sys     0m0.006s

参考

  1. https://www.runoob.com/docker/docker-dockerfile.html
  2. https://zhuanlan.zhihu.com/p/98855346

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/40468.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

卷积神经网络CNN

卷积神经网络CNN 1 应用领域1 检测任务2 分类和检索3 超分辨率重构4 医学任务5 无人驾驶6 人脸识别 2 卷积的作用3 卷积特征值计算方法4 得到特征图表示5 步长和卷积核大小对结果的影响1 步长2 卷积核 6 边缘填充方法7 特征图尺寸计算与参数共享8 池化层的作用9 整体网络架构10…

【GitLab私有仓库】如何在Linux上用Gitlab搭建自己的私有库并配置cpolar内网穿透?

文章目录 前言1. 下载Gitlab2. 安装Gitlab3. 启动Gitlab4. 安装cpolar5. 创建隧道配置访问地址6. 固定GitLab访问地址6.1 保留二级子域名6.2 配置二级子域名 7. 测试访问二级子域名 前言 GitLab 是一个用于仓库管理系统的开源项目&#xff0c;使用Git作为代码管理工具&#xf…

ngModel和formControlName处理表单控件

ngModel 和 formControlName 不能同时在同一个表单控件上使用&#xff1b; 二者都用于在 Angular 中处理表单控件的值&#xff0c;但是它们的底层实现方式不同。 ngModel 是 Angular 提供的双向数据绑定指令&#xff0c;它可以将表单控件的值与组件类中的属性进行双向绑定。当…

软考笔记——10.项目管理

进度管理 进度管理就是采用科学的方法&#xff0c;确定进度目标&#xff0c;编制进度计划和资源供应计划&#xff0c;进行进度控制&#xff0c;在与质量、成本目标协调的基础上&#xff0c;实现工期目标。 具体来说&#xff0c;包括以下过程&#xff1a; (1) 活动定义&#…

HLS实现FIR低通滤波器+System Generator仿真

硬件&#xff1a;ZYNQ7010 软件&#xff1a;MATLAB 2019b、Vivado 2017.4、HLS 2017.4、System Generator 2017.4 1、MATLAB设计低通滤波器 FPGA系统时钟 50MHz&#xff0c;也是采样频率。用 MATLAB 生成 1MHz 和 10MHz 的正弦波叠加的信号&#xff0c;并量化为 14bit 整数。把…

css 用过渡实现,鼠标离开li时,背景色缓慢消息的样式

要实现鼠标悬停时背景颜色变为黄色&#xff0c;鼠标离开时背景颜色慢慢消失并变回白色的效果&#xff0c; 可以使用CSS的过渡&#xff08;transition&#xff09;属性 li {background: #fff;color: #000;transition: background 0.5s ease-out; }li:hover {background: #fbb31…

Web网页浏览器远程访问jupyter notebook服务器【内网穿透】

文章目录 前言1. Python环境安装2. Jupyter 安装3. 启动Jupyter Notebook4. 远程访问4.1 安装配置cpolar内网穿透4.2 创建隧道映射本地端口 5. 固定公网地址 前言 Jupyter Notebook&#xff0c;它是一个交互式的数据科学和计算环境&#xff0c;支持多种编程语言&#xff0c;如…

Hyper-v导致Vmware window无法启动崩溃记录

最近有几次vmware启动window10直接崩溃情况&#xff0c;显示蓝屏报错。一开始没在意&#xff0c;以为是因为固态硬盘错了几个字节导致的&#xff1f; 但后来想想不对啊。vmware用了也有10来年了&#xff0c;稳得一笔&#xff0c;在仔细思考了一下后发现打不开的win10这三个虚拟…

Oracle/PL/SQL奇技淫巧之Lable标签与循环控制

在一些存储过程场景中&#xff0c;可能存在需要在满足某些条件时跳出循环的场景&#xff0c; 但是在PL/SQL中&#xff0c;不能使用break语句直接跳出循环, 但是可以通过lable标签的方式跳出循环&#xff0c;例&#xff1a; <<outer_loop>> FOR i IN 1..5 LOOPDBMS…

Python批量替换Excel和Word中的关键字

一、问题的提出 有时&#xff0c;我们手头上有多个Excel或者Word文件&#xff0c;但是领导突然要求对某几个术语进行批量的修改&#xff0c;你是不是有要崩溃的感觉。因为这么多文件&#xff0c;要一个一个地打开文件&#xff0c;再进行批量替换修改&#xff0c;几个文件还好&…

设计模式之构建器(Builder)C++实现

1、构建器提出 在软件功能开发中&#xff0c;有时面临“一个复杂对象”的创建工作&#xff0c;该对象的每个功能接口由于需求的变化&#xff0c;会使每个功能接口发生变化&#xff0c;但是该对象使用每个功能实现一个接口的流程是稳定的。构建器就是解决该类现象的。构建就是定…

【Java】项目管理工具Maven的安装与使用

文章目录 1. Maven概述2. Maven的下载与安装2.1 下载2.2 安装 3. Maven仓库配置3.1 修改本地仓库配置3.2 修改远程仓库配置3.3 修改后的settings.xml 4. 使用Maven创建项目4.1 手工创建Java项目4.2 原型创建Java项目4.3 原型创建Web项目 5. Tomcat启动Web项目5.1 使用Tomcat插件…

【CTF-web】备份是个好习惯(查找备份文件、双写绕过、md5加密绕过)

题目链接&#xff1a;https://ctf.bugku.com/challenges/detail/id/83.html 经过扫描可以找到index.php.bak备份文件&#xff0c;下载下来后打开发现是index.php的原代码&#xff0c;如下图所示。 由代码可知我们要绕过md5加密&#xff0c;两数如果满足科学计数法的形式的话&a…

模型预测笔记(一):数据清洗及可视化、模型搭建、模型训练和预测代码一体化和对应结果展示(可作为baseline)

模型预测 一、导入关键包二、如何载入、分析和保存文件三、修改缺失值3.1 众数3.2 平均值3.3 中位数3.4 0填充 四、修改异常值4.1 删除4.2 替换 五、数据绘图分析5.1 饼状图5.1.1 绘制某一特征的数值情况&#xff08;二分类&#xff09; 5.2 柱状图5.2.1 单特征与目标特征之间的…

OpenCV基本操作——算数操作

目录 图像的加法图像的混合 图像的加法 两个图像应该具有相同的大小和类型&#xff0c;或者第二个图像可以是标量值 注意&#xff1a;OpenCV加法和Numpy加法之间存在差异。OpenCV的加法是饱和操作&#xff0c;而Numpy添加的是模运算 import numpy as np import cv2 as cv imp…

[数据集][目标检测]钢材表面缺陷目标检测数据集VOC格式2279张10类别

数据集格式&#xff1a;Pascal VOC格式(不包含分割路径的txt文件和yolo格式的txt文件&#xff0c;仅仅包含jpg图片和对应的xml) 图片数量(jpg文件个数)&#xff1a;2279 标注数量(xml文件个数)&#xff1a;2279 标注类别数&#xff1a;10 标注类别名称:["yueyawan",&…

Qt 窗口随鼠标移动效果

实现在窗口任意位置按下鼠标左键都可以移动窗口的效果&#xff0c;完整代码如下&#xff1a; mainwindow.h&#xff1a; #ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include <QMouseEvent>QT_BEGIN_NAMESPACE namespace Ui { class MainW…

PHP混淆加密以及常用的一些加密工具

PHP混淆加密是一种将源代码转换为难以理解和阅读的方式&#xff0c;以保护代码的安全性。以下是一些常见的PHP混淆加密方法&#xff1a; 代码压缩&#xff1a;使用代码压缩工具&#xff08;如UglifyJS&#xff09;将PHP代码压缩为一行&#xff0c;去除空格、换行符等可读性的字…

jenkins 连接服务器,提示Can‘t connect to server

在Jenkins 添加服务器时&#xff0c;提示 Cant connect to server&#xff0c;如图 搞了好久&#xff0c;不知道为什么不行~原来是行的&#xff0c;现在删了 新建一个也不行。

2023牛客暑期多校训练营8-C Clamped Sequence II

2023牛客暑期多校训练营8-C Clamped Sequence II https://ac.nowcoder.com/acm/contest/57362/C 文章目录 2023牛客暑期多校训练营8-C Clamped Sequence II题意解题思路代码 题意 解题思路 先考虑不加紧密度的情况&#xff0c;要支持单点修改&#xff0c;整体查询&#xff0…