Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,77 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
-
# π
|
| 6 |
-
GEMINI_API_KEY =
|
| 7 |
-
if not GEMINI_API_KEY:
|
| 8 |
-
raise ValueError("β GEMINI_API_KEY not found. Please set it in Hugging Face Space β Settings β Secrets.")
|
| 9 |
-
|
| 10 |
-
# Configure Gemini
|
| 11 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 12 |
|
| 13 |
# Initialize model
|
| 14 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 15 |
|
| 16 |
-
|
| 17 |
# ---------- STEP 1: TRANSCRIPTION ----------
|
| 18 |
def transcribe_files(qp_file, ms_file, ans_file):
|
| 19 |
-
|
| 20 |
-
return "β οΈ Please upload an Answer Sheet."
|
| 21 |
-
|
| 22 |
uploaded_as = genai.upload_file(path=ans_file.name, display_name="Answer Sheet")
|
| 23 |
|
| 24 |
transcription_instructions = """
|
| 25 |
Persona:
|
| 26 |
You are an expert transcriptionist specializing in scientific and mathematical documents.
|
| 27 |
-
Your task is to transcribe the provided handwritten student solutions into Markdown
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
-
|
| 31 |
-
- Do NOT correct mistakes. Just transcribe faithfully.
|
| 32 |
- Ignore strikethroughs.
|
| 33 |
-
- Use **bold** for question numbering
|
| 34 |
-
- Preserve step-by-step
|
| 35 |
-
- If a symbol is illegible, mark it as [unclear].
|
| 36 |
"""
|
| 37 |
|
| 38 |
response = model.generate_content([transcription_instructions, uploaded_as])
|
| 39 |
-
|
| 40 |
-
# Direct extraction (like in your notebook)
|
| 41 |
transcription = getattr(response, "text", None)
|
| 42 |
-
if not transcription:
|
| 43 |
transcription = response.candidates[0].content.parts[0].text
|
| 44 |
-
|
| 45 |
-
return transcription
|
| 46 |
-
|
| 47 |
|
| 48 |
# ---------- STEP 2: GRADING ----------
|
| 49 |
def grade_files(qp_file, ms_file, ans_file, transcription):
|
| 50 |
-
|
| 51 |
-
return "β οΈ Please transcribe first before grading."
|
| 52 |
-
|
| 53 |
-
if qp_file is None or ms_file is None:
|
| 54 |
-
return "β οΈ Please upload Question Paper and Marking Scheme."
|
| 55 |
-
|
| 56 |
uploaded_qp = genai.upload_file(path=qp_file.name, display_name="Question Paper")
|
| 57 |
uploaded_ms = genai.upload_file(path=ms_file.name, display_name="Marking Scheme")
|
| 58 |
|
| 59 |
grading_system = """
|
| 60 |
Instructions to Examiners:
|
| 61 |
-
- M: Method marks
|
| 62 |
-
- A: Accuracy marks
|
| 63 |
-
- FT: Follow-through rules
|
| 64 |
-
-
|
| 65 |
-
- Identify errors as:
|
| 66 |
-
* Conceptual Error
|
| 67 |
-
* Silly Mistake
|
| 68 |
-
* None
|
| 69 |
-
|
| 70 |
-
Output:
|
| 71 |
-
- For each part: show awarded marks + justification.
|
| 72 |
-
- Explain reasoning step by step.
|
| 73 |
-
- Conclude with the total score.
|
| 74 |
"""
|
| 75 |
|
| 76 |
response = model.generate_content([
|
| 77 |
-
f"You are an
|
| 78 |
uploaded_qp,
|
| 79 |
uploaded_ms,
|
| 80 |
transcription
|
| 81 |
])
|
| 82 |
-
|
| 83 |
-
# Direct extraction (like in your notebook)
|
| 84 |
grading = getattr(response, "text", None)
|
| 85 |
-
if not grading:
|
| 86 |
grading = response.candidates[0].content.parts[0].text
|
| 87 |
-
|
| 88 |
-
return grading
|
| 89 |
-
|
| 90 |
|
| 91 |
# ---------- GRADIO UI ----------
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
gr.Markdown("## π Automated Transcription & Grading System")
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
-
qp = gr.File(label="
|
| 97 |
-
ms = gr.File(label="
|
| 98 |
-
ans = gr.File(label="
|
| 99 |
|
| 100 |
transcribe_btn = gr.Button("π Transcribe Answer Sheet")
|
| 101 |
-
transcription_output = gr.Textbox(label="
|
| 102 |
|
| 103 |
grade_btn = gr.Button("β
Grade Answers")
|
| 104 |
-
grading_output = gr.Textbox(label="
|
| 105 |
|
| 106 |
transcribe_btn.click(fn=transcribe_files, inputs=[qp, ms, ans], outputs=transcription_output)
|
| 107 |
grade_btn.click(fn=grade_files, inputs=[qp, ms, ans, transcription_output], outputs=grading_output)
|
|
|
|
| 1 |
+
!pip install gradio google-generativeai
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
| 6 |
+
# π Configure Gemini
|
| 7 |
+
GEMINI_API_KEY = "YOUR_API_KEY_HERE"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 9 |
|
| 10 |
# Initialize model
|
| 11 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 12 |
|
|
|
|
| 13 |
# ---------- STEP 1: TRANSCRIPTION ----------
|
| 14 |
def transcribe_files(qp_file, ms_file, ans_file):
|
| 15 |
+
# Upload Answer Sheet
|
|
|
|
|
|
|
| 16 |
uploaded_as = genai.upload_file(path=ans_file.name, display_name="Answer Sheet")
|
| 17 |
|
| 18 |
transcription_instructions = """
|
| 19 |
Persona:
|
| 20 |
You are an expert transcriptionist specializing in scientific and mathematical documents.
|
| 21 |
+
Your task is to transcribe the provided handwritten student solutions into Markdown+LaTeX.
|
| 22 |
+
Follow these rules:
|
| 23 |
+
- Use LaTeX for all math ($ ... $ or $$ ... $$).
|
| 24 |
+
- Do not correct mistakes, just transcribe.
|
|
|
|
| 25 |
- Ignore strikethroughs.
|
| 26 |
+
- Use **bold** for question numbering.
|
| 27 |
+
- Preserve step-by-step derivations.
|
|
|
|
| 28 |
"""
|
| 29 |
|
| 30 |
response = model.generate_content([transcription_instructions, uploaded_as])
|
|
|
|
|
|
|
| 31 |
transcription = getattr(response, "text", None)
|
| 32 |
+
if not transcription and response.candidates:
|
| 33 |
transcription = response.candidates[0].content.parts[0].text
|
| 34 |
+
return transcription or "No transcription generated."
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# ---------- STEP 2: GRADING ----------
|
| 37 |
def grade_files(qp_file, ms_file, ans_file, transcription):
|
| 38 |
+
# Upload QP and MS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
uploaded_qp = genai.upload_file(path=qp_file.name, display_name="Question Paper")
|
| 40 |
uploaded_ms = genai.upload_file(path=ms_file.name, display_name="Marking Scheme")
|
| 41 |
|
| 42 |
grading_system = """
|
| 43 |
Instructions to Examiners:
|
| 44 |
+
- M: Method marks
|
| 45 |
+
- A: Accuracy marks
|
| 46 |
+
- FT: Follow-through rules
|
| 47 |
+
- Apply marking strictly as per scheme.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
|
| 50 |
response = model.generate_content([
|
| 51 |
+
f"You are an examiner. Grade the transcription using the rules:\n{grading_system}",
|
| 52 |
uploaded_qp,
|
| 53 |
uploaded_ms,
|
| 54 |
transcription
|
| 55 |
])
|
|
|
|
|
|
|
| 56 |
grading = getattr(response, "text", None)
|
| 57 |
+
if not grading and response.candidates:
|
| 58 |
grading = response.candidates[0].content.parts[0].text
|
| 59 |
+
return grading or "No grading generated."
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# ---------- GRADIO UI ----------
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
gr.Markdown("## π Automated Transcription & Grading System")
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
+
qp = gr.File(label="Upload Question Paper (PDF)")
|
| 67 |
+
ms = gr.File(label="Upload Marking Scheme (PDF)")
|
| 68 |
+
ans = gr.File(label="Upload Answer Sheet (PDF)")
|
| 69 |
|
| 70 |
transcribe_btn = gr.Button("π Transcribe Answer Sheet")
|
| 71 |
+
transcription_output = gr.Textbox(label="Transcription", lines=20)
|
| 72 |
|
| 73 |
grade_btn = gr.Button("β
Grade Answers")
|
| 74 |
+
grading_output = gr.Textbox(label="Grading Result", lines=20)
|
| 75 |
|
| 76 |
transcribe_btn.click(fn=transcribe_files, inputs=[qp, ms, ans], outputs=transcription_output)
|
| 77 |
grade_btn.click(fn=grade_files, inputs=[qp, ms, ans, transcription_output], outputs=grading_output)
|