Spaces:
Running
on
Zero
Running
on
Zero
Create marker/markdown_extractor.py
Browse files- marker/markdown_extractor.py +36 -0
marker/markdown_extractor.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from marker.convert import convert_single_pdf
|
2 |
+
from marker.models import load_all_models
|
3 |
+
import tempfile
|
4 |
+
from indexify_extractor_sdk import Content, Extractor, Feature
|
5 |
+
|
6 |
+
from pydantic import BaseModel
|
7 |
+
from typing import Optional, Literal, List, Union
|
8 |
+
|
9 |
+
class MarkdownExtractorConfig(BaseModel):
|
10 |
+
max_pages: Optional[int] = None
|
11 |
+
langs: Optional[str] = None
|
12 |
+
batch_multiplier: Optional[int] = 2
|
13 |
+
|
14 |
+
class MarkdownExtractor(Extractor):
|
15 |
+
name = "tensorlake/marker"
|
16 |
+
description = "Markdown Extractor for PDFs"
|
17 |
+
system_dependencies = []
|
18 |
+
input_mime_types = ["application/pdf"]
|
19 |
+
|
20 |
+
def __init__(self):
|
21 |
+
super(MarkdownExtractor, self).__init__()
|
22 |
+
self.model_lst = load_all_models()
|
23 |
+
|
24 |
+
def extract(self, content: Content, params: MarkdownExtractorConfig) -> List[Union[Feature, Content]]:
|
25 |
+
contents = []
|
26 |
+
|
27 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as inputtmpfile:
|
28 |
+
inputtmpfile.write(content.data)
|
29 |
+
inputtmpfile.flush()
|
30 |
+
|
31 |
+
full_text, images, out_meta = convert_single_pdf(inputtmpfile.name, self.model_lst, max_pages=params.max_pages, langs=params.langs, batch_multiplier=params.batch_multiplier)
|
32 |
+
|
33 |
+
feature = Feature.metadata(value=out_meta, name="text")
|
34 |
+
contents.append(Content.from_text(full_text, features=[feature]))
|
35 |
+
|
36 |
+
return contents
|