代码拉取完成,页面将自动刷新
from flask import Flask, jsonify, request
# from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from flask_cors import CORS
from flair.models import TextClassifier
from flair.data import Sentence
from flask import session
import os
model_path = '/root/flask_vue_ML'
expose = Flask(__name__)
expose.secret_key = "super_secret_key"
# expose.config['MONGO_DBNAME'] = 'exposeModel'
# expose.config['MONGO_URI'] = 'mongodb://localhost:27017/exposeModel'
# mongo = PyMongo(expose)
CORS(expose)
classifier = TextClassifier.load_from_file(os.path.join(model_path,'models/best-model.pt'))
@expose.route('/', methods=['GET'])
def index():
return jsonify("welcome to Arafa API")
@expose.route('/api/tasks', methods=['GET'])
def get_result():
result = []
try:
data_result = session['my_result']
result.append ({'title': data_result['title'], 'tag': data_result['tag'] })
except:
result.append ({'title': 'The txt you input', 'tag': 'spam or harm' })
return jsonify(result)
@expose.route('/api/task', methods=['POST','GET'])
def input_predict_text():
title = request.get_json()['title']
# TODO: title type check
sentence = Sentence(title)
classifier.predict(sentence)
text = sentence.to_plain_string()
label = sentence.labels[0]
result = {'title' : text, 'tag' : label.value}
session['my_result'] = result
return jsonify(result)
if __name__ == '__main__':
# expose.run(debug=True)
expose.run(host='0.0.0.0')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。