Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,16 +5,13 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph
|
|
| 5 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 6 |
from reportlab.lib.pagesizes import A4
|
| 7 |
|
| 8 |
-
#
|
| 9 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 10 |
|
| 11 |
# ---------- PROMPTS ----------
|
| 12 |
TRANSCRIPTION_PROMPT = """ Transcribe the PDF into a clean, question-wise format. While doing so, ensure the following:
|
| 13 |
-
|
| 14 |
Exclude any text that has been cut, striked, or crossed out (since these may represent student mistakes).
|
| 15 |
-
|
| 16 |
Clearly distinguish between a "step cut" and when variables cancel out during a step.
|
| 17 |
-
|
| 18 |
Present the transcription neatly without including unnecessary markings. """
|
| 19 |
GRADING_PROMPT = """ Instructions to Examiners:
|
| 20 |
Abbreviations:
|
|
@@ -23,7 +20,6 @@ Abbreviations:
|
|
| 23 |
- R: Marks for clear Reasoning.
|
| 24 |
- AG: Answer given in the question; no marks awarded.
|
| 25 |
- FT: Follow Through; award marks for correct method/answer using incorrect earlier results.
|
| 26 |
-
|
| 27 |
Marking Rules:
|
| 28 |
1. Always follow the markscheme annotations (M1, A2, etc.).
|
| 29 |
2. M marks must be earned before dependent A marks are awarded (no M0 followed by A1 unless explicitly allowed).
|
|
@@ -33,12 +29,12 @@ Marking Rules:
|
|
| 33 |
6. "Show that" responses do not need to restate the AG line unless noted.
|
| 34 |
7. Once a correct answer is seen, ignore further incorrect working unless it affects a later part (then apply FT as appropriate).
|
| 35 |
8. Do not award the final A mark if an incorrect approximation is used in the same part.
|
| 36 |
-
|
| 37 |
Error Avoidance:
|
| 38 |
- **No incorrect mark allocation:** Do not award marks unless they are explicitly justified by the markscheme.
|
| 39 |
- **No misclassification of errors:** Distinguish correctly between "Conceptual Errors" and "Silly Mistakes."
|
| 40 |
- **Follow markscheme logic exactly:** Especially regarding when to withhold accuracy marks if method marks are not earned."""
|
| 41 |
|
|
|
|
| 42 |
# ---------- HELPER: Save to PDF ----------
|
| 43 |
def save_as_pdf(text, filename="output.pdf"):
|
| 44 |
styles = getSampleStyleSheet()
|
|
@@ -47,35 +43,44 @@ def save_as_pdf(text, filename="output.pdf"):
|
|
| 47 |
doc.build(story)
|
| 48 |
return filename
|
| 49 |
|
|
|
|
| 50 |
# ---------- STEP 1: TRANSCRIPTION ----------
|
| 51 |
def transcribe(ans_file):
|
| 52 |
try:
|
| 53 |
-
ans_uploaded = genai.upload_file(path=ans_file
|
| 54 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 55 |
|
| 56 |
resp = model.generate_content([TRANSCRIPTION_PROMPT, ans_uploaded])
|
| 57 |
|
| 58 |
-
transcription = getattr(resp, "text", None)
|
|
|
|
|
|
|
|
|
|
| 59 |
pdf_path = save_as_pdf(transcription, "transcription.pdf")
|
| 60 |
return transcription, pdf_path
|
| 61 |
except Exception as e:
|
| 62 |
return f"β Error during transcription: {e}", None
|
| 63 |
|
|
|
|
| 64 |
# ---------- STEP 2: GRADING ----------
|
| 65 |
def grade(qp_file, ms_file, transcription):
|
| 66 |
try:
|
| 67 |
-
qp_uploaded = genai.upload_file(path=qp_file
|
| 68 |
-
ms_uploaded = genai.upload_file(path=ms_file
|
| 69 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 70 |
|
| 71 |
resp = model.generate_content([GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
|
| 72 |
|
| 73 |
-
grading = getattr(resp, "text", None)
|
|
|
|
|
|
|
|
|
|
| 74 |
pdf_path = save_as_pdf(grading, "grading.pdf")
|
| 75 |
return grading, pdf_path
|
| 76 |
except Exception as e:
|
| 77 |
return f"β Error during grading: {e}", None
|
| 78 |
|
|
|
|
| 79 |
# ---------- GRADIO APP ----------
|
| 80 |
with gr.Blocks(title="π AI Teacher Assistant") as demo:
|
| 81 |
gr.Markdown("## π AI Teacher Assistant\nUpload exam documents to transcribe and grade student answers step by step.")
|
|
@@ -88,7 +93,7 @@ with gr.Blocks(title="π AI Teacher Assistant") as demo:
|
|
| 88 |
# Step 1: Transcription
|
| 89 |
transcribe_btn = gr.Button("Step 1: Transcribe Answer Sheet")
|
| 90 |
with gr.Row():
|
| 91 |
-
transcription_out = gr.
|
| 92 |
transcription_pdf = gr.File(label="β¬οΈ Download Transcription (PDF)")
|
| 93 |
|
| 94 |
# Step 2: Grading
|
|
@@ -97,7 +102,7 @@ with gr.Blocks(title="π AI Teacher Assistant") as demo:
|
|
| 97 |
grading_out = gr.Textbox(label="β
Grading Report (Step-by-Step)", lines=20)
|
| 98 |
grading_pdf = gr.File(label="β¬οΈ Download Grading (PDF)")
|
| 99 |
|
| 100 |
-
# Button Logic
|
| 101 |
transcribe_btn.click(
|
| 102 |
fn=transcribe,
|
| 103 |
inputs=[ans_file],
|
|
|
|
| 5 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 6 |
from reportlab.lib.pagesizes import A4
|
| 7 |
|
| 8 |
+
# -------------------- CONFIG --------------------
|
| 9 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 10 |
|
| 11 |
# ---------- PROMPTS ----------
|
| 12 |
TRANSCRIPTION_PROMPT = """ Transcribe the PDF into a clean, question-wise format. While doing so, ensure the following:
|
|
|
|
| 13 |
Exclude any text that has been cut, striked, or crossed out (since these may represent student mistakes).
|
|
|
|
| 14 |
Clearly distinguish between a "step cut" and when variables cancel out during a step.
|
|
|
|
| 15 |
Present the transcription neatly without including unnecessary markings. """
|
| 16 |
GRADING_PROMPT = """ Instructions to Examiners:
|
| 17 |
Abbreviations:
|
|
|
|
| 20 |
- R: Marks for clear Reasoning.
|
| 21 |
- AG: Answer given in the question; no marks awarded.
|
| 22 |
- FT: Follow Through; award marks for correct method/answer using incorrect earlier results.
|
|
|
|
| 23 |
Marking Rules:
|
| 24 |
1. Always follow the markscheme annotations (M1, A2, etc.).
|
| 25 |
2. M marks must be earned before dependent A marks are awarded (no M0 followed by A1 unless explicitly allowed).
|
|
|
|
| 29 |
6. "Show that" responses do not need to restate the AG line unless noted.
|
| 30 |
7. Once a correct answer is seen, ignore further incorrect working unless it affects a later part (then apply FT as appropriate).
|
| 31 |
8. Do not award the final A mark if an incorrect approximation is used in the same part.
|
|
|
|
| 32 |
Error Avoidance:
|
| 33 |
- **No incorrect mark allocation:** Do not award marks unless they are explicitly justified by the markscheme.
|
| 34 |
- **No misclassification of errors:** Distinguish correctly between "Conceptual Errors" and "Silly Mistakes."
|
| 35 |
- **Follow markscheme logic exactly:** Especially regarding when to withhold accuracy marks if method marks are not earned."""
|
| 36 |
|
| 37 |
+
|
| 38 |
# ---------- HELPER: Save to PDF ----------
|
| 39 |
def save_as_pdf(text, filename="output.pdf"):
|
| 40 |
styles = getSampleStyleSheet()
|
|
|
|
| 43 |
doc.build(story)
|
| 44 |
return filename
|
| 45 |
|
| 46 |
+
|
| 47 |
# ---------- STEP 1: TRANSCRIPTION ----------
|
| 48 |
def transcribe(ans_file):
|
| 49 |
try:
|
| 50 |
+
ans_uploaded = genai.upload_file(path=ans_file, display_name="Answer Sheet")
|
| 51 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 52 |
|
| 53 |
resp = model.generate_content([TRANSCRIPTION_PROMPT, ans_uploaded])
|
| 54 |
|
| 55 |
+
transcription = getattr(resp, "text", None)
|
| 56 |
+
if not transcription and resp.candidates:
|
| 57 |
+
transcription = resp.candidates[0].content.parts[0].text
|
| 58 |
+
|
| 59 |
pdf_path = save_as_pdf(transcription, "transcription.pdf")
|
| 60 |
return transcription, pdf_path
|
| 61 |
except Exception as e:
|
| 62 |
return f"β Error during transcription: {e}", None
|
| 63 |
|
| 64 |
+
|
| 65 |
# ---------- STEP 2: GRADING ----------
|
| 66 |
def grade(qp_file, ms_file, transcription):
|
| 67 |
try:
|
| 68 |
+
qp_uploaded = genai.upload_file(path=qp_file, display_name="Question Paper")
|
| 69 |
+
ms_uploaded = genai.upload_file(path=ms_file, display_name="Marking Scheme")
|
| 70 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 71 |
|
| 72 |
resp = model.generate_content([GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
|
| 73 |
|
| 74 |
+
grading = getattr(resp, "text", None)
|
| 75 |
+
if not grading and resp.candidates:
|
| 76 |
+
grading = resp.candidates[0].content.parts[0].text
|
| 77 |
+
|
| 78 |
pdf_path = save_as_pdf(grading, "grading.pdf")
|
| 79 |
return grading, pdf_path
|
| 80 |
except Exception as e:
|
| 81 |
return f"β Error during grading: {e}", None
|
| 82 |
|
| 83 |
+
|
| 84 |
# ---------- GRADIO APP ----------
|
| 85 |
with gr.Blocks(title="π AI Teacher Assistant") as demo:
|
| 86 |
gr.Markdown("## π AI Teacher Assistant\nUpload exam documents to transcribe and grade student answers step by step.")
|
|
|
|
| 93 |
# Step 1: Transcription
|
| 94 |
transcribe_btn = gr.Button("Step 1: Transcribe Answer Sheet")
|
| 95 |
with gr.Row():
|
| 96 |
+
transcription_out = gr.Textbox(label="π Student Transcription", lines=20)
|
| 97 |
transcription_pdf = gr.File(label="β¬οΈ Download Transcription (PDF)")
|
| 98 |
|
| 99 |
# Step 2: Grading
|
|
|
|
| 102 |
grading_out = gr.Textbox(label="β
Grading Report (Step-by-Step)", lines=20)
|
| 103 |
grading_pdf = gr.File(label="β¬οΈ Download Grading (PDF)")
|
| 104 |
|
| 105 |
+
# Button Logic
|
| 106 |
transcribe_btn.click(
|
| 107 |
fn=transcribe,
|
| 108 |
inputs=[ans_file],
|