You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB

import os
import lxml.etree as lxml
from flask import Flask, Response
from flask_httpauth import HTTPBasicAuth
import structure_assignment.pipeline as pipeline
app = Flask(__name__)
api_prefix = os.environ['API_PREFIX']
resource_directory = os.environ['API_RESOURCE_DIR']
pipeline.initialise(resource_dir=resource_directory)
pipeline.load_classla_models()
@app.route(api_prefix + '/test/<string:string>', 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.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')