forked from allenai/aristo-mini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
49 lines (40 loc) · 1.77 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""base class that solvers should inherit from"""
from typing import Any
from aristomini.common.models import MultipleChoiceQuestion, MultipleChoiceAnswer, \
SolverAnswer, parse_question
# built in `json` module doesn't serialize namedtuples correctly; `simplejson` does.
import simplejson as json
from flask import Flask, request
from flask_cors import CORS
class SolverBase:
"""
interface for solvers. to implement one just inherit from this class and override
`answer_question` and `solver_info`
"""
def run(self, host='localhost', port=8000) -> None:
"""run the solver"""
app = Flask(__name__)
CORS(app)
@app.route('/answer', methods=['GET', 'POST'])
def solve() -> Any: # pylint: disable=unused-variable
"""
get a json-serialized MultipleChoiceQuestion out of the request body, feed it to
answer_question, and return the json-serialized result
"""
body = request.get_json(force=True)
question = parse_question(body)
multiple_choice_answer = self.answer_question(question)
solver_answer = SolverAnswer(solverInfo=self.solver_info(),
multipleChoiceAnswer=multiple_choice_answer)
return json.dumps(solver_answer)
@app.route('/solver-info')
def info(): # pylint: disable=unused-variable
"""return the solver name"""
return self.solver_info()
app.run(host=host, port=port)
def answer_question(self, question: MultipleChoiceQuestion) -> MultipleChoiceAnswer:
"""answer the question"""
raise NotImplementedError()
def solver_info(self) -> str:
"""info about the solver"""
raise NotImplementedError()