atz21 commited on
Commit
3e408d2
·
verified ·
1 Parent(s): f4d049d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -35
app.py CHANGED
@@ -534,6 +534,20 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, imprint=False):
534
  ms_graph_mapping = extract_graph_questions_from_ms(qpms_text)
535
  print("🖼️ Graph-expected questions in MS:", ms_graph_mapping)
536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
537
  # Step 2: extract serial numbers (question IDs) using regex from qpms_text
538
  extracted_ids = extract_question_ids_from_qpms(qpms_text)
539
  if not extracted_ids:
@@ -562,32 +576,6 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, imprint=False):
562
  })
563
  print("🔗 Graph bundles for grading:", graph_bundles)
564
 
565
- # --- NEW: Separate and save graph pages as images ---
566
- ms_images = convert_from_path(ms_path, dpi=200)
567
- ans_images = convert_from_path(ans_path, dpi=200)
568
- graph_image_paths = [] # To collect for grading prompt if needed
569
- for bundle in graph_bundles:
570
- q = bundle['question']
571
- ms_page = bundle['ms_page']
572
- as_page = bundle['as_page']
573
- # Markscheme graph page
574
- ms_img_path = f"ms_graph_Q{q}.png"
575
- try:
576
- ms_images[ms_page-1].save(ms_img_path)
577
- print(f"🖼️ Separated Markscheme graph page for Q{q}: source=MS, page={ms_page}, output={ms_img_path}")
578
- graph_image_paths.append((f"ms_graph_Q{q}", ms_img_path))
579
- except Exception as e:
580
- print(f"❌ Failed to save Markscheme graph page for Q{q}: {e}")
581
- # Answer Sheet graph page
582
- as_img_path = f"as_graph_Q{q}.png"
583
- try:
584
- ans_images[as_page-1].save(as_img_path)
585
- print(f"🖼️ Separated Answer Sheet graph page for Q{q}: source=AS, page={as_page}, output={as_img_path}")
586
- graph_image_paths.append((f"as_graph_Q{q}", as_img_path))
587
- except Exception as e:
588
- print(f"❌ Failed to save Answer Sheet graph page for Q{q}: {e}")
589
- # --- END NEW ---
590
-
591
  # Step 4: Grading - send both transcripts to grading model, inject graph bundle info
592
  print("2) Preparing grading input and sending to Gemini for grading...")
593
  grading_input = (
@@ -607,15 +595,7 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, imprint=False):
607
  grading_input += graph_note
608
 
609
  grading_prompt_system = PROMPTS["GRADING_PROMPT"]["content"]
610
- # --- NEW: Attach graph images to grading prompt if graph bundles exist ---
611
- if graph_bundles and graph_image_paths:
612
- print(f"📎 Attaching {len(graph_image_paths)} graph images to grading prompt for visual grading.")
613
- # If gemini_generate_content supports image_obj list, pass it here. Otherwise, just print for now.
614
- # grading_text = gemini_generate_content(model, grading_prompt_system + "\n\nPlease grade the following transcripts:\n" + grading_input, image_obj=[p[1] for p in graph_image_paths])
615
- grading_text = gemini_generate_content(model, grading_prompt_system + "\n\nPlease grade the following transcripts:\n" + grading_input)
616
- else:
617
- grading_text = gemini_generate_content(model, grading_prompt_system + "\n\nPlease grade the following transcripts:\n" + grading_input)
618
- # --- END NEW ---
619
  print("🧾 Grading output received. Saving debug file: debug_grading.md")
620
  with open("debug_grading.md", "w", encoding="utf-8") as f:
621
  f.write(grading_text)
 
534
  ms_graph_mapping = extract_graph_questions_from_ms(qpms_text)
535
  print("🖼️ Graph-expected questions in MS:", ms_graph_mapping)
536
 
537
+ # NEW: Separate out graph-expected pages as images
538
+ if ms_graph_mapping:
539
+ print("📤 Separating graph-expected pages as images...")
540
+ ms_pages = convert_from_path(merged_qpms_path, dpi=200)
541
+ for qnum, page_num in ms_graph_mapping.items():
542
+ # Page numbers in PDF are 1-indexed
543
+ if 1 <= page_num <= len(ms_pages):
544
+ img = ms_pages[page_num-1]
545
+ img_path = f"graph_q{qnum}_p{page_num}.png"
546
+ img.save(img_path)
547
+ print(f"✅ Saved graph image for Question {qnum} (Page {page_num}) as {img_path}")
548
+ else:
549
+ print(f"⚠️ Page {page_num} for Question {qnum} is out of range (PDF has {len(ms_pages)} pages)")
550
+
551
  # Step 2: extract serial numbers (question IDs) using regex from qpms_text
552
  extracted_ids = extract_question_ids_from_qpms(qpms_text)
553
  if not extracted_ids:
 
576
  })
577
  print("🔗 Graph bundles for grading:", graph_bundles)
578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579
  # Step 4: Grading - send both transcripts to grading model, inject graph bundle info
580
  print("2) Preparing grading input and sending to Gemini for grading...")
581
  grading_input = (
 
595
  grading_input += graph_note
596
 
597
  grading_prompt_system = PROMPTS["GRADING_PROMPT"]["content"]
598
+ grading_text = gemini_generate_content(model, grading_prompt_system + "\n\nPlease grade the following transcripts:\n" + grading_input)
 
 
 
 
 
 
 
 
599
  print("🧾 Grading output received. Saving debug file: debug_grading.md")
600
  with open("debug_grading.md", "w", encoding="utf-8") as f:
601
  f.write(grading_text)