-
Notifications
You must be signed in to change notification settings - Fork 10
Add versioning to uploads #269
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| - bump: patch | ||
| changes: | ||
| added: | ||
| - Versioning to dataset uploads. |
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,104 @@ | ||
| from typing import List | ||
| from huggingface_hub import HfApi, CommitOperationAdd | ||
| from huggingface_hub.errors import RevisionNotFoundError | ||
| from google.cloud import storage | ||
| from pathlib import Path | ||
| from importlib import metadata | ||
| import google.auth | ||
| import logging | ||
|
|
||
|
|
||
| def upload_data_files( | ||
| files: List[str], | ||
| gcs_bucket_name: str = "policyengine-us-data", | ||
| hf_repo_name: str = "policyengine/policyengine-us-data", | ||
| hf_repo_type: str = "model", | ||
| version: str = None, | ||
| ): | ||
| if version is None: | ||
| version = metadata.version("policyengine-us-data") | ||
|
|
||
| upload_files_to_hf( | ||
| files=files, | ||
| version=version, | ||
| hf_repo_name=hf_repo_name, | ||
| hf_repo_type=hf_repo_type, | ||
| ) | ||
|
|
||
| upload_files_to_gcs( | ||
| files=files, | ||
| version=version, | ||
| gcs_bucket_name=gcs_bucket_name, | ||
| ) | ||
|
|
||
|
|
||
| def upload_files_to_hf( | ||
| files: List[str], | ||
| version: str, | ||
| hf_repo_name: str = "policyengine/policyengine-us-data", | ||
| hf_repo_type: str = "model", | ||
| ): | ||
| """ | ||
| Upload files to Hugging Face repository and tag the commit with the version. | ||
| """ | ||
| api = HfApi() | ||
| hf_operations = [] | ||
|
|
||
| for file_path in files: | ||
| file_path = Path(file_path) | ||
| if not file_path.exists(): | ||
| raise ValueError(f"File {file_path} does not exist.") | ||
| hf_operations.append( | ||
| CommitOperationAdd( | ||
| path_in_repo=file_path.name, | ||
| path_or_fileobj=str(file_path), | ||
| ) | ||
| ) | ||
| commit_info = api.create_commit( | ||
| repo_id=hf_repo_name, | ||
| operations=hf_operations, | ||
| repo_type=hf_repo_type, | ||
| commit_message=f"Upload data files for version {version}", | ||
| ) | ||
| logging.info(f"Uploaded files to Hugging Face repository {hf_repo_name}.") | ||
|
|
||
| # Tag commit with version | ||
| api.create_tag( | ||
| repo_id=hf_repo_name, | ||
| tag=version, | ||
| revision=commit_info.oid, | ||
| repo_type=hf_repo_type, | ||
| ) | ||
| logging.info( | ||
| f"Tagged commit with {version} in Hugging Face repository {hf_repo_name}." | ||
| ) | ||
|
|
||
|
|
||
| def upload_files_to_gcs( | ||
| files: List[str], | ||
| version: str, | ||
| gcs_bucket_name: str = "policyengine-us-data", | ||
| ): | ||
| """ | ||
| Upload files to Google Cloud Storage and set metadata with the version. | ||
| """ | ||
| credentials, project_id = google.auth.default() | ||
| storage_client = storage.Client( | ||
| credentials=credentials, project=project_id | ||
| ) | ||
| bucket = storage_client.bucket(gcs_bucket_name) | ||
|
|
||
| for file_path in files: | ||
| file_path = Path(file_path) | ||
| blob = bucket.blob(file_path.name) | ||
| blob.upload_from_filename(file_path) | ||
| logging.info( | ||
| f"Uploaded {file_path.name} to GCS bucket {gcs_bucket_name}." | ||
| ) | ||
|
|
||
| # Set metadata | ||
| blob.metadata = {"version": version} | ||
| blob.patch() | ||
| logging.info( | ||
| f"Set metadata for {file_path.name} in GCS bucket {gcs_bucket_name}." | ||
| ) | ||
Oops, something went wrong.
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.