当前位置: 首页 > news >正文

【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

相关文章:

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

【AI实战】BERT 文本分类模型自动化部署之 dockerfile BERTBERT 文本分类模型基于中文预训练bert的文本分类模型针对多分类模型的loss函数样本不均衡时多标签分类时 dockerfile编写 dockerfilebuild镜像运行docker测试服务 参考 本文主要介绍&#xff1a; 基于BERT的文本分类模…...

深入理解 Flutter 图片加载原理 | 京东云技术团队

前言 随着Flutter稳定版本逐步迭代更新&#xff0c;京东APP内部的Flutter业务也日益增多&#xff0c;Flutter开发为我们提供了高效的开发环境、优秀的跨平台适配、丰富的功能组件及动画、接近原生的交互体验&#xff0c;但随之也带来了一些OOM问题&#xff0c;通过线上监控信息…...

Spring Boot 支持多种环境,包括开发环境、测试环境、预发布环境和生产环境。

Spring Boot 支持多种环境&#xff0c;包括开发环境、测试环境、预发布环境和生产环境。不同的环境具有不同的配置&#xff0c;可以在不同的环境中对应用程序进行测试、验证和部署。以下是每种环境的用途和相应的代码案例。 开发环境 开发环境是开发人员在本地进行开发的环境&…...

Ctfshow web入门 命令执行RCE篇 web29-web77 与 web118-web124 详细题解 持续更新中(预计8.18完成)~

Ctfshow 命令执行 web29 pregmatch是正则匹配函数&#xff0c;匹配是否包含flag&#xff0c;if(!preg_match("/flag/i", $c))&#xff0c;/i忽略大小写 可以利用system来间接执行系统命令 flag采用f*绕过&#xff0c;或者mv fl?g.php 1.txt修改文件名&#xff0c…...

合宙Air724UG LuatOS-Air script lib API--wifiRil

wifiRil Table of Contents wifiRil wifiRil.regRsp(head, fnc, typ, formt) wifiRil.regUrc(prefix, handler) wifiRil.deRegUrc(prefix) wifiRil.request(cmd, arg, onrsp, delay, param) wifiRil 模块功能&#xff1a;esp8266 wifi模块AT命令交互管理 wifiRil.regRsp(head,…...

python读取word/pdf文档,指定文字内容和图片

读编号转文件夹目录然后放图片进去那个 一 先将word转为PDF pdf 读起来比较方便&#xff0c; 按页码读取文件: import pdfplumber from PIL import Image import cv2 import numpy as np import re import os import logging import iodef create_folder(folder_name):if not…...

零售行业供应链管理核心KPI指标(二) – 线上订单履行周期

一般品牌零售商有一个大的渠道就是全国连锁的商超、大卖场&#xff0c;非常重要的渠道&#xff0c;要去铺货。同类型的产品都在竞争这个大渠道&#xff0c;但商超、大卖场在这类产品的容量是有限的&#xff0c;所以各个品牌就要去争夺整个容量&#xff0c;看谁在有限的容量里占…...

VGG分类实战:猫狗分类

关于数据集 数据集选择的是Kaggle上的Cat and Dog&#xff0c;猫狗图片数量上达到了上万张。你可以通过这里进入Kaggle下载数据集Cat and Dog | Kaggle。 在我的Github仓库当中也放了猫狗图片各666张。 VGG网络 VGG的主要特点是使用了一系列具有相同尺寸 3x3 大小的卷积核进…...

C++11并发与多线程笔记(3)线程传参详解,detach()大坑,成员函数做线程函数

C11并发与多线程笔记&#xff08;3&#xff09;线程传参详解&#xff0c;detach 大坑&#xff0c;成员函数做线程函数 1、传递临时对象作为线程参数1.1 要避免的陷阱11.2 要避免的陷阱21.3 总结 2、临时对象作为线程参数2.1 线程id概念2.2 临时对象构造时机抓捕 3、传递类对象…...

说几个常见的语法糖

目录 面试回答 知识扩展 如何解语法糖&#xff1f; 糖块一、swith 支持 String 与枚举 糖块二、泛型 糖块三、自动装箱与拆箱 糖块四、枚举 糖块五、条件编译 糖块六、断言 糖块七、数值字面量 糖块八、for-each 糖块九、try-with-resource 可能遇到的坑 泛型 自…...

Python文件操作与输入输出:从基础到高级应用

文章目录 &#x1f340;引言&#x1f340;文件操作基础&#x1f340;上下文管理器与文件自动关闭&#x1f340;文件的迭代与逐行读取&#x1f340;文件的其他常见操作&#x1f340;输入输出基础&#x1f340; 文件输入输出&#x1f340;格式化输出&#x1f340;高级文件操作&am…...

leetcode算法题--找出最安全路径

原题链接&#xff1a;https://leetcode.cn/problems/find-the-safest-path-in-a-grid/description/ func maximumSafenessFactor(grid [][]int) int {n : len(grid)type pair struct {x inty int}p : make([]pair, 0)dis : make([][]int, n)for i : range dis {dis[i] make([…...

神经网络基础-神经网络补充概念-34-正则化

概念 正则化是一种用于控制模型复杂度并防止过拟合的技术&#xff0c;在机器学习和深度学习中广泛应用。它通过在损失函数中添加一项惩罚项来限制模型的参数&#xff0c;从而使模型更倾向于选择简单的参数配置。 理解 L1 正则化&#xff08;L1 Regularization&#xff09;&a…...

idea打jar包

目录 1、打包设置 2、打包介绍 3、开始打包 1、打包设置 先设置要打包的模块信息&#xff0c;即打包进去的内容。如下图所示&#xff1a;File --> Project Structure --> Artifacts&#xff0c;点击&#xff0b;号完成模块创建&#xff0c;其中有两种方式&#xff1a;…...

民安汇智(第三方旅游服务暗访)开展旅游景区度假区明察暗访复核检查服务

近日&#xff0c;民安汇智受客户委托对该市某旅游景区度假区进行明察暗访复核检查工作。 民安汇智通过实地调研、体验式暗访等各种方式对该市范围内3A级以上旅游景区、旅游度假区及2022年新创建的3A级以上旅游景区、旅游度假区进行明察暗访复核检查&#xff0c;对照《旅游景区…...

《游戏编程模式》学习笔记(六)单例模式 Singleton Pattern

单例模式的定义 保证一个类只有一个实例&#xff0c;并且提供了访问该实例的全局访问点。 定义这种东西一般都是不说人话的&#xff0c;要想要理解这句话的意思&#xff0c;我们得把它揉开了才能搞明白。 我们先看前半句 “保证一个类只有一个实例”&#xff0c;单例一般使用…...

《Go 语言第一课》课程学习笔记(二)

初窥门径&#xff1a;一个 Go 程序的结构是怎样的&#xff1f; 创建“hello&#xff0c;world”示例程序 在 Go 语言中编写一个可以打印出“hello&#xff0c;world”的示例程序&#xff0c;我们只需要简单两步&#xff0c;一是创建文件夹&#xff0c;二是开始编写和运行。通…...

神经网络基础-神经网络补充概念-26-前向和反向传播

简单比较 前向传播&#xff08;Forward Propagation&#xff09;&#xff1a; 前向传播是神经网络中的正向计算过程&#xff0c;用于从输入数据开始&#xff0c;逐层计算每个神经元的输出值&#xff0c;直到得到最终的预测值。在前向传播过程中&#xff0c;我们按以下步骤进行…...

Gin路由组

Gin路由组 文章目录 Gin路由组接收任意请求的路由接收没有被定义的路由路由组完整代码 接收任意请求的路由 区别于以往的GET()函数只能处理一种请求,Any()函数可以处理各种函数 语法: func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc){} 案例: …...

安防监控视频云存储平台EasyNVR通道频繁离线的原因排查与解决

安防视频监控汇聚EasyNVR视频集中存储平台&#xff0c;是基于RTSP/Onvif协议的安防视频平台&#xff0c;可支持将接入的视频流进行全平台、全终端分发&#xff0c;分发的视频流包括RTSP、RTMP、HTTP-FLV、WS-FLV、HLS、WebRTC等格式。为了满足用户的集成与二次开发需求&#xf…...

Redis-分布式锁!

分布式锁&#xff0c;顾名思义&#xff0c;分布式锁就是分布式场景下的锁&#xff0c;比如多台不同机器上的进程&#xff0c;去竞争同一项资源&#xff0c;就是分布式锁。 分布式锁特性 互斥性:锁的目的是获取资源的使用权&#xff0c;所以只让一个竞争者持有锁&#xff0c;这…...

Unity如何把游戏导出成手机安装包

文章目录 前言使用环境步骤添加场景构建APK 前言 本文章主要演示了&#xff0c;如何将制作好的游戏&#xff0c;导出成APK&#xff0c;安装到手机上。 使用环境 Unity2022。 步骤 首先打开你的项目&#xff0c;然后选择菜单栏的“File” > “Build Settings…”&#xf…...

使用爱校对软件保证公文材料质量的关键步骤

在日常的公文处理中&#xff0c;保证材料质量是每个企业和机构都追求的目标。而要实现这个目标&#xff0c;使用正确的工具是关键。爱校对软件正是这样一款专业的校对工具&#xff0c;它可以帮助我们保证公文材料的质量。接下来&#xff0c;让我们一起来看看使用爱校对软件保证…...

Spring Data Elasticsearch 的简单使用

目录 一、简介 二、配置 三、映射 四、 常用方法 五、操作&#xff08;重点&#xff09; 1、对索引表的操作 2、对文档的操作&#xff08;重点&#xff09; &#xff08;1&#xff09;、添加文档 &#xff08;2&#xff09;、删除文档 &#xff08;3&#xff09;、查询…...

2024」预备研究生mem-角平分线定理中线定理垂线定理、射影定理

一、角平分线定理 二、中线定理 三、垂线定理、射影定理 垂线定理 射影定理&#xff1a; 四、课后题...

nginx部署时http接口正常,ws接口404

可以这么配置 map $http_upgrade $connection_upgrade {default upgrade; close; }upstream wsbackend{server ip1:port1;server ip2:port2;keepalive 1000; }server {listen 20038;location /{ proxy_http_version 1.1;proxy_pass http://wsbackend;proxy_redirect off;proxy…...

数学建模的概念和学习方法(什么是数学建模)

一、初步认识数学建模 数学建模是将数学方法和技巧应用于实际问题的过程。它涉及使用数学模型来描述和分析现实世界中的现象、系统或过程&#xff0c;并通过数学分析和计算来预测、优化或解决问题。数学建模可以应用于各种领域&#xff0c;包括自然科学、工程、经济学、环境科学…...

ChatGPT在智能安全监测和入侵检测中的应用如何?

ChatGPT在智能安全监测和入侵检测领域具有潜在的应用价值。虽然ChatGPT主要是一个基于自然语言处理的模型&#xff0c;但结合其他技术和领域专业知识&#xff0c;它可以用于生成和分析文本数据&#xff0c;提供实时安全警报、威胁情报等&#xff0c;从而在安全监测和入侵检测方…...

智能数据建模软件DTEmpower 2023R2新版本功能介绍

DTEmpower是由天洑软件自主研发的一款通用的智能数据建模软件&#xff0c;致力于帮助工程师及工科专业学生&#xff0c;利用工业领域中的仿真、试验、测量等各类数据进行挖掘分析&#xff0c;建立高质量的数据模型&#xff0c;实现快速设计评估、实时仿真预测、系统参数预警、设…...

BDA初级分析——认识SQL,认识基础语法

一、认识SQL SQL作为实用技能&#xff0c;热度高、应用广泛 在对数据分析人员的调查中SQL长期作为热度排名第-一的编程语言超过Python和R SQL&#xff1a;易学易用&#xff0c;高效强大的语言 SQL&#xff1a;Structured Query Language 结构化查询语言 SQL&#xff1a;易学…...