@@ -264,11 +264,14 @@ def _verify_files_from_metadata(
264264 return False
265265
266266 def _collect_file_metadata (
267- model_dir : Path , repo_files : list [RepoFile ]
267+ model_dir : Path , revision : str , repo_files : list [RepoFile ]
268268 ) -> dict [str , dict [str , int | str ]]:
269269 meta : dict [str , dict [str , int | str ]] = {}
270270 file_info_map = {f .path : f for f in repo_files }
271- for file_path in model_dir .rglob ("*" ):
271+ # Only look at the downloaded revision's folder. The cache may also hold folders of
272+ # older revisions, whose files differ from the ones on the hub now, so checking
273+ # them would report a good download as corrupted.
274+ for file_path in (model_dir / "snapshots" / revision ).rglob ("*" ):
272275 if file_path .is_file () and file_path .name != cls .METADATA_FILE :
273276 relative_path = file_path .relative_to (model_dir )
274277 repo_file = file_info_map .get (_repo_relative_path (relative_path ))
@@ -399,9 +402,18 @@ def _save_file_metadata(model_dir: Path, meta: dict[str, dict[str, int | str]])
399402
400403 verified_metadata = False
401404
402- if snapshot_dir .exists () and metadata_file .exists ():
405+ # a forced download re-fetches every file, so show progress and re-collect the metadata
406+ if not kwargs .get ("force_download" ) and snapshot_dir .exists () and metadata_file .exists ():
403407 metadata = json .loads (metadata_file .read_text ())
404- verified_metadata = _verify_files_from_metadata (snapshot_dir , metadata , repo_files )
408+ # metadata lacking a requested file, e.g. a new model_file, can't vouch for it
409+ requested_files = {f .path for f in repo_files if f .path in allow_patterns }
410+ stored_files = {_repo_relative_path (Path (rel_path )) for rel_path in metadata }
411+ # empty metadata lists no files, so it can't vouch for the cached ones
412+ verified_metadata = (
413+ bool (metadata )
414+ and requested_files .issubset (stored_files )
415+ and _verify_files_from_metadata (snapshot_dir , metadata , repo_files )
416+ )
405417
406418 if verified_metadata :
407419 disable_progress_bars ()
@@ -418,7 +430,10 @@ def _save_file_metadata(model_dir: Path, meta: dict[str, dict[str, int | str]])
418430 not verified_metadata
419431 ): # metadata is not up-to-date, update it and check whether the files have been
420432 # downloaded correctly
421- metadata = _collect_file_metadata (snapshot_dir , repo_files )
433+ # result is <cache_dir>/.../snapshots/<revision>. Only the revision is taken from
434+ # it: the hub resolves cache_dir, so the rest of the path may not match ours.
435+ downloaded_revision = Path (result ).name
436+ metadata = _collect_file_metadata (snapshot_dir , downloaded_revision , repo_files )
422437
423438 download_successful = _verify_files_from_metadata (
424439 snapshot_dir , metadata , repo_files = []
@@ -428,7 +443,10 @@ def _save_file_metadata(model_dir: Path, meta: dict[str, dict[str, int | str]])
428443 "Files have been corrupted during downloading process. "
429444 "Please check your internet connection and try again."
430445 )
431- _save_file_metadata (snapshot_dir , metadata )
446+ # Empty means no file was checked, e.g. the files went to a local_dir instead of
447+ # the cache. Saving it would make later downloads skip this check.
448+ if metadata :
449+ _save_file_metadata (snapshot_dir , metadata )
432450
433451 return result
434452
0 commit comments