将tensorflow训练好的模型部署成sercice服务,并做预测

web框架用的是tornado,模型加载在上一篇博客已经提到,下面写了一个service.py文件:

 
    
  1. import tornado.web
  2. import logging
  3. import string
  4. import os
  5. import sys
  6. from predict import predict
  7. logger = logging.getLogger('mylogger')
  8. logger.setLevel(logging.DEBUG)
  9. # 再创建一个handler,用于输出到控制台
  10. ch = logging.StreamHandler()
  11. ch.setLevel(logging.DEBUG)
  12. # 定义handler的输出格式
  13. formatter = logging.Formatter('%(asctime)s - %(message)s')
  14. ch.setFormatter(formatter)
  15. # 给logger添加handler
  16. logger.addHandler(ch)
  17. def work(text):
  18. if text is None:
  19. return 0.
  20. else:
  21. return predict(text)
  22. class MainHandler(tornado.web.RequestHandler):
  23. def post(self):
  24. text = self.get_argument('text')
  25. p = work(text)
  26. self.write(p)
  27. class IndexHandler(tornado.web.RequestHandler):
  28. def get(self):
  29. text = self.get_argument('text')
  30. p = work(text)
  31. self.render('index.html',text=text,result='{0}'.format(p))
  32. application=tornado.web.Application(
  33. handlers=[(r"/", MainHandler),
  34. (r'/demo', IndexHandler)],
  35. template_path=os.path.join(os.path.dirname(__file__), "templates")
  36. )
  37. if __name__=='__main__':
  38. if len(sys.argv)<2:
  39. port=8410
  40. else:
  41. port=string.atoi(sys.argv[1])
  42. application.listen(port)
  43. logger.info('Starting server:port=%d, use to stop' % port)
  44. tornado.ioloop.IOLoop.instance().start()


templates/index.html问价内容:

 
    
  1. DOCTYPE html>
  2. <html>
  3. <head><title>text abumse servertitle>head>
  4. <body>
  5. <form method="get" action="/demo">
  6. <p>text content: <input type="text" name="text" value="{{text}}" style="width: 800px"><input type="submit">p>
  7. form>
  8. <p>{{result}}p>
  9. <p><img src="{{text}}" width="600px">p>
  10. body>
  11. html>


启动service.py文件:

 
     
  1. /anaconda/bin/python3 /Users/shuubiasahi/Documents/python/credit-tftextclassify-abuse/service.py
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 2017-10-13 15:58:21,732 - Starting server:port=8410, use <Ctrl-C> to stop


监控的是8410端口,可以启动在浏览器输入http://localhost:8410/demo?text=我操,日狗了,如下:



将tensorflow训练好的模型部署成sercice服务,并做预测_第1张图片



本身我是用的cnn做的辱骂识别模型,预测的概率还挺高,看到最后别喷啊 ,哈哈哈哈。。。。。

你可能感兴趣的:(python编程,机器学习)