Commit ·
85fed28
0
Parent(s):
first commit
Browse files- __pycache__/main.cpython-313.pyc +0 -0
- main.py +81 -0
- requirements.txt +5 -0
__pycache__/main.cpython-313.pyc
ADDED
|
Binary file (3.61 kB). View file
|
|
|
main.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from fastapi import FastAPI, File, HTTPException, UploadFile
|
| 6 |
+
from marker.config.parser import ConfigParser
|
| 7 |
+
from marker.converters.pdf import PdfConverter
|
| 8 |
+
from marker.models import create_model_dict
|
| 9 |
+
from marker.output import text_from_rendered
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
converter: Optional[PdfConverter] = None
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@app.on_event("startup")
|
| 16 |
+
async def startup_event() -> None:
|
| 17 |
+
global converter
|
| 18 |
+
|
| 19 |
+
config = {
|
| 20 |
+
"output_format": "markdown",
|
| 21 |
+
"force_ocr": True,
|
| 22 |
+
"use_llm": False,
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
config_parser = ConfigParser(config)
|
| 26 |
+
|
| 27 |
+
converter = PdfConverter(
|
| 28 |
+
config=config_parser.generate_config_dict(),
|
| 29 |
+
artifact_dict=create_model_dict(),
|
| 30 |
+
processor_list=config_parser.get_processors(),
|
| 31 |
+
renderer=config_parser.get_renderer(),
|
| 32 |
+
llm_service=config_parser.get_llm_service(),
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@app.post("/extract")
|
| 37 |
+
async def extract(file: UploadFile = File(...)) -> dict[str, str]:
|
| 38 |
+
if file.content_type not in {
|
| 39 |
+
"application/pdf",
|
| 40 |
+
"image/png",
|
| 41 |
+
"image/jpeg",
|
| 42 |
+
"image/tiff",
|
| 43 |
+
}:
|
| 44 |
+
raise HTTPException(
|
| 45 |
+
status_code=400,
|
| 46 |
+
detail="File must be a PDF or image (png/jpeg/tiff).",
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if converter is None:
|
| 50 |
+
raise HTTPException(
|
| 51 |
+
status_code=500,
|
| 52 |
+
detail="OCR engine not initialized.",
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
temp_path = None
|
| 56 |
+
try:
|
| 57 |
+
suffix = os.path.splitext(file.filename)[1].lower() or ".pdf"
|
| 58 |
+
if suffix not in {".pdf", ".png", ".jpg", ".jpeg", ".tif", ".tiff"}:
|
| 59 |
+
suffix = ".pdf"
|
| 60 |
+
|
| 61 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
| 62 |
+
temp_path = tmp.name
|
| 63 |
+
tmp.write(await file.read())
|
| 64 |
+
|
| 65 |
+
rendered = converter(temp_path)
|
| 66 |
+
markdown_text, _, _ = text_from_rendered(rendered)
|
| 67 |
+
|
| 68 |
+
return {"markdown": markdown_text}
|
| 69 |
+
|
| 70 |
+
except Exception as exc:
|
| 71 |
+
raise HTTPException(
|
| 72 |
+
status_code=500,
|
| 73 |
+
detail=f"OCR extraction failed: {exc}",
|
| 74 |
+
) from exc
|
| 75 |
+
|
| 76 |
+
finally:
|
| 77 |
+
if temp_path and os.path.exists(temp_path):
|
| 78 |
+
try:
|
| 79 |
+
os.remove(temp_path)
|
| 80 |
+
except OSError:
|
| 81 |
+
pass
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
python-multipart
|
| 4 |
+
marker-pdf
|
| 5 |
+
torch
|