segment_app.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # 试题切割服务:TopicSegment predict
  2. from gevent import monkey; monkey.patch_all() # linux
  3. from gevent.pywsgi import WSGIServer
  4. from flask import Flask, jsonify
  5. # from flask import request
  6. from flask import request
  7. import json
  8. import time
  9. from flask_cors import *
  10. from PointerNet import config
  11. from PointerNet.predictor import Predictor
  12. logger = config.myLog(__name__, log_cate="TopicSegment_predict_log").getlog()
  13. app = Flask(__name__)
  14. app.debug = True
  15. CORS(app, supports_credentials=True)
  16. nerArgs = config.NerArgs()
  17. pred = Predictor(nerArgs)
  18. @app.route('/math_phy_TopicSegment_predict', methods=["POST", "GET"])
  19. def topicType_predict():
  20. """
  21. 物理题型预测
  22. post以json格式上传时,content可以传str或list
  23. :return:
  24. """
  25. content = request.json.get("content", "") # 试题/试卷
  26. text_type = request.json.get("text_type", "stem_block")
  27. subject = request.json.get("subject", "数学")
  28. paper_id = request.json.get("paper_id", "temp_{}".format(int(time.time())))
  29. logger.info(json.dumps({"log_level": "info",
  30. "request_ip": request.remote_addr,
  31. "content": content,
  32. "paper_id": paper_id,
  33. "subject": subject,
  34. "text_type": text_type}, ensure_ascii=False))
  35. if not content:
  36. return ""
  37. elif type(content) != str:
  38. return "只接受str传参类型"
  39. pred_res = pred.predict(content, con_type=text_type, paper_id=paper_id, subject=subject)
  40. logger.info(json.dumps({"log_level": "info",
  41. "content": content,
  42. "pred_res": pred_res}, ensure_ascii=False))
  43. return jsonify({"res":pred_res})
  44. if __name__ == "__main__":
  45. server = WSGIServer((config.server_ip, config.server_port), app)
  46. server.serve_forever()