def train(): """ ;parm 请求参数格式 { "name":"train", "data":{ "name":"textcnn-one", "categories":"0.1", "directory":"text" } } :return: """ try: param = json.loads(request.get_data()) if param['name'] != 'train': return jsonify(Response(code=1, msg='service interface name error').__dict__) data = param['data'] task_train(current_app.instance_path,data['name'],data['categories'].split(','),data['directory']) return jsonify(Response(data={'msg':'train complete!'}).__dict__) # executor.submit(task_train,current_app.instance_path,data['name'],data['categories'].split(',')) # return jsonify(Response(data={'msg': 'start train!'}).__dict__) except Exception as e: current_app.logger.errot(e) return jsonify(Response(code=-1, msg='service call error').__dict__)
def train_cnn(): """ ;parm 请求参数格式 { "name":"traincnn", "data":{ "name":"textcnn" "directory":"D:/textcnn/dataset/train" } } :return: """ try: param = json.loads(request.get_data()) if param['name'] != 'traincnn': return jsonify(Response(code=1, msg='service interface name error').__dict__) data = param['data'] model_name = data['name'] word_model_dir = os.path.join(current_app.instance_path, 'word2vec') train_class=Train(word_model_dir) train_class.main(data['directory'],current_app.instance_path,word_model_dir,model_name) return jsonify(Response(msg='text cnn model train complete').__dict__) except Exception as e: current_app.logger.errot(e) return jsonify(Response(code=-1, msg='service call error').__dict__)
def predict_real_top(): """ 参数格式 { "name":"predict_real_top", "data":{ "name":"textcnn", "top":3, "data":"nichola nicklebi celabr human spirit unrel dickensian decenc turn me horror scroog", } } :return: """ try: param = json.loads(request.get_data()) if param['name'] != 'predict_real_top': return jsonify(Response(code=1, msg='service interface name error').__dict__) data = param['data'] model_name = data['name'] checkpoint_dir=os.path.join(current_app.instance_path, 'runs',model_name) categories=list(open(os.path.join(checkpoint_dir, 'categories.txt'),"r",encoding='utf-8').readlines()) categories_array=categories[0].split(',') if len(categories_array) < int(data['top']): msg = 'the number of model classifications is {}'.format(len(categories_array)) return jsonify(Response(code=-1, data=msg, msg='error').__dict__) else: predict_content=data['data'] top_num=data['top'] predict_real_class=Predict_Real() ruselt_data=predict_real_class.predict(checkpoint_dir,predict_content,top_num,categories_array) return jsonify(Response(data=ruselt_data, msg='service call 成功').__dict__) except Exception as e: current_app.logger.errot(e) return jsonify(Response(code=-1, msg='service call error').__dict__)
def sample(): """ 接收请求端数据,并按一个时间为一个文件进行组织存储 ;parm请求参数格式 { "name":"sample", "data":{ "id":1346268613836800, "name":"", "label":"0", "content":"", "date":1585014916672, "directory":"text" } } :return: """ try: param=json.loads(request.get_data()) if param['name'] != 'sample': return jsonify(Response(code=1,msg='service interface name error').__dict__) data=param['data'] dir_path=path.join(current_app.instance_path,'dataset',data['directory'],data['label']) if not os.path.exists(dir_path): os.makedirs(dir_path) # write file file = path.join(dir_path,data['name']) FileUtils.write(file=file,data='{}\n'.format(data['content']),append=True) return jsonify(Response(data=dir_path).__dict__) except: return jsonify(Response(code=-1, msg='service call error').__dict__)
def predict_top(): """ 参数格式 { "name":"predict", "data":{ "name":"textcnn", "top":3, "data":"nichola nicklebi celabr human spirit unrel dickensian decenc turn me horror scroog", } } :return: """ try: param = json.loads(request.get_data()) if param['name'] != 'predict': return jsonify(Response(code=1, msg='service interface name error').__dict__) data = param['data'] model = get_model(data['name']) if model.categories_len() < int(data['top']): msg='the number of model classifications is {}'.format(model.categories_len()) return jsonify(Response(code=-1,data=msg,msg='error').__dict__) else: return jsonify(Response(data=model.predict_top(data['data'],int(data['top']))).__dict__) except Exception as e: current_app.logger.errot(e) return jsonify(Response(code=-1, msg='service call error').__dict__)
def predict_cnn(): """ 参数格式 { { "name":"predict_local_top", "data":{ "name":"textcnn", "top":3, "data":"D:/textcnn/dataset/test", } } } :return: """ try: param = json.loads(request.get_data()) if param['name'] != 'predict_local_top': return jsonify(Response(code=1, msg='service interface name error').__dict__) data = param['data'] # categories_path=param['categories_path'] model_name = data['name'] checkpoint_dir=os.path.join(current_app.instance_path, 'runs',model_name) categories=list(open(os.path.join(checkpoint_dir, 'categories.txt'),"r",encoding='utf-8').readlines()) categoriesArray=categories[0].split(',') if len(categoriesArray) < int(data['top']): msg = 'the number of model classifications is {}'.format(len(categoriesArray)) return jsonify(Response(code=-1, data=msg, msg='error').__dict__) else: top_num=data['top'] testdata_dir=data['data'] predictlocalclass=Predict() ruseltdata=predictlocalclass.predict(checkpoint_dir,testdata_dir,top_num,categoriesArray) return jsonify(Response(data=ruseltdata, msg='service call 成功').__dict__) # predictlocalclass.predict(categories_path, checkpoint_filepath, testdata_dirpath) # predictlocalclass.predict(mpdel_name, top_num, testdata_dir) # return jsonify(Response(data=ruseltdata, msg='service call 成功').__dict__) except Exception as e: current_app.logger.errot(e) return jsonify(Response(code=-1, msg='service call error').__dict__)
def predict(): """ 参数格式 { "name":"predict", "data":{ "name":"textcnn", "data":"nichola nicklebi celabr human spirit unrel dickensian decenc turn me horror scroog", } } :return: """ try: param = json.loads(request.get_data()) if param['name'] != 'predict': return jsonify(Response(code=1, msg='service interface name error').__dict__) data = param['data'] model=get_model(data['name']) return jsonify(Response(data=model.predict(data['data'])).__dict__) except Exception as e: current_app.logger.errot(e) return jsonify(Response(code=-1, msg='service call error').__dict__)
def word_train(): """ ;parm 请求参数格式 { "name":"wordtrain", "data":{ "directory":"D:/data/cutdata.txt" } } :return: """ try: param = json.loads(request.get_data()) if param['name'] != 'wordtrain': return jsonify(Response(code=1, msg='service interface name error').__dict__) data = param['data'] data_path=data['directory'] word_model_dir=os.path.join(current_app.instance_path,'word2vec') word2vecclass.train_word(data_path,word_model_dir) return jsonify(Response(msg='word train complete').__dict__) except Exception as e: current_app.logger.errot(e) return jsonify(Response(code=-1, msg='service call error').__dict__)