File size: 2,363 Bytes
19907be
 
7c1f442
19907be
 
 
1a272cd
19907be
f8aea0d
 
19907be
 
 
f8aea0d
7c1f442
 
f8aea0d
7c1f442
f8aea0d
 
7c1f442
 
f8aea0d
 
 
 
 
19907be
 
 
 
 
 
 
 
 
 
 
 
 
 
1a272cd
19907be
 
1a272cd
 
19907be
f8aea0d
 
19907be
 
f8aea0d
 
 
 
 
 
19907be
 
 
 
 
 
 
 
 
 
f8aea0d
19907be
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import shutil
import os
from uuid import uuid4
from docling.document_converter import DocumentConverter
from threading import Lock
from concurrent.futures import ThreadPoolExecutor
import asyncio

app = FastAPI()

# CORS for all
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Thread pool sized according to vCPU
MAX_WORKERS = os.cpu_count() or 2  # fallback to 2 if os.cpu_count() returns None
thread_pool = ThreadPoolExecutor(max_workers=MAX_WORKERS)

# Singleton class for DocumentConverter
class PdfConverterSingleton:
    _instance = None
    _lock = Lock()

    def __new__(cls):
        if cls._instance is None:
            with cls._lock:
                if cls._instance is None:
                    instance = super().__new__(cls)
                    instance._initialize()
                    cls._instance = instance
        return cls._instance

    def _initialize(self):
        self.converter = DocumentConverter()

    def get_text(self, pdf_path: str) -> str:
        result = self.converter.convert(pdf_path)
        return result.document.export_to_markdown()

# Run sync function in threadpool
def sync_extract_text(pdf_path: str) -> str:
    return PdfConverterSingleton().get_text(pdf_path)

# Async wrapper for thread pool
async def async_extract_text(pdf_path: str) -> str:
    loop = asyncio.get_event_loop()
    return await loop.run_in_executor(thread_pool, sync_extract_text, pdf_path)

# Main endpoint
@app.post("/extract-pdf-text")
async def extract_pdf_text(file: UploadFile = File(...)):
    if file.content_type != "application/pdf":
        raise HTTPException(status_code=400, detail="Only PDF files are supported.")

    temp_filename = f"/tmp/{uuid4().hex}.pdf"
    try:
        with open(temp_filename, "wb") as buffer:
            shutil.copyfileobj(file.file, buffer)

        text = await async_extract_text(temp_filename)
        return JSONResponse(content={"markdown_text": text})

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

    finally:
        if os.path.exists(temp_filename):
            os.remove(temp_filename)