|
import os |
|
import shutil |
|
from fastapi import FastAPI, UploadFile, File, HTTPException |
|
from fastapi.responses import JSONResponse |
|
from models import EvaluationRequest |
|
from pdf_processor import extract_answers_from_pdf, evaluate_student |
|
|
|
app = FastAPI() |
|
|
|
|
|
UPLOAD_DIR = "uploads" |
|
if not os.path.exists(UPLOAD_DIR): |
|
os.makedirs(UPLOAD_DIR) |
|
|
|
@app.get("/") |
|
async def root(): |
|
""" |
|
Root endpoint that provides a welcome message. |
|
""" |
|
return { |
|
"message": "Welcome to the PDF Processing API. Use '/extract/' to extract answers from a PDF or '/evaluate/' to calculate marks using pre-extracted answers." |
|
} |
|
|
|
@app.post("/extract/") |
|
async def extract_pdf(file: UploadFile = File(...)): |
|
""" |
|
Endpoint to extract answers from a PDF file. |
|
""" |
|
try: |
|
file_location = os.path.join(UPLOAD_DIR, file.filename) |
|
with open(file_location, "wb") as f: |
|
shutil.copyfileobj(file.file, f) |
|
|
|
result = extract_answers_from_pdf(file_location) |
|
return JSONResponse(content=result.model_dump()) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
finally: |
|
if os.path.exists(file_location): |
|
os.remove(file_location) |
|
|
|
@app.post("/evaluate/") |
|
async def evaluate(evaluation_request: EvaluationRequest): |
|
""" |
|
Endpoint to evaluate student answers. |
|
Expects a JSON payload with the pre-extracted answer key and student answers. |
|
""" |
|
try: |
|
evaluation = evaluate_student(evaluation_request.answer_key, evaluation_request.student) |
|
return JSONResponse(content=evaluation.model_dump()) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |
|
|