Spaces:
Sleeping
Sleeping
Create grading_logic.py
Browse files- grading_logic.py +35 -0
grading_logic.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import docx
|
| 2 |
+
from ocr_processing import OCRProcessor
|
| 3 |
+
|
| 4 |
+
class GradingSystem:
|
| 5 |
+
def __init__(self, ocr_handler):
|
| 6 |
+
self.ocr_handler = ocr_handler
|
| 7 |
+
|
| 8 |
+
def extract_answers(self, student_images):
|
| 9 |
+
responses = []
|
| 10 |
+
for image_path in student_images:
|
| 11 |
+
try:
|
| 12 |
+
encoded_img = self.ocr_handler.encode_image(image_path)
|
| 13 |
+
prompt = "Extract the visible text from the provided image and organize it as structured responses."
|
| 14 |
+
result = self.ocr_handler.extract_text_from_image(encoded_img, prompt)
|
| 15 |
+
responses.append(result.content)
|
| 16 |
+
except Exception as err:
|
| 17 |
+
responses.append(f"[Error extracting {image_path}: {err}]")
|
| 18 |
+
return "\n".join(responses)
|
| 19 |
+
|
| 20 |
+
def extract_marking_criteria(self, docx_file):
|
| 21 |
+
try:
|
| 22 |
+
doc = docx.Document(docx_file)
|
| 23 |
+
criteria = [para.text.strip() for para in doc.paragraphs if para.text.strip()]
|
| 24 |
+
return "\n".join(criteria)
|
| 25 |
+
except Exception as err:
|
| 26 |
+
raise Exception(f"Failed to extract criteria: {err}")
|
| 27 |
+
|
| 28 |
+
def grade_answers(self, student_text, marking_criteria):
|
| 29 |
+
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}"
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
result = self.ocr_handler.extract_text_from_image("", prompt)
|
| 33 |
+
return result.content
|
| 34 |
+
except Exception as err:
|
| 35 |
+
raise Exception(f"Grading failed: {err}")
|