-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
51 lines (43 loc) · 1.77 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pandas as pd
from flask import request, jsonify
from model import train_model, test_model, predict_model
from textutils import find_best_match
def setup_routes(app):
@app.route('/ping', methods=['GET','POST'])
def ping():
return jsonify({
"Ready": "POST train, test, predict or stringmatch. https://github.com/ilya2184/TextClassify"
})
@app.route('/train', methods=['POST'])
def train():
data = pd.read_excel(request.files['file'])
model_id = request.form['model_id']
train_model(data, model_id)
return jsonify({"message": "Model trained successfully."})
@app.route('/test', methods=['POST'])
def test():
data = pd.read_excel(request.files['file'])
model_id = request.form['model_id']
accuracy = test_model(data, model_id)
return jsonify({"accuracy": accuracy})
@app.route('/predict', methods=['POST'])
def predict():
model_id = request.form['model_id']
operation = request.form['operation']
text = request.form['text']
data = pd.DataFrame({ 'ВидОперации': [operation], 'НазначениеПлатежа': [text] })
predata = predict_model(data, model_id)
return jsonify({
"article": predata.get("prediction"),
"confidence": predata.get("confidence")
})
@app.route('/stringmatch', methods=['POST'])
def stringmatch():
data = request.get_json()
strings_list = data.get('string_list', [])
text = data.get('text', '')
length_penalty_factor = data.get('length_penalty_factor', -0.01)
prediction = find_best_match(strings_list, text, length_penalty_factor)
return jsonify({
"prediction": prediction
})