Fix path reconstruction in ModelZoo download#44
Open
C-Achard wants to merge 5 commits intoDeepLabCut:mainfrom
Open
Fix path reconstruction in ModelZoo download#44C-Achard wants to merge 5 commits intoDeepLabCut:mainfrom
C-Achard wants to merge 5 commits intoDeepLabCut:mainfrom
Conversation
Replace os.rename with shutil.copy2 when handling downloaded files to avoid cross-filesystem rename errors and to preserve metadata. Ensure target_dir exists, use the path returned by hf_hub_download instead of assuming cache folder layout, normalize rename_mapping into a mapping, and call _handle_downloaded_file with the actual downloaded file path. Track and remove the HF cache folder safely (ignore_errors) only if present. These changes make HuggingFace model downloads more reliable across environments.
2 tasks
Use a temporary directory for HuggingFace cache (tempfile.TemporaryDirectory) so HF cache folders are not created under the target_dir. Improve tar.gz handling in _handle_downloaded_file by extracting members with tar.extractfile and shutil.copyfileobj (and fall back to shutil.copy2 for non-tar files), and remove the previous symlink-resolution and explicit HF-folder removal logic. Update hf_hub_download call to use repo_id/filename named args and thread per-file rename mappings through to the extractor. Adjusted tests to reflect that the HF cache is no longer created inside the target directory and to ensure the final artifact still exists.
Delete the remove_hf_folder parameter from download_huggingface_model (and its docstring) and remove the related unit test. Also modify the tar extraction logic in _handle_downloaded_file by switching the TarInfo check from member.isdir() to member.isfile(), changing which tar members are processed during extraction.
Open archives with a permissive compression mode (r:*) and only extract regular files. Skip members with empty basenames or where extractfile() returns None, use a context manager for file streams, and raise a ReadError if no regular files were extracted to fail loudly. Preserve previous fallback: if the file isn't an archive, copy it as a direct model file. Also update example usage to remove the explicit remove_hf_folder argument.
Author
|
Update : made breaking changes by removing the remove_hf_folder arg, which is currently unused in DLC and is no longer necessary as the cache dir is no longer the target dir. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scope
This PR aims to fix DeepLabCut/DeepLabCut#3256, which pointed at an inexistent cache folder due to incorrect reconstruction.
Underlying issue
Previously, after calling
hf_hub_download(), the code rebuilt the expected cache location undermodels--.../snapshots/<commit>/...instead of using the path returned by the download function itself. Sincehf_hub_download()already returns the resolved local file path, reconstructing the cache layout was unnecessary and could point to a non-existent location.Fix
Instead, this patch uses the path returned by the hf download function itself and copies it to the target directory, skipping the need for reconstruction.
Also adds tests for the newly modified code that cover behavior more extensively.
TODO