你好,我想把它改写成API接口的方式,使用flask 提供web服务,发现最后出来的结果非常差,下面试代码
def predict_line():
config = load_config(FLAGS.config_file)
logger = get_logger(FLAGS.log_file)
# limit GPU memory
tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = True
with open(FLAGS.map_file, "rb") as f:
char_to_id, id_to_char, tag_to_id, id_to_tag = pickle.load(f)
with tf.Session(config=tf_config) as sess:
model = create_model(sess, Model, FLAGS.ckpt_path, load_word2vec, config, id_to_char, logger)
#result = model.evaluate_line(sess, input_from_line(line, char_to_id), id_to_tag)
#return result
return model
先返回模型,再在flask中调用模型,原有的会每次都会create model ,
import tensorflow as tf
import numpy as np
from model import Model
from loader import load_sentences, update_tag_scheme
from loader import char_mapping, tag_mapping
from loader import augment_with_pretrained, prepare_dataset
from utils import get_logger, make_path, clean, create_model, save_model
from utils import print_config, save_config, load_config, test_ner
from data_utils import load_word2vec, create_input, input_from_line, BatchManager
#flags = tf.app.flags
#flags.DEFINE_string("map_file","maps.pkl","file for maps")
app = Flask(name)
predictmodel = predict_line()
print('model is loaded')
@app.route('/getNameModel', methods=['POST'])
def getNameModel():
title = request.json['title']
print(title)
tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = True
with open("maps.pkl", "rb") as f:
char_to_id, id_to_char, tag_to_id, id_to_tag = pickle.load(f)
result = ''
with tf.Session(config=tf_config) as sess:
sess.run(tf.global_variables_initializer())
result = predictmodel.evaluate_line(sess, input_from_line(title, char_to_id), id_to_tag)
print(result)
return json.dumps(result, ensure_ascii=False)
可是返回结果是:
{"entities": [{"end": 2, "start": 1, "type": "PER", "word": "想"}, {"end": 3, "start": 2, "type": "PER", "word": "集"}, {"end": 7, "start": 6, "type": "LOC", "word": "部"}, {"end": 8, "start": 7, "type": "ORG", "word": "位"}, {"end": 14, "start": 13, "type": "ORG", "word": "席"}, {"end": 15, "start": 12, "type": "LOC", "word": "联团的总于北京,首执"}, {"end": 16, "start": 15, "type": "PER", "word": "行"}], "string": "联想集团的总部位于北京,首席执行官是杨元庆先生"}
什么原因呢?谢谢