import os import lxml.etree as lxml from flask import Flask, Response from flask_httpauth import HTTPBasicAuth from structure_assignment.pipeline import Pipeline, create_nlp app = Flask(__name__) api_prefix = os.environ['API_PREFIX'] resource_directory = os.environ['API_RESOURCE_DIR'] nlp = create_nlp(resource_directory) @app.route(api_prefix + '/test/', methods=['GET']) def test(string): string_file_name = '/tmp/string.txt' parse_file_name = '/tmp/parse.xml' with open(string_file_name, 'w') as string_file: string_file.write(string + '\n') try: pipeline = Pipeline(nlp) pipeline.import_file(string_file_name, 'strings-list') pipeline.do_tokenise() pipeline.do_tweak_conllu() pipeline.do_parse() pipeline.do_translate_jos() pipeline.do_conllu_to_tei() pipeline.export_file(parse_file_name, 'tei-initial') pipeline.cleanup() tei = lxml.parse(parse_file_name).getroot() message = lxml.tostring(tei, encoding='UTF-8', pretty_print=True).decode() ok = True except Exception as e: message = str(e) ok = False results = {'ok':ok, 'message':message} return Response(message, mimetype='text/xml')