Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,8 +14,6 @@ Your objective is to align three sources per question/sub-question:
|
|
| 14 |
- Markscheme (MS)
|
| 15 |
- Student Answer Sheet (AS)
|
| 16 |
|
| 17 |
-
Begin with a concise checklist (3-7 bullets) of what you will do; keep items conceptual, not implementation-level.
|
| 18 |
-
|
| 19 |
## Instructions
|
| 20 |
1. Carefully parse all documents and align content per question and sub-question.
|
| 21 |
2. For each question/sub-question, create a structured Markdown block as follows:
|
|
@@ -45,104 +43,88 @@ After each alignment action, briefly validate that the content for QP, MS, and A
|
|
| 45 |
## Question 1
|
| 46 |
*QP:* Expand (1+x)^3
|
| 47 |
*MS:* M1 for binomial expansion, A1 for coefficients, A1 for final form
|
| 48 |
-
*AS:*
|
| 49 |
-
```
|
| 50 |
-
x^3 + 3x^2 + 3x + 1
|
| 51 |
-
```
|
| 52 |
---
|
| 53 |
-
|
| 54 |
## Output Format
|
| 55 |
-
Generate a single Markdown document. For each (sub-)question, output a structured block exactly in the prescribed format
|
| 56 |
-
|
| 57 |
-
---
|
| 58 |
-
## Question X [and sub-question if applicable, e.g., ### (b)(ii)]
|
| 59 |
-
*QP:* [Exact QP text or [Not found]]
|
| 60 |
-
*MS:* [Relevant MS section or [Not found]]
|
| 61 |
-
*AS:* [Cleaned answer, fenced code for math, [illegible], or [No response] as appropriate]
|
| 62 |
-
---
|
| 63 |
-
|
| 64 |
-
Sequence blocks for main questions and sub-questions according to their order in the source documents.
|
| 65 |
"""
|
| 66 |
},
|
| 67 |
"GRADING_PROMPT": {
|
| 68 |
"role": "system",
|
| 69 |
"content": """Developer: You are an official examiner. Apply the following grading rules precisely.
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
### Abbreviations:
|
| 72 |
- **M**: Marks for demonstrating a correct Method.
|
| 73 |
-
- **A**: Marks for providing an accurate Answer
|
| 74 |
- **R**: Marks for clear Reasoning.
|
| 75 |
- **AG**: Answer is given in the question—no marks awarded.
|
| 76 |
-
- **FT**: Follow Through; award marks when candidates continue with their own previous (possibly incorrect) answers
|
| 77 |
|
| 78 |
---
|
| 79 |
## Grading Instructions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
- Award marks using official annotations (e.g., M1, A2).
|
| 85 |
-
|
| 86 |
-
2. **Method and Answer/Accuracy Marks**
|
| 87 |
-
- Award marks only after verifying all relevant working.
|
| 88 |
-
- Do not award full marks for correct answers alone; check for method marks.
|
| 89 |
-
- Do not award A marks without a valid M mark unless the markscheme allows it.
|
| 90 |
-
- Multiple A marks may be independent unless specified.
|
| 91 |
-
|
| 92 |
-
3. **Implied Marks**
|
| 93 |
-
- Implied M marks can only be given when the method is clearly demonstrated or properly inferred.
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
## Output Format
|
| 120 |
|
| 121 |
-
|
| 122 |
-
-
|
| 123 |
-
- Markscheme for each part-question, detailing available marks (types: M, A, R, etc.) and required steps/answers.
|
| 124 |
|
| 125 |
-
|
| 126 |
-
| Student wrote | Marks Awarded | Reason |
|
| 127 |
-
|---------------|---------------|--------|
|
| 128 |
-
- Each row should match a markable step or point in order, following the markscheme.
|
| 129 |
-
- For blanks, write “(no answer)” and indicate the lost mark(s).
|
| 130 |
-
- If multiple reasonable interpretations exist, select the most logical one and note this in Reason.
|
| 131 |
-
- For multiple full solutions with no preference stated, only mark the first solution.
|
| 132 |
-
- Use any notation allowed by abbreviations (e.g., M1A0, M1A1, A0, etc.) or valid markscheme combos (e.g., M1A1A0).
|
| 133 |
-
|
| 134 |
-
**Special formatting rule:**
|
| 135 |
-
- Any lost mark (M0, A0, R0, etc.): Wrap in red with `<span style="color:red">M0</span>` and make the Reason column red for those marks.
|
| 136 |
-
- Awarded marks (M1, A1, etc.) appear in plain text.
|
| 137 |
-
- For partial awards (e.g., M1A0A1), only highlight lost marks and their reasons.
|
| 138 |
-
|
| 139 |
-
After the table, provide:
|
| 140 |
-
### Summary & Final Mark
|
| 141 |
-
- Show total marks obtained vs. total available.
|
| 142 |
-
- Note any FT (follow-through) used.
|
| 143 |
-
- Classify errors (Conceptual, Silly mistake, Misread, etc.).
|
| 144 |
-
|
| 145 |
-
After completing the grading, provide a brief validation: confirm that all grading rules were followed and that the awarded marks align with the markscheme. If any discrepancies or ambiguities remain, note them and suggest a minimal correction or clarification if needed.
|
| 146 |
"""
|
| 147 |
}
|
| 148 |
}
|
|
@@ -171,17 +153,14 @@ def compress_pdf(input_path, output_path=None, max_size=20*1024*1024):
|
|
| 171 |
gs_cmd = [
|
| 172 |
"gs", "-sDEVICE=pdfwrite",
|
| 173 |
"-dCompatibilityLevel=1.4",
|
| 174 |
-
"-dPDFSETTINGS=/ebook",
|
| 175 |
"-dNOPAUSE", "-dQUIET", "-dBATCH",
|
| 176 |
f"-sOutputFile={output_path}", input_path
|
| 177 |
]
|
| 178 |
subprocess.run(gs_cmd, check=True)
|
| 179 |
-
|
| 180 |
if os.path.getsize(output_path) <= max_size:
|
| 181 |
-
print(f"✅ Compressed {input_path} → {output_path}")
|
| 182 |
return output_path
|
| 183 |
else:
|
| 184 |
-
print(f"⚠️ Compression failed to reduce below {max_size/1024/1024} MB")
|
| 185 |
return input_path
|
| 186 |
except Exception as e:
|
| 187 |
print(f"⚠️ Compression error: {e}")
|
|
@@ -190,10 +169,8 @@ def compress_pdf(input_path, output_path=None, max_size=20*1024*1024):
|
|
| 190 |
# ---------- HELPER: Create Model with Fallback ----------
|
| 191 |
def create_model():
|
| 192 |
try:
|
| 193 |
-
print("⚡ Using gemini-2.5-pro model")
|
| 194 |
return genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 195 |
except Exception:
|
| 196 |
-
print("⚡ Falling back to gemini-2.5-flash model")
|
| 197 |
return genai.GenerativeModel("gemini-2.5-flash", generation_config={"temperature": 0})
|
| 198 |
|
| 199 |
# ---------- PIPELINE: ALIGN + GRADE ----------
|
|
@@ -270,3 +247,4 @@ with gr.Blocks(title="LeadIB AI Grading (Alignment + Auto-Grading)") as demo:
|
|
| 270 |
|
| 271 |
if __name__ == "__main__":
|
| 272 |
demo.launch()
|
|
|
|
|
|
| 14 |
- Markscheme (MS)
|
| 15 |
- Student Answer Sheet (AS)
|
| 16 |
|
|
|
|
|
|
|
| 17 |
## Instructions
|
| 18 |
1. Carefully parse all documents and align content per question and sub-question.
|
| 19 |
2. For each question/sub-question, create a structured Markdown block as follows:
|
|
|
|
| 43 |
## Question 1
|
| 44 |
*QP:* Expand (1+x)^3
|
| 45 |
*MS:* M1 for binomial expansion, A1 for coefficients, A1 for final form
|
| 46 |
+
*AS:* x^3 + 3x^2 + 3x + 1
|
|
|
|
|
|
|
|
|
|
| 47 |
---
|
|
|
|
| 48 |
## Output Format
|
| 49 |
+
Generate a single Markdown document. For each (sub-)question, output a structured block exactly in the prescribed format.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
"""
|
| 51 |
},
|
| 52 |
"GRADING_PROMPT": {
|
| 53 |
"role": "system",
|
| 54 |
"content": """Developer: You are an official examiner. Apply the following grading rules precisely.
|
| 55 |
|
| 56 |
+
## Grading Checklist
|
| 57 |
+
- Assess each question part against the provided markscheme.
|
| 58 |
+
- Award marks for correct methods (M), accurate answers (A), and clear reasoning (R) as specified.
|
| 59 |
+
- Use Follow Through (FT) for correctly applied subsequent working that uses a previously incorrect answer.
|
| 60 |
+
- Clearly indicate any lost marks and provide a concise reason for the deduction.
|
| 61 |
+
- Check for alternative valid methods and award marks accordingly.
|
| 62 |
+
- Summarize the total marks and classify the types of errors made by the student.
|
| 63 |
+
- Ensure the final output adheres to the specified Markdown table format.
|
| 64 |
+
|
| 65 |
### Abbreviations:
|
| 66 |
- **M**: Marks for demonstrating a correct Method.
|
| 67 |
+
- **A**: Marks for providing an accurate Answer.
|
| 68 |
- **R**: Marks for clear Reasoning.
|
| 69 |
- **AG**: Answer is given in the question—no marks awarded.
|
| 70 |
+
- **FT**: Follow Through; award marks when candidates continue with their own previous (possibly incorrect) answers.
|
| 71 |
|
| 72 |
---
|
| 73 |
## Grading Instructions
|
| 74 |
+
1. Award marks using official annotations (M1, A1, etc.).
|
| 75 |
+
2. A marks generally require valid M marks.
|
| 76 |
+
3. Allow FT unless results are nonsensical.
|
| 77 |
+
4. Accept valid alternative forms.
|
| 78 |
+
5. Apply accuracy requirements.
|
| 79 |
+
6. Ignore crossed-out work unless requested otherwise.
|
| 80 |
+
7. Mark only the first full solution unless otherwise indicated.
|
| 81 |
+
8. Assume graphs/diagrams are correct if required.
|
| 82 |
|
| 83 |
+
---
|
| 84 |
+
## Output Format
|
| 85 |
+
Produce a GitHub-flavored Markdown table:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
| Student wrote | Marks Awarded | Reason |
|
| 88 |
+
|---------------|---------------|--------|
|
| 89 |
|
| 90 |
+
Rules:
|
| 91 |
+
- Each row matches a markable step.
|
| 92 |
+
- For blanks, write “(no answer)” and indicate the lost mark(s).
|
| 93 |
+
- Lost marks: wrap in red with `<span style="color:red">A0</span>` (or M0, R0) and make Reason column red.
|
| 94 |
+
- Awarded marks remain plain text.
|
| 95 |
+
- For partial awards (e.g., M1A0A1), only highlight lost marks.
|
| 96 |
|
| 97 |
+
**New Rule (Per-Question Total):**
|
| 98 |
+
- After each question (including all subparts), show marks obtained vs total in square brackets, e.g.:
|
| 99 |
+
`[2/4]`
|
| 100 |
|
| 101 |
+
---
|
| 102 |
+
### Examiner’s Report
|
| 103 |
+
At the end of the grading, provide a summary report in this format:
|
| 104 |
|
| 105 |
+
Use codes:
|
| 106 |
+
- A : All Good
|
| 107 |
+
- B : Silly Mistake
|
| 108 |
+
- C : Conceptual Error
|
| 109 |
+
- D : Hard Question
|
| 110 |
+
- E : Not Applicable
|
| 111 |
|
| 112 |
+
| Question Number | Marks | Remark |
|
| 113 |
+
|-----------------|-------|--------|
|
| 114 |
+
| 1 | 6/9 | C |
|
| 115 |
+
| 2 | 7/7 | A |
|
| 116 |
+
| 3 | 8/14 | D |
|
| 117 |
+
| ... | ... | ... |
|
| 118 |
|
| 119 |
+
At the end, display the grand total like:
|
| 120 |
+
`Total: 40/61`
|
| 121 |
|
| 122 |
+
Optionally, if reasons are available, extend with:
|
|
|
|
| 123 |
|
| 124 |
+
| Question Number | Marks | Remark | Reason |
|
| 125 |
+
|-----------------|-------|--------|--------|
|
|
|
|
| 126 |
|
| 127 |
+
⚠️ Do NOT add any "Validation" or meta commentary. End the output after the Examiner’s Report.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
"""
|
| 129 |
}
|
| 130 |
}
|
|
|
|
| 153 |
gs_cmd = [
|
| 154 |
"gs", "-sDEVICE=pdfwrite",
|
| 155 |
"-dCompatibilityLevel=1.4",
|
| 156 |
+
"-dPDFSETTINGS=/ebook",
|
| 157 |
"-dNOPAUSE", "-dQUIET", "-dBATCH",
|
| 158 |
f"-sOutputFile={output_path}", input_path
|
| 159 |
]
|
| 160 |
subprocess.run(gs_cmd, check=True)
|
|
|
|
| 161 |
if os.path.getsize(output_path) <= max_size:
|
|
|
|
| 162 |
return output_path
|
| 163 |
else:
|
|
|
|
| 164 |
return input_path
|
| 165 |
except Exception as e:
|
| 166 |
print(f"⚠️ Compression error: {e}")
|
|
|
|
| 169 |
# ---------- HELPER: Create Model with Fallback ----------
|
| 170 |
def create_model():
|
| 171 |
try:
|
|
|
|
| 172 |
return genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 173 |
except Exception:
|
|
|
|
| 174 |
return genai.GenerativeModel("gemini-2.5-flash", generation_config={"temperature": 0})
|
| 175 |
|
| 176 |
# ---------- PIPELINE: ALIGN + GRADE ----------
|
|
|
|
| 247 |
|
| 248 |
if __name__ == "__main__":
|
| 249 |
demo.launch()
|
| 250 |
+
|