-
Notifications
You must be signed in to change notification settings - Fork 973
Feature/hfhub utils #2201
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
Open
ivelin
wants to merge
7
commits into
unit8co:master
Choose a base branch
from
ivelin:feature/hfhub-utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/hfhub utils #2201
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e0a4b24
Create hfhub.py util for HugginfFace Hub model storage
ivelin 1d3cae1
Update CHANGELOG.md
ivelin b8a5785
Update CHANGELOG.md
ivelin 9d50973
Update CHANGELOG.md
ivelin d05929f
add timeseries upload and download methods
ivelin 4e5d993
replace print with logger
ivelin 5bcb523
add token param to create_repo
ivelin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import pandas as pd | ||
| from dotenv import load_dotenv | ||
| import os | ||
| import tempfile | ||
| from typing import Optional | ||
| from darts import TimeSeries | ||
| from darts.models.forecasting.forecasting_model import ForecastingModel | ||
| from huggingface_hub import snapshot_download, upload_folder, create_repo | ||
|
|
||
|
|
||
| class HFHub: | ||
| """ | ||
| HuggingFace Hub integration using official HF API. | ||
| https://huggingface.co/docs/huggingface_hub/v0.20.3/en/guides/integrations | ||
| """ | ||
|
|
||
| def __init__(self, api_key: Optional[str] = None): | ||
| if api_key is None: | ||
| # load from .env file or OS vars if available | ||
| load_dotenv(override=True) | ||
| api_key = os.getenv("HF_TOKEN") | ||
| assert ( | ||
| api_key is not None | ||
| ), "Could not find HF_TOKEN in OS environment. Cannot interact with HF Hub." | ||
| self.HF_TOKEN = api_key | ||
|
|
||
| def upload_model( | ||
| self, | ||
| repo_id: str = None, | ||
| model: ForecastingModel = None, | ||
| private: Optional[bool] = True, | ||
| ): | ||
| # Create repo if not existing yet and get the associated repo_id | ||
| create_repo(repo_id=repo_id, repo_type="model", private=private, exist_ok=True) | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmpdirname: | ||
| # print("created temporary directory for model", tmpdirname) | ||
| model.save(path=f"{tmpdirname}/{model.model_name}") | ||
| upload_folder(repo_id=repo_id, folder_path=tmpdirname, token=self.HF_TOKEN) | ||
|
|
||
| def download_model( | ||
| self, | ||
| repo_id: str = None, | ||
| model_name: str = None, | ||
| model_class: object = None, | ||
| ) -> ForecastingModel: | ||
| with tempfile.TemporaryDirectory() as tmpdirname: | ||
| snapshot_download( | ||
| repo_id=repo_id, local_dir=tmpdirname, token=self.HF_TOKEN | ||
| ) | ||
| model = model_class.load(path=f"{tmpdirname}/{model_name}") | ||
| return model | ||
|
|
||
| def upload_timeseries( | ||
| self, | ||
| repo_id: str = None, | ||
| series: TimeSeries = None, | ||
| series_name: str = None, | ||
| private: Optional[bool] = True, | ||
| ): | ||
| # Create repo if not existing yet and get the associated repo_id | ||
| repo_info = create_repo( | ||
| repo_id=repo_id, repo_type="dataset", private=private, exist_ok=True | ||
| ) | ||
| # print(f"repo_info: ", repo_info) | ||
| df = series.pd_dataframe() | ||
| with tempfile.TemporaryDirectory() as tmpdirname: | ||
| df.to_parquet(path=f"{tmpdirname}/{series_name}.parquet") | ||
| upload_folder( | ||
| repo_id=repo_id, | ||
| repo_type="dataset", | ||
| folder_path=tmpdirname, | ||
| token=self.HF_TOKEN, | ||
| ) | ||
|
|
||
| def download_timeseries( | ||
| self, | ||
| repo_id: str = None, | ||
| series_name: str = None, | ||
| ) -> TimeSeries: | ||
| with tempfile.TemporaryDirectory() as tmpdirname: | ||
| snapshot_download( | ||
| repo_id=repo_id, | ||
| repo_type="dataset", | ||
| local_dir=tmpdirname, | ||
| token=self.HF_TOKEN, | ||
| ) | ||
| print(os.listdir(tmpdirname)) | ||
| df = pd.read_parquet( | ||
| f"{tmpdirname}/{series_name}.parquet", engine="pyarrow" | ||
| ) | ||
| ts = TimeSeries.from_dataframe(df) | ||
| return ts | ||
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.
Uh oh!
There was an error while loading. Please reload this page.