mirror of
https://github.com/docling-project/docling.git
synced 2025-12-06 15:54:47 +00:00
* add mlx-whisper support * added mlx-whisper example and test. update docling cli to use MLX automatically if present. * fix pre-commit checks and added proper type safety * fixed linter issue * DCO Remediation Commit for Ken Steele <ksteele@gmail.com> I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: a979a680e1dc2fee8461401335cfb5dda8cfdd98 I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: 9827068382ca946fe1387ed83f747ae509fcf229 I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: ebbeb45c7dc266260e1fad6bdb54a7041f8aeed4 I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: 2f6fd3cf46c8ca0bb98810191578278f1df87aa3 Signed-off-by: Ken Steele <ksteele@gmail.com> * fix unit tests and code coverage for CI * DCO Remediation Commit for Ken Steele <ksteele@gmail.com> I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: 5e61bf11139a2133978db2c8d306be6289aed732 Signed-off-by: Ken Steele <ksteele@gmail.com> * fix CI example test - mlx_whisper_example.py defaults to tests/data/audio/sample_10s.mp3 if no args specified. Signed-off-by: Ken Steele <ksteele@gmail.com> * refactor: centralize audio file extensions and MIME types in base_models.py - Move audio file extensions from CLI hardcoded set to FormatToExtensions[InputFormat.AUDIO] - Add support for additional audio formats: m4a, aac, ogg, flac, mp4, avi, mov - Update FormatToMimeType mapping to include MIME types for all audio formats - Update CLI auto-detection to use centralized FormatToExtensions mapping - Add comprehensive tests for audio file auto-detection and pipeline selection - Ensure explicit pipeline choices are not overridden by auto-detection Fixes issue where only .mp3 and .wav files were processed as audio despite CLI auto-detection working for all formats. The document converter now properly recognizes all audio formats through MIME type detection. Addresses review comments: - Centralizes audio extensions in base_models.py as suggested - Maintains existing auto-detection behavior while using centralized data - Adds proper test coverage for the audio detection functionality All examples and tests pass with the new centralized approach. All audio formats (mp3, wav, m4a, aac, ogg, flac, mp4, avi, mov) now work correctly. Signed-off-by: Ken Steele <ksteele@gmail.com> * feat: address reviewer feedback - improve CLI auto-detection and add explicit model options Review feedback addressed: 1. Fix CLI auto-detection to only switch to ASR pipeline when ALL files are audio - Previously switched if ANY file was audio, now requires ALL files to be audio - Added warning for mixed file types with guidance to use --pipeline asr 2. Add explicit WHISPER_X_MLX and WHISPER_X_NATIVE model options - Users can now force specific implementations if desired - Auto-selecting models (WHISPER_BASE, etc.) still choose best for hardware - Added 12 new explicit model options: _MLX and _NATIVE variants for each size CLI now supports: - Auto-selecting: whisper_tiny, whisper_base, etc. (choose best for hardware) - Explicit MLX: whisper_tiny_mlx, whisper_base_mlx, etc. (force MLX) - Explicit Native: whisper_tiny_native, whisper_base_native, etc. (force native) Addresses reviewer comments from @dolfim-ibm Signed-off-by: Ken Steele <ksteele@gmail.com> * DCO Remediation Commit for Ken Steele <ksteele@gmail.com> I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: c60e72d2b504a477797d183790eb74fb4fc9b019 I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: 94803317a3807451de76996e2509fc58e1ecacb0 I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: 21905e8acef341e94052c189376b0b45a7bb1fef I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: 96c669d155c8e9bd6455ecff4720933ad7d9e7cb I, Ken Steele <ksteele@gmail.com>, hereby add my Signed-off-by to this commit: 8371c060ea85295d05ad040f1d1608b560e0424d Signed-off-by: Ken Steele <ksteele@gmail.com> * test(asr): add coverage for MLX options, pipeline helpers, and VLM prompts - tests/test_asr_mlx_whisper.py: verify explicit MLX options (framework, repo ids) - tests/test_asr_pipeline.py: cover _has_text/_determine_status and backend support with proper InputDocument/NoOpBackend wiring - tests/test_interfaces.py: add BaseVlmPageModel.formulate_prompt tests (RAW/NONE/CHAT, invalid style), with minimal InlineVlmOptions scaffold Improves reliability of ASR and VLM components by validating configuration paths and helper logic. Signed-off-by: Ken Steele <ksteele@gmail.com> * test(asr): broaden coverage for model selection, pipeline flows, and VLM prompts - tests/test_asr_mlx_whisper.py - Add MLX/native selector coverage across all Whisper sizes - Validate repo_id choices under MLX and Native paths - Cover fallback path when MPS unavailable and mlx_whisper missing - tests/test_asr_pipeline.py - Relax silent-audio assertion to accept PARTIAL_SUCCESS or SUCCESS - Force CPU native path in helper tests to avoid torch in device selection - Add language handling tests for native/MLX transcribe - Cover native run success (BytesIO) and failure (exception) branches - Cover MLX run success/failure branches with mocked transcribe - Add init path coverage with artifacts_path - tests/test_interfaces.py - Add focused VLM prompt tests (NONE/CHAT variants) Result: all tests passing with significantly improved coverage for ASR model selectors, pipeline execution paths, and VLM prompt formulation. Signed-off-by: Ken Steele <ksteele@gmail.com> * simplify ASR model settings (no pipeline detection needed) Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * clean up disk space in runners Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> --------- Signed-off-by: Ken Steele <ksteele@gmail.com> Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> Co-authored-by: Michele Dolfi <dol@zurich.ibm.com>
92 lines
3.3 KiB
Python
Vendored
92 lines
3.3 KiB
Python
Vendored
# %% [markdown]
|
|
# Minimal ASR pipeline example: transcribe an audio file to Markdown text.
|
|
#
|
|
# What this example does
|
|
# - Configures the ASR pipeline with a default model spec and converts one audio file.
|
|
# - Prints the recognized speech segments in Markdown with timestamps.
|
|
#
|
|
# Prerequisites
|
|
# - Install Docling with ASR extras and any audio dependencies (ffmpeg, etc.).
|
|
# - Ensure your environment can download or access the configured ASR model.
|
|
# - Some formats require ffmpeg codecs; install ffmpeg and ensure it's on PATH.
|
|
#
|
|
# How to run
|
|
# - From the repository root, run: `python docs/examples/minimal_asr_pipeline.py`.
|
|
# - The script prints the transcription to stdout.
|
|
#
|
|
# Customizing the model
|
|
# - The script automatically selects the best model for your hardware (MLX Whisper for Apple Silicon, native Whisper otherwise).
|
|
# - Edit `get_asr_converter()` to manually override `pipeline_options.asr_options` with any model from `asr_model_specs`.
|
|
# - Keep `InputFormat.AUDIO` and `AsrPipeline` unchanged for a minimal setup.
|
|
#
|
|
# Input audio
|
|
# - Defaults to `tests/data/audio/sample_10s.mp3`. Update `audio_path` to your own file if needed.
|
|
|
|
# %%
|
|
|
|
from pathlib import Path
|
|
|
|
from docling_core.types.doc import DoclingDocument
|
|
|
|
from docling.datamodel import asr_model_specs
|
|
from docling.datamodel.base_models import ConversionStatus, InputFormat
|
|
from docling.datamodel.document import ConversionResult
|
|
from docling.datamodel.pipeline_options import AsrPipelineOptions
|
|
from docling.document_converter import AudioFormatOption, DocumentConverter
|
|
from docling.pipeline.asr_pipeline import AsrPipeline
|
|
|
|
|
|
def get_asr_converter():
|
|
"""Create a DocumentConverter configured for ASR with automatic model selection.
|
|
|
|
Uses `asr_model_specs.WHISPER_TURBO` which automatically selects the best
|
|
implementation for your hardware:
|
|
- MLX Whisper Turbo for Apple Silicon (M1/M2/M3) with mlx-whisper installed
|
|
- Native Whisper Turbo as fallback
|
|
|
|
You can swap in another model spec from `docling.datamodel.asr_model_specs`
|
|
to experiment with different model sizes.
|
|
"""
|
|
pipeline_options = AsrPipelineOptions()
|
|
pipeline_options.asr_options = asr_model_specs.WHISPER_TURBO
|
|
|
|
converter = DocumentConverter(
|
|
format_options={
|
|
InputFormat.AUDIO: AudioFormatOption(
|
|
pipeline_cls=AsrPipeline,
|
|
pipeline_options=pipeline_options,
|
|
)
|
|
}
|
|
)
|
|
return converter
|
|
|
|
|
|
def asr_pipeline_conversion(audio_path: Path) -> DoclingDocument:
|
|
"""Run the ASR pipeline and return a `DoclingDocument` transcript."""
|
|
# Check if the test audio file exists
|
|
assert audio_path.exists(), f"Test audio file not found: {audio_path}"
|
|
|
|
converter = get_asr_converter()
|
|
|
|
# Convert the audio file
|
|
result: ConversionResult = converter.convert(audio_path)
|
|
|
|
# Verify conversion was successful
|
|
assert result.status == ConversionStatus.SUCCESS, (
|
|
f"Conversion failed with status: {result.status}"
|
|
)
|
|
return result.document
|
|
|
|
|
|
if __name__ == "__main__":
|
|
audio_path = Path("tests/data/audio/sample_10s.mp3")
|
|
|
|
doc = asr_pipeline_conversion(audio_path=audio_path)
|
|
print(doc.export_to_markdown())
|
|
|
|
# Expected output:
|
|
#
|
|
# [time: 0.0-4.0] Shakespeare on Scenery by Oscar Wilde
|
|
#
|
|
# [time: 5.28-9.96] This is a LibriVox recording. All LibriVox recordings are in the public domain.
|