From c34cf8f31ed0f043ecc251f8688452467701833c Mon Sep 17 00:00:00 2001 From: Vik Paruchuri Date: Thu, 31 Oct 2024 09:27:12 -0400 Subject: [PATCH] Cleanup server --- README.md | 14 ++++++++++++++ marker_server.py | 41 +++++++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 11736eb..aba72a5 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,20 @@ marker_server --port 8001 --api_key API_KEY Note: This is not the recommended way to use the Datalab API - it's only provided as a convenience for people wrapping the marker repo. The recommended way is to make a post request to the endpoint directly from your code vs proxying through this server. +You can send requests like this: + +``` +import requests +import json + +post_data = { + 'filepath': 'FILEPATH', + # Add other params here +} + +requests.post("http://localhost:8001/local", data=json.dumps(post_data)).json() # Change to /remote if using Datalab API +``` + # Troubleshooting There are some settings that you may find useful if things aren't working the way you expect: diff --git a/marker_server.py b/marker_server.py index 256f28e..e76aa79 100644 --- a/marker_server.py +++ b/marker_server.py @@ -13,7 +13,7 @@ from typing import Optional import io -from fastapi import FastAPI, Form +from fastapi import FastAPI, Body from marker.convert import convert_single_pdf from marker.models import load_all_models @@ -48,25 +48,32 @@ async def root(): @app.post("/remote") async def convert_pdf_remote( - filepath: str = Form( + filepath: str = Body( ..., description="The path to the PDF file, word document, or powerpoint to convert." ), - max_pages: Optional[int] = Form( + max_pages: Optional[int] = Body( None, - description="The maximum number of pages in the document to convert." + description="The maximum number of pages in the document to convert.", + example=None ), - langs: Optional[str] = Form( + langs: Optional[str] = Body( None, - description="The optional languages to use if OCR is needed, comma separated. Must be either the names or codes from https://github.com/VikParuchuri/surya/blob/master/surya/languages.py." + description="The optional languages to use if OCR is needed, comma separated. Must be either the names or codes from https://github.com/VikParuchuri/surya/blob/master/surya/languages.py.", + example=None ), - force_ocr: bool = Form( + force_ocr: bool = Body( False, 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)." ), - paginate: bool = Form(False, - 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)."), - extract_images: bool = Form(True, description="Whether to extract images from the PDF. Defaults to True. If set to False, no images will be extracted from the PDF."), + paginate: bool = Body( + False, + 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)." + ), + extract_images: bool = Body( + True, + description="Whether to extract images from the PDF. Defaults to True. If set to False, no images will be extracted from the PDF." + ), ): with open(filepath, "rb") as f: filedata = f.read() @@ -101,19 +108,21 @@ async def convert_pdf_remote( @app.post("/local") async def convert_pdf_local( - filepath: str = Form( + filepath: str = Body( ..., description="The path to the PDF file to convert." ), - max_pages: Optional[int] = Form( + max_pages: Optional[int] = Body( None, - description="The maximum number of pages in the PDF to convert." + description="The maximum number of pages in the PDF to convert.", + example=None ), - langs: Optional[str] = Form( + langs: Optional[str] = Body( None, - description="The optional languages to use if OCR is needed, comma separated. Must be either the names or codes from https://github.com/VikParuchuri/surya/blob/master/surya/languages.py." + description="The optional languages to use if OCR is needed, comma separated. Must be either the names or codes from https://github.com/VikParuchuri/surya/blob/master/surya/languages.py.", + example=None ), - force_ocr: bool = Form( + force_ocr: bool = Body( False, 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)." )