ts-dataset / scripts /hf_downloader.py
uniko1's picture
Upload scripts/hf_downloader.py with huggingface_hub
9ba9c5b verified
from huggingface_hub import HfApi
from os import path
from pathlib import Path
import os
class HFClient:
def __init__(self, api_token, endpoint="https://huggingface.co"):
self._api_token = api_token
self._api = HfApi(
endpoint=endpoint, # Can be a Private Hub endpoint.
token=api_token, # Token is not persisted on the machine.
)
def exec(self, method, *args, **kwargs):
return self._api[method](*args, **kwargs)
class HFRepository:
def __init__(self, name, client, namespace=None, type=None, local_path=None):
self._client = client
self._name = name.strip()
self._namespace = namespace.strip() if namespace is not None else None
if(type is not None and type not in ['dataset', 'space', 'model']):
raise Exception("Invalid repo type")
self._type = type
self.local_path = local_path
@property
def name(self):
return self._name
@property
def namespace(self):
return self._namespace
@property
def repoid(self):
return "{0}/{1}".format(self.namespace, self.name) if self.namespace is not None else self.name
@property
def files(self):
return self._client.list_repo_files(repo_id=self.repoid, repo_type=self._type)
def create(self, **kwargs):
#https://huggingface.co/docs/huggingface_hub/v0.27.1/en/package_reference/hf_api#huggingface_hub.HfApi.create_repo
return self._client.create_repo(repo_id=self.repoid, repo_type=self._type, **kwargs)
def download_file(self, name, local_path=None, **kwargs):
local_path = self.local_path if local_path is None else local_path
print("Downloading {0}".format(name))
return self._client.hf_hub_download(repo_id=self.repoid, repo_type=self._type, filename=name, local_dir=local_path, **kwargs)
def download_folder(self, path, **kwargs):
files = self.files
folder_contents = filter(lambda x: x.startswith(path), files)
results = []
for x in folder_contents:
results.append(self.download_file(x))
return results
def upload_file(self, file_or_path, repo_path):
return self._client.upload_file(
repo_id=self.repoid,
repo_type=self._type,
path_or_fileobj=file_or_path,
path_in_repo=repo_path
)
def upload_folder(self, folder_path, repo_path):
local_files_path = filter(lambda x: path.isfile(path.join(folder_path, x)), os.listdir(folder_path))
results = []
for x in local_files_path:
results.append(self.upload_file(path.join(folder_path, x), Path(repo_path, x).as_posix()))
return results
def main():
token = os.environ.get("HF_TOKEN", None)
if token is None:
raise Exception("No HF_TOKEN set")
repository = os.environ.get("HF_REPO_NAME", "ts-dataset")
repo_namespace = os.environ.get("HF_REPO_NAMESPACE", "uniko1")
repo_type = os.environ.get("HF_REPO_TYPE", "dataset")
target_folder = os.environ.get("REMOTE_PATH", None)
local_path = os.environ.get("LOCAL_PATH", "./")
if target_folder is None:
raise Exception("No REMOTE_PATH set")
api = HfApi(endpoint="https://huggingface.co", token=token)
rep = HFRepository(repository, api, repo_namespace, "dataset", local_path=local_path)
rep.download_folder(target_folder)
if __name__ == "__main__":
main()