Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -672,6 +672,25 @@ Grading JSON:
|
|
| 672 |
print(f"❌ Failed to parse Gemini JSON mapping: {e}")
|
| 673 |
return []
|
| 674 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 675 |
def imprint_marks_using_mapping(pdf_path, grading_json, output_pdf, expected_ids=None, rows=GRID_ROWS, cols=GRID_COLS):
|
| 676 |
"""
|
| 677 |
Convert PDF to images, create grid-numbered images for batch sending to Gemini,
|
|
@@ -737,12 +756,27 @@ def imprint_marks_using_mapping(pdf_path, grading_json, output_pdf, expected_ids
|
|
| 737 |
if qid is None or cell_number is None:
|
| 738 |
continue
|
| 739 |
|
| 740 |
-
|
| 741 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 742 |
marks_list = next((g["marks_awarded"] for g in grading_json.get("grading", [])
|
| 743 |
-
if g["question"].lower() ==
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 744 |
|
| 745 |
-
marks_text = ",".join(marks_list) if marks_list else "?"
|
|
|
|
|
|
|
|
|
|
| 746 |
|
| 747 |
row = (cell_number - 1) // cols
|
| 748 |
col = (cell_number - 1) % cols
|
|
|
|
| 672 |
print(f"❌ Failed to parse Gemini JSON mapping: {e}")
|
| 673 |
return []
|
| 674 |
|
| 675 |
+
def normalize_question_id(qid):
|
| 676 |
+
"""
|
| 677 |
+
Normalize question ID to a standard format for matching.
|
| 678 |
+
Converts formats like:
|
| 679 |
+
- "1(a)" -> "1.a"
|
| 680 |
+
- "2(c).i" -> "2.c.i"
|
| 681 |
+
- "3.d.ii" -> "3.d.ii" (already normalized)
|
| 682 |
+
"""
|
| 683 |
+
if not qid:
|
| 684 |
+
return qid
|
| 685 |
+
|
| 686 |
+
# Replace parentheses format: 1(a) -> 1.a
|
| 687 |
+
qid = re.sub(r'(\d+)\(([a-zA-Z])\)', r'\1.\2', qid)
|
| 688 |
+
|
| 689 |
+
# Replace format like 2(c).i -> 2.c.i
|
| 690 |
+
qid = re.sub(r'(\d+)\(([a-zA-Z]+)\)\.', r'\1.\2.', qid)
|
| 691 |
+
|
| 692 |
+
return qid
|
| 693 |
+
|
| 694 |
def imprint_marks_using_mapping(pdf_path, grading_json, output_pdf, expected_ids=None, rows=GRID_ROWS, cols=GRID_COLS):
|
| 695 |
"""
|
| 696 |
Convert PDF to images, create grid-numbered images for batch sending to Gemini,
|
|
|
|
| 756 |
if qid is None or cell_number is None:
|
| 757 |
continue
|
| 758 |
|
| 759 |
+
# Normalize the question ID from Gemini mapping
|
| 760 |
+
normalized_qid = normalize_question_id(qid)
|
| 761 |
+
|
| 762 |
+
# Try exact match first with normalized ID
|
| 763 |
+
marks_list = next((g["marks_awarded"] for g in grading_json.get("grading", [])
|
| 764 |
+
if g["question"] == normalized_qid), [])
|
| 765 |
+
|
| 766 |
+
# If no match, try case-insensitive match
|
| 767 |
+
if not marks_list:
|
| 768 |
marks_list = next((g["marks_awarded"] for g in grading_json.get("grading", [])
|
| 769 |
+
if g["question"].lower() == normalized_qid.lower()), [])
|
| 770 |
+
|
| 771 |
+
# If still no match, try with original qid
|
| 772 |
+
if not marks_list:
|
| 773 |
+
marks_list = next((g["marks_awarded"] for g in grading_json.get("grading", [])
|
| 774 |
+
if g["question"] == qid), [])
|
| 775 |
|
| 776 |
+
marks_text = ",".join(marks_list) if marks_list else "?"
|
| 777 |
+
|
| 778 |
+
if marks_text == "?":
|
| 779 |
+
print(f"⚠️ No marks found for question '{qid}' (normalized: '{normalized_qid}') on page {page_num}")
|
| 780 |
|
| 781 |
row = (cell_number - 1) // cols
|
| 782 |
col = (cell_number - 1) % cols
|