Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -225,16 +225,25 @@ def extract_question_ids_from_qpms(text):
|
|
| 225 |
|
| 226 |
# Update AS prompt builder to include graph detection
|
| 227 |
|
| 228 |
-
def build_as_prompt_with_expected_ids(expected_ids):
|
| 229 |
"""
|
| 230 |
Construct the AS transcription prompt injecting the expected IDs block and graph detection instructions.
|
|
|
|
| 231 |
"""
|
| 232 |
if not expected_ids:
|
| 233 |
ids_block = "{NA}"
|
| 234 |
else:
|
| 235 |
ids_block = "{\n" + "\n".join(expected_ids) + "\n}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
prompt = f"""You are a high-quality handwritten transcription assistant.
|
| 237 |
-
INPUT: This PDF contains a student's handwritten answer sheet.
|
| 238 |
TASK: Transcribe the student's answers exactly (as text). Preserve step order and line breaks. Attempt to assign each answer to a question ID if the student has labelled it (e.g., "1", "1a", "2(b)", "3"). If the student hasn't labelled answers, segment contiguous answer blocks and attempt to infer question IDs from context — but mark inferred IDs clearly as "INFERRED: <id>"
|
| 239 |
Enclose all mathematical expressions in Markdown fenced code blocks (``` triple backticks).
|
| 240 |
If a diagram/graph is omitted, write [Graph omitted].
|
|
@@ -249,7 +258,7 @@ OUTPUT FORMAT:
|
|
| 249 |
Question <id>
|
| 250 |
AS:
|
| 251 |
<transcribed answer or placeholder>
|
| 252 |
-
==== GRAPH FOUND ANSWERS ====\nGraph found in:\n- Answer <number> → Page <number>\n(one per line)\n==== END GRAPH FOUND ====
|
| 253 |
return prompt
|
| 254 |
|
| 255 |
# Robust parsing functions for graph detection
|
|
@@ -532,7 +541,7 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, imprint=False):
|
|
| 532 |
|
| 533 |
# Step 1.ii: Build AS prompt injecting extracted IDs and transcribe AS
|
| 534 |
print("1.ii) Building AS transcription prompt with expected question IDs and graph detection, sending to Gemini...")
|
| 535 |
-
as_prompt = build_as_prompt_with_expected_ids(extracted_ids) + "\nAt the end, also list all answers where a graph is found, in the format:\nGraph found in:\n- Answer <number> → Page <number>\n(One per line, after all answers)"
|
| 536 |
as_text = gemini_generate_content(model, as_prompt, file_upload_obj=ans_uploaded)
|
| 537 |
print("📝 AS transcription received. Saving debug file: debug_as_transcript.txt")
|
| 538 |
with open("debug_as_transcript.txt", "w", encoding="utf-8") as f:
|
|
|
|
| 225 |
|
| 226 |
# Update AS prompt builder to include graph detection
|
| 227 |
|
| 228 |
+
def build_as_prompt_with_expected_ids(expected_ids, qpms_text=None):
|
| 229 |
"""
|
| 230 |
Construct the AS transcription prompt injecting the expected IDs block and graph detection instructions.
|
| 231 |
+
If qpms_text is provided, instruct the LLM to refer to it for ambiguous handwriting.
|
| 232 |
"""
|
| 233 |
if not expected_ids:
|
| 234 |
ids_block = "{NA}"
|
| 235 |
else:
|
| 236 |
ids_block = "{\n" + "\n".join(expected_ids) + "\n}"
|
| 237 |
+
refer_text = ""
|
| 238 |
+
if qpms_text:
|
| 239 |
+
refer_text = (
|
| 240 |
+
"\nYou are also provided with the full transcript of the Question Paper and Markscheme (QP+MS). "
|
| 241 |
+
"If you encounter ambiguous handwriting (for example, if a number could be '-1.6' or '1.6'), refer to the QP+MS transcript to infer the student's intended answer. "
|
| 242 |
+
"However, if you are confident in your transcription, you may use your own judgment. "
|
| 243 |
+
"Always prioritize accuracy and context from the QP+MS transcript when in doubt.\n"
|
| 244 |
+
)
|
| 245 |
prompt = f"""You are a high-quality handwritten transcription assistant.
|
| 246 |
+
INPUT: This PDF contains a student's handwritten answer sheet.{refer_text}
|
| 247 |
TASK: Transcribe the student's answers exactly (as text). Preserve step order and line breaks. Attempt to assign each answer to a question ID if the student has labelled it (e.g., "1", "1a", "2(b)", "3"). If the student hasn't labelled answers, segment contiguous answer blocks and attempt to infer question IDs from context — but mark inferred IDs clearly as "INFERRED: <id>"
|
| 248 |
Enclose all mathematical expressions in Markdown fenced code blocks (``` triple backticks).
|
| 249 |
If a diagram/graph is omitted, write [Graph omitted].
|
|
|
|
| 258 |
Question <id>
|
| 259 |
AS:
|
| 260 |
<transcribed answer or placeholder>
|
| 261 |
+
==== GRAPH FOUND ANSWERS ====\nGraph found in:\n- Answer <number> → Page <number>\n(one per line)\n==== END GRAPH FOUND ===="""
|
| 262 |
return prompt
|
| 263 |
|
| 264 |
# Robust parsing functions for graph detection
|
|
|
|
| 541 |
|
| 542 |
# Step 1.ii: Build AS prompt injecting extracted IDs and transcribe AS
|
| 543 |
print("1.ii) Building AS transcription prompt with expected question IDs and graph detection, sending to Gemini...")
|
| 544 |
+
as_prompt = build_as_prompt_with_expected_ids(extracted_ids, qpms_text) + "\nAt the end, also list all answers where a graph is found, in the format:\nGraph found in:\n- Answer <number> → Page <number>\n(One per line, after all answers)"
|
| 545 |
as_text = gemini_generate_content(model, as_prompt, file_upload_obj=ans_uploaded)
|
| 546 |
print("📝 AS transcription received. Saving debug file: debug_as_transcript.txt")
|
| 547 |
with open("debug_as_transcript.txt", "w", encoding="utf-8") as f:
|