-
Notifications
You must be signed in to change notification settings - Fork 15
fix: support offline mode in model import #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,18 +102,38 @@ def download_file( | |
| ) -> Path: | ||
| if progress_callback is not None: | ||
| progress_callback(DownloadingFileEvent(file_spec)) | ||
| result = huggingface_hub.hf_hub_download( | ||
| repo_id=file_spec.repo or model_repo, | ||
| local_dir=output_dir, | ||
| filename=file_spec.filename, | ||
| ) | ||
| try: | ||
| result = huggingface_hub.hf_hub_download( | ||
| repo_id=file_spec.repo or model_repo, | ||
| local_dir=output_dir, | ||
| filename=file_spec.filename, | ||
| ) | ||
| except (huggingface_hub.errors.OfflineModeIsEnabled, OSError): | ||
| result = huggingface_hub.try_to_load_from_cache( | ||
| repo_id=file_spec.repo or model_repo, | ||
| filename=file_spec.filename, | ||
| ) | ||
| if result is None or isinstance(result, str) and not Path(result).exists(): | ||
| raise | ||
| if progress_callback is not None: | ||
| progress_callback(FinishedDownloadingFileEvent(file_spec)) | ||
| return Path(result) | ||
|
|
||
|
|
||
| def _list_cached_repo_files(model_repo: str) -> list[str]: | ||
| """List files available in the local HuggingFace cache for a given repo.""" | ||
| cache_info = huggingface_hub.scan_cache_dir() | ||
| for repo_info in cache_info.repos: | ||
| if repo_info.repo_id == model_repo: | ||
| return [f.file_name for rev in repo_info.revisions for f in rev.files] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The offline fallback unions files from all cached revisions ( Useful? React with 👍 / 👎. |
||
| return [] | ||
|
|
||
|
|
||
| def list_weight_files(model_repo: str, weights_type: WeightsType) -> list[FileSpec]: | ||
| all_files = huggingface_hub.list_repo_files(model_repo) | ||
| try: | ||
| all_files = huggingface_hub.list_repo_files(model_repo) | ||
| except (huggingface_hub.errors.OfflineModeIsEnabled, OSError): | ||
| all_files = _list_cached_repo_files(model_repo) | ||
| match weights_type: | ||
| case WeightsType.SAFETENSORS: | ||
| return [FileSpec(filename) for filename in all_files if filename.endswith(".safetensors")] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try_to_load_from_cachecan return the_CACHED_NO_EXISTsentinel (not justNoneor a path string), but the current guard only rejectsNoneand missing string paths. In that case the code falls through toPath(result), which raisesTypeErrorand breaks offline imports in caches that recorded a missing file (for example after a previous lookup). Treat the sentinel/non-string return as a cache miss and re-raise the original download error instead.Useful? React with 👍 / 👎.