hnsw_app.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from gevent import monkey; monkey.patch_all()
  2. import os
  3. from flask import Flask, request, jsonify
  4. from flask_apscheduler import APScheduler
  5. from gevent.pywsgi import WSGIServer
  6. import config
  7. from hnsw_model import HNSW
  8. from log_config import LogConfig
  9. app = Flask(__name__)
  10. class APS_Config(object):
  11. SCHEDULER_API_ENABLED = True
  12. scheduler = APScheduler()
  13. # 每周定时刷新日志
  14. @scheduler.task('cron', id='log_reset', week='*', day_of_week='sun', hour='05', minute='00', second='00', timezone='Asia/Shanghai')
  15. def log_reset_schedule():
  16. # hm_LogConfig.log_reset()
  17. os.popen("nohup python restart_server.py 0 > logs/temp_app.log 2>&1 &")
  18. # hnsw模型数据检索
  19. @app.route('/retrieve', methods=['GET', 'POST'])
  20. def retrieve():
  21. if request.method == 'POST':
  22. # 获取post数据
  23. query_vec = request.get_json()
  24. # HNSW检索
  25. res_list = hnsw.retrieve(query_vec)
  26. # 返回日志采集
  27. hm_logger.info(config.log_msg.format(id="HNSW检索",
  28. type="retrieve返回",
  29. message=res_list))
  30. return jsonify(res_list)
  31. if __name__ == '__main__':
  32. # 日志采集初始化
  33. hm_LogConfig = LogConfig(config.retrieval_path, "hnsw_model")
  34. hm_logger = hm_LogConfig.get_log()
  35. # HNSW模型初始化
  36. hnsw = HNSW(hm_logger)
  37. # 定时更新HNSW模型增/改/删变化
  38. app.config.from_object(APS_Config())
  39. scheduler.init_app(app)
  40. scheduler.start()
  41. # app.run(host='0.0.0.0',port='8836')
  42. server = WSGIServer(('0.0.0.0', 8836), app)
  43. server.serve_forever()