1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- from gevent import monkey; monkey.patch_all()
- import os
- from flask import Flask, request, jsonify
- from flask_apscheduler import APScheduler
- from gevent.pywsgi import WSGIServer
- import config
- from knowledge_relate import Knowledge_Relate
- app = Flask(__name__)
- class APS_Config(object):
- SCHEDULER_API_ENABLED = True
- scheduler = APScheduler()
- # 定时开启题库自查重功能(由于自查重过程重新加载HNSW模型,所以事先需要保存当前运行模型或者重新训练模型)
- # ASR_app.py每天会定时保存当前运行模型(尽量在ASR_app.py每天定时保存当前运行模型之后运行)
- @scheduler.task('cron', id='ddc', week='*', day_of_week='sun', hour='01', minute='15', second='00', timezone='Asia/Shanghai')
- def database_duplicate_check_schedule():
- # 计算云题库重题组
- os.popen("nohup python database_dup_check.py > logs/dup_temp.log 2>&1 &")
- # 定时训练HNSW模型并重启服务(0-'mon',...2-'wed',...,6-'sun')
- # 尽量在ASR_app.py每天定时保存当前运行模型之后运行
- @scheduler.task('cron', id='hm_train', week='*', day_of_week='0,3', hour='02', minute='15', second='00', timezone='Asia/Shanghai')
- def hnsw_model_train_schedule():
- # 训练HNSW模型并重启服务
- os.popen("nohup python hnsw_model_train.py > logs/dup_temp.log 2>&1 &")
- # 每周定时刷新日志
- @scheduler.task('cron', id='log_reset', week='*', day_of_week='mon', hour='05', minute='00', second='00', timezone='Asia/Shanghai')
- def log_reset_schedule():
- rlt_ddc_LogConfig.log_reset()
- os.popen("nohup python restart_server.py > logs/temp_app.log 2>&1 &")
- # 知识点关联推荐
- @app.route('/relate', methods=['GET', 'POST'])
- def knowledge_relate_recommend():
- if request.method == 'POST':
- # 接收日志采集
- rlt_ddc_logger.info(config.log_msg.format(
- id="知识点关联推荐",
- type="relate接收",
- message=request.get_json()))
- # 获取post数据
- receive_list = request.get_json()
- if not receive_list:
- return '请输入关联数据'
- receive_dict = receive_list[0]
- key_data = dict()
- key_data["topic_id"] = int(receive_dict["topic_id"])
- key_data["content"] = receive_dict["item"]
- key_data["subject_id"] = int(receive_dict["subject_id"])
- key_data["topic_type_id"] = int(receive_dict["topic_type_id"])
- key_data["special_id"] = receive_dict["special"]
- key_data["school_id"] = int(receive_dict.get("school_id", 0))
- key_data["keyword"] = receive_dict.get("topicKeyword", '')
- key_data["examMethodIds"] = receive_dict.get("examMethodIds", [])
- # 关联规则处理
- if receive_dict.get("filterType", 1) != 2:
- res_list = kl_rlt.relate(key_data)
- else:
- res_list = kl_rlt.relate(key_data, filterType=2)
- # 返回日志采集
- rlt_ddc_logger.info(config.log_msg.format(
- id="知识点关联推荐",
- type="relate返回",
- message=res_list))
- return jsonify(res_list)
- if __name__ == "__main__":
- # 日志采集初始化
- rlt_ddc_LogConfig = config.LogConfig(config.rlt_ddc_path, "rlt_ddc")
- rlt_ddc_logger = rlt_ddc_LogConfig.get_log()
- # Knowledge_Relate初始化
- kl_rlt = Knowledge_Relate(rlt_ddc_logger)
- # 定时开启题库自查重功能
- app.config.from_object(APS_Config())
- scheduler.init_app(app)
- scheduler.start()
- # app.run(host='0.0.0.0',port='8856')
- server = WSGIServer(('0.0.0.0', 8856), app)
- server.serve_forever()
|