I want to organize the demerits returned by GPT into three parts:
1、 “problem_type”: “Fill-in-the-Blank”,
2、 “printed_problem_statement”:
3、“handwritten_student_answers”
But the sorting could not be completed. Always error json format.
Can anyone tell me where I’m going wrong?
response1 = client_gpt4o.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": """
Extract the content from the image, but only focus on the content inside the blue and red boxes.
The blue box contains the printed problem statement, and the red box contains the handwritten student answers.
Additionally, determine the problem type:
1. Multiple Choice
2. True/False
3. Fill-in-the-Blank
4. Short Answer
Please output the content in the following JSON format:
{
"problem_type": "Problem type here",
"printed_problem_statement": "Printed problem statement here",
"handwritten_student_answers": "Handwritten student answers here"
}
"""
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{problem_statement_image_path_base64_image}",
"detail": "high"
}
}
]
}
],
max_tokens=300,
temperature=0.01,
top_p=0.01,
seed=123654789
)
logger.info("Extracted Content:\n" + extracted_content)
try:
content_dict = json.loads(extracted_content)
problem_type = content_dict.get("problem_type", "").strip()
printed_problem = content_dict.get("printed_problem_statement", "").strip()
handwritten_answers = content_dict.get("handwritten_student_answers", "").strip()
except json.JSONDecodeError as e:
logger.error("Error parsing JSON content: " + str(e))
problem_type = "Unknown"
printed_problem = extracted_content
handwritten_answers = ""
logger.info("Problem Type:\n" + problem_type)
logger.info("Printed Problem:\n" + printed_problem)
logger.info("Handwritten Answers:\n" + handwritten_answers)
APT Return results
INFO:__main__:Extracted Content:
```json
{
"problem_type": "Fill-in-the-Blank",
"printed_problem_statement": "The perimeter of a parallelogram is 10 centimeters, and the lengths of the two adjacent sides are x centimeters and y centimeters respectively. Then the analytical formula of y as a function of x is ________, and the domain of this function is ________.",
"handwritten_student_answers": "y = 5 - x, 0 < x <= 5"
}
ERROR:__main__:Error parsing JSON content: Expecting value: line 1 column 1 (char 0)
INFO:__main__:Problem Type:
Unknown
INFO:__main__:Printed Problem:
```json
{
"problem_type": "Fill-in-the-Blank",
"printed_problem_statement": "The perimeter of a parallelogram is 10 centimeters, and the lengths of the two adjacent sides are x centimeters and y centimeters respectively. Then the analytical formula of y as a function of x is ________, and the domain of this function is ________.",
"handwritten_student_answers": "y = 5 - x, 0 < x <= 5"
}
INFO:__main__:Handwritten Answers: