Hammad712 commited on
Commit
9680b8f
·
verified ·
1 Parent(s): 5d4da82

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -24
main.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import shutil
3
  from fastapi import FastAPI, UploadFile, File, HTTPException
4
  from fastapi.responses import JSONResponse
 
5
  from pdf_processor import extract_answers_from_pdf, evaluate_student
6
 
7
  app = FastAPI()
@@ -14,10 +15,11 @@ if not os.path.exists(UPLOAD_DIR):
14
  @app.get("/")
15
  async def root():
16
  """
17
- Root endpoint that provides a welcome message and basic instructions.
18
  """
19
- return {"message": "Welcome to the PDF Processing API. Use '/extract/' to extract answers from a PDF or '/evaluate/' to evaluate student answers."}
20
-
 
21
 
22
  @app.post("/extract/")
23
  async def extract_pdf(file: UploadFile = File(...)):
@@ -38,33 +40,16 @@ async def extract_pdf(file: UploadFile = File(...)):
38
  os.remove(file_location)
39
 
40
  @app.post("/evaluate/")
41
- async def evaluate_pdfs(answer_key_file: UploadFile = File(...), student_file: UploadFile = File(...)):
42
  """
43
- Endpoint to evaluate student answers by comparing the answer key and student's answer PDFs.
 
44
  """
45
  try:
46
- answer_key_path = os.path.join(UPLOAD_DIR, answer_key_file.filename)
47
- student_path = os.path.join(UPLOAD_DIR, student_file.filename)
48
-
49
- with open(answer_key_path, "wb") as f:
50
- shutil.copyfileobj(answer_key_file.file, f)
51
- with open(student_path, "wb") as f:
52
- shutil.copyfileobj(student_file.file, f)
53
-
54
- # Extract answers from both PDFs.
55
- answer_key_result = extract_answers_from_pdf(answer_key_path)
56
- student_result = extract_answers_from_pdf(student_path)
57
-
58
- # Evaluate the student answers.
59
- evaluation = evaluate_student(answer_key_result, student_result)
60
  return JSONResponse(content=evaluation.model_dump())
61
  except Exception as e:
62
  raise HTTPException(status_code=500, detail=str(e))
63
- finally:
64
- if os.path.exists(answer_key_path):
65
- os.remove(answer_key_path)
66
- if os.path.exists(student_path):
67
- os.remove(student_path)
68
 
69
  if __name__ == "__main__":
70
  import uvicorn
 
2
  import shutil
3
  from fastapi import FastAPI, UploadFile, File, HTTPException
4
  from fastapi.responses import JSONResponse
5
+ from models import EvaluationRequest
6
  from pdf_processor import extract_answers_from_pdf, evaluate_student
7
 
8
  app = FastAPI()
 
15
  @app.get("/")
16
  async def root():
17
  """
18
+ Root endpoint that provides a welcome message.
19
  """
20
+ return {
21
+ "message": "Welcome to the PDF Processing API. Use '/extract/' to extract answers from a PDF or '/evaluate/' to calculate marks using pre-extracted answers."
22
+ }
23
 
24
  @app.post("/extract/")
25
  async def extract_pdf(file: UploadFile = File(...)):
 
40
  os.remove(file_location)
41
 
42
  @app.post("/evaluate/")
43
+ async def evaluate(evaluation_request: EvaluationRequest):
44
  """
45
+ Endpoint to evaluate student answers.
46
+ Expects a JSON payload with the pre-extracted answer key and student answers.
47
  """
48
  try:
49
+ evaluation = evaluate_student(evaluation_request.answer_key, evaluation_request.student)
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  return JSONResponse(content=evaluation.model_dump())
51
  except Exception as e:
52
  raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
53
 
54
  if __name__ == "__main__":
55
  import uvicorn