Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -163,7 +163,23 @@ def create_model():
|
|
| 163 |
return model
|
| 164 |
except Exception as e:
|
| 165 |
print("β Failed to create any Gemini model:", e)
|
| 166 |
-
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
def merge_pdfs(paths, output_path):
|
| 169 |
writer = PdfWriter()
|
|
@@ -542,11 +558,29 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, imprint=False):
|
|
| 542 |
merge_pdfs([qp_path, ms_path], merged_qpms_path)
|
| 543 |
print("π Merged QP + MS ->", merged_qpms_path)
|
| 544 |
|
| 545 |
-
# Upload files to Gemini
|
| 546 |
print("πΌ Uploading files to Gemini...")
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
|
| 551 |
# Create model and print which selected
|
| 552 |
model = create_model()
|
|
@@ -638,8 +672,8 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, imprint=False):
|
|
| 638 |
return f"β Error: {e}", None, None, None, None
|
| 639 |
|
| 640 |
# ---------------- GRADIO UI ----------------
|
| 641 |
-
with gr.Blocks(title=" AI Grading (Final Flow )") as demo:
|
| 642 |
-
gr.Markdown("## π
|
| 643 |
|
| 644 |
with gr.Row():
|
| 645 |
qp_file = gr.File(label="π Upload Question Paper (PDF)")
|
|
|
|
| 163 |
return model
|
| 164 |
except Exception as e:
|
| 165 |
print("β Failed to create any Gemini model:", e)
|
| 166 |
+
raise
|
| 167 |
+
|
| 168 |
+
def validate_uploaded_file(uploaded_file):
|
| 169 |
+
"""
|
| 170 |
+
Validate that an uploaded file is accessible and hasn't expired.
|
| 171 |
+
"""
|
| 172 |
+
try:
|
| 173 |
+
# Try to access the file info to check if it's valid
|
| 174 |
+
file_info = genai.get_file(uploaded_file.name)
|
| 175 |
+
if file_info.state.name == "ACTIVE":
|
| 176 |
+
return True
|
| 177 |
+
else:
|
| 178 |
+
print(f"β οΈ File {uploaded_file.name} is not active (state: {file_info.state.name})")
|
| 179 |
+
return False
|
| 180 |
+
except Exception as e:
|
| 181 |
+
print(f"β File validation failed: {e}")
|
| 182 |
+
return False
|
| 183 |
|
| 184 |
def merge_pdfs(paths, output_path):
|
| 185 |
writer = PdfWriter()
|
|
|
|
| 558 |
merge_pdfs([qp_path, ms_path], merged_qpms_path)
|
| 559 |
print("π Merged QP + MS ->", merged_qpms_path)
|
| 560 |
|
| 561 |
+
# Upload files to Gemini with error handling and validation
|
| 562 |
print("πΌ Uploading files to Gemini...")
|
| 563 |
+
try:
|
| 564 |
+
merged_uploaded = genai.upload_file(path=merged_qpms_path, display_name="QP+MS (merged)")
|
| 565 |
+
ans_uploaded = genai.upload_file(path=ans_path, display_name="Answer Sheet")
|
| 566 |
+
|
| 567 |
+
# Validate uploads
|
| 568 |
+
if not validate_uploaded_file(merged_uploaded):
|
| 569 |
+
raise Exception("QP+MS file upload validation failed")
|
| 570 |
+
if not validate_uploaded_file(ans_uploaded):
|
| 571 |
+
raise Exception("Answer Sheet file upload validation failed")
|
| 572 |
+
|
| 573 |
+
print("β
Upload and validation complete.")
|
| 574 |
+
except Exception as e:
|
| 575 |
+
print(f"β File upload/validation failed: {e}")
|
| 576 |
+
print("π Retrying file upload...")
|
| 577 |
+
try:
|
| 578 |
+
merged_uploaded = genai.upload_file(path=merged_qpms_path, display_name="QP+MS (merged)")
|
| 579 |
+
ans_uploaded = genai.upload_file(path=ans_path, display_name="Answer Sheet")
|
| 580 |
+
print("β
Retry upload successful.")
|
| 581 |
+
except Exception as retry_e:
|
| 582 |
+
print(f"β Retry failed: {retry_e}")
|
| 583 |
+
return f"β File upload error: {retry_e}", None, None, None, None
|
| 584 |
|
| 585 |
# Create model and print which selected
|
| 586 |
model = create_model()
|
|
|
|
| 672 |
return f"β Error: {e}", None, None, None, None
|
| 673 |
|
| 674 |
# ---------------- GRADIO UI ----------------
|
| 675 |
+
with gr.Blocks(title="LeadIB AI Grading (Final Flow β Verbose)") as demo:
|
| 676 |
+
gr.Markdown("## π LeadIB AI Grading β Final Flow\nUpload **Question Paper**, **Markscheme**, and **Student Answer Sheet**.\nFlow: merge QP+MS -> transcribe QP+MS (questions first, full markscheme) -> extract IDs -> transcribe AS with expected IDs -> grade -> (optional) imprint. Console prints show progress.")
|
| 677 |
|
| 678 |
with gr.Row():
|
| 679 |
qp_file = gr.File(label="π Upload Question Paper (PDF)")
|