File size: 2,385 Bytes
9aebc0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def _extract_pdf_text_worker(

    pdf_path: str,

    max_pages: int,

    max_chars: int,

    out_queue,

):
    """

    Lightweight PDF parser worker used by multiprocessing spawn.



    Keep this module free of FastAPI, LangChain, Torch, and Transformers imports.

    On Windows, spawn imports the target function's module in the child process;

    pointing at main.py would load the full RAG stack before parsing starts.

    """
    try:
        from pypdf import PdfReader

        reader = PdfReader(pdf_path, strict=False)

        if getattr(reader, "is_encrypted", False):
            try:
                reader.decrypt("")
            except Exception:
                out_queue.put({"ok": False, "error": "Unable to read this PDF. It may be encrypted."})
                return

        pages = getattr(reader, "pages", [])
        page_count = len(pages)
        if page_count == 0:
            out_queue.put({"ok": False, "error": "No readable pages were found in the PDF."})
            return
        if page_count > max_pages:
            out_queue.put(
                {
                    "ok": False,
                    "error": f"PDF has too many pages ({page_count}). Max allowed is {max_pages}.",
                    "page_count": page_count,
                }
            )
            return

        extracted = []
        used = 0
        for idx, page in enumerate(pages):
            if idx >= max_pages:
                break
            text = page.extract_text() or ""
            if not text.strip():
                continue

            remaining = max_chars - used
            if remaining <= 0:
                break
            if len(text) > remaining:
                text = text[:remaining]
            used += len(text)
            extracted.append({"page": idx, "text": text})

        if not extracted:
            out_queue.put({"ok": False, "error": "No readable text was found in the PDF."})
            return

        out_queue.put(
            {
                "ok": True,
                "page_count": page_count,
                "extracted": extracted,
                "extracted_chars": used,
            }
        )
    except Exception as exc:
        out_queue.put({"ok": False, "error": "Unable to read this PDF. It may be corrupted.", "details": str(exc)})