Spaces:
Running
Running
File size: 12,050 Bytes
641b53a 10ea2c4 641b53a ae86c40 641b53a ce71763 641b53a 10ea2c4 641b53a ce71763 ae86c40 641b53a d5149c9 ce71763 641b53a ce71763 d5149c9 ae86c40 ce71763 641b53a d5149c9 ce71763 641b53a ce71763 641b53a d5149c9 ce71763 641b53a 0315b16 641b53a | 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | '''
Multi-format support (PDF, DOCX, TXT, MD, HTML)
- Intelligent chunking with overlap
- Metadata extraction (title, author, date, file type)
- Text cleaning and normalization
- Duplicate detection
'''
import hashlib
import os
import re
from datetime import datetime
from typing import Any, Dict, List, Optional, Protocol
import PyPDF2
import tiktoken
from bs4 import BeautifulSoup
from docx import Document
class _RegexTokenizer:
"""Offline fallback tokenizer when tiktoken encoding cannot be loaded."""
_token_pattern = re.compile(r"\w+|[^\w\s]", re.UNICODE)
def encode(self, text: str) -> List[str]:
return self._token_pattern.findall(text)
def decode(self, token_ids: List[str]) -> str:
if not token_ids:
return ""
return " ".join(token_ids)
class _Tokenizer(Protocol):
def encode(self, text: str) -> List[Any]:
...
def decode(self, token_ids: List[Any]) -> str:
...
class DocumentProcessor:
def __init__(
self,
chunk_size: int = 600,
overlap: int = 100,
tokenizer_name: str = "gpt2",
chunk_strategy: str = "tiktoken",
):
if chunk_size <= 0:
raise ValueError("chunk_size must be > 0")
if overlap < 0:
raise ValueError("overlap must be >= 0")
if overlap >= chunk_size:
raise ValueError("overlap must be smaller than chunk_size")
self.chunk_size = chunk_size
self.overlap = overlap
self.tokenizer_name = tokenizer_name
self.chunk_strategy = (chunk_strategy or "tiktoken").strip().lower()
self._tokenizer: _Tokenizer
try:
self._tokenizer = tiktoken.get_encoding(tokenizer_name)
except Exception:
self._tokenizer = _RegexTokenizer()
self._seen_hashes: set = set()
def process_document(self, file_path: str) -> Optional[Dict]:
text = self.extract_text(file_path)
if self._is_duplicate(text):
return None
metadata = self.extract_metadata(file_path)
cleaned_text = self.clean_text(text)
chunks = self.chunk_text(cleaned_text)
return {
'metadata': metadata,
'chunks': chunks,
}
def _is_duplicate(self, text: str) -> bool:
digest = hashlib.sha256(text.encode('utf-8')).hexdigest()
if digest in self._seen_hashes:
return True
self._seen_hashes.add(digest)
return False
def extract_text(self, file_path: str) -> str:
ext = os.path.splitext(file_path)[1].lower()
extractors = {
'.pdf': self._extract_pdf_text,
'.docx': self._extract_docx_text,
'.txt': self._extract_plain_text,
'.md': self._extract_plain_text,
'.html': self._extract_html_text,
}
extractor = extractors.get(ext)
if extractor is None:
raise ValueError(f"Unsupported file type: {ext!r}")
return extractor(file_path)
def extract_metadata(self, file_path: str) -> Dict:
ext = os.path.splitext(file_path)[1].lower()
base = {
'title': os.path.basename(file_path),
'author': 'Unknown',
'date': None,
'file_type': ext,
}
if ext == '.pdf':
base.update(self._pdf_metadata(file_path))
elif ext == '.docx':
base.update(self._docx_metadata(file_path))
if base['date'] is None:
base['date'] = datetime.now().isoformat()
return base
def clean_text(self, text: str) -> str:
text = re.sub(r'\s+', ' ', text)
return text.strip()
def chunk_text(self, text: str) -> List[str]:
strategy = self.chunk_strategy
if strategy == "tiktoken":
return self._chunk_text_token_window(text)
if strategy == "spacy":
return self._chunk_text_spacy(text)
if strategy == "nltk":
return self._chunk_text_nltk(text)
if strategy == "medical":
return self._chunk_text_domain(text, domain="medical")
if strategy == "legal":
return self._chunk_text_domain(text, domain="legal")
raise ValueError(f"Unknown chunking strategy: {strategy!r}")
def _chunk_text_token_window(self, text: str) -> List[str]:
token_ids = self._tokenizer.encode(text)
if not token_ids:
return []
chunks: List[str] = []
step = self.chunk_size - self.overlap
start = 0
while start < len(token_ids):
end = min(start + self.chunk_size, len(token_ids))
chunk = self._tokenizer.decode(token_ids[start:end])
if chunk.strip():
chunks.append(chunk)
start += step
return chunks
def _chunk_text_spacy(self, text: str) -> List[str]:
sentences = self._sentences_spacy(text)
return self._chunk_sentences(sentences)
def _sentences_spacy(self, text: str) -> List[str]:
try:
import spacy # noqa: PLC0415
except Exception as exc: # pragma: no cover - depends on optional runtime deps
raise ValueError("spaCy is not installed. Install spaCy and an English model.") from exc
nlp = None
load_errors: List[str] = []
for model_name in ("en_core_web_sm", "en_core_web_md", "en_core_web_lg"):
try:
nlp = spacy.load(model_name, disable=["ner", "lemmatizer", "textcat"])
break
except Exception as exc: # pragma: no cover - environment dependent
load_errors.append(f"{model_name}: {exc}")
if nlp is None:
try:
nlp = spacy.blank("en")
nlp.add_pipe("sentencizer")
except Exception as exc: # pragma: no cover
details = "; ".join(load_errors)
raise ValueError(f"spaCy sentence pipeline unavailable. {details}") from exc
doc = nlp(text)
return [s.text.strip() for s in doc.sents if s.text.strip()]
def _chunk_text_nltk(self, text: str) -> List[str]:
sentences = self._sentences_nltk(text)
return self._chunk_sentences(sentences)
def _sentences_nltk(self, text: str) -> List[str]:
try:
import nltk # noqa: PLC0415
from nltk.tokenize import sent_tokenize # noqa: PLC0415
except Exception as exc: # pragma: no cover
raise ValueError("NLTK is not installed. Install nltk package.") from exc
try:
sentences = sent_tokenize(text)
except LookupError:
nltk.download("punkt", quiet=True)
try:
nltk.download("punkt_tab", quiet=True)
except Exception:
pass
sentences = sent_tokenize(text)
return [s.strip() for s in sentences if s.strip()]
def _chunk_text_domain(self, text: str, *, domain: str) -> List[str]:
if domain == "medical":
boundaries = re.compile(
(
r"(?i)\b(history of present illness|assessment and plan|chief complaint|"
r"diagnosis|medications|allergies|impression|plan)\b"
)
)
else:
boundaries = re.compile(
r"(?i)\b(section\s+\d+|article\s+\d+|clause\s+\d+|whereas|hereby|pursuant to|party|agreement)\b"
)
sentences = self._split_sentences_with_nlp_fallback(text)
if not sentences:
return self._chunk_text_token_window(text)
units: List[str] = []
current: List[str] = []
for sentence in sentences:
if boundaries.search(sentence) and current:
units.append(" ".join(current).strip())
current = [sentence]
else:
current.append(sentence)
if current:
units.append(" ".join(current).strip())
return self._chunk_sentences(units)
def _split_sentences_with_nlp_fallback(self, text: str) -> List[str]:
try:
sentences = self._sentences_spacy(text)
if sentences:
return sentences
except Exception:
pass
try:
sentences = self._sentences_nltk(text)
if sentences:
return sentences
except Exception:
pass
try:
return [s.strip() for s in re.split(r"(?<=[.!?])\s+", text) if s.strip()]
except Exception:
return [text.strip()] if text.strip() else []
def _chunk_sentences(self, sentences: List[str]) -> List[str]:
if not sentences:
return []
chunks: List[str] = []
current: List[str] = []
current_tokens = 0
overlap_tail: List[str] = []
for sentence in sentences:
sent_tokens = max(1, self.count_tokens(sentence))
if current and (current_tokens + sent_tokens) > self.chunk_size:
chunk_text = " ".join(current).strip()
if chunk_text:
chunks.append(chunk_text)
overlap_tail = self._overlap_tail_sentences(current)
current = list(overlap_tail)
current_tokens = sum(max(1, self.count_tokens(s)) for s in current)
current.append(sentence)
current_tokens += sent_tokens
final_chunk = " ".join(current).strip()
if final_chunk:
chunks.append(final_chunk)
return chunks
def _overlap_tail_sentences(self, sentences: List[str]) -> List[str]:
if self.overlap <= 0 or not sentences:
return []
tail: List[str] = []
token_count = 0
for sentence in reversed(sentences):
s_tokens = max(1, self.count_tokens(sentence))
if token_count + s_tokens > self.overlap and tail:
break
tail.append(sentence)
token_count += s_tokens
if token_count >= self.overlap:
break
tail.reverse()
return tail
def count_tokens(self, text: str) -> int:
return len(self._tokenizer.encode(text))
# --- private extractors ---
def _extract_pdf_text(self, file_path: str) -> str:
with open(file_path, 'rb') as f:
reader = PyPDF2.PdfReader(f)
return ''.join(page.extract_text() or '' for page in reader.pages)
def _extract_docx_text(self, file_path: str) -> str:
doc = Document(file_path)
return '\n'.join(para.text for para in doc.paragraphs)
def _extract_plain_text(self, file_path: str) -> str:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
def _extract_html_text(self, file_path: str) -> str:
with open(file_path, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f, 'html.parser')
return soup.get_text(separator=' ')
# --- private metadata helpers ---
def _pdf_metadata(self, file_path: str) -> Dict:
result = {}
try:
with open(file_path, 'rb') as f:
info: Dict = dict(PyPDF2.PdfReader(f).metadata or {})
if info.get('/Title'):
result['title'] = info['/Title']
if info.get('/Author'):
result['author'] = info['/Author']
if info.get('/CreationDate'):
result['date'] = info['/CreationDate']
except Exception:
pass
return result
def _docx_metadata(self, file_path: str) -> Dict:
result = {}
try:
props = Document(file_path).core_properties
if props.title:
result['title'] = props.title
if props.author:
result['author'] = props.author
if props.created:
result['date'] = props.created.isoformat()
except Exception:
pass
return result
|