Sign inSign up

mathematicalmichael/md-api

By mathematicalmichael

Updated over 1 year ago

Image
0

294

mathematicalmichael/md-api repository overview

litserve Implementation of Microsoft markitdown

This docker image exposes Microsoft's markitdown Python package via API on port 8000 at /convert.

Start Server

docker run --rm -p 8000:8000 mathematicalmichael/md-api

Usage

Example client.py file:

import argparse
import json
from datetime import datetime
from pathlib import Path

import requests

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


def send_request(path):
    try:
        with open(path, "rb") as inputFile:
            # Use a tuple (filename, file object) for 'files'
            response = requests.post(
                API_URL, files={"content": (Path(path).name, inputFile)}
            )
    except FileNotFoundError:
        # passing a URL for markdownit to scrape
        response = requests.post(API_URL, json={"content": path})
    if response.status_code == 200:
        output = json.loads(response.content)["output"]
        save_output(output)
    else:
        print(
            f"Error: Response with status code {response.status_code} - {response.text}"
        )


def save_output(output):
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S").lower()
    Path("output").mkdir(exist_ok=True)
    filename = f"output/{timestamp}.md"

    with open(filename, "wb") as output_file:
        output = output.encode("utf-8")
        output_file.write(output)

    print(f"File saved to {filename}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Send file to markdownit server and receive generated markdown content."
    )
    parser.add_argument("--path", required=True, help="Path for the document file.")
    args = parser.parse_args()

    send_request(args.path)

Client usage:

python client.py --path <file-or-url>
docker run --rm -ti --network=host \
	-v ./output:/app/output:rw \
	-v ./tests:/tests:ro \
	-v ./client.py:/app/client.py:ro \
	-u $$(id -u):$$(id -g) \
	md-api \
	python client.py --path /tests/test_files.zip

Curl examples:

curl --request POST \
    --url http://0.0.0.0:8000/convert \
    --header 'Content-Type: application/json' \
    --data '{
        "content": "https://wikipedia.org"
    }'
curl -X POST -F "content=@tests/test_rss.xml;filename=test_rss.xml" http://127.0.0.1:8000/convert

Tag summary

Content type

Image

Digest

sha256:82a3675cb

Size

403.7 MB

Last updated

over 1 year ago

docker pull mathematicalmichael/md-api