Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,24 @@ pip install -r requirements.txt

### **3. Convert a Single File**

Use the ``docuparse`` CLI to convert a PDF into Markdown:

```bash
python -m docuparse.cli convert /path/to/file.pdf /path/to/output.md --max-pages 10
```

### **4. Run the API Server**

Start a FastAPI server that exposes a ``/convert`` endpoint:

```bash
python convert_single.py /path/to/file.pdf /path/to/output.md --parallel_factor 2 --max_pages 10
uvicorn docuparse.fastapi_app:app --reload
```

### **4. Convert Multiple Files**
### **5. Launch the Gradio UI**

```bash
python convert.py /path/to/input/folder /path/to/output/folder --workers 10 --max 10
python -m docuparse.gradio_app
```

---
Expand Down
37 changes: 0 additions & 37 deletions convert_single.py

This file was deleted.

5 changes: 5 additions & 0 deletions docuparse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""DocuParse package."""

__all__ = ["convert_pdf_to_markdown", "save_markdown"]

from .converter import convert_pdf_to_markdown, save_markdown
4 changes: 4 additions & 0 deletions docuparse/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .cli import cli

if __name__ == "__main__":
cli()
23 changes: 23 additions & 0 deletions docuparse/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import click
from .converter import convert_pdf_to_markdown, save_markdown


@click.group()
def cli() -> None:
"""DocuParse command line interface."""
pass


@cli.command()
@click.argument("input_pdf")
@click.argument("output_md")
@click.option("--max-pages", type=int, default=None, help="Maximum pages to process")
def convert(input_pdf: str, output_md: str, max_pages: int | None) -> None:
"""Convert a PDF file to Markdown."""
text = convert_pdf_to_markdown(input_pdf, max_pages=max_pages)
save_markdown(text, output_md)
click.echo(f"Markdown saved to {output_md}")


if __name__ == "__main__":
cli()
13 changes: 13 additions & 0 deletions docuparse/converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pdfminer.high_level import extract_text


def convert_pdf_to_markdown(input_path: str, max_pages: int | None = None) -> str:
"""Extract text from a PDF and return it as Markdown string."""
text = extract_text(input_path, maxpages=max_pages)
return text


def save_markdown(text: str, output_path: str) -> None:
"""Write the Markdown text to the specified file."""
with open(output_path, "w", encoding="utf-8") as f:
f.write(text)
20 changes: 20 additions & 0 deletions docuparse/fastapi_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import PlainTextResponse
import tempfile

from .converter import convert_pdf_to_markdown

app = FastAPI(title="DocuParse API")


@app.post("/convert", response_class=PlainTextResponse)
async def convert_endpoint(
file: UploadFile = File(...), max_pages: int | None = Form(None)
) -> str:
"""Convert an uploaded PDF and return Markdown."""
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
contents = await file.read()
tmp.write(contents)
tmp.flush()
text = convert_pdf_to_markdown(tmp.name, max_pages=max_pages)
return text
23 changes: 23 additions & 0 deletions docuparse/gradio_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
import gradio as gr

API_URL = "http://localhost:8000/convert"

def convert_pdf(file: gr.files.FileData, max_pages: int | None) -> str:
"""Send PDF to the API and return Markdown text."""
with open(file.name, "rb") as f:
files = {"file": (file.name, f, "application/pdf")}
data = {"max_pages": max_pages} if max_pages is not None else {}
resp = requests.post(API_URL, files=files, data=data)
resp.raise_for_status()
return resp.text

iface = gr.Interface(
fn=convert_pdf,
inputs=[gr.File(label="PDF"), gr.Number(label="Max pages", precision=0)],
outputs="text",
title="DocuParse UI",
)

if __name__ == "__main__":
iface.launch()
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ pdf2image
pillow
numpy
tqdm
click
fastapi
uvicorn
gradio
requests