Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -531,7 +531,7 @@ def merge_pdfs(paths, output_path):
|
|
| 531 |
writer.write(f)
|
| 532 |
return output_path
|
| 533 |
|
| 534 |
-
def gemini_generate_content(prompt_text, file_upload_obj=None, image_obj=None, model_name="gemini-2.5-pro"):
|
| 535 |
"""
|
| 536 |
Send prompt_text and optionally an uploaded file (or an image object/list) to the model using NEW SDK.
|
| 537 |
Returns textual response and prints progress.
|
|
@@ -569,10 +569,10 @@ def gemini_generate_content(prompt_text, file_upload_obj=None, image_obj=None, m
|
|
| 569 |
except Exception as e:
|
| 570 |
print(f"β Generation failed: {e}")
|
| 571 |
# Try fallback model
|
| 572 |
-
print("β‘ Trying fallback model:
|
| 573 |
try:
|
| 574 |
response = client.models.generate_content(
|
| 575 |
-
model=
|
| 576 |
contents=contents
|
| 577 |
)
|
| 578 |
raw_text = response.text
|
|
@@ -893,7 +893,7 @@ Grading JSON:
|
|
| 893 |
print("β οΈ Trying fallback model for mapping...")
|
| 894 |
contents = [prompt] + images
|
| 895 |
response = client.models.generate_content(
|
| 896 |
-
model="gemini-
|
| 897 |
contents=contents
|
| 898 |
)
|
| 899 |
raw_text = response.text
|
|
@@ -1092,7 +1092,7 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, subject="Maths", imprin
|
|
| 1092 |
|
| 1093 |
print("1.i) Transcribing QP+MS (questions first, then full markscheme, with graph detection)...")
|
| 1094 |
qpms_prompt = QP_MS_TRANSCRIPTION_PROMPT["content"] + "\nAt the end, also list all questions in the markscheme where a graph is expected, in the format:\nGraph expected in:\n- Question <number> β Page <number>\n(One per line, after ==== MARKSCHEME END ====)"
|
| 1095 |
-
qpms_text = gemini_generate_content(qpms_prompt, file_upload_obj=merged_uploaded, model_name="gemini-2.5-flash")
|
| 1096 |
print("π QP+MS transcription received. Saving debug file: debug_qpms_transcript.txt")
|
| 1097 |
with open("debug_qpms_transcript.txt", "w", encoding="utf-8") as f:
|
| 1098 |
f.write(qpms_text)
|
|
@@ -1110,7 +1110,7 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, subject="Maths", imprin
|
|
| 1110 |
|
| 1111 |
print("1.ii) Building AS transcription prompt with expected question IDs and graph detection, sending to Gemini...")
|
| 1112 |
as_prompt = build_as_cot_prompt_with_expected_ids(extracted_ids, qpms_text) + "\nAt the end, also list all answers where a graph is found, in the format:\nGraph found in:\n- Answer <number> β Page <number>\n(One per line, after all answers)"
|
| 1113 |
-
as_text = gemini_generate_content(as_prompt, file_upload_obj=ans_uploaded, model_name="gemini-2.5-flash")
|
| 1114 |
print("π AS transcription received. Saving debug file: debug_as_transcript.txt")
|
| 1115 |
with open("debug_as_transcript.txt", "w", encoding="utf-8") as f:
|
| 1116 |
f.write(as_text)
|
|
@@ -1137,7 +1137,7 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, subject="Maths", imprin
|
|
| 1137 |
grading_prompt_obj = get_grading_prompt(subject.lower())
|
| 1138 |
grading_prompt_system = grading_prompt_obj["content"]
|
| 1139 |
grading_images = ms_graph_images + as_graph_images
|
| 1140 |
-
grading_text = gemini_generate_content(grading_prompt_system + "\n\nPlease grade the following transcripts:\n" + grading_input, image_obj=grading_images if grading_images else None, model_name="gemini-2.5-pro")
|
| 1141 |
print("π§Ύ Grading output received. Saving debug file: debug_grading.md")
|
| 1142 |
with open("debug_grading.md", "w", encoding="utf-8") as f:
|
| 1143 |
f.write(grading_text)
|
|
@@ -1189,9 +1189,9 @@ def align_and_grade_pipeline(qp_path, ms_path, ans_path, subject="Maths", imprin
|
|
| 1189 |
return f"β Error: {e}", None, None, None, None, {}
|
| 1190 |
|
| 1191 |
# ---------------- GRADIO UI ----------------
|
| 1192 |
-
with gr.Blocks(title="AI Grading
|
| 1193 |
-
gr.Markdown("## π AI Grading
|
| 1194 |
-
|
| 1195 |
|
| 1196 |
if supabase_client:
|
| 1197 |
gr.Markdown("**βοΈ Supabase Storage: Enabled** - All files will be uploaded to cloud storage")
|
|
|
|
| 531 |
writer.write(f)
|
| 532 |
return output_path
|
| 533 |
|
| 534 |
+
def gemini_generate_content(prompt_text, file_upload_obj=None, image_obj=None, model_name="gemini-2.5-pro", fallback_model="gemini-2.5-flash"):
|
| 535 |
"""
|
| 536 |
Send prompt_text and optionally an uploaded file (or an image object/list) to the model using NEW SDK.
|
| 537 |
Returns textual response and prints progress.
|
|
|
|
| 569 |
except Exception as e:
|
| 570 |
print(f"β Generation failed: {e}")
|
| 571 |
# Try fallback model
|
| 572 |
+
print(f"β‘ Trying fallback model: {fallback_model}")
|
| 573 |
try:
|
| 574 |
response = client.models.generate_content(
|
| 575 |
+
model=fallback_model,
|
| 576 |
contents=contents
|
| 577 |
)
|
| 578 |
raw_text = response.text
|
|
|
|
| 893 |
print("β οΈ Trying fallback model for mapping...")
|
| 894 |
contents = [prompt] + images
|
| 895 |
response = client.models.generate_content(
|
| 896 |
+
model="gemini-2.5-flash-preview-09-2025",
|
| 897 |
contents=contents
|
| 898 |
)
|
| 899 |
raw_text = response.text
|
|
|
|
| 1092 |
|
| 1093 |
print("1.i) Transcribing QP+MS (questions first, then full markscheme, with graph detection)...")
|
| 1094 |
qpms_prompt = QP_MS_TRANSCRIPTION_PROMPT["content"] + "\nAt the end, also list all questions in the markscheme where a graph is expected, in the format:\nGraph expected in:\n- Question <number> β Page <number>\n(One per line, after ==== MARKSCHEME END ====)"
|
| 1095 |
+
qpms_text = gemini_generate_content(qpms_prompt, file_upload_obj=merged_uploaded, model_name="gemini-2.5-flash", fallback_model="gemini-2.5-flash-preview-09-2025")
|
| 1096 |
print("π QP+MS transcription received. Saving debug file: debug_qpms_transcript.txt")
|
| 1097 |
with open("debug_qpms_transcript.txt", "w", encoding="utf-8") as f:
|
| 1098 |
f.write(qpms_text)
|
|
|
|
| 1110 |
|
| 1111 |
print("1.ii) Building AS transcription prompt with expected question IDs and graph detection, sending to Gemini...")
|
| 1112 |
as_prompt = build_as_cot_prompt_with_expected_ids(extracted_ids, qpms_text) + "\nAt the end, also list all answers where a graph is found, in the format:\nGraph found in:\n- Answer <number> β Page <number>\n(One per line, after all answers)"
|
| 1113 |
+
as_text = gemini_generate_content(as_prompt, file_upload_obj=ans_uploaded, model_name="gemini-2.5-flash", fallback_model="gemini-2.5-flash-preview-09-2025")
|
| 1114 |
print("π AS transcription received. Saving debug file: debug_as_transcript.txt")
|
| 1115 |
with open("debug_as_transcript.txt", "w", encoding="utf-8") as f:
|
| 1116 |
f.write(as_text)
|
|
|
|
| 1137 |
grading_prompt_obj = get_grading_prompt(subject.lower())
|
| 1138 |
grading_prompt_system = grading_prompt_obj["content"]
|
| 1139 |
grading_images = ms_graph_images + as_graph_images
|
| 1140 |
+
grading_text = gemini_generate_content(grading_prompt_system + "\n\nPlease grade the following transcripts:\n" + grading_input, image_obj=grading_images if grading_images else None, model_name="gemini-2.5-pro", fallback_model="gemini-2.5-flash")
|
| 1141 |
print("π§Ύ Grading output received. Saving debug file: debug_grading.md")
|
| 1142 |
with open("debug_grading.md", "w", encoding="utf-8") as f:
|
| 1143 |
f.write(grading_text)
|
|
|
|
| 1189 |
return f"β Error: {e}", None, None, None, None, {}
|
| 1190 |
|
| 1191 |
# ---------------- GRADIO UI ----------------
|
| 1192 |
+
with gr.Blocks(title="AI Grading") as demo:
|
| 1193 |
+
gr.Markdown("## π AI Grading ")
|
| 1194 |
+
|
| 1195 |
|
| 1196 |
if supabase_client:
|
| 1197 |
gr.Markdown("**βοΈ Supabase Storage: Enabled** - All files will be uploaded to cloud storage")
|