-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_loader.py
More file actions
180 lines (137 loc) · 4.72 KB
/
document_loader.py
File metadata and controls
180 lines (137 loc) · 4.72 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
# document_loader.py
# ===================
# Loads pharma documents for the RAG system
# Supports PDF files and text documents
# Uses PyMuPDF for reliable PDF text extraction
import os
import fitz # PyMuPDF
from typing import List, Dict
def load_pdf(file_path: str) -> List[Dict]:
"""
Load a PDF file and extract text by page.
Args:
file_path: Path to the PDF file
Returns:
List of dictionaries with page text and metadata
"""
print(f"Loading PDF: {file_path}")
try:
doc = fitz.open(file_path)
pages = []
for page_num in range(len(doc)):
page = doc[page_num]
text = page.get_text()
# Skip empty pages
if len(text.strip()) < 50:
continue
pages.append({
"text": text,
"page_number": page_num + 1,
"source": os.path.basename(file_path),
"file_path": file_path
})
doc.close()
print(f"Extracted {len(pages)} pages from {os.path.basename(file_path)}")
return pages
except Exception as e:
print(f"Error loading PDF: {e}")
return []
def load_pdf_from_bytes(file_bytes: bytes, filename: str) -> List[Dict]:
"""
Load a PDF from bytes (for Streamlit file uploads).
Args:
file_bytes: PDF file as bytes
filename: Name of the file
Returns:
List of dictionaries with page text and metadata
"""
print(f"Loading uploaded PDF: {filename}")
try:
doc = fitz.open(stream=file_bytes, filetype="pdf")
pages = []
for page_num in range(len(doc)):
page = doc[page_num]
text = page.get_text()
if len(text.strip()) < 50:
continue
pages.append({
"text": text,
"page_number": page_num + 1,
"source": filename,
"file_path": filename
})
doc.close()
print(f"Extracted {len(pages)} pages from {filename}")
return pages
except Exception as e:
print(f"Error loading PDF bytes: {e}")
return []
def chunk_pages(pages: List[Dict],
chunk_size: int = 1000,
chunk_overlap: int = 100) -> List[Dict]:
"""
Split pages into smaller chunks for better RAG retrieval.
Args:
pages: List of page dictionaries
chunk_size: Maximum characters per chunk
chunk_overlap: Characters to overlap between chunks
Returns:
List of chunk dictionaries
"""
chunks = []
for page in pages:
text = page["text"]
# If page is short enough — keep as single chunk
if len(text) <= chunk_size:
chunks.append({
"text": text,
"page_number": page["page_number"],
"source": page["source"],
"chunk_id": f"{page['source']}_p{page['page_number']}_c0"
})
continue
# Split long pages into overlapping chunks
start = 0
chunk_num = 0
while start < len(text):
end = start + chunk_size
chunk_text = text[start:end]
chunks.append({
"text": chunk_text,
"page_number": page["page_number"],
"source": page["source"],
"chunk_id": f"{page['source']}_p{page['page_number']}_c{chunk_num}"
})
start = end - chunk_overlap
chunk_num += 1
print(f"Created {len(chunks)} chunks from {len(pages)} pages")
return chunks
def get_document_stats(pages: List[Dict]) -> Dict:
"""
Get statistics about a loaded document.
"""
if not pages:
return {"pages": 0, "total_chars": 0, "avg_chars_per_page": 0}
total_chars = sum(len(p["text"]) for p in pages)
return {
"pages": len(pages),
"total_chars": total_chars,
"avg_chars_per_page": total_chars // len(pages),
"source": pages[0]["source"]
}
# Quick test
if __name__ == "__main__":
# Test with a sample text to verify the module works
sample_pages = [
{
"text": "This is a sample pharma document. " * 50,
"page_number": 1,
"source": "test_document.pdf",
"file_path": "test_document.pdf"
}
]
chunks = chunk_pages(sample_pages)
stats = get_document_stats(sample_pages)
print(f"Test passed!")
print(f"Stats: {stats}")
print(f"Chunks created: {len(chunks)}")