123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- # -*- coding: utf-8 -*-
- """
- Created on Fri Jun 1 18:03:03 2018
- @author: DONG
- """
- import sys
- import os
- sys.path.append(os.path.abspath("../.."))
- print(sys.path)
- from flask import Flask, jsonify
- from flask import abort
- from flask import request
- import logging
- import time
- import BiddingKG.dl.interface.predictor as predictor
- import BiddingKG.dl.interface.Preprocessing as Preprocessing
- from Entity2DB import *
- import psycopg2
- os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
- os.environ["CUDA_VISIBLE_DEVICES"] = ""
- from DBUtils.PooledDB import PooledDB
- pool = None
- def getConnection():
- global pool
- if pool is None:
- pool = PooledDB(psycopg2, 10,dbname="article_label", host="192.168.2.101",user="postgres",password="postgres",port="5432")
- return pool.connection()
- app = Flask(__name__)
- app.config['JSON_AS_ASCII'] = False
- gunicorn_logger = logging.getLogger('gunicorn.error')
- app.logger.handlers = gunicorn_logger.handlers
- app.logger.setLevel(gunicorn_logger.level)
- codeNamePredict = predictor.CodeNamePredict()
- premPredict = predictor.PREMPredict()
- epcPredict = predictor.EPCPredict()
- @app.route('/article_extract', methods=['POST'])
- def text_predict():
- start_time = time.time()
- # 初始化待返回结果
- data = {}
- # 确保请求符合要求
- if request.method == "POST":
- if (not request.json) or ('content' not in request.json):
- abort(400)
- else:
- conn = psycopg2.connect(dbname="article_label",user="postgres",password="postgres",host="192.168.2.101")
- id = request.json['id']
- content = request.json['content']
- cursor = conn.cursor()
- sql = " select count(1) from articles_processed where id='"+id+"' "
- cursor.execute(sql)
- rows = cursor.fetchall()
- conn.close()
- '''
- if rows[0][0]>0:
- data["success"] = True
- return jsonify(data), 201
- '''
- try:
-
- conn = psycopg2.connect(dbname="article_label",user="postgres",password="postgres",host="192.168.2.101")
- list_articles,list_sentences,list_entitys = Preprocessing.get_preprocessed([[id,content]])
- role_datas = Preprocessing.search_role_data(list_sentences,list_entitys)
- money_datas = Preprocessing.search_money_data(list_sentences,list_entitys)
- person_datas = Preprocessing.search_person_data(list_sentences,list_entitys)
-
- premPredict.predict(role_datas,money_datas)
- epcPredict.predict(person_datas)
-
-
- codeName = codeNamePredict.predict(list_articles)
-
- '''
- persistArticle(conn, list_articles,codeName)
- for sentences in list_sentences:
- persistSentence(conn, sentences)
- for entitys in list_entitys:
- persistEntity(conn, entitys)
- '''
-
- except Exception as e:
- app.logger.info("error:"+str(e))
-
- finally:
-
- conn.commit()
- conn.close()
- data["success"] = True
-
- # 以json形式返回结果
- app.logger.info(" time from receive to send: "+str(time.time()-start_time))
- return jsonify(data), 201
- if __name__ == '__main__':
- handler = logging.FileHandler('flask.log', encoding='UTF-8')
- app.logger.addHandler(handler)
- app.run(host='0.0.0.0', port=15010, threaded=True, debug=True)
|