-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serve command to start CORS enabled Flask server
Add a `serve` command to start a CORS-enabled Flask server for file conversion. * **New Flask Server**: Add `src/markitdown/server.py` to define a Flask server with CORS enabled and a route to convert files to markdown. * **Dependencies**: Update `pyproject.toml` to include `flask` and `flask-cors` as dependencies and add the `serve` command to the `[project.scripts]` section. * **Documentation**: Update `README.md` with instructions on how to use the `serve` command. * **Tests**: Add tests in `tests/test_markitdown.py` to verify the functionality of the `serve` command, including handling both URL and file POST data. In case we decide not to have `serve` command we have [markdown-converter](https://pypi.org/project/markdown-converter/).
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -72,6 +72,23 @@ print(result.text_content) | |
docker build -t markitdown:latest . | ||
docker run --rm -i markitdown:latest < ~/your-file.pdf > output.md | ||
``` | ||
|
||
### Serve Command | ||
|
||
You can start a CORS-enabled Flask server to convert files to markdown using the `serve` command: | ||
|
||
```sh | ||
markitdown serve | ||
``` | ||
|
||
The server will be available at `http://localhost:5000`. You can send a POST request to the `/convert` endpoint with a file to convert it to markdown. | ||
|
||
Example using `curl`: | ||
|
||
```sh | ||
curl -X POST -F '[email protected]' http://localhost:5000/convert | ||
``` | ||
|
||
<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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from flask import Flask, request, jsonify | ||
from flask_cors import CORS | ||
from markitdown import MarkItDown | ||
|
||
app = Flask(__name__) | ||
CORS(app) | ||
|
||
markitdown = MarkItDown() | ||
|
||
@app.route('/convert', methods=['POST']) | ||
def convert(): | ||
if 'file' in request.files: | ||
file = request.files['file'] | ||
result = markitdown.convert(file.stream, file_extension=file.filename.split('.')[-1]) | ||
return jsonify({'content': result.text_content}) | ||
elif 'url' in request.form: | ||
url = request.form['url'] | ||
result = markitdown.convert(url) | ||
return jsonify({'content': result.text_content}) | ||
else: | ||
return jsonify({'error': 'No file or URL provided'}), 400 | ||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0', port=5000) |
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