From eb4f8021223e33dbe00726e66dd7d243440da9db Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 4 Nov 2024 00:05:19 -0800 Subject: [PATCH] feat: API server file upload support --- .gitignore | 1 + marker_server.py | 148 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 119 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 933a116e..93a6edea 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ wandb report.json benchmark_data debug_data +uploads # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/marker_server.py b/marker_server.py index 4387838d..58155949 100644 --- a/marker_server.py +++ b/marker_server.py @@ -14,12 +14,20 @@ from typing import Optional, Annotated import io -from fastapi import FastAPI, Body +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: @@ -33,10 +41,11 @@ async def lifespan(app: FastAPI): app = FastAPI(lifespan=lifespan) + @app.get("/") async def root(): return HTMLResponse( -""" + """

Marker API