Spaces:
Sleeping
Sleeping
Update routes.py
Browse files
routes.py
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
from app.services import init_genai, grade_all_students
|
| 5 |
+
|
| 6 |
+
router = APIRouter()
|
| 7 |
+
|
| 8 |
+
@router.post('/grade')
|
| 9 |
+
async def grade(
|
| 10 |
+
api_key: str,
|
| 11 |
+
answer_key: UploadFile = File(...),
|
| 12 |
+
student_pdf: UploadFile = File(...)
|
| 13 |
+
):
|
| 14 |
+
"""
|
| 15 |
+
Single endpoint: initializes GenAI, grades students against the answer key,
|
| 16 |
+
and returns the result cards.
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
# Initialize GenAI client with provided API key
|
| 20 |
+
init_genai(api_key)
|
| 21 |
+
|
| 22 |
+
# Save uploaded PDFs to temp files
|
| 23 |
+
with tempfile.NamedTemporaryFile(suffix=answer_key.filename, delete=False) as akf:
|
| 24 |
+
akf.write(await answer_key.read())
|
| 25 |
+
ak_path = akf.name
|
| 26 |
+
with tempfile.NamedTemporaryFile(suffix=student_pdf.filename, delete=False) as spf:
|
| 27 |
+
spf.write(await student_pdf.read())
|
| 28 |
+
sp_path = spf.name
|
| 29 |
+
|
| 30 |
+
# Grade all students and return results
|
| 31 |
+
results = grade_all_students(ak_path, sp_path)
|
| 32 |
+
return results
|
| 33 |
+
except Exception as e:
|
| 34 |
+
raise HTTPException(status_code=500, detail=str(e))
|