将tensorflow训练好的模型部署成sercice服务,并做预测
分类:
文章
•
2025-02-06 14:18:10
web框架用的是tornado,模型加载在上一篇博客已经提到,下面写了一个service.py文件:
import tornado.web
import logging
import string
import os
import sys
from predict import predict
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(message)s')
ch.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(ch)
def work(text):
if text is None:
return 0.
else:
return predict(text)
class MainHandler(tornado.web.RequestHandler):
def post(self):
text = self.get_argument('text')
p = work(text)
self.write(p)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
text = self.get_argument('text')
p = work(text)
self.render('index.html',text=text,result='{0}'.format(p))
application=tornado.web.Application(
handlers=[(r"/", MainHandler),
(r'/demo', IndexHandler)],
template_path=os.path.join(os.path.dirname(__file__), "templates")
)
if __name__=='__main__':
if len(sys.argv)<2:
port=8410
else:
port=string.atoi(sys.argv[1])
application.listen(port)
logger.info('Starting server:port=%d, use <Ctrl-C> to stop' % port)
tornado.ioloop.IOLoop.instance().start()
templates/index.html问价内容:
<!DOCTYPE html>
<html>
<head><title>text abumse server</title></head>
<body>
<form method="get" action="/demo">
<p>text content: <input type="text" name="text" value="{{text}}" style="width: 800px"><input type="submit"></p>
</form>
<p>{{result}}</p>
<p><img src="{{text}}" width="600px"></p>
</body>
</html>
启动service.py文件:
/anaconda/bin/python3 /Users/shuubiasahi/Documents/python/credit-tftextclassify-abuse/service.py
2017-10-13 15:58:20.625704: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-10-13 15:58:20.625723: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-10-13 15:58:20.625728: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-10-13 15:58:20.625733: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-10-13 15:58:21,732 - Starting server:port=8410, use <Ctrl-C> to stop
本身我是用的cnn做的辱骂识别模型,预测的概率还挺高,看到最后别喷啊 ,哈哈哈哈。。。。。