Update handler.py
Browse files- handler.py +45 -39
handler.py
CHANGED
@@ -1,40 +1,46 @@
|
|
1 |
-
from typing import Dict, Any, List
|
2 |
-
import os
|
3 |
-
|
4 |
-
current_dir = os.getcwd()
|
5 |
-
os.environ['HF_HOME'] = os.path.join(current_dir)
|
6 |
-
|
7 |
-
from marker.convert import convert_single_pdf
|
8 |
-
from marker.logger import configure_logging
|
9 |
-
from marker.models import load_all_models
|
10 |
-
from marker.output import save_markdown
|
11 |
-
from io import BytesIO
|
12 |
-
class EndpointHandler:
|
13 |
-
def __init__(self, path=""):
|
14 |
-
# Initialize the OCR model
|
15 |
-
self.models = load_all_models()
|
16 |
-
self.file_location = "input/temp.pdf"
|
17 |
-
os.makedirs("input", exist_ok=True)
|
18 |
-
|
19 |
-
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
20 |
-
"""
|
21 |
-
data args:
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
max_pages =
|
31 |
-
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
return True
|
|
|
1 |
+
from typing import Dict, Any, List
|
2 |
+
import os
|
3 |
+
|
4 |
+
current_dir = os.getcwd()
|
5 |
+
os.environ['HF_HOME'] = os.path.join(current_dir)
|
6 |
+
|
7 |
+
from marker.convert import convert_single_pdf
|
8 |
+
from marker.logger import configure_logging
|
9 |
+
from marker.models import load_all_models
|
10 |
+
from marker.output import save_markdown
|
11 |
+
from io import BytesIO
|
12 |
+
class EndpointHandler:
|
13 |
+
def __init__(self, path=""):
|
14 |
+
# Initialize the OCR model
|
15 |
+
self.models = load_all_models()
|
16 |
+
self.file_location = "input/temp.pdf"
|
17 |
+
os.makedirs("input", exist_ok=True)
|
18 |
+
|
19 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
20 |
+
"""
|
21 |
+
data args:
|
22 |
+
inputs (:obj: dict): A dictionary containing the inputs.
|
23 |
+
max_pages (:obj: int): The maximum number of pages to process.
|
24 |
+
file (:obj: str): The base64-encoded PDF file content.
|
25 |
+
Return:
|
26 |
+
A list of dictionaries containing the extracted text.
|
27 |
+
"""
|
28 |
+
inputs = data.get("inputs", {})
|
29 |
+
file_content = inputs.get("file")
|
30 |
+
max_pages = inputs.get("max_pages", None)
|
31 |
+
|
32 |
+
# Decode the base64-encoded file content
|
33 |
+
file_bytes = base64.b64decode(file_content)
|
34 |
+
self.upload_file(BytesIO(file_bytes))
|
35 |
+
|
36 |
+
pdf_path = self.file_location
|
37 |
+
|
38 |
+
# Perform OCR on the input PDF
|
39 |
+
extracted_text, _, _ = convert_single_pdf(pdf_path, self.models, max_pages=max_pages, langs=["vi"])
|
40 |
+
# Return the extracted text
|
41 |
+
return [{"extracted_text": extracted_text}]
|
42 |
+
|
43 |
+
def upload_file(self, file: BytesIO, max_pages: int = None):
|
44 |
+
with open(self.file_location, "wb") as f:
|
45 |
+
f.write(file.read())
|
46 |
return True
|