Skip to content

Commit

Permalink
Hotfix: huggingface loading issues (#81)
Browse files Browse the repository at this point in the history
* Hotfix: huggingface loading
  • Loading branch information
HCookie authored Dec 14, 2024
1 parent 3afa7ed commit b4b87f6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/anemoi/inference/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def _download_huggingfacehub(huggingface_config):
except ImportError as e:
raise ImportError("Could not import `huggingface_hub`, please run `pip install huggingface_hub`.") from e

if isinstance(huggingface_config, str):
huggingface_config = {"repo_id": huggingface_config}

if "filename" in huggingface_config:
config_path = hf_hub_download(**huggingface_config)
else:
Expand All @@ -48,7 +51,7 @@ class Checkpoint:
"""Represents an inference checkpoint."""

def __init__(self, path, *, patch_metadata=None):
self.path = path
self._path = path
self.patch_metadata = patch_metadata

def __repr__(self):
Expand All @@ -59,17 +62,17 @@ def path(self):
import json

try:
self._model = json.loads(self._model)
except TypeError:
pass

if isinstance(self._model, str):
return self._model
elif isinstance(self._model, dict):
if "huggingface" in self._model:
return _download_huggingfacehub(self._model["huggingface"])
path = json.loads(self._path)
except Exception:
path = self._path

if isinstance(path, (Path, str)):
return path
elif isinstance(path, dict):
if "huggingface" in path:
return _download_huggingfacehub(path["huggingface"])
pass
raise TypeError(f"Cannot parse model path: {self._model}. It must be a path or dict")
raise TypeError(f"Cannot parse model path: {path}. It must be a path or dict")

@cached_property
def _metadata(self):
Expand Down
2 changes: 1 addition & 1 deletion src/anemoi/inference/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Config:

description: str | None = None

checkpoint: str | Dict[Literal["huggingface"], Dict[str, Any]]
checkpoint: str | Dict[Literal["huggingface"], Dict[str, Any] | str]
"""A path to an Anemoi checkpoint file."""

date: str | int | datetime.datetime | None = None
Expand Down

0 comments on commit b4b87f6

Please sign in to comment.