CoolFace
Apppublic

SolusOps/Study-with-ChampAI

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
ingestion.py52 linesDownload Raw Back to services
1from __future__ import annotations2import os3import base644from typing import Tuple5 6def load_file(path: str) -> Tuple[str, str]:7    """8    Returns (text, image_b64).9    - text: extracted text layer (empty if image-only)10    - image_b64: base64 image (empty if plain text)11    Callers decide which to use. For PDF: both may be non-empty.12    """13    if not os.path.exists(path):14        raise FileNotFoundError(f"File not found: {path}")15    ext = os.path.splitext(path)[1].lower()16    if ext in {".txt", ".md"}:17        return _read_text(path), ""18    if ext == ".pdf":19        return _pdf_text(path), _pdf_first_page_b64(path)20    if ext in {".png", ".jpg", ".jpeg", ".webp"}:21        return "", _image_b64(path)22    return _read_text(path), ""23 24def _read_text(path: str) -> str:25    with open(path, "r", encoding="utf-8", errors="replace") as f:26        return f.read()27 28def _pdf_text(path: str) -> str:29    try:30        import fitz31        doc = fitz.open(path)32        text = "\n".join(page.get_text("text") for page in doc).strip()33        doc.close()34        return text35    except Exception:36        return ""37 38def _pdf_first_page_b64(path: str) -> str:39    try:40        import fitz41        doc = fitz.open(path)42        pix = doc[0].get_pixmap(dpi=150)43        b64 = base64.b64encode(pix.tobytes("png")).decode("utf-8")44        doc.close()45        return b6446    except Exception:47        return ""48 49def _image_b64(path: str) -> str:50    with open(path, "rb") as f:51        return base64.b64encode(f.read()).decode("utf-8")52