Commit 31041a93 by 柴鹏飞

精排模型接口

parent 0a597634
# -*- coding: utf-8 -*-
import json
from concurrent.futures import ThreadPoolExecutor
import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
from tornado.concurrent import run_on_executor
from ydl_ai_recommender.src.utils.log import create_logger
from ydl_ai_recommender.src.core.rank.ranker import Ranker
logger = create_logger(__name__, 'rank_service.log', is_rotating=True)
ranker = Ranker()
class RankHandler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(1)
@tornado.gen.coroutine
def get(self):
uid = self.get_argument('uid', None)
if uid is None:
logger.warn('请求参数不正确,无uid')
counselors = self.get_argument('counselors', '')
ret = yield self.run(uid, counselors)
self.write(ret)
@tornado.gen.coroutine
def post(self):
param = json.loads(self.request.body.decode('utf-8'))
uid = param.get('uid', None)
counselors = param.get('counselors', '')
if uid is None:
logger.warn('请求参数不正确,无uid')
ret = yield self.run(uid, counselors)
self.write(ret)
@run_on_executor
def run(self, uid, counselors=''):
logger.info('request@@uid=%s@@counselors=%s', uid, counselors)
try:
rank_result = ranker.rank(uid, counselors)
ret = {
'status': 'success',
'code': 0,
'data': rank_result,
'total_count': len(rank_result),
}
except Exception as e:
logger.error('执行精排报错', exc_info=True)
ret = {
'status': 'error',
'code': 1,
'data': [],
'total_count': 0,
}
ret_str = json.dumps(ret, ensure_ascii=False)
logger.info('response@@uid=%s@@counselors=%s@@ret=%s', uid, counselors, ret_str)
return ret
if __name__ == '__main__':
tornado.options.define('port', default=8868, type=int, help='服务启动的端口号')
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r'/ai_counselor_rank', RankHandler)], autoreload=False, debug=False)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(tornado.options.options.port)
tornado.ioloop.IOLoop.instance().start()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment