refactoring minimal_vlm_pipeline

Signed-off-by: Peter Staar <taa@zurich.ibm.com>
This commit is contained in:
Peter Staar
2025-05-14 13:57:32 +02:00
parent 7c97b494ec
commit a3716b1961
5 changed files with 53 additions and 73 deletions

View File

@@ -11,6 +11,10 @@ from docling.datamodel.pipeline_options import (
InferenceFramework,
ResponseFormat,
VlmPipelineOptions,
smoldocling_vlm_mlx_conversion_options,
smoldocling_vlm_conversion_options,
granite_vision_vlm_conversion_options,
granite_vision_vlm_ollama_conversion_options,
)
from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.pipeline.vlm_pipeline import VlmPipeline
@@ -33,7 +37,7 @@ pipeline_options.force_backend_text = False
# pipeline_options.vlm_options = smoldocling_vlm_conversion_options
## Pick a VLM model. Fast Apple Silicon friendly implementation for SmolDocling-256M via MLX
## pipeline_options.vlm_options = smoldocling_vlm_mlx_conversion_options
pipeline_options.vlm_options = smoldocling_vlm_mlx_conversion_options
## Alternative VLM models:
# pipeline_options.vlm_options = granite_vision_vlm_conversion_options
@@ -45,7 +49,7 @@ pixtral_vlm_conversion_options = HuggingFaceVlmOptions(
response_format=ResponseFormat.MARKDOWN,
inference_framework=InferenceFramework.TRANSFORMERS_LlavaForConditionalGeneration,
)
vlm_conversion_options = pixtral_vlm_conversion_options
pipeline_options.vlm_options = pixtral_vlm_conversion_options
"""
"""
@@ -55,7 +59,7 @@ pixtral_vlm_conversion_options = HuggingFaceVlmOptions(
response_format=ResponseFormat.MARKDOWN,
inference_framework=InferenceFramework.TRANSFORMERS_LlavaForConditionalGeneration,
)
vlm_conversion_options = pixtral_vlm_conversion_options
pipeline_options.vlm_options = pixtral_vlm_conversion_options
"""
"""
@@ -66,16 +70,19 @@ phi_vlm_conversion_options = HuggingFaceVlmOptions(
response_format=ResponseFormat.MARKDOWN,
inference_framework=InferenceFramework.TRANSFORMERS_AutoModelForCausalLM,
)
vlm_conversion_options = phi_vlm_conversion_options
pipeline_options.vlm_options = phi_vlm_conversion_options
"""
"""
pixtral_vlm_conversion_options = HuggingFaceVlmOptions(
repo_id="mlx-community/pixtral-12b-bf16",
prompt="Convert this page to markdown. Do not miss any text and only output the bare MarkDown!",
response_format=ResponseFormat.MARKDOWN,
inference_framework=InferenceFramework.MLX,
scale=1.0,
)
vlm_conversion_options = pixtral_vlm_conversion_options
pipeline_options.vlm_options = pixtral_vlm_conversion_options
"""
"""
qwen_vlm_conversion_options = HuggingFaceVlmOptions(
@@ -84,11 +91,9 @@ qwen_vlm_conversion_options = HuggingFaceVlmOptions(
response_format=ResponseFormat.MARKDOWN,
inference_framework=InferenceFramework.MLX,
)
vlm_conversion_options = qwen_vlm_conversion_options
pipeline_options.vlm_options = qwen_vlm_conversion_options
"""
pipeline_options.vlm_options = vlm_conversion_options
## Set up pipeline for PDF or image inputs
converter = DocumentConverter(
format_options={
@@ -116,19 +121,16 @@ for source in sources:
res = converter.convert(source)
print("")
print(res.document.export_to_markdown())
#print(res.document.export_to_markdown())
for page in res.pages:
for i,page in enumerate(res.pages):
print("")
print(f"Predicted page in {pipeline_options.vlm_options.response_format}:")
print(f" ---------- Predicted page {i} in {pipeline_options.vlm_options.response_format}:")
print(page.predictions.vlm_response.text)
print(f" ---------- ")
res.document.save_as_html(
filename=Path(f"{out_path}/{res.input.file.stem}.html"),
image_mode=ImageRefMode.REFERENCED,
labels=[*DEFAULT_EXPORT_LABELS, DocItemLabel.FOOTNOTE],
)
print("===== Final output of the converted document =======")
with (out_path / f"{res.input.file.stem}.json").open("w") as fp:
fp.write(json.dumps(res.document.export_to_dict()))
@@ -136,19 +138,27 @@ for source in sources:
out_path / f"{res.input.file.stem}.json",
image_mode=ImageRefMode.PLACEHOLDER,
)
print(f" => produced {out_path / res.input.file.stem}.json")
res.document.save_as_markdown(
out_path / f"{res.input.file.stem}.md",
image_mode=ImageRefMode.PLACEHOLDER,
)
print(f" => produced {out_path / res.input.file.stem}.md")
res.document.save_as_html(
out_path / f"{res.input.file.stem}.html",
image_mode=ImageRefMode.EMBEDDED,
labels=[*DEFAULT_EXPORT_LABELS, DocItemLabel.FOOTNOTE],
# split_page_view=True,
)
print(f" => produced {out_path / res.input.file.stem}.html")
pg_num = res.document.num_pages()
print("")
inference_time = time.time() - start_time
print(
f"Total document prediction time: {inference_time:.2f} seconds, pages: {pg_num}"
)
print("================================================")
print("done!")
print("================================================")
print("====================================================")