AutoGraderPro / grading_logic.py
mohammedriza-rahman's picture
Create grading_logic.py
e54d523 verified
import docx
from ocr_processing import OCRProcessor
class GradingSystem:
def __init__(self, ocr_handler):
self.ocr_handler = ocr_handler
def extract_answers(self, student_images):
responses = []
for image_path in student_images:
try:
encoded_img = self.ocr_handler.encode_image(image_path)
prompt = "Extract the visible text from the provided image and organize it as structured responses."
result = self.ocr_handler.extract_text_from_image(encoded_img, prompt)
responses.append(result.content)
except Exception as err:
responses.append(f"[Error extracting {image_path}: {err}]")
return "\n".join(responses)
def extract_marking_criteria(self, docx_file):
try:
doc = docx.Document(docx_file)
criteria = [para.text.strip() for para in doc.paragraphs if para.text.strip()]
return "\n".join(criteria)
except Exception as err:
raise Exception(f"Failed to extract criteria: {err}")
def grade_answers(self, student_text, marking_criteria):
prompt = f"Compare the student's responses with the marking scheme and assign grades accordingly. Marking Scheme:\n{marking_criteria}\n\nStudent Response:\n{student_text}"
try:
result = self.ocr_handler.extract_text_from_image("", prompt)
return result.content
except Exception as err:
raise Exception(f"Grading failed: {err}")