def update_train_task_by_id(train_job_id, train_task_id, is_check_train_terms, model_type, args): """ 1. 根据字段状态更新训练状态和结果 2. 直接设置训练状态和结果 3. 模型上线状态更新(分类和抽取还不一样) """ train_job = TrainJobModel().get_by_id(train_job_id) train_task = TrainTaskModel().get_by_id(train_task_id) if is_check_train_terms: # 是否需要检查train_term的状态 _, training_terms = TrainTermTaskModel().get_by_filter(limit=99999, train_task_id=train_task_id, train_term_status=int(StatusEnum.training)) _, failed_terms = TrainTermTaskModel().get_by_filter(limit=99999, train_task_id=train_task_id, train_term_status=int(StatusEnum.fail)) if not training_terms: # 没有处于训练中 if not failed_terms: # 没有处于失败的 args["train_status"] = int(StatusEnum.success) else: args["train_status"] = int(StatusEnum.fail) else: args["train_status"] = int(StatusEnum.training) else: # no limit to set model_train_state=success/failed if args["train_status"] == int(StatusEnum.online): # validation if train_task.train_status == StatusEnum.online: abort(400, message="该模型已经上线") if train_task.train_status != StatusEnum.success: abort(400, message="只能上线训练成功的模型") # send model train http request service_url = _get("CLASSIFY_MODEL_ONLINE") if model_type == "classify" else _get("EXTRACT_MODEL_ONLINE") resp = requests.post(f"{service_url}?model_version={train_task.model_version}") if resp.status_code < 200 or resp.status_code >= 300: abort(500, message=f"上线服务 <{service_url}> 出现错误: {resp.text}") # find all online model under this doc_type_id online_models = TrainTaskModel().get_by_doc_type_id(doc_type_id=train_job.doc_type_id, train_status=int(StatusEnum.online)) # unload online models TrainTaskModel().bulk_update([train.train_task_id for train in online_models], train_status=int(StatusEnum.success)) # update train task train_task = TrainTaskModel().update(train_task_id, **args) session.commit() return train_task
def create_evaluate_task_by_train_job_id(train_job_id, evaluate_task_name, evaluate_task_desc, mark_job_ids, doc_term_ids, doc_relation_ids, use_rule=0): """ 如果后面要加重新训练的逻辑,这部分要改,不能根据train_job_id去创建评估任务,而是根据train_task_id, 目前先保留,因为目前train_job和train_task是一一对应,不会有影响 """ # get correspondent train_job, doc_type, train_task, nlp_task by train_job_id train_job = TrainJobModel().get_by_id(train_job_id) doc_type = DocTypeModel().get_by_id(train_job.doc_type_id) doc_term_list = DocTermModel().get_by_filter(limit=99999, doc_type_id=doc_type.doc_type_id) doc_type.doc_term_list = doc_term_list nlp_task = NlpTaskEnum(doc_type.nlp_task_id) _, train_task_list = TrainTaskModel().get_by_filter(train_job_id=train_job_id) train_task = train_task_list[0] # create evaluate_task evaluate_task = EvaluateTaskModel().create(evaluate_task_name=evaluate_task_name, evaluate_task_desc=evaluate_task_desc, train_task_id=train_task.train_task_id, evaluate_task_status=int(StatusEnum.processing)) # bulk create evaluate m2m mark evaluate_m2m_mark_list = [{"evaluate_task_id": evaluate_task.evaluate_task_id, "mark_job_id": _id} for _id in mark_job_ids] EvaluateM2mMarkModel().bulk_create(evaluate_m2m_mark_list) # push to evaluate redis queue doc_term_ids = [str(t.doc_term_id) for t in RelationM2mTermModel().get_by_filter(limit=99999, doc_relation_ids=[int(rl) for rl in doc_relation_ids])] push_evaluate_task_to_redis(nlp_task, evaluate_task, train_task, doc_type, mark_job_ids, doc_term_ids, doc_relation_ids, use_rule) session.commit() return evaluate_task
def update_train_task_by_model_version(model_version, is_check_train_terms, args): train_task = TrainTaskModel().get_by_filter(model_version=model_version)[1][0] if is_check_train_terms: _, training_terms = TrainTermTaskModel().get_by_filter(limit=99999, train_task_id=train_task.train_task_id, train_term_status=int(StatusEnum.training)) _, failed_terms = TrainTermTaskModel().get_by_filter(limit=99999, train_task_id=train_task.train_task_id, train_term_status=int(StatusEnum.fail)) if not training_terms: # 没有处于训练中 if not failed_terms: # 没有处于失败的 args["train_status"] = int(StatusEnum.success) else: args["train_status"] = int(StatusEnum.fail) else: args["train_status"] = int(StatusEnum.training) train_task = TrainTaskModel().update(train_task.train_task_id, **args) session.commit() return train_task
def create_train_task(): from app.model import TrainTaskModel if TrainTaskModel().is_empty_table(): TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train1", train_status=int(StatusEnum.online), train_job_id=1) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train2", train_status=int(StatusEnum.success), train_job_id=2) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train3", train_status=int(StatusEnum.training), train_job_id=3) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train4", train_status=int(StatusEnum.training), train_job_id=4) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train5", train_status=int(StatusEnum.training), train_job_id=5) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train6", train_status=int(StatusEnum.training), train_job_id=6) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train7", train_status=int(StatusEnum.training), train_job_id=7) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train8", train_status=int(StatusEnum.online), train_job_id=8) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train9", train_status=int(StatusEnum.success), train_job_id=9) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train10", train_status=int(StatusEnum.training), train_job_id=10) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train11", train_status=int(StatusEnum.training), train_job_id=11) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train12", train_status=int(StatusEnum.training), train_job_id=12) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train13", train_status=int(StatusEnum.training), train_job_id=13) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train14", train_status=int(StatusEnum.training), train_job_id=14) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train15", train_status=int(StatusEnum.online), train_job_id=15) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train16", train_status=int(StatusEnum.success), train_job_id=16) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train17", train_status=int(StatusEnum.training), train_job_id=17) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train18", train_status=int(StatusEnum.training), train_job_id=18) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train19", train_status=int(StatusEnum.training), train_job_id=19) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train20", train_status=int(StatusEnum.training), train_job_id=20) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train21", train_status=int(StatusEnum.training), train_job_id=21) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train22", train_status=int(StatusEnum.training), train_job_id=22) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train23", train_status=int(StatusEnum.online), train_job_id=23) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train24", train_status=int(StatusEnum.success), train_job_id=24) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train25", train_status=int(StatusEnum.training), train_job_id=25) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train26", train_status=int(StatusEnum.training), train_job_id=26) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train27", train_status=int(StatusEnum.training), train_job_id=27) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train28", train_status=int(StatusEnum.training), train_job_id=28) TrainTaskModel().create(app_id=1, created_by=1, train_model_name="train29", train_status=int(StatusEnum.training), train_job_id=29) session.commit()
def get_evaluate_task_by_id(evaluate_task_id): evaluate_task = EvaluateTaskModel().get_by_id(evaluate_task_id) evaluate_task.train_job_id = TrainTaskModel().get_by_id(evaluate_task.train_task_id).train_job_id return evaluate_task
def get_train_task_by_id(train_task_id) -> TrainTask: train_task = TrainTaskModel().get_by_id(train_task_id) train_task.mark_job_ids = [m2m.mark_job_id for m2m in TrainM2mMarkbModel().get_by_filter(limit=99999, train_job_id=train_task.train_job_id)] return train_task
def get_train_task_list_by_train_job_id(train_job_id, order_by, order_by_desc, offset, limit) -> (int, [TrainTask]): count, result = TrainTaskModel().get_by_filter(order_by=order_by, order_by_desc=order_by_desc, offset=offset, limit=limit, train_job_id=train_job_id) return count, result
def delete_train_task_by_id(train_task_id): TrainTaskModel().delete(train_task_id) session.commit()