MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / _DocumentConversionInput

Class _DocumentConversionInput

docling/datamodel/document.py:229–394  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

227
228
229class _DocumentConversionInput(BaseModel):
230
231 path_or_stream_iterator: Iterable[Union[Path, str, DocumentStream]]
232 headers: Optional[Dict[str, str]] = None
233 limits: Optional[DocumentLimits] = DocumentLimits()
234
235 def docs(
236 self, format_options: Dict[InputFormat, "FormatOption"]
237 ) -> Iterable[InputDocument]:
238 for item in self.path_or_stream_iterator:
239 obj = (
240 resolve_source_to_stream(item, self.headers)
241 if isinstance(item, str)
242 else item
243 )
244 format = self._guess_format(obj)
245 backend: Type[AbstractDocumentBackend]
246 if format not in format_options.keys():
247 _log.error(
248 f"Input document {obj.name} does not match any allowed format."
249 )
250 backend = _DummyBackend
251 else:
252 backend = format_options[format].backend
253
254 if isinstance(obj, Path):
255 yield InputDocument(
256 path_or_stream=obj,
257 format=format, # type: ignore[arg-type]
258 filename=obj.name,
259 limits=self.limits,
260 backend=backend,
261 )
262 elif isinstance(obj, DocumentStream):
263 yield InputDocument(
264 path_or_stream=obj.stream,
265 format=format, # type: ignore[arg-type]
266 filename=obj.name,
267 limits=self.limits,
268 backend=backend,
269 )
270 else:
271 raise RuntimeError(f"Unexpected obj type in iterator: {type(obj)}")
272
273 def _guess_format(self, obj: Union[Path, DocumentStream]) -> Optional[InputFormat]:
274 content = b"" # empty binary blob
275 formats: list[InputFormat] = []
276
277 if isinstance(obj, Path):
278 mime = filetype.guess_mime(str(obj))
279 if mime is None:
280 ext = obj.suffix[1:]
281 mime = _DocumentConversionInput._mime_from_extension(ext)
282 if mime is None: # must guess from
283 with obj.open("rb") as f:
284 content = f.read(1024) # Read first 1KB
285
286 elif isinstance(obj, DocumentStream):

Callers 1

convert_allMethod · 0.90

Calls 1

DocumentLimitsClass · 0.90

Tested by

no test coverage detected