Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Configure Gemini API
|
| 6 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 7 |
|
| 8 |
# ---------- PROMPTS ----------
|
|
@@ -40,13 +44,10 @@ def transcribe(ans_file):
|
|
| 40 |
yield "β³ Processing transcription..."
|
| 41 |
try:
|
| 42 |
ans_uploaded = genai.upload_file(path=ans_file.name, display_name="Answer Sheet")
|
| 43 |
-
|
| 44 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 45 |
resp = model.generate_content([TRANSCRIPTION_PROMPT, ans_uploaded])
|
| 46 |
-
|
| 47 |
transcription = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
|
| 48 |
-
|
| 49 |
-
yield transcription # Markdown will render LaTeX properly
|
| 50 |
except Exception as e:
|
| 51 |
yield f"β Error during transcription: {e}"
|
| 52 |
|
|
@@ -59,15 +60,37 @@ def grade(qp_file, ms_file, transcription):
|
|
| 59 |
|
| 60 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 61 |
resp = model.generate_content([GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
|
| 62 |
-
|
| 63 |
grading = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
-
yield f"β Error during grading: {e}"
|
| 68 |
|
| 69 |
# ---------- GRADIO APP ----------
|
| 70 |
with gr.Blocks(title="π AI Teacher Assistant") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
gr.Markdown("## π AI Teacher Assistant\nUpload exam documents to transcribe and grade student answers step by step.")
|
| 72 |
|
| 73 |
with gr.Row():
|
|
@@ -77,15 +100,16 @@ with gr.Blocks(title="π AI Teacher Assistant") as demo:
|
|
| 77 |
|
| 78 |
# Step 1: Transcription
|
| 79 |
transcribe_btn = gr.Button("Step 1: Transcribe Answer Sheet")
|
| 80 |
-
transcription_out = gr.Markdown(label="π Student Transcription"
|
| 81 |
|
| 82 |
# Step 2: Grading
|
| 83 |
grade_btn = gr.Button("Step 2: Grade the Student")
|
| 84 |
grading_out = gr.Textbox(label="β
Grading Report (Step-by-Step)", lines=20)
|
|
|
|
| 85 |
|
| 86 |
-
# Button Logic
|
| 87 |
transcribe_btn.click(fn=transcribe, inputs=[ans_file], outputs=[transcription_out])
|
| 88 |
-
grade_btn.click(fn=grade, inputs=[qp_file, ms_file, transcription_out], outputs=[grading_out])
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 5 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 6 |
+
from reportlab.lib.pagesizes import A4
|
| 7 |
+
from reportlab.lib.units import cm
|
| 8 |
|
| 9 |
+
# Configure Gemini API
|
| 10 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 11 |
|
| 12 |
# ---------- PROMPTS ----------
|
|
|
|
| 44 |
yield "β³ Processing transcription..."
|
| 45 |
try:
|
| 46 |
ans_uploaded = genai.upload_file(path=ans_file.name, display_name="Answer Sheet")
|
|
|
|
| 47 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 48 |
resp = model.generate_content([TRANSCRIPTION_PROMPT, ans_uploaded])
|
|
|
|
| 49 |
transcription = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
|
| 50 |
+
yield transcription
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
yield f"β Error during transcription: {e}"
|
| 53 |
|
|
|
|
| 60 |
|
| 61 |
model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
|
| 62 |
resp = model.generate_content([GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
|
|
|
|
| 63 |
grading = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
|
| 64 |
|
| 65 |
+
# Save grading report as PDF
|
| 66 |
+
pdf_path = "/tmp/grading_report.pdf"
|
| 67 |
+
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
| 68 |
+
styles = getSampleStyleSheet()
|
| 69 |
+
elements = []
|
| 70 |
+
|
| 71 |
+
elements.append(Paragraph("π AI Teacher Assistant - Grading Report", styles["Title"]))
|
| 72 |
+
elements.append(Spacer(1, 0.5*cm))
|
| 73 |
+
for para in grading.split("\n"):
|
| 74 |
+
if para.strip():
|
| 75 |
+
elements.append(Paragraph(para, styles["Normal"]))
|
| 76 |
+
elements.append(Spacer(1, 0.2*cm))
|
| 77 |
+
|
| 78 |
+
doc.build(elements)
|
| 79 |
+
|
| 80 |
+
yield grading, pdf_path
|
| 81 |
except Exception as e:
|
| 82 |
+
yield f"β Error during grading: {e}", None
|
| 83 |
|
| 84 |
# ---------- GRADIO APP ----------
|
| 85 |
with gr.Blocks(title="π AI Teacher Assistant") as demo:
|
| 86 |
+
# Inject MathJax for LaTeX rendering
|
| 87 |
+
gr.HTML("""
|
| 88 |
+
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
|
| 89 |
+
<script id="MathJax-script" async
|
| 90 |
+
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
|
| 91 |
+
</script>
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
gr.Markdown("## π AI Teacher Assistant\nUpload exam documents to transcribe and grade student answers step by step.")
|
| 95 |
|
| 96 |
with gr.Row():
|
|
|
|
| 100 |
|
| 101 |
# Step 1: Transcription
|
| 102 |
transcribe_btn = gr.Button("Step 1: Transcribe Answer Sheet")
|
| 103 |
+
transcription_out = gr.Markdown(label="π Student Transcription")
|
| 104 |
|
| 105 |
# Step 2: Grading
|
| 106 |
grade_btn = gr.Button("Step 2: Grade the Student")
|
| 107 |
grading_out = gr.Textbox(label="β
Grading Report (Step-by-Step)", lines=20)
|
| 108 |
+
download_pdf = gr.File(label="β¬οΈ Download Grading Report (PDF)")
|
| 109 |
|
| 110 |
+
# Button Logic
|
| 111 |
transcribe_btn.click(fn=transcribe, inputs=[ans_file], outputs=[transcription_out])
|
| 112 |
+
grade_btn.click(fn=grade, inputs=[qp_file, ms_file, transcription_out], outputs=[grading_out, download_pdf])
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
demo.launch()
|