Create pdf_processor.py
Browse files- pdf_processor.py +50 -0
pdf_processor.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from langchain.document_loaders import PyPDFLoader
|
| 3 |
+
from models import ExtractionResult, EvaluationResult
|
| 4 |
+
from llm import get_llm
|
| 5 |
+
|
| 6 |
+
llm = get_llm()
|
| 7 |
+
|
| 8 |
+
def extract_answers_from_pdf(pdf_path: str) -> ExtractionResult:
|
| 9 |
+
"""
|
| 10 |
+
Loads a PDF, extracts its content, and uses the LLM to output a JSON of the answers.
|
| 11 |
+
"""
|
| 12 |
+
loader = PyPDFLoader(pdf_path)
|
| 13 |
+
pages = loader.load_and_split()
|
| 14 |
+
all_page_content = "\n".join(page.page_content for page in pages)
|
| 15 |
+
|
| 16 |
+
# Build the system message with JSON schema instructions.
|
| 17 |
+
extraction_schema = ExtractionResult.model_json_schema()
|
| 18 |
+
system_message = (
|
| 19 |
+
"You are a document analysis tool that extracts the options and correct answers from the provided document content. "
|
| 20 |
+
"The output must be a JSON object that strictly follows the schema: " + json.dumps(extraction_schema, indent=2)
|
| 21 |
+
)
|
| 22 |
+
user_message = (
|
| 23 |
+
"Please extract the correct answers and options (A, B, C, D, E) from the following document content:\n\n"
|
| 24 |
+
+ all_page_content
|
| 25 |
+
)
|
| 26 |
+
prompt = system_message + "\n\n" + user_message
|
| 27 |
+
|
| 28 |
+
response = llm.invoke(prompt, response_format={"type": "json_object"})
|
| 29 |
+
result = ExtractionResult.model_validate_json(response.content)
|
| 30 |
+
return result
|
| 31 |
+
|
| 32 |
+
def evaluate_student(answer_key: ExtractionResult, student: ExtractionResult) -> EvaluationResult:
|
| 33 |
+
"""
|
| 34 |
+
Compares the answer key with a student's answers and returns the evaluation result.
|
| 35 |
+
"""
|
| 36 |
+
evaluation_schema = EvaluationResult.model_json_schema()
|
| 37 |
+
system_message = (
|
| 38 |
+
"You are an academic evaluation tool that compares the answer key with a student's answers. "
|
| 39 |
+
"Calculate the total marks, grade, and percentage based on the provided JSON objects. "
|
| 40 |
+
"The output must be a JSON object that strictly follows the schema: " + json.dumps(evaluation_schema, indent=2)
|
| 41 |
+
)
|
| 42 |
+
user_message = (
|
| 43 |
+
"Answer Key JSON:\n" + json.dumps(answer_key.model_dump(), indent=2) + "\n\n"
|
| 44 |
+
"Student Answer JSON:\n" + json.dumps(student.model_dump(), indent=2)
|
| 45 |
+
)
|
| 46 |
+
prompt = system_message + "\n\n" + user_message
|
| 47 |
+
|
| 48 |
+
response = llm.invoke(prompt, response_format={"type": "json_object"})
|
| 49 |
+
evaluation_result = EvaluationResult.model_validate_json(response.content)
|
| 50 |
+
return evaluation_result
|