Add perfect answers to speaking
This commit is contained in:
87
app.py
87
app.py
@@ -317,11 +317,25 @@ def grade_speaking_task_1():
|
|||||||
download_firebase_file(FIREBASE_BUCKET, answer_firebase_path, sound_file_name)
|
download_firebase_file(FIREBASE_BUCKET, answer_firebase_path, sound_file_name)
|
||||||
answer = speech_to_text(sound_file_name)
|
answer = speech_to_text(sound_file_name)
|
||||||
if has_10_words(answer):
|
if has_10_words(answer):
|
||||||
messages = get_grading_messages(QuestionType.SPEAKING_1, question, answer)
|
message = (
|
||||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
"Grade this Speaking Part 1 answer according to ielts grading system and provide an elaborated "
|
||||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
"comment where you deep dive into what is wrong and right about the answer."
|
||||||
response = make_openai_call(GPT_3_5_TURBO, messages, token_count, GRADING_FIELDS, GRADING_TEMPERATURE)
|
"Please assign a grade of 0 if the answer provided does not address the question."
|
||||||
os.remove(sound_file_name)
|
"Provide your answer on the following json format: {'comment': 'comment about answer quality', 'overall': 0.0, "
|
||||||
|
"'task_response': {'Fluency and Coherence': 0.0, 'Lexical Resource': 0.0, 'Grammatical Range and Accuracy': 0.0, "
|
||||||
|
"'Pronunciation': 0.0}}\n The question was '" + question + "' and the answer was '" + answer + "'")
|
||||||
|
token_count = count_tokens(message)["n_tokens"]
|
||||||
|
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, message, token_count,
|
||||||
|
["comment"],
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
|
perfect_answer_message = ("Provide a perfect answer according to ielts grading system to the following "
|
||||||
|
"Speaking Part 1 question: '" + question + "'")
|
||||||
|
token_count = count_tokens(perfect_answer_message)["n_tokens"]
|
||||||
|
response['perfect_answer'] = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT,
|
||||||
|
perfect_answer_message,
|
||||||
|
token_count,
|
||||||
|
None,
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
return {
|
return {
|
||||||
@@ -369,11 +383,25 @@ def grade_speaking_task_2():
|
|||||||
download_firebase_file(FIREBASE_BUCKET, answer_firebase_path, sound_file_name)
|
download_firebase_file(FIREBASE_BUCKET, answer_firebase_path, sound_file_name)
|
||||||
answer = speech_to_text(sound_file_name)
|
answer = speech_to_text(sound_file_name)
|
||||||
if has_10_words(answer):
|
if has_10_words(answer):
|
||||||
messages = get_grading_messages(QuestionType.SPEAKING_2, question, answer)
|
message = (
|
||||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
"Grade this Speaking Part 2 answer according to ielts grading system and provide an elaborated "
|
||||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
"comment where you deep dive into what is wrong and right about the answer."
|
||||||
response = make_openai_call(GPT_3_5_TURBO, messages, token_count, GRADING_FIELDS, GRADING_TEMPERATURE)
|
"Please assign a grade of 0 if the answer provided does not address the question."
|
||||||
os.remove(sound_file_name)
|
"Provide your answer on the following json format: {'comment': 'comment about answer quality', 'overall': 0.0, "
|
||||||
|
"'task_response': {'Fluency and Coherence': 0.0, 'Lexical Resource': 0.0, 'Grammatical Range and Accuracy': 0.0, "
|
||||||
|
"'Pronunciation': 0.0}}\n The question was '" + question + "' and the answer was '" + answer + "'")
|
||||||
|
token_count = count_tokens(message)["n_tokens"]
|
||||||
|
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, message, token_count,
|
||||||
|
["comment"],
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
|
perfect_answer_message = ("Provide a perfect answer according to ielts grading system to the following "
|
||||||
|
"Speaking Part 2 question: '" + question + "'")
|
||||||
|
token_count = count_tokens(perfect_answer_message)["n_tokens"]
|
||||||
|
response['perfect_answer'] = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT,
|
||||||
|
perfect_answer_message,
|
||||||
|
token_count,
|
||||||
|
None,
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
return {
|
return {
|
||||||
@@ -436,12 +464,12 @@ def grade_speaking_task_3():
|
|||||||
try:
|
try:
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
answers = data.get('answers')
|
answers = data.get('answers')
|
||||||
|
perfect_answers = []
|
||||||
for item in answers:
|
for item in answers:
|
||||||
sound_file_name = AUDIO_FILES_PATH + str(uuid.uuid4())
|
sound_file_name = AUDIO_FILES_PATH + str(uuid.uuid4())
|
||||||
download_firebase_file(FIREBASE_BUCKET, item["answer"], sound_file_name)
|
download_firebase_file(FIREBASE_BUCKET, item["answer"], sound_file_name)
|
||||||
answer_text = speech_to_text(sound_file_name)
|
answer_text = speech_to_text(sound_file_name)
|
||||||
item["answer_text"] = answer_text
|
item["answer"] = answer_text
|
||||||
os.remove(sound_file_name)
|
os.remove(sound_file_name)
|
||||||
if not has_10_words(answer_text):
|
if not has_10_words(answer_text):
|
||||||
return {
|
return {
|
||||||
@@ -454,11 +482,36 @@ def grade_speaking_task_3():
|
|||||||
"Pronunciation": 0
|
"Pronunciation": 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
perfect_answer_message = ("Provide a perfect answer according to ielts grading system to the following "
|
||||||
|
"Speaking Part 3 question: '" + item["question"] + "'")
|
||||||
|
token_count = count_tokens(perfect_answer_message)["n_tokens"]
|
||||||
|
perfect_answers.append(make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT,
|
||||||
|
perfect_answer_message,
|
||||||
|
token_count,
|
||||||
|
None,
|
||||||
|
GEN_QUESTION_TEMPERATURE))
|
||||||
|
message = (
|
||||||
|
"Grade this Speaking Part 3 answer according to ielts grading system and provide "
|
||||||
|
"an elaborated comment where you deep dive into what is wrong and right about the answers."
|
||||||
|
"Please assign a grade of 0 if the answer provided does not address the question."
|
||||||
|
"\n\n The questions and answers are: \n\n'")
|
||||||
|
|
||||||
messages = get_speaking_grading_messages(answers)
|
formatted_text = ""
|
||||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
for i, entry in enumerate(answers, start=1):
|
||||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
formatted_text += f"**Question {i}:**\n{entry['question']}\n\n"
|
||||||
response = make_openai_call(GPT_3_5_TURBO, messages, token_count, GRADING_FIELDS, GRADING_TEMPERATURE)
|
formatted_text += f"**Answer {i}:**\n{entry['answer']}\n\n"
|
||||||
|
|
||||||
|
message += formatted_text
|
||||||
|
message += ("'\n\nProvide your answer on the following json format: {'comment': 'comment about answer quality', "
|
||||||
|
"'overall': 0.0, 'task_response': {'Fluency and Coherence': 0.0, 'Lexical Resource': 0.0, "
|
||||||
|
"'Grammatical Range and Accuracy': 0.0, 'Pronunciation': 0.0}}")
|
||||||
|
|
||||||
|
token_count = count_tokens(message)["n_tokens"]
|
||||||
|
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, message, token_count,
|
||||||
|
["comment"],
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
|
for i, answer in enumerate(perfect_answers, start=1):
|
||||||
|
response['perfect_answer_' + str(i)] = answer
|
||||||
return response
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e), 400
|
return str(e), 400
|
||||||
@@ -522,7 +575,7 @@ def save_speaking():
|
|||||||
print("Failed to create video for part 3 question: " + question)
|
print("Failed to create video for part 3 question: " + question)
|
||||||
template["exercises"][2]["prompts"] = sp3_questions
|
template["exercises"][2]["prompts"] = sp3_questions
|
||||||
template["exercises"][2]["title"] = exercises[2]["topic"]
|
template["exercises"][2]["title"] = exercises[2]["topic"]
|
||||||
|
|
||||||
(result, id) = save_to_db("speaking", template)
|
(result, id) = save_to_db("speaking", template)
|
||||||
if result:
|
if result:
|
||||||
return {**template, "id": id}
|
return {**template, "id": id}
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ def make_openai_instruct_call(model, message: str, token_count, fields_to_check,
|
|||||||
)["choices"][0]["text"]
|
)["choices"][0]["text"]
|
||||||
|
|
||||||
if fields_to_check is None:
|
if fields_to_check is None:
|
||||||
return response
|
return response.replace("\n\n", " ").strip()
|
||||||
|
|
||||||
processed_response = process_response(response, fields_to_check[0])
|
processed_response = process_response(response, fields_to_check[0])
|
||||||
if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT:
|
if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT:
|
||||||
|
|||||||
Reference in New Issue
Block a user