Skip to content

Commit

Permalink
Feature/term table (#3)
Browse files Browse the repository at this point in the history
* makes more sense to preload BERTje before copying app.py

* updated aethel version

* return serialized proof and lexical phrases in response

* parse errors should be (imo) 500
  • Loading branch information
bbonf authored Nov 30, 2023
1 parent dbf1d0e commit a85bfd1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"]
27 changes: 23 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit a85bfd1

Please sign in to comment.