Download document_processor.py from sesharaghav/ai-tutor-document-processor: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/spaces/sesharaghav/ai-tutor-document-processor/resolve/main/document_processor.py
- Command line
-
hf download hf://spaces/sesharaghav/ai-tutor-document-processor/document_processor.py
-
curl -L -o document_processor.py https://huggingface.co/spaces/sesharaghav/ai-tutor-document-processor/resolve/main/document_processor.py
1 kB
| 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, | |
| } |