Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -165,6 +165,38 @@ def create_model():
|
|
| 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.
|
|
@@ -562,21 +594,28 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, imprint=False):
|
|
| 562 |
print("πΌ Uploading files to Gemini...")
|
| 563 |
try:
|
| 564 |
merged_uploaded = genai.upload_file(path=merged_qpms_path, display_name="QP+MS (merged)")
|
| 565 |
-
|
|
|
|
|
|
|
| 566 |
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
raise Exception("Answer Sheet file upload validation failed")
|
| 572 |
|
| 573 |
-
print("β
|
| 574 |
except Exception as e:
|
| 575 |
-
print(f"β File upload/
|
| 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}")
|
|
|
|
| 165 |
print("β Failed to create any Gemini model:", e)
|
| 166 |
raise
|
| 167 |
|
| 168 |
+
def wait_for_file_active(uploaded_file, timeout=300):
|
| 169 |
+
"""
|
| 170 |
+
Wait for an uploaded file to become ACTIVE before proceeding.
|
| 171 |
+
Polls the file status every 2 seconds until ACTIVE or timeout.
|
| 172 |
+
"""
|
| 173 |
+
import time
|
| 174 |
+
start_time = time.time()
|
| 175 |
+
|
| 176 |
+
while time.time() - start_time < timeout:
|
| 177 |
+
try:
|
| 178 |
+
file_info = genai.get_file(uploaded_file.name)
|
| 179 |
+
state = file_info.state.name
|
| 180 |
+
print(f"π File {uploaded_file.name} status: {state}")
|
| 181 |
+
|
| 182 |
+
if state == "ACTIVE":
|
| 183 |
+
print(f"β
File {uploaded_file.name} is now ACTIVE and ready to use")
|
| 184 |
+
return True
|
| 185 |
+
elif state == "FAILED":
|
| 186 |
+
print(f"β File {uploaded_file.name} processing failed")
|
| 187 |
+
return False
|
| 188 |
+
|
| 189 |
+
# Still processing, wait a bit
|
| 190 |
+
print(f"β³ File still processing, waiting 2 seconds...")
|
| 191 |
+
time.sleep(2)
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
print(f"β Error checking file status: {e}")
|
| 195 |
+
time.sleep(2)
|
| 196 |
+
|
| 197 |
+
print(f"β° Timeout waiting for file {uploaded_file.name} to become active")
|
| 198 |
+
return False
|
| 199 |
+
|
| 200 |
def validate_uploaded_file(uploaded_file):
|
| 201 |
"""
|
| 202 |
Validate that an uploaded file is accessible and hasn't expired.
|
|
|
|
| 594 |
print("πΌ Uploading files to Gemini...")
|
| 595 |
try:
|
| 596 |
merged_uploaded = genai.upload_file(path=merged_qpms_path, display_name="QP+MS (merged)")
|
| 597 |
+
print("π€ QP+MS file uploaded, waiting for processing...")
|
| 598 |
+
if not wait_for_file_active(merged_uploaded):
|
| 599 |
+
raise Exception("QP+MS file failed to become active")
|
| 600 |
|
| 601 |
+
ans_uploaded = genai.upload_file(path=ans_path, display_name="Answer Sheet")
|
| 602 |
+
print("π€ Answer Sheet uploaded, waiting for processing...")
|
| 603 |
+
if not wait_for_file_active(ans_uploaded):
|
| 604 |
+
raise Exception("Answer Sheet file failed to become active")
|
|
|
|
| 605 |
|
| 606 |
+
print("β
All files uploaded and active.")
|
| 607 |
except Exception as e:
|
| 608 |
+
print(f"β File upload/processing failed: {e}")
|
| 609 |
print("π Retrying file upload...")
|
| 610 |
try:
|
| 611 |
merged_uploaded = genai.upload_file(path=merged_qpms_path, display_name="QP+MS (merged)")
|
| 612 |
+
if not wait_for_file_active(merged_uploaded):
|
| 613 |
+
raise Exception("QP+MS retry failed to become active")
|
| 614 |
+
|
| 615 |
ans_uploaded = genai.upload_file(path=ans_path, display_name="Answer Sheet")
|
| 616 |
+
if not wait_for_file_active(ans_uploaded):
|
| 617 |
+
raise Exception("Answer Sheet retry failed to become active")
|
| 618 |
+
|
| 619 |
print("β
Retry upload successful.")
|
| 620 |
except Exception as retry_e:
|
| 621 |
print(f"β Retry failed: {retry_e}")
|