File size: 1,460 Bytes
2a0cd88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, Any, List
import os

current_dir = os.getcwd()
os.environ['HF_HOME'] = os.path.join(current_dir)

from marker.convert import convert_single_pdf
from marker.logger import configure_logging
from marker.models import load_all_models
from marker.output import save_markdown
from io import BytesIO
class EndpointHandler:
    def __init__(self, path=""):
        # Initialize the OCR model 
        self.models = load_all_models()
        self.file_location = "input/temp.pdf"
        os.makedirs("input", exist_ok=True)

    def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
        """

        data args:

            max_pages (:obj: int): The maximum number of pages to process.

            file (:obj: UploadFile): The uploaded PDF file.

        Return:

            A list of dictionaries containing the extracted text.

        """
        # Get inputs
        self.upload_file(data['file'])
        pdf_path = self.file_location
        max_pages = data.get("max_pages", None)

        # Perform OCR on the input PDF
        extracted_text, _, _ = convert_single_pdf(pdf_path, self.models, max_pages=max_pages, langs=["vi"])
        # Return the extracted text
        return [{"extracted_text": extracted_text}]

    def upload_file(self, file: BytesIO, max_pages: int = None):
        with open(self.file_location, "wb") as f:
            f.write(file.read())
        return True