80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
from pathlib import Path
|
|
from docling.datamodel.base_models import InputFormat
|
|
from docling.datamodel.pipeline_options import (
|
|
VlmConvertOptions,
|
|
VlmPipelineOptions,
|
|
OcrAutoOptions, # Importeret fra her
|
|
OcrMode, # Importeret fra her
|
|
)
|
|
from docling.datamodel.stage_model_specs import ApiModelConfig
|
|
from docling.datamodel.vlm_engine_options import (
|
|
ApiVlmEngineOptions,
|
|
VlmEngineType,
|
|
)
|
|
from docling.document_converter import DocumentConverter, PdfFormatOption
|
|
from docling.pipeline.vlm_pipeline import VlmPipeline
|
|
|
|
INPUT_FILE = Path("Skemaer_2026_2027.pdf")
|
|
OUTPUT_FILE = INPUT_FILE.with_suffix(".json")
|
|
OLLAMA_MODEL = "ibm-granite/granite-vision-4.1-4b:latest"
|
|
OLLAMA_URL = "http://localhost:11434/v1/chat/completions"
|
|
|
|
def main():
|
|
if not INPUT_FILE.is_file():
|
|
raise FileNotFoundError(f"PDF not found: {INPUT_FILE}")
|
|
|
|
# 1. Konfigurer dine specifikke OCR-indstillinger her
|
|
ocr_options = OcrAutoOptions(
|
|
mode=OcrMode.FULL_PAGE,
|
|
lang=["iso:da"],
|
|
scale=2.0,
|
|
force_full_page_ocr=True
|
|
)
|
|
|
|
# 2. Opsæt VLM-konfigurationen
|
|
vlm_options = VlmConvertOptions.from_preset(
|
|
"granite_vision",
|
|
engine_options=ApiVlmEngineOptions(
|
|
engine_type=VlmEngineType.API_OLLAMA,
|
|
url=OLLAMA_URL,
|
|
timeout=300,
|
|
),
|
|
)
|
|
|
|
vlm_options.model_spec.api_overrides[
|
|
VlmEngineType.API_OLLAMA
|
|
] = ApiModelConfig(
|
|
params={"model": OLLAMA_MODEL}
|
|
)
|
|
|
|
# 3. Opret pipeline options og tilføj OCR-indstillingerne
|
|
pipeline_options = VlmPipelineOptions(
|
|
vlm_options=vlm_options,
|
|
enable_remote_services=True,
|
|
# Her sætter vi vores specifikke OCR-parametre:
|
|
ocr_options=ocr_options,
|
|
)
|
|
|
|
converter = DocumentConverter(
|
|
format_options={
|
|
InputFormat.PDF: PdfFormatOption(
|
|
pipeline_cls=VlmPipeline,
|
|
pipeline_options=pipeline_options,
|
|
)
|
|
}
|
|
)
|
|
|
|
print(f"Input: {INPUT_FILE}")
|
|
print(f"Engine: {vlm_options.engine_options.engine_type}")
|
|
print(f"Model: {OLLAMA_MODEL}")
|
|
print(f"OCR Lang: ['iso:da']")
|
|
print(f"Full Page OCR: True")
|
|
print(f"Output: {OUTPUT_FILE}")
|
|
|
|
result = converter.convert(INPUT_FILE)
|
|
result.document.save_as_json(OUTPUT_FILE)
|
|
print(f"Conversion complete: {OUTPUT_FILE}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|