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

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

本文主要介绍:

  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 = gamma
        self.reduction = reduction

    def 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_loss
    
def focal_loss(outputs, targets):
    
    import json
    with 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 json
    with 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:latest

ADD bert_model.tar /home

RUN ["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.0
      Downloading 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.2
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fa/1a/f191d32818e5cd985bdd3f47a6e4f525e2db1ce5e8150045ca0c31813686/Flask-2.3.2-py3-none-any.whl (96 kB)
    Collecting gensim==4.3.1
      Downloading 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.0
      Downloading 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.4
      Downloading 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.0
      Downloading 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.0
      Downloading 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.0
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/52/d1/f70cdafba20030cbc1412d7a7d6a89c5035071835cc50e47fc5ed8da553c/ipython-8.14.0-py3-none-any.whl (798 kB)
    Collecting jieba==0.42.1
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c6/cb/18eeb235f833b726522d7ebed54f2278ce28ba9438e3135ab0278d9792a2/jieba-0.42.1.tar.gz (19.2 MB)
    Collecting joblib==1.3.1
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/08/9dcdaa5aac4634e4c23af26d92121f7ce445c630efa0d3037881ae2407fb/joblib-1.3.1-py3-none-any.whl (301 kB)
    Collecting matplotlib==3.7.2
      Downloading 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.6
      Downloading 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.1
      Downloading 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.0
      Downloading 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.1
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ab/c3/57f0601a2d4fe15de7a553c00adbc901425661bf048f2a22dfc500caf121/packaging-23.1-py3-none-any.whl (48 kB)
    Collecting pandas==2.0.3
      Downloading 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.3
      Downloading 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.0
      Downloading 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.5
      Downloading 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.0
      Downloading 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.0
      Downloading 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.39
      Downloading 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.4
      Downloading 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.5
      Downloading 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.9
      Downloading 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.2
      Downloading 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.2
      Downloading 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.3
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/99/ad6bd37e748257dd70d6f85d916cafe79c0b0f5e2e95b11f7fbc82bf3110/pytz-2023.3-py2.py3-none-any.whl (502 kB)
    Collecting PyYAML==6.0
      Downloading 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.0
      Downloading 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.1
      Downloading 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.0
      Downloading 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.1
      Downloading 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.99
      Downloading 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.2
      Downloading 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.0
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/81/12/fd4dea011af9d69e1cad05c75f3f7202cdcbeac9b712eea58ca779a72865/threadpoolctl-3.2.0-py3-none-any.whl (15 kB)
    Collecting tokenizers==0.12.1
      Downloading 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.1
      Downloading 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.0
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e6/02/a2cff6306177ae6bc73bc0665065de51dfb3b9db7373e122e2735faf0d97/tqdm-4.65.0-py3-none-any.whl (77 kB)
    Collecting transformers==4.30.2
      Downloading 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.16
      Downloading 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.2
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0d/f1/5f39e771cd730d347539bb74c6d496737b9d5f0a53bc9fdbf3e170f1ee48/blinker-1.6.2-py3-none-any.whl (13 kB)
    Collecting click>=8.1.3
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/1a/70/e63223f8116931d365993d4a6b7ef653a4d920b41d03de7c59499962821f/click-8.1.6-py3-none-any.whl (97 kB)
    Collecting contourpy>=1.0.1
      Downloading 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.10
      Downloading 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.0
      Downloading 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.2
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/68/5f/447e04e828f47465eeab35b5d408b7ebaaaee207f48b7136c5a7267a30ae/itsdangerous-2.1.2-py3-none-any.whl (15 kB)
    Collecting jedi>=0.16
      Downloading 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.2
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bc/c3/f068337a370801f372f2f8f6bad74a5c140f6fda3d9de154052708dd3c65/Jinja2-3.1.2-py3-none-any.whl (133 kB)
    Collecting kiwisolver>=1.0.1
      Downloading 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.0
      Downloading 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.5
      Downloading 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.0
      Downloading 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.3
      Downloading 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.5
      Downloading 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.1
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/47/80/c2d1bdd36c6b64ae566d9a29724291510e4f3796ce99639d3c2999286284/smart_open-6.3.0-py3-none-any.whl (56 kB)
    Collecting traitlets>=5
      Downloading 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.3
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ec/6b/63cc3df74987c36fe26157ee12e09e8f9db4de771e0f3404263117e75b95/typing_extensions-4.7.1-py3-none-any.whl (33 kB)
    Collecting tzdata>=2022.1
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d5/fb/a79efcab32b8a1f1ddca7f35109a50e4a80d42ac1c9187ab46522b2407d7/tzdata-2023.3-py2.py3-none-any.whl (341 kB)
    Collecting Werkzeug>=2.3.3
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9b/59/a7c32e3d8d0e546a206e0552a2c04444544f15c1da4a01df8938d20c6ffc/werkzeug-2.3.7-py3-none-any.whl (242 kB)
    Collecting zipp>=0.5
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8c/08/d3006317aefe25ea79d3b76c9650afabaf6d63d1c8443b236e7405447503/zipp-3.16.2-py3-none-any.whl (7.2 kB)
    Collecting backcall
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4c/1c/ff6546b6c12603d8dd1070aa3c3d273ad4c07f5771689a7b69a550e8c951/backcall-0.2.0-py2.py3-none-any.whl (11 kB)
    Collecting boto3
      Downloading 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.27
      Downloading 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.1
      Downloading 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.0
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d9/17/a3b666f5ef9543cfd3c661d39d1e193abb9649d0cfbbfee3cf3b51d5af02/s3transfer-0.6.2-py3-none-any.whl (79 kB)
    Collecting decorator
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d5/50/83c593b07763e1161326b3b8c6686f0f4b0f24d5526546bee538c89837d6/decorator-5.1.1-py3-none-any.whl (9.1 kB)
    Collecting filelock
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/00/45/ec3407adf6f6b5bf867a4462b2b0af27597a26bd3cd6e2534cb6ab029938/filelock-3.12.2-py3-none-any.whl (10 kB)
    Collecting fsspec
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e3/bd/4c0a4619494188a9db5d77e2100ab7d544a42e76b2447869d8e124e981d8/fsspec-2023.6.0-py3-none-any.whl (163 kB)
    Collecting requests
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/70/8e/0e2d847013cb52cd35b38c009bb167a1a26b2ce6cd6965bf26b47bc0bf44/requests-2.31.0-py3-none-any.whl (62 kB)
    Collecting certifi>=2017.4.17
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4c/dd/2234eab22353ffc7d94e8d13177aaa050113286e93e7b40eae01fbf7c3d9/certifi-2023.7.22-py3-none-any.whl (158 kB)
    Collecting idna<4,>=2.5
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fc/34/3030de6f1370931b9dbb4dad48f6ab1015ab1d32447850b9fc94e60097be/idna-3.4-py3-none-any.whl (61 kB)
    Collecting stack-data
      Downloading 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.0
      Downloading 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.0
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/3c/bc3819dd8b1a1588c9215a87271b6178cc5498acaa83885211f5d4d9e693/executing-1.2.0-py2.py3-none-any.whl (24 kB)
    Collecting pure-eval
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2b/27/77f9d5684e6bce929f5cfe18d6cfbe5133013c06cb2fbf5933670e60761d/pure_eval-0.2.2-py3-none-any.whl (11 kB)
    Collecting wcwidth
      Downloading 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: jieba
      Building wheel for jieba (setup.py): started
      Building 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=57e3819b1f7805dacd361648a3cb7f041a401078f47663d2f5d58f4523b2c1c7
      Stored 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
相关推荐
大多_C1 天前
BERT outputs
人工智能·深度学习·bert
通信仿真实验室3 天前
BERT模型入门(1)BERT的基本概念
人工智能·深度学习·自然语言处理·bert·transformer
goomind3 天前
BERT模型
人工智能·深度学习·bert
通信仿真实验室4 天前
BERT模型入门(2)BERT的工作原理
人工智能·深度学习·自然语言处理·bert·transformer
weixin_543662867 天前
BERT的中文问答系统55
人工智能·python·bert
微雨盈萍cbb9 天前
BERT--自然语言处理的革命性进展
人工智能·自然语言处理·bert
Jacob_AI10 天前
为什么 Bert 的三个 Embedding 可以进行相加?
人工智能·bert·embedding
傅科摆 _ py11 天前
ANOMALY BERT 解读
人工智能·深度学习·bert
通信仿真实验室11 天前
Google BERT入门(5)Transformer通过位置编码学习位置
人工智能·深度学习·神经网络·自然语言处理·nlp·bert·transformer
python_知世14 天前
【NLP修炼系列之Bert】Bert多分类&多标签文本分类实战(附源码下载)
人工智能·深度学习·自然语言处理·大模型·nlp·bert·多标签文本分类