File size: 2,560 Bytes
92da9af
 
 
 
ffa6434
 
92da9af
ffa6434
 
92da9af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-

from unicodedata import normalize

from flask import render_template, request, jsonify
from kami.Kami import Kami

from config import app
from modules.utils import serialize_scores, show_diff_color_html


@app.route('/')
@app.route('/compute_results', methods=['GET', 'POST'])
def index():
    if request.method == "POST":
        #try:
        response = dict(request.json)
        kevaluator = Kami([
            response['reference'],
            response['prediction']],
            verbosity=app.config["KAMI_OPT_VERB"],
            truncate=app.config["KAMI_OPT_TRUNC"],
            percent=app.config["KAMI_OPT_PERC"],
            round_digits=app.config["KAMI_OPT_ROUND"],
            apply_transforms=response['preprocessingOpts']
        )
        # get preprocess sentences from kami evaluator
        # uncomment to display this sentence
        # in versus text
        """

        reference = kevaluator.reference_preprocess \

            if kevaluator.reference_preprocess != "" \

            else response['reference']

        prediction = kevaluator.prediction_preprocess \

            if kevaluator.prediction_preprocess != "" \

            else response['prediction']

        """
        # test if versustext feature activate by user
        versus_text = {"comparaison": []}
        if bool(response['vtOpt']):
            versus_text = show_diff_color_html(
                normalize("NFKD", str(response['reference'])),
                normalize("NFKD", str(response['prediction']))
            )
        # add this to jsonify if something wrong
        # with show_diff_color_html() : **{"reference": reference, "prediction": prediction},
        return jsonify({**serialize_scores(kevaluator.scores.board),
                        **versus_text
                        }), 200
        #except MemoryError as e:
        #    print(e)
        #    return jsonify({}), 400
    return render_template('page/index.html',
                           title="KaMI App",
                           kami_version=app.config['KAMI_VERSION']), 200


@app.errorhandler(404)
def cant_find_page(error):
    """Redirect to 404.html"""
    return render_template("error/404.html", title="KaMI App | Error 404", kami_version=app.config['KAMI_VERSION']), 404

@app.errorhandler(500)
def server_unavailable(error):
    """Redirect to 500.html"""
    return render_template("error/500.html", title="KaMI App | Error 500", kami_version=app.config['KAMI_VERSION']), 500