Spaces:
Sleeping
Sleeping
Upload src/labdaps/ingestion/pdf_extractor.py with huggingface_hub
Browse files
src/labdaps/ingestion/pdf_extractor.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import pdfplumber
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
@dataclass
|
| 7 |
+
class RawPage:
|
| 8 |
+
text: str
|
| 9 |
+
page_number: int
|
| 10 |
+
source_file: str
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def extract_pdf(pdf_path: Path) -> list[RawPage]:
|
| 14 |
+
pages = []
|
| 15 |
+
source_file = pdf_path.name
|
| 16 |
+
try:
|
| 17 |
+
with pdfplumber.open(pdf_path) as pdf:
|
| 18 |
+
for i, page in enumerate(pdf.pages, start=1):
|
| 19 |
+
text = (page.extract_text() or "").strip()
|
| 20 |
+
if len(text) > 50:
|
| 21 |
+
pages.append(RawPage(text=text, page_number=i, source_file=source_file))
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"[WARNING] Falha ao extrair {pdf_path.name}: {e}")
|
| 24 |
+
return pages
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def extract_all_pdfs(docs_dir: Path) -> list[RawPage]:
|
| 28 |
+
all_pages = []
|
| 29 |
+
for pdf_path in sorted(docs_dir.glob("*.pdf")):
|
| 30 |
+
all_pages.extend(extract_pdf(pdf_path))
|
| 31 |
+
return all_pages
|