Spaces:
Running
Running
File size: 5,129 Bytes
3f7e152 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import os
from pathlib import Path
from docling.datamodel.document import ConversionResult
from huggingface_hub import snapshot_download
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import EasyOcrOptions, OcrMacOptions, PdfPipeline, PdfPipelineOptions, PipelineOptions, RapidOcrOptions, TesseractOcrOptions
from docling.document_converter import DocumentConverter, ImageFormatOption, PdfFormatOption
from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend, PyPdfiumPageBackend
from docling.pipeline.standard_pdf_pipeline import StandardPdfPipeline
from docling.pipeline.simple_pipeline import SimplePipeline
# TODO: REFACTOR LOAD OCR MODEL TO JUST EITHER USE SERVER MODELS OR MOBILE MODELS
def load_rapid_ocr_model(det_model: str, rec_model: str, cls_model: str) -> DocumentConverter:
"""
Load the RapidOCR model from Hugging Face Hub.
Args:
det_model (str): Path to the detection model.
rec_model (str): Path to the recognition model.
cls_model (str): Path to the classification model.
Returns:
DocumentConverter: The loaded RapidOCR model.
"""
print("Downloading RapidOCR models")
download_path = snapshot_download(repo_id="SWHL/RapidOCR")
det_model_path = os.path.join(
download_path, det_model
)
rec_model_path = os.path.join(
download_path, rec_model
)
cls_model_path = os.path.join(
download_path, cls_model
)
ocr_options = RapidOcrOptions(
det_model_path=det_model_path,
rec_model_path=rec_model_path,
cls_model_path=cls_model_path
)
pipeline_options = PdfPipelineOptions(
ocr_options=ocr_options
)
doc_converter = DocumentConverter(
format_options={
InputFormat.IMAGE: ImageFormatOption(
pipeline_options=pipeline_options
)
}
)
return doc_converter
def load_ocr_mac_model() -> DocumentConverter:
"""
Load the OCR Mac model.
Returns:
DocumentConverter: The loaded OCR Mac model.
"""
ocr_options = OcrMacOptions(
framework='vision'
)
pipeline_options = PdfPipelineOptions(
ocr_options=ocr_options
)
doc_converter = DocumentConverter(
allowed_formats=[
InputFormat.PDF,
InputFormat.IMAGE,
],
format_options={
InputFormat.PDF: PdfFormatOption(
pipeline_cls=StandardPdfPipeline, backend=PyPdfiumDocumentBackend, pipeline_options=pipeline_options
),
InputFormat.IMAGE: PdfFormatOption(
pipeline_cls=StandardPdfPipeline, backend=PyPdfiumDocumentBackend, pipeline_options=pipeline_options
)
}
)
return doc_converter
def load_tesseract_model(tessdata_path: str) -> DocumentConverter:
"""
Load the Tesseract OCR model.
Args:
tessdata_path (str): Path to the Tesseract data directory.
Returns:
DocumentConverter: The loaded Tesseract OCR model.
"""
os.environ["TESSDATA_PREFIX"] = tessdata_path
ocr_options = TesseractOcrOptions()
pipeline_options = PdfPipelineOptions(
ocr_options=ocr_options
)
doc_converter = DocumentConverter(
allowed_formats=[
InputFormat.PDF,
InputFormat.IMAGE
],
format_options={
InputFormat.PDF: PdfFormatOption(
pipeline_cls=StandardPdfPipeline, backend=PyPdfiumDocumentBackend, pipeline_options=pipeline_options
),
InputFormat.IMAGE: PdfFormatOption(
pipeline_cls=StandardPdfPipeline, backend=PyPdfiumDocumentBackend, pipeline_options=pipeline_options
)
}
)
return doc_converter
def load_easy_ocr_model() -> DocumentConverter:
"""
Load the EasyOCR model.
Returns:
DocumentConverter: The loaded EasyOCR model.
"""
ocr_options = EasyOcrOptions()
pipeline_options = PdfPipelineOptions(
ocr_options=ocr_options
)
doc_converter = DocumentConverter(
allowed_formats=[
InputFormat.PDF,
InputFormat.IMAGE
],
format_options={
InputFormat.PDF: PdfFormatOption(
pipeline_cls=StandardPdfPipeline, backend=PyPdfiumDocumentBackend, pipeline_options=pipeline_options
),
InputFormat.IMAGE: PdfFormatOption(
pipeline_cls=StandardPdfPipeline, backend=PyPdfiumDocumentBackend, pipeline_options=pipeline_options
)
}
)
return doc_converter
def image_to_text(document_converter: DocumentConverter, file_path: Path) -> ConversionResult:
"""
Convert an image to text using the specified document converter.
Args:
document_converter (DocumentConverter): The document converter to use.
file_path (Path): Path to the image file.
Returns:
ConversionResult: The result of the conversion.
"""
conv_results = document_converter.convert(file_path)
return conv_results
|