From a1577029b5c5a2f96825e94cf08f5f7dc30e6ce1 Mon Sep 17 00:00:00 2001 From: Cyprian Laskowski Date: Mon, 1 Mar 2021 15:37:18 +0100 Subject: [PATCH] Redmine #1835: preloaded classla models and used xml for api response --- package/structure_assignment/api.py | 6 +++--- package/structure_assignment/pipeline.py | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/package/structure_assignment/api.py b/package/structure_assignment/api.py index 66de9fc..d9b5e28 100644 --- a/package/structure_assignment/api.py +++ b/package/structure_assignment/api.py @@ -2,7 +2,7 @@ import os import lxml.etree as lxml -from flask import Flask, jsonify, make_response, request +from flask import Flask, Response from flask_httpauth import HTTPBasicAuth import structure_assignment.pipeline as pipeline @@ -11,6 +11,7 @@ app = Flask(__name__) api_prefix = os.environ['API_PREFIX'] resource_directory = os.environ['API_RESOURCE_DIR'] tmp_directory = os.environ['API_TMP'] +pipeline.initialise(temp_dir=tmp_directory, resource_dir=resource_directory) @app.route(api_prefix + '/test/', methods=['GET']) def test(string): @@ -21,7 +22,6 @@ def test(string): string_file.write(string + '\n') try: - pipeline.initialise(temp_dir=tmp_directory, resource_dir=resource_directory) pipeline.import_string_file(string_file_name) pipeline.do_tokenise() pipeline.do_tweak_conllu() @@ -37,4 +37,4 @@ def test(string): ok = False results = {'ok':ok, 'message':message} - return jsonify(results) + return Response(message, mimetype='text/xml') \ No newline at end of file diff --git a/package/structure_assignment/pipeline.py b/package/structure_assignment/pipeline.py index 762c1db..4a42584 100644 --- a/package/structure_assignment/pipeline.py +++ b/package/structure_assignment/pipeline.py @@ -24,16 +24,19 @@ XML_ID_PREFIX = 's' resource_directory = None tmp_directory = None +nlp = None def __get_tmp_file_name(file_name): return tmp_directory + '/' + file_name def initialise(**argument_map): - global tmp_directory, resource_directory + global tmp_directory, resource_directory, nlp tmp_directory = argument_map['temp_dir'] resource_directory = argument_map['resource_dir'] shutil.rmtree(tmp_directory, True) os.makedirs(tmp_directory, exist_ok=True) + NLP_CONFIG_MAP['models_dir'] = resource_directory + '/classla' + nlp = classla.Pipeline('sl', **NLP_CONFIG_MAP) def import_string_file(file_name): shutil.copyfile(file_name, __get_tmp_file_name(STRING_LIST_FILE_NAME)) @@ -54,8 +57,6 @@ def do_parse(): doc = Document(text=None) conll_file = CoNLLFile(filename=input_file_name) doc.conll_file = conll_file - NLP_CONFIG_MAP['models_dir'] = resource_directory + '/classla' - nlp = classla.Pipeline('sl', **NLP_CONFIG_MAP) result = nlp(doc) result.conll_file.write_conll(output_file_name)