Update app.py
Browse files
app.py
CHANGED
|
@@ -34,7 +34,6 @@ Error Avoidance:
|
|
| 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,44 +42,67 @@ def save_as_pdf(text, filename="output.pdf"):
|
|
| 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(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="LeadIB AI Grading") as demo:
|
| 86 |
gr.Markdown("## LeadIB AI Grading\nUpload exam documents to transcribe and grade student answers step by step.")
|
|
|
|
| 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 |
# ---------- HELPER: Save to PDF ----------
|
| 38 |
def save_as_pdf(text, filename="output.pdf"):
|
| 39 |
styles = getSampleStyleSheet()
|
|
|
|
| 42 |
doc.build(story)
|
| 43 |
return filename
|
| 44 |
|
| 45 |
+
# ---------- HELPER: Safe Generate ----------
|
| 46 |
+
def safe_generate(model, inputs):
|
| 47 |
+
resp = model.generate_content(inputs)
|
| 48 |
+
cand = resp.candidates[0] if resp.candidates else None
|
| 49 |
+
|
| 50 |
+
if not cand or not cand.content or not cand.content.parts:
|
| 51 |
+
reason = getattr(cand, "finish_reason", "None")
|
| 52 |
+
ratings = getattr(cand, "safety_ratings", "None")
|
| 53 |
+
return None, f"❌ Empty/blocked response. finish_reason={reason}, safety_ratings={ratings}"
|
| 54 |
+
|
| 55 |
+
return resp.text, None
|
| 56 |
|
| 57 |
# ---------- STEP 1: TRANSCRIPTION ----------
|
| 58 |
def transcribe(ans_file):
|
| 59 |
try:
|
| 60 |
ans_uploaded = genai.upload_file(path=ans_file, display_name="Answer Sheet")
|
| 61 |
+
model = genai.GenerativeModel(
|
| 62 |
+
"gemini-2.5-pro",
|
| 63 |
+
generation_config={"temperature": 0},
|
| 64 |
+
safety_settings={
|
| 65 |
+
genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
|
| 66 |
+
genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
|
| 67 |
+
genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
|
| 68 |
+
genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
|
| 69 |
+
}
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
transcription, error = safe_generate(model, [TRANSCRIPTION_PROMPT, ans_uploaded])
|
| 73 |
+
if error:
|
| 74 |
+
return error, None
|
| 75 |
|
| 76 |
pdf_path = save_as_pdf(transcription, "transcription.pdf")
|
| 77 |
return transcription, pdf_path
|
| 78 |
except Exception as e:
|
| 79 |
return f"❌ Error during transcription: {e}", None
|
| 80 |
|
|
|
|
| 81 |
# ---------- STEP 2: GRADING ----------
|
| 82 |
def grade(qp_file, ms_file, transcription):
|
| 83 |
try:
|
| 84 |
qp_uploaded = genai.upload_file(path=qp_file, display_name="Question Paper")
|
| 85 |
ms_uploaded = genai.upload_file(path=ms_file, display_name="Marking Scheme")
|
| 86 |
+
model = genai.GenerativeModel(
|
| 87 |
+
"gemini-2.5-pro",
|
| 88 |
+
generation_config={"temperature": 0},
|
| 89 |
+
safety_settings={
|
| 90 |
+
genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
|
| 91 |
+
genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
|
| 92 |
+
genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
|
| 93 |
+
genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
|
| 94 |
+
}
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
grading, error = safe_generate(model, [GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
|
| 98 |
+
if error:
|
| 99 |
+
return error, None
|
| 100 |
|
| 101 |
pdf_path = save_as_pdf(grading, "grading.pdf")
|
| 102 |
return grading, pdf_path
|
| 103 |
except Exception as e:
|
| 104 |
return f"❌ Error during grading: {e}", None
|
| 105 |
|
|
|
|
| 106 |
# ---------- GRADIO APP ----------
|
| 107 |
with gr.Blocks(title="LeadIB AI Grading") as demo:
|
| 108 |
gr.Markdown("## LeadIB AI Grading\nUpload exam documents to transcribe and grade student answers step by step.")
|