from __future__ import annotations import os import base64 from typing import Tuple def load_file(path: str) -> Tuple[str, str]: """ Returns (text, image_b64). - text: extracted text layer (empty if image-only) - image_b64: base64 image (empty if plain text) Callers decide which to use. For PDF: both may be non-empty. """ if not os.path.exists(path): raise FileNotFoundError(f"File not found: {path}") ext = os.path.splitext(path)[1].lower() if ext in {".txt", ".md"}: return _read_text(path), "" if ext == ".pdf": return _pdf_text(path), _pdf_first_page_b64(path) if ext in {".png", ".jpg", ".jpeg", ".webp"}: return "", _image_b64(path) return _read_text(path), "" def _read_text(path: str) -> str: with open(path, "r", encoding="utf-8", errors="replace") as f: return f.read() def _pdf_text(path: str) -> str: try: import fitz doc = fitz.open(path) text = "\n".join(page.get_text("text") for page in doc).strip() doc.close() return text except Exception: return "" def _pdf_first_page_b64(path: str) -> str: try: import fitz doc = fitz.open(path) pix = doc[0].get_pixmap(dpi=150) b64 = base64.b64encode(pix.tobytes("png")).decode("utf-8") doc.close() return b64 except Exception: return "" def _image_b64(path: str) -> str: with open(path, "rb") as f: return base64.b64encode(f.read()).decode("utf-8")