Add mini test compatibility.
This commit is contained in:
91
app.py
91
app.py
@@ -3,6 +3,8 @@ import threading
|
||||
from flask import Flask, request
|
||||
from flask_jwt_extended import JWTManager, jwt_required
|
||||
from functools import reduce
|
||||
|
||||
from helper.ExamVariant import ExamVariant
|
||||
from helper.api_messages import *
|
||||
from helper.exercises import *
|
||||
from helper.file_helper import delete_files_older_than_one_day
|
||||
@@ -161,6 +163,7 @@ def save_listening():
|
||||
try:
|
||||
data = request.get_json()
|
||||
parts = data.get('parts')
|
||||
minTimer = data.get('minTimer', LISTENING_MIN_TIMER_DEFAULT)
|
||||
template = getListeningTemplate()
|
||||
id = str(uuid.uuid4())
|
||||
for i, part in enumerate(parts, start=0):
|
||||
@@ -175,6 +178,12 @@ def save_listening():
|
||||
template["parts"][i]["audio"]["source"] = file_url
|
||||
template["parts"][i]["exercises"] = part["exercises"]
|
||||
|
||||
if minTimer != LISTENING_MIN_TIMER_DEFAULT:
|
||||
template["minTimer"] = minTimer
|
||||
template["variant"] = ExamVariant.PARTIAL.value
|
||||
else:
|
||||
template["variant"] = ExamVariant.FULL.value
|
||||
|
||||
(result, id) = save_to_db_with_id("listening", template, id)
|
||||
if result:
|
||||
return {**template, "id": id}
|
||||
@@ -293,25 +302,25 @@ def get_writing_task_2_general_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/writing', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_writing_task():
|
||||
try:
|
||||
data = request.get_json()
|
||||
exercises = data.get('exercises')
|
||||
template = getWritingTemplate()
|
||||
id = str(uuid.uuid4())
|
||||
for i, exercise in enumerate(exercises, start=0):
|
||||
template["exercises"][i]["prompt"] = exercise
|
||||
|
||||
(result, id) = save_to_db_with_id("writing", template, id)
|
||||
if result:
|
||||
return {**template, "id": id}
|
||||
else:
|
||||
raise Exception("Failed to save writing: " + template)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
# THE SAVING OF WRITING IS DONE WITHOUT THE API ON THE FRONTEND
|
||||
# @app.route('/writing', methods=['POST'])
|
||||
# @jwt_required()
|
||||
# def save_writing_task():
|
||||
# try:
|
||||
# data = request.get_json()
|
||||
# exercises = data.get('exercises')
|
||||
# template = getWritingTemplate()
|
||||
# id = str(uuid.uuid4())
|
||||
# for i, exercise in enumerate(exercises, start=0):
|
||||
# template["exercises"][i]["prompt"] = exercise
|
||||
#
|
||||
# (result, id) = save_to_db_with_id("writing", template, id)
|
||||
# if result:
|
||||
# return {**template, "id": id}
|
||||
# else:
|
||||
# raise Exception("Failed to save writing: " + template)
|
||||
# except Exception as e:
|
||||
# return str(e)
|
||||
|
||||
|
||||
@app.route('/speaking_task_1', methods=['POST'])
|
||||
@@ -377,6 +386,7 @@ def get_speaking_task_1_question():
|
||||
token_count = count_tokens(gen_sp1_question)["n_tokens"]
|
||||
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_sp1_question, token_count, GEN_FIELDS,
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
response["type"] = 1
|
||||
return response
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
@@ -446,6 +456,7 @@ def get_speaking_task_2_question():
|
||||
token_count = count_tokens(gen_sp2_question)["n_tokens"]
|
||||
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_sp2_question, token_count, GEN_FIELDS,
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
response["type"] = 2
|
||||
return response
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
@@ -466,6 +477,7 @@ def get_speaking_task_3_question():
|
||||
# Remove the numbers from the questions only if the string starts with a number
|
||||
response["questions"] = [re.sub(r"^\d+\.\s*", "", question) if re.match(r"^\d+\.", question) else question for
|
||||
question in response["questions"]]
|
||||
response["type"] = 3
|
||||
return response
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
@@ -544,7 +556,15 @@ def save_speaking():
|
||||
try:
|
||||
data = request.get_json()
|
||||
exercises = data.get('exercises')
|
||||
minTimer = data.get('minTimer', SPEAKING_MIN_TIMER_DEFAULT)
|
||||
template = getSpeakingTemplate()
|
||||
|
||||
if minTimer != SPEAKING_MIN_TIMER_DEFAULT:
|
||||
template["minTimer"] = minTimer
|
||||
template["variant"] = ExamVariant.PARTIAL.value
|
||||
else:
|
||||
template["variant"] = ExamVariant.FULL.value
|
||||
|
||||
id = str(uuid.uuid4())
|
||||
app.logger.info('Received request to save speaking with id: ' + id)
|
||||
thread_event.set()
|
||||
@@ -646,22 +666,23 @@ def get_reading_passage_3_question():
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/reading', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_reading_passage():
|
||||
try:
|
||||
data = request.get_json()
|
||||
parts = data.get('parts')
|
||||
template = getReadingTemplate()
|
||||
template["parts"] = parts
|
||||
id = str(uuid.uuid4())
|
||||
(result, id) = save_to_db_with_id("reading", template, id)
|
||||
if result:
|
||||
return {**template, "id": id}
|
||||
else:
|
||||
raise Exception("Failed to save reading: " + template)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
# THE SAVING OF READING IS DONE WITHOUT THE API ON THE FRONTEND
|
||||
# @app.route('/reading', methods=['POST'])
|
||||
# @jwt_required()
|
||||
# def save_reading_passage():
|
||||
# try:
|
||||
# data = request.get_json()
|
||||
# parts = data.get('parts')
|
||||
# template = getReadingTemplate()
|
||||
# template["parts"] = parts
|
||||
# id = str(uuid.uuid4())
|
||||
# (result, id) = save_to_db_with_id("reading", template, id)
|
||||
# if result:
|
||||
# return {**template, "id": id}
|
||||
# else:
|
||||
# raise Exception("Failed to save reading: " + template)
|
||||
# except Exception as e:
|
||||
# return str(e)
|
||||
|
||||
|
||||
@app.route('/level', methods=['GET'])
|
||||
|
||||
Reference in New Issue
Block a user