加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.py 2.89 KB
一键复制 编辑 原始数据 按行查看 历史
honey 提交于 2024-04-25 17:22 . 新增接口耗时中间件 HttpBasic
""""
Copyright (c) 2024 [XKW.Beijing]
All rights reserved.
Author: [tangxiaojun]
Email: [417281862@qq.com]
"""
from flask import Flask, jsonify, request
"""
导入自定义中间件
"""
from middleware_timing import TimingMiddleware
from middleware_auth import HttpBasicAuthMiddleware
"""
导入业务层
"""
from resource_user import UserResource
from resource_bill import BillResource
from resource_nlp import NlpResource
from resource_cczh import CczhResource
from resource_ltp import LtpResource
app = Flask(__name__)
"""
注册中间件
"""
middleware_timing = TimingMiddleware()
middleware_timing.init_app(app)
middleware_auth = HttpBasicAuthMiddleware()
middleware_auth.init_app(app)
"""
注册业务层
"""
user_resource = UserResource()
bill_resource = BillResource()
npl_resource = NlpResource()
cczh_resource = CczhResource()
ltp_resource = LtpResource()
"""
加载数据
"""
cczh_resource.load_model();
"""
-------------------- User API --------------------
"""
@app.route('/user/info/<user_id>', methods=['GET'])
def user_info(user_id):
return jsonify(user_resource.get(user_id))
"""
--------------------Bill API --------------------
"""
@app.route('/bill/info', methods=['POST'])
def bill_info():
return jsonify(bill_resource.post())
"""
-------------------- NLP API --------------------
"""
@app.route('/nlp/chinese-classification/predict', methods=['GET'])
def nlp_chinese_classification():
return jsonify(npl_resource.predict_model_list())
@app.route('/nlp/chinese-classification-text', methods=['POST'])
def nlp_chinese_classification_text():
print(request.args)
text = request.args.get('text') # 从查询参数中获取 text
if not text:
return jsonify(error='Missing required parameter: text'), 400
return jsonify(npl_resource.predict_model_by_text(text))
"""
-------------------CCZH API --------------------
"""
@app.route('/cczh/get_nearest_neighbors', methods=['POST'])
def get_nearest_neighbors():
text = request.args.get('predictions_text') # 从查询参数中获取 text
if not text:
return jsonify(error='Missing required parameter: predictions_text'), 400
return jsonify(cczh_resource.get_nearest_neighbors(text))
"""
--------------------LTP API --------------------
"""
@app.route('/ltp/get_ner', methods=['POST'])
def get_ner():
text = request.args.get('predictions_text') # 从查询参数中获取 text
if not text:
return jsonify(error='Missing required parameter: predictions_text'), 400
return jsonify(ltp_resource.predict_model_by_text(text))
@app.route('/ltp/predict_words_ltp', methods=['POST'])
def predict_words_ltp():
text = request.args.get('predictions_text') # 从查询参数中获取 text
if not text:
return jsonify(error='Missing required parameter: predictions_text'), 400
return jsonify(ltp_resource.predict_words_ltp(text))
if __name__ == '__main__':
app.run(port=5000)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化