Add speaking task 3 grading endpoint.
This commit is contained in:
37
app.py
37
app.py
@@ -3,7 +3,8 @@ from flask_jwt_extended import JWTManager, jwt_required
|
||||
from functools import reduce
|
||||
import firebase_admin
|
||||
from firebase_admin import credentials, firestore
|
||||
from helper.api_messages import QuestionType, get_grading_messages, get_question_gen_messages, get_question_tips
|
||||
from helper.api_messages import QuestionType, get_grading_messages, get_question_gen_messages, get_question_tips, \
|
||||
get_speaking_grading_messages
|
||||
from helper.file_helper import delete_files_older_than_one_day
|
||||
from helper.firebase_helper import download_firebase_file, upload_file_firebase, upload_file_firebase_get_url, \
|
||||
save_to_db
|
||||
@@ -473,6 +474,40 @@ def save_speaking_task_2_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
@app.route('/speaking_task_3', methods=['POST'])
|
||||
@jwt_required()
|
||||
def grade_speaking_task_3():
|
||||
delete_files_older_than_one_day(AUDIO_FILES_PATH)
|
||||
try:
|
||||
data = request.get_json()
|
||||
answers = data.get('answers')
|
||||
|
||||
for item in answers:
|
||||
sound_file_name = AUDIO_FILES_PATH + str(uuid.uuid4())
|
||||
download_firebase_file(FIREBASE_BUCKET, item["answer"], sound_file_name)
|
||||
answer_text = speech_to_text(sound_file_name)
|
||||
item["answer_text"] = answer_text
|
||||
os.remove(sound_file_name)
|
||||
if not has_10_words(answer_text):
|
||||
return {
|
||||
"comment": "The audio recorded does not contain enough english words to be graded.",
|
||||
"overall": 0,
|
||||
"task_response": {
|
||||
"Fluency and Coherence": 0,
|
||||
"Lexical Resource": 0,
|
||||
"Grammatical Range and Accuracy": 0,
|
||||
"Pronunciation": 0
|
||||
}
|
||||
}
|
||||
|
||||
messages = get_speaking_grading_messages(answers)
|
||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
||||
response = make_openai_call(GPT_3_5_TURBO, messages, token_count, GRADING_FIELDS, GRADING_TEMPERATURE)
|
||||
return response
|
||||
except Exception as e:
|
||||
return str(e), 400
|
||||
|
||||
@app.route('/save_speaking_task_3', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_speaking_task_3_question():
|
||||
|
||||
Reference in New Issue
Block a user