Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
-
#
|
| 6 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 7 |
|
| 8 |
# ---------- PROMPTS ----------
|
|
@@ -34,44 +34,54 @@ Rules:
|
|
| 34 |
6. Do not give marks outside the markscheme.
|
| 35 |
Output must be a structured grading report with reasoning.
|
| 36 |
"""
|
| 37 |
-
def grade_student(qp_file, ms_file, ans_file):
|
| 38 |
-
try:
|
| 39 |
-
# Get file paths
|
| 40 |
-
qp_path = qp_file.name
|
| 41 |
-
ms_path = ms_file.name
|
| 42 |
-
ans_path = ans_file.name
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
ans_uploaded = genai.upload_file(path=
|
| 48 |
|
| 49 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
-
return f"β Error during
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
gr.File(label="Upload
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
+
# Configure Gemini API (key must be set in Hugging Face Space secrets)
|
| 6 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 7 |
|
| 8 |
# ---------- PROMPTS ----------
|
|
|
|
| 34 |
6. Do not give marks outside the markscheme.
|
| 35 |
Output must be a structured grading report with reasoning.
|
| 36 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# ---------- STEP 1: TRANSCRIPTION ----------
|
| 39 |
+
def transcribe(ans_file):
|
| 40 |
+
try:
|
| 41 |
+
ans_uploaded = genai.upload_file(path=ans_file.name, display_name="Answer Sheet")
|
| 42 |
|
| 43 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 44 |
+
resp = model.generate_content([TRANSCRIPTION_PROMPT, ans_uploaded])
|
| 45 |
+
|
| 46 |
+
transcription = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
|
| 47 |
+
return transcription
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"β Error during transcription: {e}"
|
| 50 |
|
| 51 |
+
# ---------- STEP 2: GRADING ----------
|
| 52 |
+
def grade(qp_file, ms_file, transcription):
|
| 53 |
+
try:
|
| 54 |
+
qp_uploaded = genai.upload_file(path=qp_file.name, display_name="Question Paper")
|
| 55 |
+
ms_uploaded = genai.upload_file(path=ms_file.name, display_name="Marking Scheme")
|
| 56 |
|
| 57 |
+
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 58 |
+
resp = model.generate_content([GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
|
| 59 |
|
| 60 |
+
grading = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
|
| 61 |
+
return grading
|
| 62 |
except Exception as e:
|
| 63 |
+
return f"β Error during grading: {e}"
|
| 64 |
+
|
| 65 |
+
# ---------- GRADIO APP ----------
|
| 66 |
+
with gr.Blocks(title="π AI Teacher Assistant") as demo:
|
| 67 |
+
gr.Markdown("## π AI Teacher Assistant\nUpload exam documents to transcribe and grade student answers step by step.")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
qp_file = gr.File(label="Upload Question Paper (PDF)", type="filepath")
|
| 71 |
+
ms_file = gr.File(label="Upload Mark Scheme (PDF)", type="filepath")
|
| 72 |
+
ans_file = gr.File(label="Upload Student Answer Sheet (PDF)", type="filepath")
|
| 73 |
+
|
| 74 |
+
# Step 1: Transcription
|
| 75 |
+
transcribe_btn = gr.Button("Step 1: Transcribe Answer Sheet")
|
| 76 |
+
transcription_out = gr.Markdown(label="π Student Transcription")
|
| 77 |
+
|
| 78 |
+
# Step 2: Grading
|
| 79 |
+
grade_btn = gr.Button("Step 2: Grade the Student")
|
| 80 |
+
grading_out = gr.Textbox(label="β
Grading Report (Step-by-Step)", lines=20)
|
| 81 |
+
|
| 82 |
+
# Button Logic
|
| 83 |
+
transcribe_btn.click(fn=transcribe, inputs=[ans_file], outputs=[transcription_out])
|
| 84 |
+
grade_btn.click(fn=grade, inputs=[qp_file, ms_file, transcription_out], outputs=[grading_out])
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|