class WordsegMarkJobListResource(Resource): @parse({ "is_superuser": fields.Boolean(missing=False), "query": fields.String(missing=''), "offset": fields.Integer(missing=0), "limit": fields.Integer(missing=10), "doc_type_id": fields.Integer(missing=None), 'order_by': fields.String(missing='-created_time'), }) def get(self: Resource, args: typing.Dict) -> typing.Tuple[typing.Dict, int]: count, result = MarkJobService().get_mark_job_list_by_nlp_task( args, nlp_task=NlpTaskEnum.wordseg) return { "message": "请求成功", "result": result, "count": count, }, 200 @parse( { "mark_job_name": fields.String(required=True), "mark_job_type": fields.String(required=True), "mark_job_desc": fields.String(), "doc_type_id": fields.Integer(required=True), "files": fields.List(fields.File(), required=True), "assign_mode": fields.String(required=True, validate=lambda x: x in ['average', 'together']), "assessor_id": fields.Integer(missing=0), "labeler_ids": fields.List(fields.Integer(), required=True), }, locations=('form', 'files')) def post(self: Resource, args: typing.Dict) -> typing.Tuple[typing.Dict, int]: files = args["files"] assign_mode = args["assign_mode"] if assign_mode == AssignModeEnum.together: abort(400, message="不支持共同标注") job_type = Common().check_job_type_by_files(files) if job_type != "text": abort(400, message="请上传纯文本文档(txt/csv)") else: args['mark_job_type'] = job_type try: result = MarkJobService().create_mark_job(files, NlpTaskEnum.wordseg, args) return {"message": "创建成功", "result": result}, 201 except TypeError: abort(400, message="上传文件类型错误")
class PredictJobSchema(Schema): doc_type = fields.Nested(DocTypeSchema) task_list = fields.List(fields.Nested(PredictTaskSchema)) extract_job_id = fields.Integer(attribute="predict_job_id") extract_job_name = fields.String(attribute="predict_job_name") extract_job_type = fields.String(attribute="predict_job_type.value") extract_job_state = fields.Function(lambda obj: StatusEnum(obj.predict_job_status).name) extract_job_desc = fields.String(attribute="predict_job_desc") is_batch = fields.Boolean() created_time = fields.DateTime()
class RelationMarkJobListResource(Resource): @parse({ "is_superuser": fields.Boolean(missing=False), "query": fields.String(missing=''), "offset": fields.Integer(missing=0), "limit": fields.Integer(missing=10), "doc_type_id": fields.Integer(missing=None), 'order_by': fields.String(missing='-created_time'), }) def get(self: Resource, args: typing.Dict) -> typing.Tuple[typing.Dict, int]: count, result = MarkJobService().get_mark_job_list_by_nlp_task( args, NlpTaskEnum.relation) return { "message": "请求成功", "result": result, "count": count, }, 200 @parse( { "mark_job_name": fields.String(required=True), "mark_job_type": fields.String(required=True), "mark_job_desc": fields.String(), "doc_type_id": fields.Integer(required=True), "files": fields.List(fields.File(), required=True), "assign_mode": fields.String(required=True, validate=lambda x: x in ['average', 'together']), "assessor_id": fields.Integer(), "labeler_ids": fields.List(fields.Integer(), required=True), "use_rule": fields.Integer(missing=1) # 默认使用规则 }, locations=('form', 'files')) def post(self: Resource, args: typing.Dict) -> typing.Tuple[typing.Dict, int]: files = args['files'] job_type = Common().check_job_type_by_files(files) if not job_type: abort(400, message='请上传全部纯文本文档(txt/csv)或者全部电子文档(pdf/word文档)') else: args['mark_job_type'] = job_type result = MarkJobService().create_mark_job(files, NlpTaskEnum.relation, args) return {"message": "创建成功", "result": result}, 201
class DocTypeSchema(Schema): doc_terms = fields.List(fields.Integer()) doc_term_list = fields.List(fields.Nested(DocTermSchema)) doc_relation_list = fields.List(fields.Nested(EntityDocRelationSchema)) doc_lexicon_list = fields.List(fields.Nested(WordsegDocLexiconSchema), attribute='doc_rules') doc_type_id = fields.Integer() doc_type_name = fields.String() doc_type_desc = fields.String() is_top = fields.Boolean(attribute="is_favorite") created_time = fields.DateTime() group_id = fields.Integer() status = fields.Function(lambda obj: not obj.is_deleted)
class UpdateModelTrainResource(Resource): @parse({ "model_version": fields.String(required=True), "check_train_terms": fields.Boolean(required=True), "model_train_state": fields.String(), "model_train_result": fields.Dict(), "model_type": fields.String(required=True, validate=lambda x: x in ('extract', 'classify', 'relation', 'wordseg')) }) def put(self, args): """ 修改模型的状态 """ update_params = {} if args.get("model_train_state"): # 后端返回结果转换,失败时后端目前还返回failed update_params.update(train_status=status_str2int_mapper()[args["model_train_state"]]) train_task = ModelTrainService().update_train_task_by_model_version(model_version=args["model_version"], is_check_train_terms=args["check_train_terms"], args=update_params) result = TrainTaskSchema().dump(train_task) return { "message": "更新成功", "result": result, }, 201
class ModelTrainItemResource(Resource): @parse({ "model_type": fields.String(required=True, validate=lambda x: x in ("extract", "classify", "relation", "wordseg")) }) def get( self: Resource, args: typing.Dict, model_id: int, model_train_id: int ) -> typing.Tuple[typing.Dict, int]: """ 获取单条模型训练记录 """ # get single task task by id train_task = ModelTrainService().get_train_task_by_id(train_task_id=model_train_id) result = TrainTaskSchema().dump(train_task) # add extra algorithm information for extract and relation. if args["model_type"] in ["extract", "relation"]: result = ModelTrainService().add_algo_dict_for_extract_relation(result) return { "message": "请求成功", "result": result, }, 200 @parse({ "model_train_state": fields.String(), "model_train_result": fields.Dict(), "check_train_terms": fields.Boolean(missing=False), "model_type": fields.String(required=True, validate=lambda x: x in ("extract", "classify", "relation", "wordseg")) }) def patch( self: Resource, args: typing.Dict, model_id: int, model_train_id: int ) -> typing.Tuple[typing.Dict, int]: """ 修改模型的状态和结果 """ update_params = {} if args.get("model_train_state"): # 这里不考虑model_train_result因为新的表结构里没有这个列了 update_params.update(train_status=status_str2int_mapper()[args["model_train_state"]]) train_task = ModelTrainService().update_train_task_by_id(train_job_id=model_id, train_task_id=model_train_id, is_check_train_terms=args["check_train_terms"], model_type = args["model_type"], args=update_params) result = TrainTaskSchema().dump(train_task) return { "message": "更新成功", "result": result, }, 200 @parse({ "model_type": fields.String(required=True, validate=lambda x: x in ("extract", "classify")) }) def delete( self: Resource, args: typing.Dict, model_id: int, model_train_id: int ) -> typing.Tuple[typing.Dict, int]: """ 删除模型 """ ModelTrainService().delete_train_task_by_id(train_task_id=model_train_id) return { "message": "删除成功", }, 200