Download document_manager.py from BajajHackathon/Bajaj_Hackathon_Space: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/spaces/BajajHackathon/Bajaj_Hackathon_Space/resolve/main/document_manager.py
- Command line
-
hf download hf://spaces/BajajHackathon/Bajaj_Hackathon_Space/document_manager.py
-
curl -L -o document_manager.py https://huggingface.co/spaces/BajajHackathon/Bajaj_Hackathon_Space/resolve/main/document_manager.py
1.32 kB
| import os | |
| import uuid | |
| import requests | |
| from typing import Tuple | |
| import shelve | |
| import tempfile | |
| class DocumentManager: | |
| def __init__(self, document_url: str): | |
| self.document_url = document_url | |
| self.DIR = os.path.join(tempfile.gettempdir(), "doc_cache") | |
| os.makedirs(self.DIR, exist_ok=True) | |
| cached_path = self._get_cached_path() | |
| if cached_path and os.path.exists(cached_path): | |
| print("Document already cached.") | |
| self.file_path = cached_path | |
| else: | |
| self.file_path, _ = self._download_and_cache() | |
| print("Document not cached.") | |
| def _get_cached_path(self) -> str: | |
| with shelve.open(os.path.join(self.DIR, 'cache')) as cache: | |
| return cache.get(self.document_url, '') | |
| def _download_and_cache(self) -> Tuple[str, str]: | |
| response = requests.get(self.document_url, timeout=60) | |
| response.raise_for_status() | |
| filename = f"{uuid.uuid4()}.pdf" | |
| file_path = os.path.join(self.DIR, filename) | |
| with open(file_path, "wb") as f: | |
| f.write(response.content) | |
| with shelve.open(os.path.join(self.DIR, 'cache')) as cache: | |
| cache[self.document_url] = file_path | |
| return file_path, filename | |
| def get_filepath(self) -> str: | |
| return self.file_path |