Update main.py
Browse files
main.py
CHANGED
@@ -22,6 +22,11 @@ client = genai.Client(api_key=GENAI_API_KEY)
|
|
22 |
|
23 |
app = FastAPI(title="Student Result Card API")
|
24 |
|
|
|
|
|
|
|
|
|
|
|
25 |
# -----------------------------
|
26 |
# Preprocessing Methods
|
27 |
# -----------------------------
|
@@ -294,28 +299,48 @@ async def process_pdfs(
|
|
294 |
result_card["Student Index"] = idx + 1
|
295 |
student_result_cards.append(result_card)
|
296 |
|
|
|
|
|
297 |
if download:
|
298 |
-
# Create downloadable JSON file
|
299 |
-
json_bytes = json.dumps(
|
|
|
|
|
|
|
300 |
return StreamingResponse(
|
301 |
io.BytesIO(json_bytes),
|
302 |
media_type="application/json",
|
303 |
headers={"Content-Disposition": "attachment; filename=result_cards.json"}
|
304 |
)
|
305 |
else:
|
306 |
-
return JSONResponse(content=
|
307 |
|
308 |
except Exception as e:
|
309 |
raise HTTPException(status_code=500, detail=str(e))
|
310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
|
312 |
@app.get("/")
|
313 |
async def root():
|
314 |
return {
|
315 |
"message": "Welcome to the Student Result Card API.",
|
316 |
-
"usage": "POST PDFs to /process with 'student_pdf' and 'answer_key_pdf' fields. Use ?download=true for file download."
|
317 |
}
|
318 |
|
319 |
-
|
320 |
if __name__ == "__main__":
|
321 |
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
22 |
|
23 |
app = FastAPI(title="Student Result Card API")
|
24 |
|
25 |
+
# Ensure downloads folder exists
|
26 |
+
DOWNLOADS_FOLDER = "downloads"
|
27 |
+
if not os.path.exists(DOWNLOADS_FOLDER):
|
28 |
+
os.makedirs(DOWNLOADS_FOLDER)
|
29 |
+
|
30 |
# -----------------------------
|
31 |
# Preprocessing Methods
|
32 |
# -----------------------------
|
|
|
299 |
result_card["Student Index"] = idx + 1
|
300 |
student_result_cards.append(result_card)
|
301 |
|
302 |
+
response_data = {"result_cards": student_result_cards}
|
303 |
+
|
304 |
if download:
|
305 |
+
# Create downloadable JSON file and save to disk
|
306 |
+
json_bytes = json.dumps(response_data, indent=2).encode("utf-8")
|
307 |
+
file_path = os.path.join(DOWNLOADS_FOLDER, "result_cards.json")
|
308 |
+
with open(file_path, "wb") as f:
|
309 |
+
f.write(json_bytes)
|
310 |
return StreamingResponse(
|
311 |
io.BytesIO(json_bytes),
|
312 |
media_type="application/json",
|
313 |
headers={"Content-Disposition": "attachment; filename=result_cards.json"}
|
314 |
)
|
315 |
else:
|
316 |
+
return JSONResponse(content=response_data)
|
317 |
|
318 |
except Exception as e:
|
319 |
raise HTTPException(status_code=500, detail=str(e))
|
320 |
|
321 |
+
# -----------------------------
|
322 |
+
# New Download Endpoint
|
323 |
+
# -----------------------------
|
324 |
+
@app.get("/download")
|
325 |
+
async def download_result_cards():
|
326 |
+
"""
|
327 |
+
Returns the previously generated result_cards.json file.
|
328 |
+
"""
|
329 |
+
file_path = os.path.join(DOWNLOADS_FOLDER, "result_cards.json")
|
330 |
+
if not os.path.exists(file_path):
|
331 |
+
raise HTTPException(status_code=404, detail="File not found")
|
332 |
+
return StreamingResponse(
|
333 |
+
open(file_path, "rb"),
|
334 |
+
media_type="application/json",
|
335 |
+
headers={"Content-Disposition": "attachment; filename=result_cards.json"}
|
336 |
+
)
|
337 |
|
338 |
@app.get("/")
|
339 |
async def root():
|
340 |
return {
|
341 |
"message": "Welcome to the Student Result Card API.",
|
342 |
+
"usage": "POST PDFs to /process with 'student_pdf' and 'answer_key_pdf' fields. Use ?download=true for file download or GET /download to re-download the JSON file."
|
343 |
}
|
344 |
|
|
|
345 |
if __name__ == "__main__":
|
346 |
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|