-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Draft] Add Web API for MarkItDown #202
Open
vs4vijay
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
vs4vijay:add-web-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,12 +12,12 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ | |||||||||
ffmpeg \ | ||||||||||
&& rm -rf /var/lib/apt/lists/* | ||||||||||
|
||||||||||
RUN pip install markitdown | ||||||||||
RUN pip install markitdown fastapi uvicorn | ||||||||||
|
||||||||||
# Default USERID and GROUPID | ||||||||||
ARG USERID=10000 | ||||||||||
ARG GROUPID=10000 | ||||||||||
|
||||||||||
USER $USERID:$GROUPID | ||||||||||
|
||||||||||
ENTRYPOINT [ "markitdown" ] | ||||||||||
ENTRYPOINT ["uvicorn", "src.markitdown.api:app", "--host", "0.0.0.0", "--port", "8000"] | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't work: You must copy the
Suggested change
Note that in *
!/src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,42 @@ print(result.text_content) | |
docker build -t markitdown:latest . | ||
docker run --rm -i markitdown:latest < ~/your-file.pdf > output.md | ||
``` | ||
|
||
### Web API | ||
|
||
You can also use MarkItDown via a REST endpoint. The Web API is built using FastAPI and can be run using Docker. | ||
|
||
#### Running the Web API | ||
|
||
1. Build the Docker image: | ||
|
||
```sh | ||
docker build -t markitdown-api:latest . | ||
``` | ||
|
||
2. Run the Docker container: | ||
|
||
```sh | ||
docker run --rm -p 8000:8000 markitdown-api:latest | ||
``` | ||
|
||
The Web API will be available at `http://localhost:8000`. | ||
|
||
#### Using the Web API | ||
|
||
The Web API provides a single endpoint `/convert` that accepts a file and returns the converted markdown. | ||
|
||
- **Endpoint:** `/convert` | ||
- **Method:** `POST` | ||
- **Request Body:** Multipart form data with a file field named `file` | ||
- **Response:** JSON object with a `markdown` field containing the converted markdown | ||
|
||
Example using `curl`: | ||
|
||
```sh | ||
curl -X POST "http://localhost:8000/convert" -F "[email protected]" | ||
``` | ||
|
||
<details> | ||
|
||
<summary>Batch Processing Multiple Files</summary> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from fastapi import FastAPI, File, UploadFile, HTTPException | ||
from fastapi.responses import FileResponse | ||
from markitdown import MarkItDown | ||
import os | ||
|
||
app = FastAPI() | ||
|
||
@app.post("/convert") | ||
async def convert(file: UploadFile = File(...)): | ||
if not file.filename: | ||
raise HTTPException(status_code=400, detail="No file uploaded") | ||
|
||
try: | ||
contents = await file.read() | ||
temp_file_path = f"/tmp/{file.filename}" | ||
with open(temp_file_path, "wb") as temp_file: | ||
temp_file.write(contents) | ||
|
||
markitdown = MarkItDown() | ||
result = markitdown.convert(temp_file_path) | ||
|
||
# output_file_path = f"/tmp/{os.path.splitext(file.filename)[0]}.md" | ||
# with open(output_file_path, "w") as output_file: | ||
# output_file.write(result.text_content) | ||
|
||
os.remove(temp_file_path) | ||
|
||
# return FileResponse(output_file_path, filename=f"{os.path.splitext(file.filename)[0]}.md") | ||
return {"markdown": result.text_content} | ||
|
||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without
python-multipart
the API won't start.It's still weird that
markitdown
is being installed here. This means if the source gets changed and you want to check if the Docker image works you first need to publish the package to pip... It would be better if the currentsrc
folder would be included here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
pip install "fastapi[standard]"