-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathfile_manipulator.py
More file actions
72 lines (54 loc) · 2.22 KB
/
Copy pathfile_manipulator.py
File metadata and controls
72 lines (54 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
import os
import logging
from src.filler import Filler
from src.llm import LLM
from commonforms import prepare_form
from src.utils.extraction_validator import ExtractionValidator
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class FileManipulator:
def __init__(self):
self.filler = Filler()
self.llm = LLM()
def create_template(self, pdf_path: str):
"""
By using commonforms, we create an editable .pdf template and store it.
"""
template_path = pdf_path[:-4] + "_template.pdf"
prepare_form(pdf_path, template_path)
return template_path
def fill_form(self, user_input: str, fields: list, pdf_form_path: str):
"""
Receives raw data, runs extraction + validation + PDF filling,
and returns the output file path with review flag.
"""
logger.info("[1] Received request from frontend.")
print(f"[2] PDF template path: {pdf_form_path}")
if not os.path.exists(pdf_form_path):
print(f"Error: PDF template not found at {pdf_form_path}")
return None, True
logger.info("[3] Starting extraction...")
try:
self.llm._target_fields = fields
self.llm._transcript_text = user_input
success = self.llm.extract_structured_safe()
if not success:
print("Structured extraction failed → fallback to old extraction")
self.llm.main_loop()
extracted_data = self.llm.get_data()
validator = ExtractionValidator()
validation_result = validator.validate(extracted_data)
review_flag = validation_result["requires_review"]
print("\n[4] Validation Result")
print(validation_result)
output_name = self.filler.fill_form(
pdf_form=pdf_form_path,
llm=self.llm
)
print("\n----------------------------------")
print("✅ Process Complete.")
print(f"Output saved to: {output_name}")
return output_name, review_flag
except Exception as e:
print(f"An error occurred during PDF generation: {e}")
raise e