121 lines
3.9 KiB
Python
121 lines
3.9 KiB
Python
import sys
|
|
from pathlib import Path
|
|
from docling.datamodel.base_models import InputFormat
|
|
from docling.datamodel.pipeline_options import (
|
|
VlmConvertOptions,
|
|
VlmPipelineOptions,
|
|
OcrAutoOptions,
|
|
OcrMode,
|
|
)
|
|
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
|
|
|
|
# Standard indstillinger for Ollama (kan evt. ændres herfra)
|
|
OLLAMA_MODEL = "ibm-granite/granite-vision-4.1-4b:latest"
|
|
OLLAMA_URL = "http://localhost:11434/v1/chat/completions"
|
|
|
|
# Sprog og OCR indstillinger (de du ønskede)
|
|
DEFAULT_OCR_OPTIONS = OcrAutoOptions(
|
|
mode=OcrMode.DEFAULT,
|
|
lang=["iso:da"],
|
|
scale=3.0,
|
|
force_full_page_ocr=True
|
|
)
|
|
|
|
def main():
|
|
# 1. Hent filnavnet fra kommandolinjen (sys.argv[1])
|
|
if len(sys.argv) < 2:
|
|
print(f"Fejl: Mangler filsti.")
|
|
print(f"Brug eksempelvis: python convert_with_ollama.py test.pdf")
|
|
sys.exit(1)
|
|
|
|
INPUT_FILE = Path(sys.argv[1])
|
|
|
|
# Tjek om filen findes
|
|
if not INPUT_FILE.is_file():
|
|
print(f"Fejl: Fil ikke fundet: {INPUT_FILE}")
|
|
sys.exit(1)
|
|
|
|
# 2. Bestem om input er en PDF eller et Billede (PNG/JPG)
|
|
file_ext = INPUT_FILE.suffix.lower()
|
|
|
|
if file_ext in ['.png', '.jpg', '.jpeg']:
|
|
fmt = InputFormat.IMAGE
|
|
print(f"Detekteret format: {fmt}")
|
|
|
|
# Opsætning til Billeder: Her behøver vi ofte ikke de dybe PDF-pipeline options,
|
|
# men vi skal stadig definere VLM-engine og outputformat.
|
|
# Docling kan ofte håndtere dette dynamisk med den generiske DocumentConverter.
|
|
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}
|
|
)
|
|
|
|
# For billeder kan vi ofte nøjes med VlmPipelineOptions uden PDF-specifikke renderings-indstillinger
|
|
pipeline_options = VlmPipelineOptions(
|
|
vlm_options=vlm_options,
|
|
enable_remote_services=True,
|
|
ocr_options=DEFAULT_OCR_OPTIONS # Vi beholder OCR indstillingerne, men de bruges sjældent på rene PNG'er
|
|
)
|
|
|
|
else:
|
|
# Default til PDF (eller andre dokumenttyper)
|
|
fmt = InputFormat.PDF
|
|
print(f"Detekteret format: {fmt}")
|
|
|
|
# Opsætning til PDF (som i din tidligere version)
|
|
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}
|
|
)
|
|
|
|
pipeline_options = VlmPipelineOptions(
|
|
vlm_options=vlm_options,
|
|
enable_remote_services=True,
|
|
ocr_options=DEFAULT_OCR_OPTIONS
|
|
)
|
|
|
|
# 3. Konvertereren med dynamisk formatopsætning
|
|
converter = DocumentConverter(
|
|
format_options={
|
|
fmt: PdfFormatOption(
|
|
pipeline_cls=VlmPipeline,
|
|
pipeline_options=pipeline_options,
|
|
)
|
|
}
|
|
)
|
|
|
|
OUTPUT_FILE = INPUT_FILE.with_suffix(".json")
|
|
|
|
print(f"Input: {INPUT_FILE}")
|
|
print(f"Model: {OLLAMA_MODEL}")
|
|
print(f"Output: {OUTPUT_FILE}\n")
|
|
|
|
result = converter.convert(INPUT_FILE)
|
|
result.document.save_as_json(OUTPUT_FILE)
|
|
|
|
print(f"Klar! Resultat gemt i: {OUTPUT_FILE}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|