-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_processing_utils.py
189 lines (162 loc) · 7.44 KB
/
file_processing_utils.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import base64
import logging
import requests
import time
from typing import List, Dict, Tuple, Optional
from dataclasses import dataclass
from unstructured.partition.pdf import partition_pdf
from langchain_core.messages import HumanMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class DocumentElements:
texts: List[str]
tables: List[str]
table_summaries: List[str]
image_base64: List[str]
image_summaries: List[str]
class DocumentProcessor:
def __init__(self, llm, api_base_url: str):
self.llm = llm
self.api_base_url = api_base_url
def partition_document(self, path: str, file_name: str, image_folder: str) -> List:
"""
Partition PDF document into elements using unstructured
"""
try:
raw_pdf_elements = partition_pdf(
filename=os.path.join(path, file_name),
chunking_strategy="by_title",
max_characters=4000,
new_after_n_chars=3800,
combine_text_under_n_chars=2000,
infer_table_structure=True,
strategy='hi_res',
extract_images_in_pdf=True,
extract_image_block_output_dir= image_folder
)
logger.info(f'Successfully Split {file_name}')
return raw_pdf_elements
except Exception as e:
logger.error(f"Error partitioning document: {str(e)}")
raise
def categorize_elements(self, raw_pdf_elements: List) -> Tuple[List[str], List[str]]:
"""
Categorize PDF elements into tables and texts
"""
tables = []
texts = []
for element in raw_pdf_elements:
if "unstructured.documents.elements.Table" in str(type(element)):
tables.append(str(element))
elif "unstructured.documents.elements.CompositeElement" in str(type(element)):
texts.append(str(element))
return texts, tables
def summarize_tables(self, tables: List[str], table_rate_limit: int = 10) -> List[str]:
"""
Summarize tables using LLM
"""
prompt_text = """You are an assistant tasked with summarizing tables. \
Give a concise summary of the table. Table chunk: {element}"""
prompt = ChatPromptTemplate.from_template(prompt_text)
summarize_chain = {"element": lambda x: x} | prompt | self.llm | StrOutputParser()
table_summaries = []
for idx, table in enumerate(tables, 1):
logger.info(f"Table Being Summarized: {idx}. Sleeping for {table_rate_limit}s to avoid rate limiting...")
try:
summary = summarize_chain.invoke(table)
table_summaries.append(summary)
if idx < len(tables): # Don't sleep after the last table
time.sleep(table_rate_limit)
except Exception as e:
logger.error(f"Error summarizing table {idx}: {str(e)}")
table_summaries.append(f"Error summarizing table: {str(e)}")
return table_summaries
def process_images(self, image_folder: str, image_rate_limit: int = 60) -> Tuple[List[str], List[str]]:
"""
Process and summarize images from the specified folder
"""
def encode_image(image_path: str) -> str:
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
prompt = "Describe the image in detail. Be specific about graphs, such as bar plots."
img_base64_list = []
image_summaries = []
image_files = [f for f in sorted(os.listdir(image_folder))
if f.endswith(('.jpg', '.png'))]
for idx, img_file in enumerate(image_files, 1):
try:
img_path = os.path.join(image_folder, img_file)
base64_image = encode_image(img_path)
img_base64_list.append(base64_image)
message = HumanMessage(content=[
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
])
logger.info(f"Image Being Summarized: {idx}. Sleeping for {image_rate_limit}s to avoid rate limiting...")
ai_msg = self.llm.invoke([message])
image_summaries.append(ai_msg.content)
if idx < len(image_files): # Don't sleep after the last image
time.sleep(image_rate_limit)
except Exception as e:
logger.error(f"Error processing image {img_file}: {str(e)}")
image_summaries.append(f"Error processing image: {str(e)}")
return img_base64_list, image_summaries
def push_to_api(self, doc_elements: DocumentElements) -> bool:
"""
Push processed document elements to API endpoints
"""
try:
if doc_elements.texts:
response = requests.post(
f"{self.api_base_url}/api/insert-text",
json={"texts": doc_elements.texts}
)
if response.status_code != 200:
logger.error(f"Failed to insert texts: {response.text}")
return False
if doc_elements.tables and doc_elements.table_summaries:
response = requests.post(
f"{self.api_base_url}/api/insert-table",
json={
"tables": doc_elements.tables,
"table_summaries": doc_elements.table_summaries
}
)
if response.status_code != 200:
logger.error(f"Failed to insert tables: {response.text}")
return False
if doc_elements.image_base64 and doc_elements.image_summaries:
response = requests.post(
f"{self.api_base_url}/api/insert-image",
json={
"image_b64_list": doc_elements.image_base64,
"image_summaries": doc_elements.image_summaries
}
)
if response.status_code != 200:
logger.error(f"Failed to insert images: {response.text}")
return False
logger.info("All data inserted successfully")
return True
except Exception as e:
logger.error(f"Error pushing data to API: {str(e)}")
return False
def process_document(self, path: str, file_name: str, image_folder: str = 'figures') -> DocumentElements:
"""
Main method to process a document end-to-end
"""
raw_elements = self.partition_document(path, file_name, image_folder=image_folder)
texts, tables = self.categorize_elements(raw_elements)
table_summaries = self.summarize_tables(tables)
img_base64_list, image_summaries = self.process_images(image_folder)
return DocumentElements(
texts=texts,
tables=tables,
table_summaries=table_summaries,
image_base64=img_base64_list,
image_summaries=image_summaries
)