-
Notifications
You must be signed in to change notification settings - Fork 1k
/
marker_server.py
263 lines (215 loc) · 7.41 KB
/
marker_server.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import argparse
import asyncio
import os
import requests
import uvicorn
from pydantic import BaseModel, Field
from starlette.responses import HTMLResponse
os.environ["PDFTEXT_CPU_WORKERS"] = "1"
import base64
from contextlib import asynccontextmanager
from typing import Optional, Annotated
import io
from fastapi import FastAPI, Body, Form, File, UploadFile, HTTPException
from marker.convert import convert_single_pdf
from marker.models import load_all_models
app_data = {}
UPLOAD_DIRECTORY = "./uploads" # Directory to store uploaded files
# Ensure the upload directory exists
if not os.path.exists(UPLOAD_DIRECTORY):
os.makedirs(UPLOAD_DIRECTORY)
@asynccontextmanager
async def lifespan(app: FastAPI):
if app.state.LOCAL:
app_data["models"] = load_all_models()
yield
if "models" in app_data:
del app_data["models"]
app = FastAPI(lifespan=lifespan)
@app.get("/")
async def root():
return HTMLResponse(
"""
<h1>Marker API</h1>
<ul>
<li><a href="/docs">API Documentation</a></li>
<li><a href="/marker">Run marker (post request only)</a></li>
</ul>
"""
)
class CommonParams(BaseModel):
filepath: Annotated[
Optional[str], Field(description="The path to the PDF file to convert.")
]
max_pages: Annotated[
Optional[int],
Field(
description="The maximum number of pages in the document to convert.",
example=None,
),
] = None
langs: Annotated[
Optional[str],
Field(
description="The optional languages to use if OCR is needed, comma separated. Must be either the names or codes from from https://github.com/VikParuchuri/surya/blob/master/surya/languages.py.",
example=None,
),
] = None
force_ocr: Annotated[
bool,
Field(
description="Force OCR on all pages of the PDF. Defaults to False. This can lead to worse results if you have good text in your PDFs (which is true in most cases)."
),
] = False
paginate: Annotated[
bool,
Field(
description="Whether to paginate the output. Defaults to False. If set to True, each page of the output will be separated by a horizontal rule that contains the page number (2 newlines, {PAGE_NUMBER}, 48 - characters, 2 newlines)."
),
] = False
extract_images: Annotated[
bool,
Field(
description="Whether to extract images from the PDF. Defaults to True. If set to False, no images will be extracted from the PDF."
),
] = True
@app.post("/marker")
async def convert_pdf(params: CommonParams):
if app.state.LOCAL:
print(f"Converting {params.filepath} locally.")
assert all(
[
params.extract_images is True,
params.paginate is False,
]
), "Local conversion API does not support image extraction or pagination."
return await convert_pdf_local(params)
else:
print(f"Converting {params.filepath} using the Datalab API.")
return await convert_pdf_remote(params)
@app.post("/marker/upload")
async def convert_pdf_upload(
max_pages: Optional[int] = Form(default=None),
langs: Optional[str] = Form(default=None),
force_ocr: Optional[bool] = Form(default=False),
paginate: Optional[bool] = Form(default=False),
extract_images: Optional[bool] = Form(default=True),
file: UploadFile = File(
..., description="The PDF file to convert.", media_type="application/pdf"
),
):
params = CommonParams(
filepath=None,
max_pages=max_pages,
langs=langs,
force_ocr=force_ocr,
paginate=paginate,
extract_images=extract_images,
)
print(f"Converting the uploaded PDF file: {file.filename}")
return await convert_pdf_from_upload(file, params)
async def convert_pdf_from_upload(file: UploadFile, params: CommonParams):
# Check that the uploaded file is a PDF
if file.content_type != "application/pdf":
raise HTTPException(status_code=400, detail="Only PDF files are allowed.")
try:
# Save the uploaded file in the ./uploads directory
upload_path = os.path.join(UPLOAD_DIRECTORY, file.filename)
with open(upload_path, "wb") as upload_file:
file_contents = await file.read()
upload_file.write(file_contents)
# Proceed with the conversion using the saved file path
params.filepath = upload_path
if app.state.LOCAL:
return await convert_pdf_local(params)
else:
return await convert_pdf_remote(params)
except Exception as e:
return {
"success": False,
"error": str(e),
}
finally:
# Clean up the uploaded file after processing
if os.path.exists(upload_path):
os.remove(upload_path)
async def convert_pdf_remote(params: CommonParams):
with open(params.filepath, "rb") as f:
filedata = f.read()
filename = os.path.basename(params.filepath)
form_data = {
"file": (filename, filedata, "application/pdf"),
"max_pages": (None, params.max_pages),
"langs": (None, params.langs),
"force_ocr": (None, params.force_ocr),
"paginate": (None, params.paginate),
"extract_images": (None, params.extract_images),
}
headers = {"X-API-Key": app.state.API_KEY}
response = requests.post(app.state.DATALAB_URL, files=form_data, headers=headers)
data = response.json()
check_url = data["request_check_url"]
for i in range(300):
await asyncio.sleep(2)
response = requests.get(check_url, headers=headers)
data = response.json()
if data["status"] == "complete":
break
return data
async def convert_pdf_local(params: CommonParams):
try:
full_text, images, metadata = convert_single_pdf(
params.filepath,
app_data["models"],
max_pages=params.max_pages,
langs=params.langs,
ocr_all_pages=params.force_ocr,
)
except Exception as e:
return {
"success": False,
"error": str(e),
}
encoded = {}
for k, v in images.items():
byte_stream = io.BytesIO()
v.save(byte_stream, format="PNG")
encoded[k] = base64.b64encode(byte_stream.getvalue()).decode("utf-8")
return {
"markdown": full_text,
"images": encoded,
"metadata": metadata,
"success": True,
}
def main():
parser = argparse.ArgumentParser(description="Convert PDFs to markdown.")
parser.add_argument(
"--port", type=int, default=8000, help="Port to run the server on"
)
parser.add_argument(
"--host", type=str, default="127.0.0.1", help="Host to run the server on"
)
parser.add_argument(
"--api_key",
type=str,
default=None,
help="API key for the Datalab API. If not specified, API will run locally.",
)
parser.add_argument(
"--datalab_url",
type=str,
default="https://api.datalab.to/api/v1/marker",
help="The URL for the Datalab API",
)
args = parser.parse_args()
app.state.API_KEY = args.api_key
app.state.LOCAL = args.api_key is None
app.state.DATALAB_URL = args.datalab_url
# Run the server
uvicorn.run(
app,
host=args.host,
port=args.port,
)
if __name__ == "__main__":
main()