File size: 1,004 Bytes
7826d20 53843a5 ac57c76 7826d20 53843a5 ac57c76 53843a5 ac57c76 53843a5 7826d20 53843a5 cd7e591 7826d20 53843a5 7826d20 ac57c76 cd01d80 53843a5 ac57c76 53843a5 ac57c76 53843a5 7826d20 | 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 | from pathlib import Path
from typing import Any, Dict
from docling.document_converter import DocumentConverter
class DocumentProcessor:
SUPPORTED_EXTENSIONS = {
".pdf",
".docx",
".pptx",
".html",
".htm",
".md",
".txt",
}
def __init__(self):
self.converter = DocumentConverter()
def process(
self,
file_path: str,
) -> Dict[str, Any]:
path = Path(file_path)
extension = path.suffix.lower()
if extension not in self.SUPPORTED_EXTENSIONS:
raise ValueError(
f"Unsupported file type: {extension}"
)
result = self.converter.convert(
str(path)
)
document = result.document
raw_json = document.export_to_dict()
markdown = document.export_to_markdown()
return {
"document": document,
"raw_json": raw_json,
"markdown": markdown,
} |