Reformat code.

This commit is contained in:
Cristiano Ferreira
2024-05-20 14:40:09 +01:00
parent e568aff4e4
commit 9654d9ff64

41
app.py
View File

@@ -260,8 +260,8 @@ def grade_writing_task_1():
] ]
token_count = count_total_tokens(messages) token_count = count_total_tokens(messages)
response = make_openai_call(GPT_3_5_TURBO, messages, token_count, response = make_openai_call(GPT_3_5_TURBO, messages, token_count,
["comment"], ["comment"],
GRADING_TEMPERATURE) GRADING_TEMPERATURE)
response["overall"] = fix_writing_overall(response["overall"], response["task_response"]) response["overall"] = fix_writing_overall(response["overall"], response["task_response"])
response['fixed_text'] = get_fixed_text(answer) response['fixed_text'] = get_fixed_text(answer)
return response return response
@@ -295,7 +295,7 @@ def get_writing_task_1_general_question():
] ]
token_count = count_total_tokens(messages) token_count = count_total_tokens(messages)
response = make_openai_call(GPT_3_5_TURBO, messages, token_count, "prompt", response = make_openai_call(GPT_3_5_TURBO, messages, token_count, "prompt",
GEN_QUESTION_TEMPERATURE) GEN_QUESTION_TEMPERATURE)
return { return {
"question": response["prompt"].strip(), "question": response["prompt"].strip(),
"difficulty": difficulty, "difficulty": difficulty,
@@ -346,7 +346,8 @@ def grade_writing_task_2():
}, },
{ {
"role": "user", "role": "user",
"content": ('Evaluate the given Writing Task 2 response based on the IELTS grading system, ensuring a ' "content": (
'Evaluate the given Writing Task 2 response based on the IELTS grading system, ensuring a '
'strict assessment that penalizes errors. Deduct points for deviations from the task, and ' 'strict assessment that penalizes errors. Deduct points for deviations from the task, and '
'assign a score of 0 if the response fails to address the question. Additionally, provide an ' 'assign a score of 0 if the response fails to address the question. Additionally, provide an '
'exemplary answer with a minimum of 250 words, along with a detailed commentary highlighting ' 'exemplary answer with a minimum of 250 words, along with a detailed commentary highlighting '
@@ -356,13 +357,14 @@ def grade_writing_task_2():
] ]
token_count = count_total_tokens(messages) token_count = count_total_tokens(messages)
response = make_openai_call(GPT_4_O, messages, token_count, ["comment"], response = make_openai_call(GPT_4_O, messages, token_count, ["comment"],
GEN_QUESTION_TEMPERATURE) GEN_QUESTION_TEMPERATURE)
response["overall"] = fix_writing_overall(response["overall"], response["task_response"]) response["overall"] = fix_writing_overall(response["overall"], response["task_response"])
response['fixed_text'] = get_fixed_text(answer) response['fixed_text'] = get_fixed_text(answer)
return response return response
except Exception as e: except Exception as e:
return str(e) return str(e)
def fix_writing_overall(overall: float, task_response: dict): def fix_writing_overall(overall: float, task_response: dict):
if overall > max(task_response.values()) or overall < min(task_response.values()): if overall > max(task_response.values()) or overall < min(task_response.values()):
total_sum = sum(task_response.values()) total_sum = sum(task_response.values())
@@ -371,6 +373,7 @@ def fix_writing_overall(overall: float, task_response: dict):
return rounded_average return rounded_average
return overall return overall
@app.route('/writing_task2_general', methods=['GET']) @app.route('/writing_task2_general', methods=['GET'])
@jwt_required() @jwt_required()
def get_writing_task_2_general_question(): def get_writing_task_2_general_question():
@@ -385,7 +388,8 @@ def get_writing_task_2_general_question():
}, },
{ {
"role": "user", "role": "user",
"content": ('Craft a comprehensive question of ' + difficulty + 'difficulty like the ones for IELTS Writing Task 2 General Training that directs the candidate ' "content": (
'Craft a comprehensive question of ' + difficulty + 'difficulty like the ones for IELTS Writing Task 2 General Training that directs the candidate '
'to delve into an in-depth analysis of contrasting perspectives on the topic of "' + topic + '". ' 'to delve into an in-depth analysis of contrasting perspectives on the topic of "' + topic + '". '
'The candidate should be asked to discuss the strengths and weaknesses of both viewpoints, provide evidence or ' 'The candidate should be asked to discuss the strengths and weaknesses of both viewpoints, provide evidence or '
'examples, and present a well-rounded argument before concluding with their personal opinion on the subject.') 'examples, and present a well-rounded argument before concluding with their personal opinion on the subject.')
@@ -427,24 +431,27 @@ def grade_speaking_task_1():
messages = [ messages = [
{ {
"role": "system", "role": "system",
"content": ('You are a helpful assistant designed to output JSON on this format: ' "content": (
'{"comment": "comment about answer quality", "overall": 0.0, ' 'You are a helpful assistant designed to output JSON on this format: '
'"task_response": {"Fluency and Coherence": 0.0, "Lexical Resource": 0.0, ' '{"comment": "comment about answer quality", "overall": 0.0, '
'"Grammatical Range and Accuracy": 0.0, "Pronunciation": 0.0}}') '"task_response": {"Fluency and Coherence": 0.0, "Lexical Resource": 0.0, '
'"Grammatical Range and Accuracy": 0.0, "Pronunciation": 0.0}}')
}, },
{ {
"role": "user", "role": "user",
"content": ('Evaluate the given Speaking Part 1 response based on the IELTS grading system, ensuring a ' "content": (
'strict assessment that penalizes errors. Deduct points for deviations from the task, and ' 'Evaluate the given Speaking Part 1 response based on the IELTS grading system, ensuring a '
'assign a score of 0 if the response fails to address the question. Additionally, provide ' 'strict assessment that penalizes errors. Deduct points for deviations from the task, and '
'detailed commentary highlighting both strengths and weaknesses in the response.' 'assign a score of 0 if the response fails to address the question. Additionally, provide '
'\n Question: "'+ question + '" \n Answer: "'+ answer + '"') 'detailed commentary highlighting both strengths and weaknesses in the response.'
'\n Question: "' + question + '" \n Answer: "' + answer + '"')
} }
] ]
token_count = count_total_tokens(messages) token_count = count_total_tokens(messages)
logging.info("POST - speaking_task_1 - " + str(request_id) + " - Requesting grading of the answer.") logging.info("POST - speaking_task_1 - " + str(request_id) + " - Requesting grading of the answer.")
response = make_openai_call(GPT_3_5_TURBO, messages, token_count,["comment"], response = make_openai_call(GPT_3_5_TURBO, messages, token_count, ["comment"],
GRADING_TEMPERATURE) GRADING_TEMPERATURE)
logging.info("POST - speaking_task_1 - " + str(request_id) + " - Answer graded: " + str(response)) logging.info("POST - speaking_task_1 - " + str(request_id) + " - Answer graded: " + str(response))
perfect_answer_messages = [ perfect_answer_messages = [