Update main.py
Browse files
main.py
CHANGED
|
@@ -1,81 +1,63 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from typing import List
|
| 4 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
from langchain.document_loaders import PyPDFLoader
|
| 8 |
|
| 9 |
-
|
| 10 |
-
API_KEY = os.getenv("GROQ_API_KEY")
|
| 11 |
-
if not API_KEY:
|
| 12 |
-
raise ValueError("GROQ_API_KEY environment variable not set.")
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def get_llm():
|
| 22 |
-
return ChatGroq(
|
| 23 |
-
model="llama-3.3-70b-versatile",
|
| 24 |
-
temperature=0,
|
| 25 |
-
max_tokens=1024,
|
| 26 |
-
api_key=API_KEY
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
llm = get_llm()
|
| 30 |
-
|
| 31 |
-
# Root endpoint: Provides a welcome message and instructions
|
| 32 |
-
@app.get("/")
|
| 33 |
-
async def root():
|
| 34 |
-
return {
|
| 35 |
-
"message": "Welcome to the PDF Question Extractor API.",
|
| 36 |
-
"usage": "POST your PDF to /extract-answers/ to extract answers."
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
# PDF extraction endpoint: Processes a PDF file upload
|
| 40 |
-
@app.post("/extract-answers/")
|
| 41 |
-
async def extract_answers(file: UploadFile = File(...)):
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
os.remove(file_path)
|
| 77 |
-
|
| 78 |
-
return result.model_dump()
|
| 79 |
-
|
| 80 |
except Exception as e:
|
| 81 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
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()
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Directory to temporarily store uploaded files.
|
| 10 |
+
UPLOAD_DIR = "uploads"
|
| 11 |
+
if not os.path.exists(UPLOAD_DIR):
|
| 12 |
+
os.makedirs(UPLOAD_DIR)
|
| 13 |
|
| 14 |
+
@app.post("/extract/")
|
| 15 |
+
async def extract_pdf(file: UploadFile = File(...)):
|
| 16 |
+
"""
|
| 17 |
+
Endpoint to extract answers from a PDF file.
|
| 18 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
+
file_location = os.path.join(UPLOAD_DIR, file.filename)
|
| 21 |
+
with open(file_location, "wb") as f:
|
| 22 |
+
shutil.copyfileobj(file.file, f)
|
| 23 |
+
|
| 24 |
+
result = extract_answers_from_pdf(file_location)
|
| 25 |
+
return JSONResponse(content=result.model_dump())
|
| 26 |
+
except Exception as e:
|
| 27 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 28 |
+
finally:
|
| 29 |
+
if os.path.exists(file_location):
|
| 30 |
+
os.remove(file_location)
|
| 31 |
+
|
| 32 |
+
@app.post("/evaluate/")
|
| 33 |
+
async def evaluate_pdfs(answer_key_file: UploadFile = File(...), student_file: UploadFile = File(...)):
|
| 34 |
+
"""
|
| 35 |
+
Endpoint to evaluate student answers by comparing the answer key and student's answer PDFs.
|
| 36 |
+
"""
|
| 37 |
+
try:
|
| 38 |
+
answer_key_path = os.path.join(UPLOAD_DIR, answer_key_file.filename)
|
| 39 |
+
student_path = os.path.join(UPLOAD_DIR, student_file.filename)
|
| 40 |
+
|
| 41 |
+
with open(answer_key_path, "wb") as f:
|
| 42 |
+
shutil.copyfileobj(answer_key_file.file, f)
|
| 43 |
+
with open(student_path, "wb") as f:
|
| 44 |
+
shutil.copyfileobj(student_file.file, f)
|
| 45 |
+
|
| 46 |
+
# Extract answers from both PDFs.
|
| 47 |
+
answer_key_result = extract_answers_from_pdf(answer_key_path)
|
| 48 |
+
student_result = extract_answers_from_pdf(student_path)
|
| 49 |
+
|
| 50 |
+
# Evaluate the student answers.
|
| 51 |
+
evaluation = evaluate_student(answer_key_result, student_result)
|
| 52 |
+
return JSONResponse(content=evaluation.model_dump())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
raise HTTPException(status_code=500, detail=str(e))
|
| 55 |
+
finally:
|
| 56 |
+
if os.path.exists(answer_key_path):
|
| 57 |
+
os.remove(answer_key_path)
|
| 58 |
+
if os.path.exists(student_path):
|
| 59 |
+
os.remove(student_path)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
import uvicorn
|
| 63 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|