-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
168 lines (139 loc) · 5.87 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from flask import Flask, render_template, request, jsonify
from module_outputs import generate_question,correct_answer
import json
#Flask tutorials:
#https://web.itu.edu.tr/uyar/fad/forms.html
#https://medium.com/analytics-vidhya/creating-login-page-on-flask-9d20738d9f42
#https://programminghistorian.org/en/lessons/creating-apis-with-python-and-flask
#TODO: multilinguality employing Wikidata
#TODO: teacher and student login and question distribution
#TODO: allow for formula string input (combine with MathQA functionality)
#TODO: fix greek to latin converted letter duplicate issue
app = Flask(__name__)
# REQUEST
@app.route('/api/v1', methods=['GET'])
def api_qid():
# Check if a Wikidata item concept name or QID was provided as part of the URL.
# If QID is provided, assign it to a variable.
# If no QID is provided, display an error in the browser.
if 'name' in request.args:
name_qid = request.args['name']
elif 'qid' in request.args:
name_qid = request.args['qid']
else:
return "Error: No Wikidata item name or qid provided. Please specify a name or qid."
# Generate response
question = generate_question(name_qid)
question_text = question[0]
identifier_values = question[1]
formula_unit_dimension = question[2]
explanation_text = question[3]
response = {'question_text': question_text, 'identifier_values': identifier_values,
'formula_unit_dimension': formula_unit_dimension, 'explanation_text': explanation_text}
return jsonify(response)
# POST
def my_form_post():
# Init variables
answer = ''
correction = ''
explanation = ''
# Read from cache
with open('cache.json', 'r') as f:
cache = json.load(f)
concept = cache['concept']
question = cache['question']
if 'concept' in request.form:# and cache['question_generated']==False:
# QUESTION GENERATION
try:
concept = request.form['concept']
question = 'Unknown error'
question_text, identifier_info, formula_info, explanation = generate_question(concept)
question = question_text
cache['correct_value'] = str(identifier_info[0])
cache['correct_unit'] = formula_info
cache['explanation'] = explanation
explanation = ''
cache['question_generated'] = True
except:
pass
#question = 'Question could not be generated'
if 'answer' in request.form:# and cache['question_generated']==True:
# ANSWER CORRECTION
try:
answer = request.form['answer']
value_correct, unit_correct = correct_answer(cache['correct_value'],cache['correct_unit'],answer)
# Set correction text
# correction = value_correct, unit_correct
# correction = 'Value answer correct: ' + str(value_correct) + ", " + 'Unit answer correct: ' + str(unit_correct)
value_prefix = 'Value '
unit_prefix = 'Unit '
if value_correct:
value_suffix = 'correct!'
elif not value_correct:
value_suffix = 'incorrect!'
if unit_correct:
unit_suffix = 'correct!'
elif not unit_correct:
unit_suffix = 'incorrect!'
value_text = value_prefix + value_suffix
unit_text = unit_prefix + unit_suffix
# TODO: add Wikipedia URL to correction text
explanation = cache['explanation']
correction = ' '.join([value_text,unit_text])
# No question given
if not '=' in question:
correction = 'No question given'
explanation = ''
# Wrong format
if len(answer.split()) < 2:
correction = 'Please input value AND unit (space-separated)!'
explanation = ''
cache['question_generated'] = False
except:
pass
# Write to cache
cache['concept'] = concept
cache['question'] = question
with open('cache.json', 'w') as f:
json.dump(cache,f)
return {'concept':concept,'question':question,
'answer':answer,'correction':correction,'explanation':explanation}
# TEACHER
@app.route('/teacher')
def teacher():
#return render_template('teacher.html')
r = my_form_post()
return render_template('teacher.html',
concept=r['concept'], question=r['question'],
answer=r['answer'], correction=r['correction'], explanation=r['explanation'])
@app.route('/teacher',methods=['POST'])
def teacher_post():
r = my_form_post()
return render_template('teacher.html',
concept=r['concept'], question=r['question'],
answer=r['answer'], correction=r['correction'], explanation=r['explanation'])
# STUDENT
@app.route('/student')
def student():
#return render_template('student.html')
r = my_form_post()
return render_template('student.html',
concept=r['concept'], question=r['question'],
answer=r['answer'], correction=r['correction'], explanation=r['explanation'])
@app.route('/student',methods=['POST'])
def student_post():
r = my_form_post()
return render_template('student.html',
concept=r['concept'], question=r['question'],
answer=r['answer'], correction=r['correction'], explanation=r['explanation'])
# MAIN APP
@app.route('/')
def app_form():
return render_template('my-form.html')
@app.route('/', methods=['POST'])
def app_form_post():
r = my_form_post()
return render_template('select.html',concept=r['concept'], question=r['question'],
answer=r['answer'], correction=r['correction'], explanation=r['explanation'])
if __name__ == '__main__':
app.run(host='0.0.0.0')#debug=True