diff --git a/app.py b/app.py index 3a07062..17545bc 100644 --- a/app.py +++ b/app.py @@ -317,11 +317,25 @@ def grade_speaking_task_1(): download_firebase_file(FIREBASE_BUCKET, answer_firebase_path, sound_file_name) answer = speech_to_text(sound_file_name) if has_10_words(answer): - messages = get_grading_messages(QuestionType.SPEAKING_1, question, answer) - 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) - os.remove(sound_file_name) + message = ( + "Grade this Speaking Part 1 answer according to ielts grading system and provide an elaborated " + "comment where you deep dive into what is wrong and right about the answer." + "Please assign a grade of 0 if the answer provided does not address the question." + "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 else: return { @@ -369,11 +383,25 @@ def grade_speaking_task_2(): download_firebase_file(FIREBASE_BUCKET, answer_firebase_path, sound_file_name) answer = speech_to_text(sound_file_name) if has_10_words(answer): - messages = get_grading_messages(QuestionType.SPEAKING_2, question, answer) - 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) - os.remove(sound_file_name) + message = ( + "Grade this Speaking Part 2 answer according to ielts grading system and provide an elaborated " + "comment where you deep dive into what is wrong and right about the answer." + "Please assign a grade of 0 if the answer provided does not address the question." + "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 else: return { @@ -436,12 +464,12 @@ def grade_speaking_task_3(): try: data = request.get_json() answers = data.get('answers') - + perfect_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 + item["answer"] = answer_text os.remove(sound_file_name) if not has_10_words(answer_text): return { @@ -454,11 +482,36 @@ def grade_speaking_task_3(): "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) - 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) + formatted_text = "" + for i, entry in enumerate(answers, start=1): + formatted_text += f"**Question {i}:**\n{entry['question']}\n\n" + 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 except Exception as e: return str(e), 400 @@ -522,7 +575,7 @@ def save_speaking(): print("Failed to create video for part 3 question: " + question) template["exercises"][2]["prompts"] = sp3_questions template["exercises"][2]["title"] = exercises[2]["topic"] - + (result, id) = save_to_db("speaking", template) if result: return {**template, "id": id} diff --git a/helper/openai_interface.py b/helper/openai_interface.py index 85ddd5e..5c6bf99 100644 --- a/helper/openai_interface.py +++ b/helper/openai_interface.py @@ -132,7 +132,7 @@ def make_openai_instruct_call(model, message: str, token_count, fields_to_check, )["choices"][0]["text"] if fields_to_check is None: - return response + return response.replace("\n\n", " ").strip() processed_response = process_response(response, fields_to_check[0]) if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT: