Skip to content
Snippets Groups Projects
Commit c0609749 authored by Vladimíra Kimlová's avatar Vladimíra Kimlová
Browse files

Merge branch 'ai_output_transform' into 'main'

issue #11406 Výstup evaluace upraven do přijatelného formátu pro Moodle

See merge request !56
parents abc922bd 20ca9c42
No related branches found
No related tags found
1 merge request!56issue #11406 Výstup evaluace upraven do přijatelného formátu pro Moodle
Pipeline #9972 passed
......@@ -9,6 +9,7 @@ sys.path.append(os.path.join(os.getcwd(), ".."))
from ai.src.generator.generator_handler import generate_sheets
from ai.src.evaluator.preprocessor import preprocess_image
from ai.src.utils import transform_eval_output_to_moodle
# Initialize the Flask app
......@@ -87,10 +88,14 @@ def evaluate_answers():
fp.write(file_data)
# Extract text from the PDF
json_data = preprocess_image(collection, "temp.pdf")
json_data, test_id = preprocess_image(collection, "temp.pdf")
os.remove("temp.pdf")
return jsonify(json_data)
# Transform the output to a Moodle happy output
db_data = collection.find_one({"test_id": test_id})
result = transform_eval_output_to_moodle(json_data, db_data)
return jsonify(result)
return catch_errors(inner_func)()
......@@ -103,4 +108,4 @@ if __name__ == '__main__':
if os.environ.get('ENV') == 'production':
app.run(host='0.0.0.0', port=8081)
else:
app.run(debug=True)
\ No newline at end of file
app.run(debug=True)
......@@ -281,4 +281,4 @@ def preprocess_image(collection, path_to_image):
output = {"student_id": ''.join(str(column.index(1)) for column in zip(*answers[0]) if 1 in column),
"answers": [item for sublist in answers[1:] for item in sublist]}
return output
return output, test_id
import json
import numpy as np
# Config filepath
CONFIG_FILE = "config.json"
......@@ -78,3 +79,49 @@ def get_num_of_rects_per_page(num_of_rect, num_of_pages, num_of_rects_per_page):
num_of_rects_in_page.append(num_of_rects_per_page)
return num_of_rects_in_page
def transform_eval_output_to_moodle(json_data, db_data):
"""
Transform the evaluation output to a Moodle happy output
:param json_data: Evaluation output
:param db_data: Data from the database
:return: JSON response containing the student ID and answers
"""
student_id = int(json_data["student_id"])
questions = db_data["questions"]
student_answers = json_data["answers"]
student_dict = {}
for student in db_data["students"]:
if student["id"] == student_id:
student_dict = student
result = {
"lastname": student_dict["surname"],
"firstname": student_dict["name"],
"emailaddress": "anonymous@anonymous.anonymous",
"state": "Finished",
"startedon": "1 January 2024 00:00 AM",
"completed": "1 January 2024 00:00 AM",
"timetaken": "0 secs",
"grade1000": "0.00"
}
shuffle = student_dict["shuffle"]
undo_shuffler = np.argsort(shuffle)
unshuffled_answers = [student_answers[i] for i in undo_shuffler]
for i, question in enumerate(questions):
answers = question["answers"]
student_answers = unshuffled_answers[i]
for j, answer in enumerate(answers):
student_answer = student_answers[j]
if student_answer == 1:
answer_value = answer["text"]
if answer_value == "true":
answer_value = "Pravda"
elif answer_value == "false":
answer_value = "Nepravda"
result[f"response{i + 1}"] = answer_value
return [[result]]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment