GoutamSachdev commited on
Commit
1b4a915
·
1 Parent(s): 1e5247f

Force Docling accelerator to CPU (avoid ZeroGPU CUDA init outside @spaces.GPU)

Browse files
docling_pdf_processor/services/extractor.py CHANGED
@@ -16,6 +16,22 @@ from docling.datamodel.base_models import InputFormat
16
  from docling.datamodel.pipeline_options import PdfPipelineOptions
17
  from docling.document_converter import DocumentConverter, PdfFormatOption
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  from docling_pdf_processor.exceptions import ExtractionError, PDFNotFoundError
20
  from docling_pdf_processor.models import AssetType, ExtractionResult, ExtractedAsset
21
 
@@ -44,7 +60,7 @@ class ExtractorService:
44
  self.do_table_structure = do_table_structure
45
 
46
  def _build_converter(self) -> DocumentConverter:
47
- pipeline_options = PdfPipelineOptions(
48
  extract_images=True,
49
  images_scale=self.images_scale,
50
  generate_page_images=self.generate_page_images,
@@ -52,6 +68,11 @@ class ExtractorService:
52
  do_ocr=self.do_ocr,
53
  do_table_structure=self.do_table_structure,
54
  )
 
 
 
 
 
55
  return DocumentConverter(
56
  format_options={
57
  InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
 
16
  from docling.datamodel.pipeline_options import PdfPipelineOptions
17
  from docling.document_converter import DocumentConverter, PdfFormatOption
18
 
19
+ # Force Docling onto CPU. On HF ZeroGPU Spaces the `spaces` library patches
20
+ # PyTorch so CUDA calls are routed to an on-demand GPU *only* inside a
21
+ # @spaces.GPU function. Our pipeline runs on CPU (its generator+thread model
22
+ # isn't picklable for ZeroGPU's process isolation), so any CUDA init Docling
23
+ # does raises "Low-level CUDA init reached". Pinning the accelerator to CPU
24
+ # keeps model loading off CUDA entirely.
25
+ try:
26
+ from docling.datamodel.accelerator_options import (
27
+ AcceleratorDevice,
28
+ AcceleratorOptions,
29
+ )
30
+
31
+ _HAS_ACCEL_OPTS = True
32
+ except ImportError: # very old docling without accelerator_options
33
+ _HAS_ACCEL_OPTS = False
34
+
35
  from docling_pdf_processor.exceptions import ExtractionError, PDFNotFoundError
36
  from docling_pdf_processor.models import AssetType, ExtractionResult, ExtractedAsset
37
 
 
60
  self.do_table_structure = do_table_structure
61
 
62
  def _build_converter(self) -> DocumentConverter:
63
+ opts = dict(
64
  extract_images=True,
65
  images_scale=self.images_scale,
66
  generate_page_images=self.generate_page_images,
 
68
  do_ocr=self.do_ocr,
69
  do_table_structure=self.do_table_structure,
70
  )
71
+ if _HAS_ACCEL_OPTS:
72
+ opts["accelerator_options"] = AcceleratorOptions(
73
+ device=AcceleratorDevice.CPU
74
+ )
75
+ pipeline_options = PdfPipelineOptions(**opts)
76
  return DocumentConverter(
77
  format_options={
78
  InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)