atz21 commited on
Commit
fa2ac16
Β·
verified Β·
1 Parent(s): 1e9212d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import google.generativeai as genai
4
+
5
+ # Configure Gemini API (key must be set in Hugging Face Space secrets)
6
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
7
+
8
+ # ---------- PROMPTS ----------
9
+ TRANSCRIPTION_PROMPT = """
10
+ Persona:
11
+ You are an expert transcriptionist specializing in scientific and mathematical documents.
12
+ Your primary goal is to convert handwritten mathematical work into a perfectly formatted,
13
+ machine-readable Markdown document using LaTeX for all mathematical notation.
14
+
15
+ Rules:
16
+ - Transcribe exactly what is written, do not correct errors.
17
+ - Use $...$ for inline math, $$...$$ for block math.
18
+ - Ignore struck-through text.
19
+ - Preserve structure: bold for Q numbers (**1.**), step-by-step math with \\begin{align*}.
20
+ - If a symbol is ambiguous, mark as [x?].
21
+ Output must be a clean Markdown string.
22
+ """
23
+
24
+ GRADING_PROMPT = """
25
+ You are an official examiner. Grade the student transcription using the question paper
26
+ and the official marking scheme.
27
+
28
+ Rules:
29
+ 1. Apply marks exactly as per the markscheme (M1, A1, etc.).
30
+ 2. M marks must be earned before A marks.
31
+ 3. Justify each awarded or withheld mark with clear reasoning.
32
+ 4. Classify all errors as Conceptual Error, Silly Mistake, or None.
33
+ 5. Follow dependency between M and A strictly.
34
+ 6. Do not give marks outside the markscheme.
35
+ Output must be a structured grading report with reasoning.
36
+ """
37
+
38
+ # ---------- STEP 1: TRANSCRIPTION ----------
39
+ def transcribe(ans_file):
40
+ yield "⏳ Processing transcription..."
41
+ try:
42
+ ans_uploaded = genai.upload_file(path=ans_file.name, display_name="Answer Sheet")
43
+
44
+ model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
45
+ resp = model.generate_content([TRANSCRIPTION_PROMPT, ans_uploaded])
46
+
47
+ transcription = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
48
+
49
+ yield transcription # Markdown will render LaTeX properly
50
+ except Exception as e:
51
+ yield f"❌ Error during transcription: {e}"
52
+
53
+ # ---------- STEP 2: GRADING ----------
54
+ def grade(qp_file, ms_file, transcription):
55
+ yield "⏳ Grading in progress..."
56
+ try:
57
+ qp_uploaded = genai.upload_file(path=qp_file.name, display_name="Question Paper")
58
+ ms_uploaded = genai.upload_file(path=ms_file.name, display_name="Marking Scheme")
59
+
60
+ model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
61
+ resp = model.generate_content([GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
62
+
63
+ grading = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
64
+
65
+ yield grading
66
+ except Exception as e:
67
+ yield f"❌ Error during grading: {e}"
68
+
69
+ # ---------- GRADIO APP ----------
70
+ with gr.Blocks(title="πŸ“˜ AI Teacher Assistant") as demo:
71
+ gr.Markdown("## πŸ“˜ AI Teacher Assistant\nUpload exam documents to transcribe and grade student answers step by step.")
72
+
73
+ with gr.Row():
74
+ qp_file = gr.File(label="Upload Question Paper (PDF)", type="filepath")
75
+ ms_file = gr.File(label="Upload Mark Scheme (PDF)", type="filepath")
76
+ ans_file = gr.File(label="Upload Student Answer Sheet (PDF)", type="filepath")
77
+
78
+ # Step 1: Transcription
79
+ transcribe_btn = gr.Button("Step 1: Transcribe Answer Sheet")
80
+ transcription_out = gr.Markdown(label="πŸ“„ Student Transcription", elem_id="transcription_box")
81
+
82
+ # Step 2: Grading
83
+ grade_btn = gr.Button("Step 2: Grade the Student")
84
+ grading_out = gr.Textbox(label="βœ… Grading Report (Step-by-Step)", lines=20)
85
+
86
+ # Button Logic with streaming updates
87
+ transcribe_btn.click(fn=transcribe, inputs=[ans_file], outputs=[transcription_out])
88
+ grade_btn.click(fn=grade, inputs=[qp_file, ms_file, transcription_out], outputs=[grading_out])
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch()