Skip to content
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

api v1alpha1 #17

Merged
merged 25 commits into from
Feb 3, 2025
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
528ca3a
api v1alpha1
guimou Dec 10, 2024
1f17348
use actual types in request models and refactor
dolfim-ibm Jan 24, 2025
32f358a
make gradio optional and update README
dolfim-ibm Jan 24, 2025
04e2457
Run workflow jobs sequentially to avoid disk space outage (#19)
vishnoianil Jan 22, 2025
930d3fd
Add github job to build image (and not publish) on PR creation (#20)
vishnoianil Jan 23, 2025
c3836ed
add start_server script for local dev
dolfim-ibm Jan 27, 2025
fda5862
fix 3.12-only syntax
dolfim-ibm Jan 27, 2025
26c6ac4
fix more py3.10-11 compatibility
dolfim-ibm Jan 27, 2025
5bedade
rework output format and background tasks
dolfim-ibm Jan 27, 2025
26765ac
speficy return schemas for openapi
dolfim-ibm Jan 27, 2025
6a5aa98
add processing time and update REDAME
dolfim-ibm Jan 27, 2025
407d827
lint markdown
dolfim-ibm Jan 27, 2025
8a09a10
add MD033 to config
dolfim-ibm Jan 27, 2025
13e281e
Merge remote-tracking branch 'origin/main' into api-upgrade
dolfim-ibm Jan 28, 2025
bae6b71
use port 5000
dolfim-ibm Jan 28, 2025
de49a13
use port 5001 as default
dolfim-ibm Jan 28, 2025
1bcfe7f
update deps
dolfim-ibm Jan 28, 2025
2758bf6
refactor input request
dolfim-ibm Jan 28, 2025
ca47ef8
return docling document
dolfim-ibm Jan 28, 2025
c567a82
update new payload in README
dolfim-ibm Jan 28, 2025
95f448d
add base64 example
dolfim-ibm Jan 28, 2025
c7f2601
wrap example in <details>
dolfim-ibm Jan 28, 2025
76d08a9
rename /url in /source
dolfim-ibm Feb 2, 2025
daf959e
Merge remote-tracking branch 'origin/main' into api-upgrade
dolfim-ibm Feb 2, 2025
574d190
move main execution to __main__
dolfim-ibm Feb 2, 2025
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
Prev Previous commit
Next Next commit
use actual types in request models and refactor
Signed-off-by: Michele Dolfi <[email protected]>
dolfim-ibm committed Jan 24, 2025
commit 1f17348eba5d92f8d9cfb8be15d5436a33f6a56d
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -16,6 +16,14 @@ repos:
pass_filenames: false
language: system
files: '\.py$'
- repo: local
hooks:
- id: autoflake
name: autoflake
entry: poetry run autoflake docling_serve tests
pass_filenames: false
language: system
files: '\.py$'
- repo: local
hooks:
- id: system
80 changes: 20 additions & 60 deletions docling_serve/app.py
Original file line number Diff line number Diff line change
@@ -4,27 +4,27 @@
import tempfile
from contextlib import asynccontextmanager
from pathlib import Path
from typing import List, Optional, Union
from typing import Annotated, List

import gradio as gr
from docling.datamodel.base_models import InputFormat
from docling.document_converter import DocumentConverter
from docling_conversion import (
from dotenv import load_dotenv
from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from pydantic import BaseModel

from docling_serve.docling_conversion import (
ConvertDocumentsParameters,
ConvertDocumentsRequest,
convert_documents,
converters,
get_pdf_pipeline_opts,
)
from dotenv import load_dotenv
from fastapi import Depends, FastAPI, File, Form, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from gradio_ui import ui as gradio_ui
from helper_functions import _str_to_bool, _to_list_of_strings
from pydantic import BaseModel
from response_preparation import process_results
from uvicorn import run
from docling_serve.gradio_ui import ui as gradio_ui
from docling_serve.helper_functions import FormDepends, _str_to_bool
from docling_serve.response_preparation import process_results

# Load local env vars if present
load_dotenv()
@@ -160,45 +160,12 @@ def process_url(conversion_request: ConvertDocumentsRequest):
# Convert a document from file(s)


# Parameters parser: Form object needed for the file(s) conversion endpoint
def _parse_parameters(
from_formats: Optional[Union[List[str], str]] = Form(["pdf", "docx"]),
to_formats: Optional[Union[List[str], str]] = Form(["md"]),
image_export_mode: Optional[str] = Form("embedded"),
do_ocr: Optional[Union[bool, str]] = Form("true"),
force_ocr: Optional[Union[bool, str]] = Form("false"),
ocr_engine: Optional[str] = Form("easyocr"),
ocr_lang: Optional[str] = Form("en"),
pdf_backend: Optional[str] = Form("dlparse_v2"),
table_mode: Optional[str] = Form("fast"),
abort_on_error: Optional[Union[bool, str]] = Form("false"),
return_as_file: Optional[Union[bool, str]] = Form("false"),
do_table_structure: Optional[Union[bool, str]] = Form("true"),
include_images: Optional[Union[bool, str]] = Form("true"),
images_scale: Optional[float] = Form(2.0),
) -> ConvertDocumentsParameters:
return ConvertDocumentsParameters(
from_formats=_to_list_of_strings(from_formats) if from_formats else None,
to_formats=_to_list_of_strings(to_formats) if to_formats else None,
image_export_mode=image_export_mode.strip() if image_export_mode else None,
ocr=_str_to_bool(do_ocr),
force_ocr=_str_to_bool(force_ocr),
ocr_engine=ocr_engine.strip() if ocr_engine else None,
ocr_lang=ocr_lang.strip() if ocr_lang else None,
pdf_backend=pdf_backend.strip() if pdf_backend else None,
table_mode=table_mode.strip() if table_mode else None,
abort_on_error=_str_to_bool(abort_on_error),
return_as_file=_str_to_bool(return_as_file),
do_table_structure=_str_to_bool(do_table_structure),
include_images=_str_to_bool(include_images),
images_scale=images_scale,
)


@app.post("/v1alpha/convert/file")
async def process_file(
files: List[UploadFile] = File(...),
parameters: ConvertDocumentsParameters = Depends(_parse_parameters),
files: List[UploadFile],
parameters: Annotated[
ConvertDocumentsParameters, FormDepends(ConvertDocumentsParameters)
],
):

_log.info(f"Received {len(files)} files for processing.")
@@ -207,6 +174,8 @@ async def process_file(
tmp_input_dir = Path(tempfile.mkdtemp())

# Save the uploaded files to the temporary directory
# TODO: we could use the binary stream with Docling directly, using the file could
# indeed help when many jobs are queued with background tasks.
file_paths = []
for file in files:
file_location = tmp_input_dir / file.filename # type: ignore [operator]
@@ -216,18 +185,7 @@ async def process_file(

# Process the files
conversion_request = ConvertDocumentsRequest(
input_sources=file_paths,
from_formats=parameters.from_formats,
to_formats=parameters.to_formats,
image_export_mode=parameters.image_export_mode,
ocr=parameters.do_ocr,
force_ocr=parameters.force_ocr,
ocr_engine=parameters.ocr_engine,
ocr_lang=parameters.ocr_lang,
pdf_backend=parameters.pdf_backend,
table_mode=parameters.table_mode,
abort_on_error=parameters.abort_on_error,
return_as_file=parameters.return_as_file,
input_sources=file_paths, **parameters.model_dump()
)

results = convert_documents(conversion_request)
@@ -243,6 +201,8 @@ async def process_file(

# Launch the FastAPI server
if __name__ == "__main__":
from uvicorn import run

port = int(os.getenv("PORT", "8080"))
workers = int(os.getenv("UVICORN_WORKERS", "1"))
reload = _str_to_bool(os.getenv("RELOAD", "False"))
Loading