hnsw_app.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 > 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. query_labels = hnsw.retrieve(query_vec)
  26. return jsonify(query_labels)
  27. if __name__ == '__main__':
  28. # 日志采集初始化
  29. hm_LogConfig = LogConfig(config.retrieval_path, "hnsw_model")
  30. hm_logger = hm_LogConfig.get_log()
  31. # HNSW模型初始化
  32. hnsw = HNSW(hm_logger)
  33. # 定时更新HNSW模型增/改/删变化
  34. app.config.from_object(APS_Config())
  35. scheduler.init_app(app)
  36. scheduler.start()
  37. # app.run(host='0.0.0.0',port='8836')
  38. server = WSGIServer(('0.0.0.0', 8836), app)
  39. server.serve_forever()