atz21 commited on
Commit
14a6aca
·
verified ·
1 Parent(s): 0cd9921

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -35
app.py CHANGED
@@ -28,7 +28,7 @@ Your task is to **align three sources**:
28
  - Use `##` for main questions and `###` for sub-questions.
29
  - Write **QP | MS | AS** exactly in that order.
30
  - Preserve all mathematical expressions inside fenced code blocks.
31
- - Do not re-create diagrams/graphs. Write `[Graph]`.
32
  - If part of the student's answer is unreadable, write `[illegible]`.
33
  - If a student skipped a question, write `[No response]`.
34
  - Keep MS annotations (M1, A1, R1, etc.) exactly as in the original.
@@ -98,7 +98,6 @@ After the table, provide:
98
  }
99
  }
100
 
101
-
102
  # -------------------- CONFIG --------------------
103
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
104
 
@@ -112,88 +111,76 @@ def save_as_pdf(text, filename="output.pdf"):
112
  # ---------- HELPER: Create Model with Fallback ----------
113
  def create_model():
114
  try:
 
115
  return genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
116
  except Exception:
 
117
  return genai.GenerativeModel("gemini-2.5-flash", generation_config={"temperature": 0})
118
 
119
- # ---------- STEP 1: ALIGNMENT ----------
120
- def align_documents(qp_file, ms_file, ans_file):
121
  try:
 
122
  qp_uploaded = genai.upload_file(path=qp_file, display_name="Question Paper")
123
  ms_uploaded = genai.upload_file(path=ms_file, display_name="Markscheme")
124
  ans_uploaded = genai.upload_file(path=ans_file, display_name="Answer Sheet")
125
 
126
  model = create_model()
 
 
127
  resp = model.generate_content([
128
  PROMPTS["ALIGNMENT_PROMPT"]["content"],
129
  qp_uploaded,
130
  ms_uploaded,
131
  ans_uploaded
132
  ])
133
-
134
  aligned_text = getattr(resp, "text", None)
135
  if not aligned_text and resp.candidates:
136
  aligned_text = resp.candidates[0].content.parts[0].text
137
 
138
- pdf_path = save_as_pdf(aligned_text, "aligned_qp_ms_as.pdf")
139
- return aligned_text, pdf_path
140
- except Exception as e:
141
- return f"❌ Error during alignment: {e}", None
142
 
143
- # ---------- STEP 2: GRADING ----------
144
- def grade(aligned_text, ans_file):
145
- try:
146
- model = create_model()
147
  response = model.generate_content([
148
  PROMPTS["GRADING_PROMPT"]["content"],
149
  aligned_text
150
  ])
151
-
152
  grading = getattr(response, "text", None)
153
  if not grading and response.candidates:
154
  grading = response.candidates[0].content.parts[0].text
155
 
156
- # Use answer sheet filename for graded PDF
157
  base_name = os.path.splitext(os.path.basename(ans_file))[0]
158
- pdf_name = f"{base_name}_graded.pdf"
 
 
159
 
160
- pdf_path = save_as_pdf(grading, pdf_name)
161
- return grading, pdf_path
162
  except Exception as e:
163
- return f"❌ Error during grading: {e}", None
164
 
165
  # ---------- GRADIO APP ----------
166
- with gr.Blocks(title="LeadIB AI Grading (Alignment + Grading)") as demo:
167
- gr.Markdown("## LeadIB AI Grading\nUpload Question Paper, Markscheme, and Student Answer Sheet to align and grade.")
168
 
169
  with gr.Row():
170
  qp_file = gr.File(label="Upload Question Paper (PDF)", type="filepath")
171
  ms_file = gr.File(label="Upload Markscheme (PDF)", type="filepath")
172
  ans_file = gr.File(label="Upload Student Answer Sheet (PDF)", type="filepath")
173
 
174
- # Step 1: Alignment
175
- align_btn = gr.Button("Step 1: Align QP | MS | AS")
176
  with gr.Row():
177
  aligned_out = gr.Textbox(label="📄 Aligned QP | MS | AS", lines=20)
178
  aligned_pdf = gr.File(label="⬇️ Download Aligned (PDF)")
179
 
180
- # Step 2: Grading
181
- grade_btn = gr.Button("Step 2: Grade the Student")
182
  with gr.Row():
183
  grading_out = gr.Textbox(label="✅ Grading Report", lines=20)
184
  grading_pdf = gr.File(label="⬇️ Download Grading Report (PDF)")
185
 
186
- # Button Logic
187
- align_btn.click(
188
- fn=align_documents,
189
  inputs=[qp_file, ms_file, ans_file],
190
- outputs=[aligned_out, aligned_pdf],
191
- show_progress=True
192
- )
193
- grade_btn.click(
194
- fn=grade,
195
- inputs=[aligned_out, ans_file],
196
- outputs=[grading_out, grading_pdf],
197
  show_progress=True
198
  )
199
 
 
28
  - Use `##` for main questions and `###` for sub-questions.
29
  - Write **QP | MS | AS** exactly in that order.
30
  - Preserve all mathematical expressions inside fenced code blocks.
31
+ - Do not re-create diagrams/graphs. Write `[Graph omitted]`.
32
  - If part of the student's answer is unreadable, write `[illegible]`.
33
  - If a student skipped a question, write `[No response]`.
34
  - Keep MS annotations (M1, A1, R1, etc.) exactly as in the original.
 
98
  }
99
  }
100
 
 
101
  # -------------------- CONFIG --------------------
102
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
103
 
 
111
  # ---------- HELPER: Create Model with Fallback ----------
112
  def create_model():
113
  try:
114
+ print("⚡ Using gemini-2.5-pro model")
115
  return genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
116
  except Exception:
117
+ print("⚡ Falling back to gemini-2.5-flash model")
118
  return genai.GenerativeModel("gemini-2.5-flash", generation_config={"temperature": 0})
119
 
120
+ # ---------- PIPELINE: ALIGN + GRADE ----------
121
+ def align_and_grade(qp_file, ms_file, ans_file):
122
  try:
123
+ # Uploads
124
  qp_uploaded = genai.upload_file(path=qp_file, display_name="Question Paper")
125
  ms_uploaded = genai.upload_file(path=ms_file, display_name="Markscheme")
126
  ans_uploaded = genai.upload_file(path=ans_file, display_name="Answer Sheet")
127
 
128
  model = create_model()
129
+
130
+ # Step 1: Alignment
131
  resp = model.generate_content([
132
  PROMPTS["ALIGNMENT_PROMPT"]["content"],
133
  qp_uploaded,
134
  ms_uploaded,
135
  ans_uploaded
136
  ])
 
137
  aligned_text = getattr(resp, "text", None)
138
  if not aligned_text and resp.candidates:
139
  aligned_text = resp.candidates[0].content.parts[0].text
140
 
141
+ aligned_pdf_path = save_as_pdf(aligned_text, "aligned_qp_ms_as.pdf")
 
 
 
142
 
143
+ # Step 2: Grading (automatic)
 
 
 
144
  response = model.generate_content([
145
  PROMPTS["GRADING_PROMPT"]["content"],
146
  aligned_text
147
  ])
 
148
  grading = getattr(response, "text", None)
149
  if not grading and response.candidates:
150
  grading = response.candidates[0].content.parts[0].text
151
 
152
+ # Save grading report with student's answer filename
153
  base_name = os.path.splitext(os.path.basename(ans_file))[0]
154
+ grading_pdf_path = save_as_pdf(grading, f"{base_name}_graded.pdf")
155
+
156
+ return aligned_text, aligned_pdf_path, grading, grading_pdf_path
157
 
 
 
158
  except Exception as e:
159
+ return f"❌ Error: {e}", None, None, None
160
 
161
  # ---------- GRADIO APP ----------
162
+ with gr.Blocks(title="LeadIB AI Grading (Alignment + Auto-Grading)") as demo:
163
+ gr.Markdown("## LeadIB AI Grading\nUpload Question Paper, Markscheme, and Student Answer Sheet.\nThe system will align and grade automatically.")
164
 
165
  with gr.Row():
166
  qp_file = gr.File(label="Upload Question Paper (PDF)", type="filepath")
167
  ms_file = gr.File(label="Upload Markscheme (PDF)", type="filepath")
168
  ans_file = gr.File(label="Upload Student Answer Sheet (PDF)", type="filepath")
169
 
170
+ run_btn = gr.Button("Start Alignment + Auto-Grading")
171
+
172
  with gr.Row():
173
  aligned_out = gr.Textbox(label="📄 Aligned QP | MS | AS", lines=20)
174
  aligned_pdf = gr.File(label="⬇️ Download Aligned (PDF)")
175
 
 
 
176
  with gr.Row():
177
  grading_out = gr.Textbox(label="✅ Grading Report", lines=20)
178
  grading_pdf = gr.File(label="⬇️ Download Grading Report (PDF)")
179
 
180
+ run_btn.click(
181
+ fn=align_and_grade,
 
182
  inputs=[qp_file, ms_file, ans_file],
183
+ outputs=[aligned_out, aligned_pdf, grading_out, grading_pdf],
 
 
 
 
 
 
184
  show_progress=True
185
  )
186