rlt_ddc_app.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 knowledge_relate import Knowledge_Relate
  8. app = Flask(__name__)
  9. class APS_Config(object):
  10. SCHEDULER_API_ENABLED = True
  11. scheduler = APScheduler()
  12. # 定时开启题库自查重功能(由于自查重过程重新加载HNSW模型,所以事先需要保存当前运行模型或者重新训练模型)
  13. # ASR_app.py每天会定时保存当前运行模型(尽量在ASR_app.py每天定时保存当前运行模型之后运行)
  14. @scheduler.task('cron', id='ddc', week='*', day_of_week='sun', hour='01', minute='15', second='00', timezone='Asia/Shanghai')
  15. def database_duplicate_check_schedule():
  16. # 计算云题库重题组
  17. os.popen("nohup python database_dup_check.py > logs/dup_temp.log 2>&1 &")
  18. # 定时训练HNSW模型并重启服务(0-'mon',...2-'wed',...,6-'sun')
  19. # 尽量在ASR_app.py每天定时保存当前运行模型之后运行
  20. @scheduler.task('cron', id='hm_train', week='*', day_of_week='0,3', hour='02', minute='15', second='00', timezone='Asia/Shanghai')
  21. def hnsw_model_train_schedule():
  22. # 训练HNSW模型并重启服务
  23. os.popen("nohup python hnsw_model_train.py > logs/dup_temp.log 2>&1 &")
  24. # 每周定时刷新日志
  25. @scheduler.task('cron', id='log_reset', week='*', day_of_week='mon', hour='05', minute='00', second='00', timezone='Asia/Shanghai')
  26. def log_reset_schedule():
  27. rlt_ddc_LogConfig.log_reset()
  28. os.popen("nohup python restart_server.py > logs/temp_app.log 2>&1 &")
  29. # 知识点关联推荐
  30. @app.route('/relate', methods=['GET', 'POST'])
  31. def knowledge_relate_recommend():
  32. if request.method == 'POST':
  33. # 接收日志采集
  34. rlt_ddc_logger.info(config.log_msg.format(
  35. id="知识点关联推荐",
  36. type="relate接收",
  37. message=request.get_json()))
  38. # 获取post数据
  39. receive_list = request.get_json()
  40. if not receive_list:
  41. return '请输入关联数据'
  42. receive_dict = receive_list[0]
  43. key_data = dict()
  44. key_data["topic_id"] = int(receive_dict["topic_id"])
  45. key_data["content"] = receive_dict["item"]
  46. key_data["subject_id"] = int(receive_dict["subject_id"])
  47. key_data["topic_type_id"] = int(receive_dict["topic_type_id"])
  48. key_data["special_id"] = receive_dict["special"]
  49. key_data["school_id"] = int(receive_dict.get("school_id", 0))
  50. key_data["keyword"] = receive_dict.get("topicKeyword", '')
  51. key_data["examMethodIds"] = receive_dict.get("examMethodIds", [])
  52. # 关联规则处理
  53. if receive_dict.get("filterType", 1) != 2:
  54. res_list = kl_rlt.relate(key_data)
  55. else:
  56. res_list = kl_rlt.relate(key_data, filterType=2)
  57. # 返回日志采集
  58. rlt_ddc_logger.info(config.log_msg.format(
  59. id="知识点关联推荐",
  60. type="relate返回",
  61. message=res_list))
  62. return jsonify(res_list)
  63. if __name__ == "__main__":
  64. # 日志采集初始化
  65. rlt_ddc_LogConfig = config.LogConfig(config.rlt_ddc_path, "rlt_ddc")
  66. rlt_ddc_logger = rlt_ddc_LogConfig.get_log()
  67. # Knowledge_Relate初始化
  68. kl_rlt = Knowledge_Relate(rlt_ddc_logger)
  69. # 定时开启题库自查重功能
  70. app.config.from_object(APS_Config())
  71. scheduler.init_app(app)
  72. scheduler.start()
  73. # app.run(host='0.0.0.0',port='8856')
  74. server = WSGIServer(('0.0.0.0', 8856), app)
  75. server.serve_forever()