Download pdf_loader.py from BajajHackathon/Bajaj_Hackathon_Space: direct link, hf CLI and curl.
- Browser
- Download file 881 Bytes
-
https://huggingface.co/spaces/BajajHackathon/Bajaj_Hackathon_Space/resolve/main/pdf_loader.py
- Command line
-
hf download hf://spaces/BajajHackathon/Bajaj_Hackathon_Space/pdf_loader.py
-
curl -L -o pdf_loader.py https://huggingface.co/spaces/BajajHackathon/Bajaj_Hackathon_Space/resolve/main/pdf_loader.py
881 Bytes
| import os | |
| import pymupdf4llm | |
| from langchain_core.documents import Document | |
| from typing import List | |
| class PDFLoader: | |
| def __init__(self, file_path: str): | |
| if not os.path.exists(file_path): | |
| raise FileNotFoundError(f"The file {file_path} does not exist.") | |
| self.file_path = file_path | |
| def load(self) -> List[Document]: | |
| documents = [] | |
| page_markdowns = pymupdf4llm.to_markdown(self.file_path, page_chunks=True) | |
| for i, md_output in enumerate(page_markdowns): | |
| content = str(md_output) | |
| doc = Document( | |
| page_content=content, | |
| metadata={ | |
| "page": i + 1, | |
| "source": self.file_path, | |
| "source_file": os.path.basename(self.file_path), | |
| } | |
| ) | |
| documents.append(doc) | |
| return documents |