1. 程式人生 > >將tensorflow訓練好的模型部署成sercice服務,並做預測

將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 isNone:
  19. return0.
  20. else:
  21. return predict(text)
  22. classMainHandler(tornado.web.RequestHandler):
  23. def post(self):
  24. text = self.get_argument('text')
  25. p = work
    (text)
  26. self.write(p)
  27. classIndexHandler(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 <Ctrl-C> to stop'% port)
  44. tornado.ioloop.IOLoop.instance().start()

templates/index.html問價內容:

  1. <!DOCTYPE html>
  2. <html>
  3. <head><title>text abumse server</title></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-1315:58:20.625704: W tensorflow/core/platform/cpu_feature_guard.cc:45]TheTensorFlow 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-1315:58:20.625723: W tensorflow/core/platform/cpu_feature_guard.cc:45]TheTensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
  4. 2017-10-1315:58:20.625728: W tensorflow/core/platform/cpu_feature_guard.cc:45]TheTensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
  5. 2017-10-1315:58:20.625733: W tensorflow/core/platform/cpu_feature_guard.cc:45]TheTensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
  6. 2017-10-1315:58:21,732-Starting server:port=8410, use <Ctrl-C> to stop



本身我是用的cnn做的辱罵識別模型,預測的概率還挺高,看到最後別噴啊 ,哈哈哈哈。。。。。