Upload scripts/hf_downloader.py with huggingface_hub
Browse files- scripts/hf_downloader.py +91 -0
scripts/hf_downloader.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import HfApi
|
2 |
+
from os import path
|
3 |
+
from pathlib import Path
|
4 |
+
import os
|
5 |
+
|
6 |
+
class HFClient:
|
7 |
+
def __init__(self, api_token, endpoint="https://huggingface.co"):
|
8 |
+
self._api_token = api_token
|
9 |
+
self._api = HfApi(
|
10 |
+
endpoint=endpoint, # Can be a Private Hub endpoint.
|
11 |
+
token=api_token, # Token is not persisted on the machine.
|
12 |
+
)
|
13 |
+
def exec(self, method, *args, **kwargs):
|
14 |
+
return self._api[method](*args, **kwargs)
|
15 |
+
|
16 |
+
class HFRepository:
|
17 |
+
def __init__(self, name, client, namespace=None, type=None, local_path=None):
|
18 |
+
self._client = client
|
19 |
+
self._name = name.strip()
|
20 |
+
self._namespace = namespace.strip() if namespace is not None else None
|
21 |
+
if(type is not None and type not in ['dataset', 'space', 'model']):
|
22 |
+
raise Exception("Invalid repo type")
|
23 |
+
self._type = type
|
24 |
+
self.local_path = local_path
|
25 |
+
|
26 |
+
@property
|
27 |
+
def name(self):
|
28 |
+
return self._name
|
29 |
+
|
30 |
+
@property
|
31 |
+
def namespace(self):
|
32 |
+
return self._namespace
|
33 |
+
|
34 |
+
@property
|
35 |
+
def repoid(self):
|
36 |
+
return "{0}/{1}".format(self.namespace, self.name) if self.namespace is not None else self.name
|
37 |
+
|
38 |
+
@property
|
39 |
+
def files(self):
|
40 |
+
return self._client.list_repo_files(repo_id=self.repoid, repo_type=self._type)
|
41 |
+
|
42 |
+
def create(self, **kwargs):
|
43 |
+
#https://huggingface.co/docs/huggingface_hub/v0.27.1/en/package_reference/hf_api#huggingface_hub.HfApi.create_repo
|
44 |
+
return self._client.create_repo(repo_id=self.repoid, repo_type=self._type, **kwargs)
|
45 |
+
|
46 |
+
def download_file(self, name, local_path=None, **kwargs):
|
47 |
+
local_path = self.local_path if local_path is None else local_path
|
48 |
+
print("Downloading {0}".format(name))
|
49 |
+
return self._client.hf_hub_download(repo_id=self.repoid, repo_type=self._type, filename=name, local_dir=local_path, **kwargs)
|
50 |
+
|
51 |
+
def download_folder(self, path, **kwargs):
|
52 |
+
files = self.files
|
53 |
+
folder_contents = filter(lambda x: x.startswith(path), files)
|
54 |
+
results = []
|
55 |
+
for x in folder_contents:
|
56 |
+
results.append(self.download_file(x))
|
57 |
+
return results
|
58 |
+
def upload_file(self, file_or_path, repo_path):
|
59 |
+
return self._client.upload_file(
|
60 |
+
repo_id=self.repoid,
|
61 |
+
repo_type=self._type,
|
62 |
+
path_or_fileobj=file_or_path,
|
63 |
+
path_in_repo=repo_path
|
64 |
+
)
|
65 |
+
|
66 |
+
def upload_folder(self, folder_path, repo_path):
|
67 |
+
local_files_path = filter(lambda x: path.isfile(path.join(folder_path, x)), os.listdir(folder_path))
|
68 |
+
results = []
|
69 |
+
for x in local_files_path:
|
70 |
+
results.append(self.upload_file(path.join(folder_path, x), Path(repo_path, x).as_posix()))
|
71 |
+
return results
|
72 |
+
|
73 |
+
|
74 |
+
def main():
|
75 |
+
token = os.environ.get("HF_TOKEN", None)
|
76 |
+
if token is None:
|
77 |
+
raise Exception("No HF_TOKEN set")
|
78 |
+
repository = os.environ.get("HF_REPO_NAME", "ts-dataset")
|
79 |
+
repo_namespace = os.environ.get("HF_REPO_NAMESPACE", "uniko1")
|
80 |
+
repo_type = os.environ.get("HF_REPO_TYPE", "dataset")
|
81 |
+
target_folder = os.environ.get("REMOTE_PATH", None)
|
82 |
+
local_path = os.environ.get("LOCAL_PATH", "./")
|
83 |
+
|
84 |
+
if target_folder is None:
|
85 |
+
|
86 |
+
api = HfApi(endpoint="https://huggingface.co", token=API_TOKEN)
|
87 |
+
rep = HFRepository(repository, api, repo_namespace, "dataset", local_path=local_path)
|
88 |
+
rep.download_folder(target_folder)
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
main()
|