diff --git a/Dockerfile b/Dockerfile index 0b99a8b..a188310 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ RUN git clone https://github.com/konstantinosKokos/spindle.git WORKDIR /spindle # Install aethel -RUN pip install git+https://github.com/konstantinosKokos/aethel@795f34046b7970a28e0e2491ba23dea5e716f1d2 +RUN pip install git+https://github.com/konstantinosKokos/aethel@41eab8fb178a197cdf8de738b68e386f07e6e4f5 # Install PyTorch and its dependencies RUN pip3 install torch==1.12.0 opt_einsum --extra-index-url https://download.pytorch.org/whl/cpu @@ -23,6 +23,9 @@ RUN pip3 install --no-index \ RUN pip3 install transformers==4.20.1 six Flask +# Download BERTje model ahead of time +RUN python -c 'from transformers import pipeline; pipeline("fill-mask", model="GroNLP/bert-base-dutch-cased")' + # Copy data files COPY atom_map.tsv data/atom_map.tsv COPY bert_config.json data/bert_config.json @@ -39,8 +42,5 @@ ENV FLASK_APP=app.py # Shows print logs from our server in the container logs. ENV PYTHONUNBUFFERED=1 -# Download BERTje model ahead of time -RUN python -c 'from transformers import pipeline; pipeline("fill-mask", model="GroNLP/bert-base-dutch-cased")' - # Run the Flask server CMD ["flask", "run", "--host=0.0.0.0"] diff --git a/app.py b/app.py index c44a511..53e6414 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ from flask import Flask, abort, jsonify, request import json from inference import InferenceWrapper +from aethel.mill.serialization import serial_proof_to_json, serialize_proof from aethel.utils.tex import sample_to_tex import logging @@ -44,16 +45,34 @@ def handle_request(): log.info("Analysis complete!") log.info("Results: %s", results) + if len(results) < 1: + log.error("Got no results") + abort(500) + + analysis = results[0] + # spindle will store an exception value in the proof variable, at least in some failure modes + if isinstance(analysis.proof, Exception): + log.error("Error in analysis", exc_info=analysis.proof) + abort(500) + try: - tex_from_sample = sample_to_tex(results[0]) + tex_from_sample = sample_to_tex(analysis) except: - log.error("Failed to convert result to TeX.") - abort(400) + log.exception("Failed to convert result to TeX.") + abort(500) + return # not necessary given abort, but helps type-checker understand that we leave the function here log.info("TeX conversion successful.") log.info("TeX: %s", tex_from_sample) - response = {"results": tex_from_sample} + # prepare json-ready version of proof and lexical phrases + proof = serial_proof_to_json(serialize_proof(analysis.proof)) + lexical_phrases = [phrase.json() for phrase in analysis.lexical_phrases] + + response = dict( + tex=tex_from_sample, + proof=proof, + lexical_phrases=lexical_phrases) return jsonify(response)