1 Star 0 Fork 2

neal23333/albert_ner

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.py 2.96 KB
一键复制 编辑 原始数据 按行查看 历史
MarsRoger 提交于 2019-11-21 17:34 . albert嵌入
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:roger
# datetime:19-11-12 下午5:21
# software: PyCharm
import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
#定义端口为9000
define("port", default=9000, help="run on the given port", type=int)
import os
import pickle
import tensorflow as tf
from utils import create_model, get_logger
from model import Model
from loader import input_from_line
from train import FLAGS, load_config
def extract(event):
tf.reset_default_graph()
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:
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, config, logger)
result = model.evaluate_line(sess, input_from_line(event, FLAGS.max_seq_len, tag_to_id), id_to_tag)
return result['entities']
# GET请求
class QueryHandler(tornado.web.RequestHandler):
# get函数
def get(self):
self.render('index.html')
# POST请求
# POST请求参数:query_string
class PostHandler(tornado.web.RequestHandler):
# post函数
def post(self):
# 前端展示
event = self.get_argument('event')
result = extract(event)
print(result)
time = ''
title = ''
obj = ''
keyword = ''
subj = ''
for item in result:
if item['type'] == 'TIME':
if item['word'] not in time:
time += item['word']+','
if item['type'] == 'TITLE':
if item['word'] not in title:
title += item['word']+','
if item['type'] == 'OBJ':
if item['word'] not in obj:
obj += item['word']+','
if item['type'] == 'KEYWORD':
if item['word'] not in keyword:
keyword = item['word']+','
if item['type'] == 'SUBJ':
if item['word'] not in subj:
subj += item['word']+','
print(time, title, obj, keyword, subj)
self.render('event_extraction_result.html', event=event, time=time, title=title, obj=obj, subj=subj, keyword=keyword)
# 主函数
def main():
# 开启tornado服务
tornado.options.parse_command_line()
# 定义app
app = tornado.web.Application(
handlers=[(r'/query', QueryHandler),
(r'/result', PostHandler)
], #网页路径控制
template_path=os.path.join(os.path.dirname(__file__), "templates") # 模板路径
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neal23333/albert_ner.git
git@gitee.com:neal23333/albert_ner.git
neal23333
albert_ner
albert_ner
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385