一天搞定两大技术点,成就满满。

一,dockerfile

FROM harbor.xxx.com.cn/3rd_part/tensorflow:1.14.0-gpu-py3-jupyter

LABEL "maintainer"="xxx4k"
LABEL "version"="1.0"

#COPY numpy-1.17.4-cp36-none-linux_x86_64.whl /tmp/
#COPY pyzmq-18.1.0-cp36-none-linux_x86_64.whl /tmp/

#RUN pip install /tmp/numpy-1.17.4-cp36-none-linux_x86_64.whl \
#    && pip install /tmp/pyzmq-18.1.0-cp36-none-linux_x86_64.whl \
RUN     pip install --no-cache-dir \
      -i http://xxx.com.cn/root/pypi/+simple/  \
      --trusted-host xxx.com.cn \
      tensorflow==1.14.0 bert-base==0.0.9 flask flask_compress flask_cors  flask_json \
    && rm -rf /tmp/* \
    && rm -rf ~/.cache/pip \
    && echo "finished"

二,修改Http.py

参考URL:
https://www.jianshu.com/p/beab4df088df
https://blog.csdn.net/jusang486/article/details/82382358
https://blog.csdn.net/AbeBetter/article/details/77652457
https://blog.csdn.net/anh3000/article/details/83047027

如果在flask里,使用app.run()的模式,输出总会提示:

* Serving Flask app "bert_base.server.http" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
I1113 :: _internal.py:] * Running on http://0.0.0.0:8091/ (Press CTRL+C to quit)

那如何改进呢?
可以选用nginx或是tornado。
如果是代码模式下,tornador是首选。

from multiprocessing import Process
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
import asyncio
from termcolor import colored

from .helper import set_logger

class BertHTTPProxy(Process):
    def __init__(self, args):
        super().__init__()
        self.args = args

    def create_flask_app(self):
        try:
            from flask import Flask, request
            from flask_compress import Compress
            from flask_cors import CORS
            from flask_json import FlaskJSON, as_json, JsonError
            from bert_base.client import ConcurrentBertClient
        except ImportError:
            raise ImportError('BertClient or Flask or its dependencies are not fully installed, '
                              'they are required for serving HTTP requests.'
                              'Please use "pip install -U bert-serving-server[http]" to install it.')

        # support up to 10 concurrent HTTP requests
        bc = ConcurrentBertClient(max_concurrency=self.args.http_max_connect,
                                  port=self.args.port, port_out=self.args.port_out,
                                  output_fmt='list', mode=self.args.mode)
        app = Flask(__name__)
        logger = set_logger(colored('PROXY', 'red'))

        @app.route('/status/server', methods=['GET'])
        @as_json
        def get_server_status():
            return bc.server_status

        @app.route('/status/client', methods=['GET'])
        @as_json
        def get_client_status():
            return bc.status

        @app.route('/encode', methods=['POST'])
        @as_json
        def encode_query():
            data = request.form if request.form else request.json
            try:
                logger.info('new request from %s' % request.remote_addr)
                print(data)
                return {'id': data['id'],
                        'result': bc.encode(data['texts'], is_tokenized=bool(
                            data['is_tokenized']) if 'is_tokenized' in data else False)}

            except Exception as e:
                logger.error('error when handling HTTP request', exc_info=True)
                raise JsonError(description=str(e), type=str(type(e).__name__))

        CORS(app, origins=self.args.cors)
        FlaskJSON(app)
        Compress().init_app(app)
        return app

    def run(self):
        app = self.create_flask_app()
        # app.run(port=self.args.http_port, threaded=True, host='0.0.0.0')
        # tornado 5 中引入asyncio.set_event_loop即可
        asyncio.set_event_loop(asyncio.new_event_loop())
        http_server = HTTPServer(WSGIContainer(app))
        http_server.listen(self.args.http_port)
        IOLoop.instance().start()

三,启动命令

bert-base-serving-start -bert_model_dir  -http_port  -port  -port_out  

最新文章

  1. 浏览器对localstorage的支持情况以及localstorage在saas系统中的应用实践思考
  2. Upgrade from SharePoint 2010 to SharePoint 2016
  3. UIScrollView的delegate方法妙用之让UICollectionView滑动到某个你想要的位置
  4. knockoutJS学习笔记01:从拼接字符串到编写模板引擎
  5. 让人一用钟情的VS插件系列之一——Web Essentials(Web开发必备利器)
  6. nginx实现访问网站或目录密码认证保护
  7. iOS边练边学--NSURLSession、NSURLSessionTask的介绍与使用以及url中包含了中文的处理方法
  8. Linux配置全局环境变量的方法
  9. 几个代码片段-计算程序运行时间+获得当前目录+生成MD5
  10. POJ2222+暴力搜索
  11. DHTML【8】--CSS
  12. asp.net core 部署到服务器之后外网访问不了
  13. [PA 2014]Bohater
  14. mac无法访问samba共享 提示输入用户名密码
  15. paddle——docker实践
  16. 《DSP using MATLAB》Problem 6.1
  17. Flutter应用打包发布
  18. 解题:SDOI 2017 硬币游戏
  19. asp.net MVC把Areas区域绑定成二级域名
  20. oracle profile 概要文件

热门文章

  1. UWP GridView切换数据时界面闪动
  2. MySql数据库基础之数据库简介及安装
  3. 【解决】Failed to restart network.service: Unit network.service not found.
  4. k8s 使用 Init Container 确保依赖的服务已经启动
  5. How to: Use XPO Upcasting in XAF 如何:在 XAF 中使用 XPO 强制转换
  6. DotNet Core中使用dapper
  7. jqgrid addRowData报错
  8. arcgis api 4.x for js 自定义叠加图片图层实现地图叠加图片展示(附源码下载)
  9. orm终极大爆炸
  10. mongo shell 通过返回信息定位错误点