forked from vx6Fid/VectorizeDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
215 lines (194 loc) · 8.85 KB
/
Copy pathprocessor.py
File metadata and controls
215 lines (194 loc) · 8.85 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import asyncio
import gc
import logging
import os
import traceback
from typing import Any, Dict, List
from gpu_embedder import embed_chunks
from utils.mongo_utils import vector_collection
from utils.pdf_processing import process_pdf
from utils.s3_utils import fetch_pdf, list_s3_pdfs
# Configure logging with timestamps and level
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger("tender_processor")
async def process_single_tender(payload: dict[str, Any]) -> dict[str, Any]:
tender_id = payload["tender_id"]
result = {
"tender_id": tender_id,
"processed_docs": 0,
"skipped_docs": 0,
"empty_docs": 0,
"scanned_pages": 0,
"regular_pages": 0,
"errors": [],
}
s3_prefix = f"tender-documents/{tender_id}/"
try:
logger.debug("Listing S3 PDFs with prefix: %s", s3_prefix)
pdf_keys = await list_s3_pdfs(s3_prefix)
logger.info("Found %d pdf keys for tender %s", len(pdf_keys), tender_id)
except Exception as e:
tb = traceback.format_exc()
logger.exception("Failed to list S3 PDFs for prefix %s: %s", s3_prefix, e)
result["errors"].append(f"list_s3_pdfs: {str(e)}\n{tb}")
return result
for pdf_key in pdf_keys:
document_name = os.path.basename(pdf_key)
logger.debug("Processing pdf_key=%s document_name=%s", pdf_key, document_name)
# Count documents in Mongo in a thread to avoid blocking the event loop
try:
logger.debug(
"Checking for existing documents in Mongo for %s / %s",
tender_id,
document_name,
)
exists = await asyncio.to_thread(
lambda: vector_collection.count_documents(
{"tender_id": tender_id, "document_name": document_name}
)
)
logger.debug("Mongo count for %s: %s", document_name, exists)
if exists > 0:
result["skipped_docs"] += 1
logger.info(
"Skipping %s because it already exists in Mongo", document_name
)
continue
except Exception as e:
tb = traceback.format_exc()
logger.exception("Error checking Mongo for %s: %s", document_name, e)
result["errors"].append(f"mongo_count_{document_name}: {str(e)}\n{tb}")
# continue to next pdf rather than fail everything
continue
try:
logger.debug("Fetching PDF from S3: %s", pdf_key)
pdf_stream = await fetch_pdf(pdf_key)
logger.info("Fetched PDF %s (type=%s)", document_name, type(pdf_stream))
# process_pdf contains blocking PDF parsing; run it in a separate thread/process
# IMPORTANT: don't call asyncio.run from within an existing event loop / thread
logger.debug("Calling process_pdf in a thread for %s", document_name)
pdf_result = await asyncio.to_thread(process_pdf, pdf_stream)
logger.info("process_pdf finished for %s", document_name)
# Validate pdf_result structure
if not isinstance(pdf_result, dict):
raise TypeError(f"process_pdf returned non-dict: {type(pdf_result)}")
chunks: List[Dict[str, Any]] = pdf_result.get("chunks", [])
logger.debug(
"pdf_result contains %d chunks for %s", len(chunks), document_name
)
scanned_pages = pdf_result.get("scanned_pages", 0)
regular_pages = pdf_result.get("regular_pages", 0)
result["scanned_pages"] += scanned_pages
result["regular_pages"] += regular_pages
logger.info(
"Pages for %s -> scanned: %s, regular: %s",
document_name,
scanned_pages,
regular_pages,
)
if not chunks:
result["empty_docs"] += 1
logger.warning("No chunks extracted for %s", document_name)
continue
# Print a sample of the first chunk keys / text length to debug schema mismatch
try:
first = chunks[0]
logger.debug("First chunk keys: %s", list(first.keys()))
sample_text = (
first.get("text") or first.get("data") or "<no-text-field>"
)
logger.debug(
"First chunk sample length: %d",
len(sample_text) if isinstance(sample_text, str) else -1,
)
except Exception:
logger.exception("Failed to inspect first chunk for %s", document_name)
# Run embedding in a thread so heavy CPU/GPU work does not block the loop.
# If you have a GPU embedder (external) use it; otherwise use local embed_chunks below.
try:
logger.debug(
"Starting embedding for %s with %d chunks",
document_name,
len(chunks),
)
# If you have an external GPU embedder function, use it; otherwise fallback to local embed_chunks
# We call it in a thread to avoid blocking
embeddings = await asyncio.to_thread(embed_chunks, chunks)
logger.info(
"Embedding finished for %s: received %d embeddings",
document_name,
len(embeddings),
)
except Exception as e:
tb = traceback.format_exc()
logger.exception("Embedding failed for %s: %s", document_name, e)
result["errors"].append(f"embed_{document_name}: {str(e)}\n{tb}")
continue
# Build docs for Mongo insert
docs = []
try:
for c, emb in zip(chunks, embeddings):
# If embedding returns dicts (like our embed_chunks below), handle both shapes
if isinstance(emb, dict) and "embedding" in emb:
embedding_vector = emb["embedding"]
else:
embedding_vector = emb
doc = {
"tender_id": tender_id,
"document_name": document_name,
"text": c.get("text") or c.get("data") or "",
"embedding": embedding_vector.tolist()
if hasattr(embedding_vector, "tolist")
else embedding_vector,
# preserve chunk metadata if present:
"chunk_meta": {
"page": c.get("page"),
"position": c.get("position"),
"sub_position": c.get("sub_position"),
"type": c.get("type"),
"is_scanned": c.get("is_scanned"),
},
}
docs.append(doc)
logger.debug(
"Prepared %d docs for Mongo insert for %s", len(docs), document_name
)
except Exception as e:
tb = traceback.format_exc()
logger.exception(
"Failed to prepare docs for insert for %s: %s", document_name, e
)
result["errors"].append(f"prepare_docs_{document_name}: {str(e)}\n{tb}")
continue
# Insert into Mongo in a thread
try:
if docs:
logger.debug(
"Inserting %d docs into Mongo for %s", len(docs), document_name
)
await asyncio.to_thread(vector_collection.insert_many, docs)
result["processed_docs"] += 1
logger.info("Inserted docs into Mongo for %s", document_name)
else:
logger.warning("No docs to insert for %s", document_name)
except Exception as e:
tb = traceback.format_exc()
logger.exception("Mongo insert failed for %s: %s", document_name, e)
result["errors"].append(f"mongo_insert_{document_name}: {str(e)}\n{tb}")
continue
except Exception as e:
tb = traceback.format_exc()
logger.exception("Unhandled exception processing %s: %s", document_name, e)
result["errors"].append(f"{document_name}: {str(e)}\n{tb}")
finally:
# free memory regularly
try:
gc.collect()
logger.debug("gc.collect() called after processing %s", document_name)
except Exception:
logger.exception("gc.collect() failed for %s", document_name)
return result