From c275cb887d693576a044e889f7616ea2abc4a7ce Mon Sep 17 00:00:00 2001 From: Cristiano Ferreira Date: Tue, 5 Sep 2023 21:18:42 +0100 Subject: [PATCH] Add verification for words in writing grading. --- app.py | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 8c83186..c9c95dd 100644 --- a/app.py +++ b/app.py @@ -128,11 +128,23 @@ def grade_writing_task_1(): question = data.get('question') context = data.get('context') answer = data.get('answer') - messages = get_grading_messages(QuestionType.WRITING_TASK_1, question, answer, context) - 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 + if has_words(answer): + messages = get_grading_messages(QuestionType.WRITING_TASK_1, question, answer, context) + 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 + else: + return { + 'comment': "The answer does not contain any english words.", + 'overall': 0, + 'task_response': { + 'Coherence and Cohesion': 0, + 'Grammatical Range and Accuracy': 0, + 'Lexical Resource': 0, + 'Task Achievement': 0 + } + } except Exception as e: return str(e) @@ -144,11 +156,23 @@ def grade_writing_task_2(): data = request.get_json() question = data.get('question') answer = data.get('answer') - messages = get_grading_messages(QuestionType.WRITING_TASK_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) - return response + if has_words(answer): + messages = get_grading_messages(QuestionType.WRITING_TASK_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) + return response + else: + return { + 'comment': "The answer does not contain any english words.", + 'overall': 0, + 'task_response': { + 'Coherence and Cohesion': 0, + 'Grammatical Range and Accuracy': 0, + 'Lexical Resource': 0, + 'Task Achievement': 0 + } + } except Exception as e: return str(e) @@ -200,7 +224,7 @@ 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_words(answer): + if has_words("ajajajajd"): 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)