Add endpoint for custom level exams.
This commit is contained in:
49
app.py
49
app.py
@@ -1481,6 +1481,55 @@ def get_level_utas():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class CustomLevelExerciseTypes(Enum):
|
||||||
|
MULTIPLE_CHOICE_4 = "multiple_choice_4"
|
||||||
|
MULTIPLE_CHOICE_BLANK_SPACE = "multiple_choice_blank_space"
|
||||||
|
MULTIPLE_CHOICE_UNDERLINED = "multiple_choice_underlined"
|
||||||
|
BLANK_SPACE_TEXT = "blank_space_text"
|
||||||
|
READING_PASSAGE_UTAS = "reading_passage_utas"
|
||||||
|
|
||||||
|
@app.route('/custom_level', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
def get_custom_level():
|
||||||
|
nr_exercises = int(request.args.get('nr_exercises'))
|
||||||
|
|
||||||
|
exercise_id = 1
|
||||||
|
response = {
|
||||||
|
"exercises": {},
|
||||||
|
"module": "level"
|
||||||
|
}
|
||||||
|
for i in range(1, nr_exercises + 1, 1):
|
||||||
|
exercise_type = request.args.get('exercise_' + str(i) + '_type')
|
||||||
|
exercise_qty = int(request.args.get('exercise_' + str(i) + '_qty', -1))
|
||||||
|
exercise_topic = request.args.get('exercise_' + str(i) + '_topic')
|
||||||
|
exercise_text_size = int(request.args.get('exercise_' + str(i) + '_text_size', -1))
|
||||||
|
exercise_sa_qty = int(request.args.get('exercise_' + str(i) + '_sa_qty', -1))
|
||||||
|
exercise_mc_qty = int(request.args.get('exercise_' + str(i) + '_mc_qty', -1))
|
||||||
|
|
||||||
|
if exercise_type == CustomLevelExerciseTypes.MULTIPLE_CHOICE_4.value:
|
||||||
|
response["exercises"]["exercise_" + str(i)] = generate_level_mc(exercise_id, exercise_qty)
|
||||||
|
response["exercises"]["exercise_" + str(i)]["type"] = "multipleChoice"
|
||||||
|
exercise_id = exercise_id + exercise_qty
|
||||||
|
elif exercise_type == CustomLevelExerciseTypes.MULTIPLE_CHOICE_BLANK_SPACE.value:
|
||||||
|
response["exercises"]["exercise_" + str(i)] = gen_multiple_choice_blank_space_utas(exercise_qty, exercise_id)
|
||||||
|
response["exercises"]["exercise_" + str(i)]["type"] = "multipleChoice"
|
||||||
|
exercise_id = exercise_id + exercise_qty
|
||||||
|
elif exercise_type == CustomLevelExerciseTypes.MULTIPLE_CHOICE_UNDERLINED.value:
|
||||||
|
response["exercises"]["exercise_" + str(i)] = gen_multiple_choice_underlined_utas(exercise_qty, exercise_id)
|
||||||
|
response["exercises"]["exercise_" + str(i)]["type"] = "multipleChoice"
|
||||||
|
exercise_id = exercise_id + exercise_qty
|
||||||
|
elif exercise_type == CustomLevelExerciseTypes.BLANK_SPACE_TEXT.value:
|
||||||
|
response["exercises"]["exercise_" + str(i)] = gen_blank_space_text_utas(exercise_qty, exercise_id, exercise_text_size)
|
||||||
|
response["exercises"]["exercise_" + str(i)]["type"] = "blankSpaceText"
|
||||||
|
exercise_id = exercise_id + exercise_qty
|
||||||
|
elif exercise_type == CustomLevelExerciseTypes.READING_PASSAGE_UTAS.value:
|
||||||
|
response["exercises"]["exercise_" + str(i)] = gen_reading_passage_utas(exercise_id, exercise_sa_qty, exercise_mc_qty, exercise_topic)
|
||||||
|
response["exercises"]["exercise_" + str(i)]["type"] = "readingExercises"
|
||||||
|
exercise_id = exercise_id + exercise_qty
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
@app.route('/fetch_tips', methods=['POST'])
|
@app.route('/fetch_tips', methods=['POST'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
|
|||||||
@@ -1330,7 +1330,7 @@ def parse_conversation(conversation_data):
|
|||||||
return "\n".join(readable_text)
|
return "\n".join(readable_text)
|
||||||
|
|
||||||
|
|
||||||
def gen_multiple_choice_blank_space_utas(quantity: int, start_id: int, all_exams):
|
def gen_multiple_choice_blank_space_utas(quantity: int, start_id: int, all_exams=None):
|
||||||
gen_multiple_choice_for_text = "Generate " + str(
|
gen_multiple_choice_for_text = "Generate " + str(
|
||||||
quantity) + " multiple choice blank space questions of 4 options for an english level exam, some easy questions, some intermediate " \
|
quantity) + " multiple choice blank space questions of 4 options for an english level exam, some easy questions, some intermediate " \
|
||||||
"questions and some advanced questions. Ensure that the questions cover a range of topics such as " \
|
"questions and some advanced questions. Ensure that the questions cover a range of topics such as " \
|
||||||
@@ -1362,11 +1362,12 @@ def gen_multiple_choice_blank_space_utas(quantity: int, start_id: int, all_exams
|
|||||||
if len(question["questions"]) != quantity:
|
if len(question["questions"]) != quantity:
|
||||||
return gen_multiple_choice_level(quantity, start_id)
|
return gen_multiple_choice_level(quantity, start_id)
|
||||||
else:
|
else:
|
||||||
seen_keys = set()
|
if all_exams is not None:
|
||||||
for i in range(len(question["questions"])):
|
seen_keys = set()
|
||||||
question["questions"][i], seen_keys = replace_exercise_if_exists_utas(all_exams, question["questions"][i],
|
for i in range(len(question["questions"])):
|
||||||
question,
|
question["questions"][i], seen_keys = replace_exercise_if_exists_utas(all_exams, question["questions"][i],
|
||||||
seen_keys)
|
question,
|
||||||
|
seen_keys)
|
||||||
return fix_exercise_ids(question, start_id)
|
return fix_exercise_ids(question, start_id)
|
||||||
|
|
||||||
|
|
||||||
@@ -1436,7 +1437,7 @@ def gen_multiple_choice_underlined_utas(quantity: int, start_id: int):
|
|||||||
if len(question["questions"]) != quantity:
|
if len(question["questions"]) != quantity:
|
||||||
return gen_multiple_choice_level(quantity, start_id)
|
return gen_multiple_choice_level(quantity, start_id)
|
||||||
else:
|
else:
|
||||||
return fix_exercise_ids(question, start_id)["questions"]
|
return fix_exercise_ids(question, start_id)
|
||||||
|
|
||||||
|
|
||||||
def gen_blank_space_text_utas(quantity: int, start_id: int, size: int, topic=random.choice(mti_topics)):
|
def gen_blank_space_text_utas(quantity: int, start_id: int, size: int, topic=random.choice(mti_topics)):
|
||||||
@@ -1590,3 +1591,57 @@ def gen_text_multiple_choice_utas(text: str, start_id: int, mc_quantity: int):
|
|||||||
return gen_multiple_choice_level(mc_quantity, start_id)
|
return gen_multiple_choice_level(mc_quantity, start_id)
|
||||||
else:
|
else:
|
||||||
return fix_exercise_ids(question, start_id)["questions"]
|
return fix_exercise_ids(question, start_id)["questions"]
|
||||||
|
|
||||||
|
|
||||||
|
def generate_level_mc(start_id: int, quantity: int):
|
||||||
|
json_format = {
|
||||||
|
"questions": [
|
||||||
|
{
|
||||||
|
"id": "9",
|
||||||
|
"options": [
|
||||||
|
{
|
||||||
|
"id": "A",
|
||||||
|
"text": "a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "B",
|
||||||
|
"text": "b"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "C",
|
||||||
|
"text": "c"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "D",
|
||||||
|
"text": "d"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"prompt": "prompt",
|
||||||
|
"solution": "A",
|
||||||
|
"variant": "text"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
messages = [
|
||||||
|
{
|
||||||
|
"role": "system",
|
||||||
|
"content": 'You are a helpful assistant designed to output JSON on this format: ' + str(json_format)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": ('Generate ' + str(quantity) + ' multiple choice question of 4 options for an english level '
|
||||||
|
'exam, it can be easy, intermediate or advanced.')
|
||||||
|
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": 'Make sure every question only has 1 correct answer.'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
token_count = count_total_tokens(messages)
|
||||||
|
|
||||||
|
question = make_openai_call(GPT_4_O, messages, token_count, ["questions"],
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
|
|
||||||
|
return fix_exercise_ids(question, start_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user