Redmine #1835: preloaded classla models and used xml for api response

This commit is contained in:
Cyprian Laskowski 2021-03-01 15:37:18 +01:00
parent 5f25682036
commit a1577029b5
2 changed files with 7 additions and 6 deletions

View File

@ -2,7 +2,7 @@ import os
import lxml.etree as lxml import lxml.etree as lxml
from flask import Flask, jsonify, make_response, request from flask import Flask, Response
from flask_httpauth import HTTPBasicAuth from flask_httpauth import HTTPBasicAuth
import structure_assignment.pipeline as pipeline import structure_assignment.pipeline as pipeline
@ -11,6 +11,7 @@ app = Flask(__name__)
api_prefix = os.environ['API_PREFIX'] api_prefix = os.environ['API_PREFIX']
resource_directory = os.environ['API_RESOURCE_DIR'] resource_directory = os.environ['API_RESOURCE_DIR']
tmp_directory = os.environ['API_TMP'] tmp_directory = os.environ['API_TMP']
pipeline.initialise(temp_dir=tmp_directory, resource_dir=resource_directory)
@app.route(api_prefix + '/test/<string:string>', methods=['GET']) @app.route(api_prefix + '/test/<string:string>', methods=['GET'])
def test(string): def test(string):
@ -21,7 +22,6 @@ def test(string):
string_file.write(string + '\n') string_file.write(string + '\n')
try: try:
pipeline.initialise(temp_dir=tmp_directory, resource_dir=resource_directory)
pipeline.import_string_file(string_file_name) pipeline.import_string_file(string_file_name)
pipeline.do_tokenise() pipeline.do_tokenise()
pipeline.do_tweak_conllu() pipeline.do_tweak_conllu()
@ -37,4 +37,4 @@ def test(string):
ok = False ok = False
results = {'ok':ok, 'message':message} results = {'ok':ok, 'message':message}
return jsonify(results) return Response(message, mimetype='text/xml')

View File

@ -24,16 +24,19 @@ XML_ID_PREFIX = 's'
resource_directory = None resource_directory = None
tmp_directory = None tmp_directory = None
nlp = None
def __get_tmp_file_name(file_name): def __get_tmp_file_name(file_name):
return tmp_directory + '/' + file_name return tmp_directory + '/' + file_name
def initialise(**argument_map): def initialise(**argument_map):
global tmp_directory, resource_directory global tmp_directory, resource_directory, nlp
tmp_directory = argument_map['temp_dir'] tmp_directory = argument_map['temp_dir']
resource_directory = argument_map['resource_dir'] resource_directory = argument_map['resource_dir']
shutil.rmtree(tmp_directory, True) shutil.rmtree(tmp_directory, True)
os.makedirs(tmp_directory, exist_ok=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): def import_string_file(file_name):
shutil.copyfile(file_name, __get_tmp_file_name(STRING_LIST_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) doc = Document(text=None)
conll_file = CoNLLFile(filename=input_file_name) conll_file = CoNLLFile(filename=input_file_name)
doc.conll_file = conll_file doc.conll_file = conll_file
NLP_CONFIG_MAP['models_dir'] = resource_directory + '/classla'
nlp = classla.Pipeline('sl', **NLP_CONFIG_MAP)
result = nlp(doc) result = nlp(doc)
result.conll_file.write_conll(output_file_name) result.conll_file.write_conll(output_file_name)