atz21 commited on
Commit
e49b802
·
verified ·
1 Parent(s): b9e41b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -3,22 +3,29 @@ import os
3
  import io
4
  from google import generativeai as genai
5
 
6
- def process_exam_papers(question_paper, marking_scheme, answer_sheet, api_key):
7
  """
8
  Process uploaded exam papers and return transcription and grading
9
  """
10
  if not api_key:
11
- return "Please provide a valid Gemini API key.", ""
 
 
 
12
 
13
  try:
14
  # Configure Gemini API
15
  genai.configure(api_key=api_key)
16
 
 
 
17
  # Upload files to Gemini
18
  qp_file = genai.upload_file(path=question_paper.name, display_name="Question Paper")
19
  ms_file = genai.upload_file(path=marking_scheme.name, display_name="Marking Scheme")
20
  ans_file = genai.upload_file(path=answer_sheet.name, display_name="Answer Sheet")
21
 
 
 
22
  # Transcription instructions
23
  transcription_instructions = """
24
  Persona:
@@ -79,6 +86,8 @@ $$
79
  generation_config={"temperature": 0}
80
  )
81
 
 
 
82
  # Generate transcription
83
  response = model.generate_content([
84
  transcription_instructions,
@@ -90,6 +99,11 @@ $$
90
  if not student_transcription:
91
  student_transcription = response.candidates[0].content.parts[0].text
92
 
 
 
 
 
 
93
  # Grading system instructions
94
  grading_system = """
95
  Instructions to Examiners:
@@ -116,6 +130,7 @@ Error Avoidance:
116
  - **Follow markscheme logic exactly:** Especially regarding when to withhold accuracy marks if method marks are not earned.
117
  """
118
 
 
119
  # Generate grading
120
  grading_response = model.generate_content([
121
  f"You are an official examiner. Use the following grading system and rules to assess the answers:\n\n{grading_system}\n\n"
@@ -128,9 +143,11 @@ Error Avoidance:
128
  "6. Provide a step-by-step reasoning for each mark awarded or withheld, explaining your thought process clearly.\n",
129
  qp_file,
130
  ms_file,
131
- student_transcription
132
  ])
133
 
 
 
134
  # Extract grading safely
135
  grading_text = getattr(grading_response, "text", None)
136
  if not grading_text and grading_response.candidates:
@@ -138,10 +155,13 @@ Error Avoidance:
138
  elif not grading_text:
139
  grading_text = "No Response"
140
 
141
- return student_transcription, grading_text
 
 
 
142
 
143
  except Exception as e:
144
- return f"Error processing files: {str(e)}", ""
145
 
146
  # Create Gradio interface
147
  with gr.Blocks(title="Exam Paper Grading System", theme=gr.themes.Soft()) as demo:
@@ -185,7 +205,8 @@ with gr.Blocks(title="Exam Paper Grading System", theme=gr.themes.Soft()) as dem
185
  label="Transcribed Answers",
186
  lines=15,
187
  max_lines=25,
188
- show_copy_button=True
 
189
  )
190
 
191
  with gr.Column():
@@ -194,14 +215,24 @@ with gr.Blocks(title="Exam Paper Grading System", theme=gr.themes.Soft()) as dem
194
  label="Detailed Grading",
195
  lines=15,
196
  max_lines=25,
197
- show_copy_button=True
 
198
  )
199
 
 
 
 
 
 
 
 
 
 
200
  # Set up the processing function
201
  process_btn.click(
202
  fn=process_exam_papers,
203
  inputs=[question_paper, marking_scheme, answer_sheet, api_key],
204
- outputs=[transcription_output, grading_output]
205
  )
206
 
207
  gr.Markdown("""
 
3
  import io
4
  from google import generativeai as genai
5
 
6
+ def process_exam_papers(question_paper, marking_scheme, answer_sheet, api_key, progress=gr.Progress()):
7
  """
8
  Process uploaded exam papers and return transcription and grading
9
  """
10
  if not api_key:
11
+ return "Please provide a valid Gemini API key.", "", "Ready"
12
+
13
+ if not all([question_paper, marking_scheme, answer_sheet]):
14
+ return "Please upload all three files.", "", "Ready"
15
 
16
  try:
17
  # Configure Gemini API
18
  genai.configure(api_key=api_key)
19
 
20
+ progress(0.1, desc="Uploading files to Gemini...")
21
+
22
  # Upload files to Gemini
23
  qp_file = genai.upload_file(path=question_paper.name, display_name="Question Paper")
24
  ms_file = genai.upload_file(path=marking_scheme.name, display_name="Marking Scheme")
25
  ans_file = genai.upload_file(path=answer_sheet.name, display_name="Answer Sheet")
26
 
27
+ progress(0.3, desc="Files uploaded. Starting transcription...")
28
+
29
  # Transcription instructions
30
  transcription_instructions = """
31
  Persona:
 
86
  generation_config={"temperature": 0}
87
  )
88
 
89
+ progress(0.4, desc="Transcribing handwritten answers...")
90
+
91
  # Generate transcription
92
  response = model.generate_content([
93
  transcription_instructions,
 
99
  if not student_transcription:
100
  student_transcription = response.candidates[0].content.parts[0].text
101
 
102
+ progress(0.7, desc="Transcription complete. Starting grading process...")
103
+
104
+ # Return transcription first, then continue with grading
105
+ yield student_transcription, "⏳ Grading in progress...", "Grading"
106
+
107
  # Grading system instructions
108
  grading_system = """
109
  Instructions to Examiners:
 
130
  - **Follow markscheme logic exactly:** Especially regarding when to withhold accuracy marks if method marks are not earned.
131
  """
132
 
133
+ # Now start grading using the transcribed text
134
  # Generate grading
135
  grading_response = model.generate_content([
136
  f"You are an official examiner. Use the following grading system and rules to assess the answers:\n\n{grading_system}\n\n"
 
143
  "6. Provide a step-by-step reasoning for each mark awarded or withheld, explaining your thought process clearly.\n",
144
  qp_file,
145
  ms_file,
146
+ student_transcription # Use the transcribed text, not the original PDF
147
  ])
148
 
149
+ progress(0.9, desc="Finalizing grading results...")
150
+
151
  # Extract grading safely
152
  grading_text = getattr(grading_response, "text", None)
153
  if not grading_text and grading_response.candidates:
 
155
  elif not grading_text:
156
  grading_text = "No Response"
157
 
158
+ progress(1.0, desc="Complete!")
159
+
160
+ # Return final results
161
+ yield student_transcription, grading_text, "Complete"
162
 
163
  except Exception as e:
164
+ yield f"Error processing files: {str(e)}", "", "Error"
165
 
166
  # Create Gradio interface
167
  with gr.Blocks(title="Exam Paper Grading System", theme=gr.themes.Soft()) as demo:
 
205
  label="Transcribed Answers",
206
  lines=15,
207
  max_lines=25,
208
+ show_copy_button=True,
209
+ placeholder="Transcribed answers will appear here first..."
210
  )
211
 
212
  with gr.Column():
 
215
  label="Detailed Grading",
216
  lines=15,
217
  max_lines=25,
218
+ show_copy_button=True,
219
+ placeholder="Grading results will appear here after transcription is complete..."
220
  )
221
 
222
+ # Add status indicator
223
+ with gr.Row():
224
+ status_display = gr.Textbox(
225
+ label="Status",
226
+ value="Ready",
227
+ interactive=False,
228
+ show_label=True
229
+ )
230
+
231
  # Set up the processing function
232
  process_btn.click(
233
  fn=process_exam_papers,
234
  inputs=[question_paper, marking_scheme, answer_sheet, api_key],
235
+ outputs=[transcription_output, grading_output, status_display]
236
  )
237
 
238
  gr.Markdown("""