Skip to content

Commit 7e2c72b

Browse files
authored
Merge pull request #16 from OpenRL-Lab/huangshiyu
support upload file and directory
2 parents 02e96d1 + c994a26 commit 7e2c72b

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ Firstly, you need to login with `huggingface-cli login` (you can create or find
3232

3333
You can also use htool to upload datasets and models to huggingface.
3434

35+
- Upload a file with: `htool upload-file <local_filepath> <organization_or_username/repo_name>:<remote_filepath> -r <model/dataset>`. `-r` means the repo is a model or dataset repo. By default, it is a model repo.
36+
- For example: `htool upload-file README.md OpenRL/tizero:README.md`
37+
- For example: `htool upload-file README.md OpenRL/DeepFakeFace:README.md -r dataset`
38+
- Upload a directory with: `htool upload-dir <local_dirpath> <organization_or_username/repo_name>:<remote_dirpath> -r <model/dataset>`. `-r` means the repo is a model or dataset repo. By default, it is a model repo.
39+
- For example: `htool upload-dir ./tizero OpenRL/tizero:./`
40+
- For example: `htool upload-dir ./tizero/models OpenRL/tizero:./models`
41+
- For example: `htool upload-dir ./DeepFakeFace OpenRL/DeepFakeFace:./ -r dataset`
42+
- For example: `htool upload-dir ./DeepFakeFace/images OpenRL/DeepFakeFace:./images -r dataset`
3543
- Upload dataset with: `htool upload-data <local_dataset_dir> <organization_or_username/dataset_name>`
3644
- For example: `htool upload-data ./daily_dialog OpenRL/daily_dialog`
3745
- Upload model with: `htool upload-model <local_model_dir> <organization_or_username/model_name>`

huggingface_tool/cli/cli.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,53 @@ def upload_data(dataset_dir, dataset_name):
198198
else:
199199
uploader.logger.info("Dataset not valid")
200200

201+
@cli.command()
202+
@click.argument("file_path")
203+
@click.argument("remote_file_path")
204+
@click.option(
205+
"--repo_type",
206+
"-r",
207+
type=click.Choice(
208+
[
209+
"model",
210+
"dataset",
211+
]
212+
),
213+
default="model",
214+
help="repo type",
215+
)
216+
def upload_file(file_path, remote_file_path,repo_type):
217+
from huggingface_tool.uploaders.file_uploader import FileUploader
218+
219+
uploader = FileUploader(file_path, remote_file_path,repo_type)
220+
if uploader.check():
221+
uploader.push()
222+
else:
223+
uploader.logger.info("File not valid")
224+
225+
@cli.command()
226+
@click.argument("dir_path")
227+
@click.argument("remote_dir_path")
228+
@click.option(
229+
"--repo_type",
230+
"-r",
231+
type=click.Choice(
232+
[
233+
"model",
234+
"dataset",
235+
]
236+
),
237+
default="model",
238+
help="repo type",
239+
)
240+
def upload_dir(dir_path, remote_dir_path,repo_type):
241+
from huggingface_tool.uploaders.dir_uploader import DirUploader
242+
243+
uploader = DirUploader(dir_path, remote_dir_path,repo_type)
244+
if uploader.check():
245+
uploader.push()
246+
else:
247+
uploader.logger.info("Directory not valid")
201248

202249
@cli.command()
203250
@click.argument("model_dir")
@@ -209,4 +256,4 @@ def upload_model(model_dir, model_name):
209256
if uploader.check():
210257
uploader.push()
211258
else:
212-
uploader.logger.info("Dataset not valid")
259+
uploader.logger.info("Model not valid")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright 2023 The OpenRL Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
""""""
18+
from typing import Dict
19+
from abc import ABC, abstractmethod
20+
21+
from pathlib import Path
22+
from huggingface_tool.uploaders.base_uploader import BaseUploader
23+
24+
25+
class BaseAPIUploader(BaseUploader, ABC):
26+
def __init__(self, file_or_dir: str, remote_path: str, repo_type: str):
27+
super().__init__(file_or_dir,remote_path)
28+
self.info = self.get_info(remote_path)
29+
self.repo_type = repo_type
30+
31+
def get_info(self, remote_path: str) -> Dict[str, str]:
32+
split_string = remote_path.split(":")
33+
assert len(split_string) == 2
34+
repo_id = split_string[0]
35+
file_or_directory = split_string[1]
36+
return {"repo_id": repo_id, "file_or_directory": file_or_directory}
37+
38+
def check(self) -> bool:
39+
assert Path(
40+
self.file_or_dir
41+
).exists(), f"File or directory {self.file_or_dir} does not exist"
42+
return True
43+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright 2023 The OpenRL Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
""""""
18+
19+
from huggingface_hub import HfApi
20+
from huggingface_tool.uploaders.base_api_uploader import BaseAPIUploader
21+
22+
23+
class DirUploader(BaseAPIUploader):
24+
def _push(self) -> bool:
25+
try:
26+
api = HfApi()
27+
api.upload_folder(
28+
folder_path=self.file_or_dir,
29+
path_in_repo=self.info["file_or_directory"],
30+
repo_id=self.info["repo_id"],
31+
repo_type=self.repo_type, )
32+
except:
33+
print("Cannot upload to hub")
34+
return False
35+
return True
36+
37+
def _success_message(self):
38+
middle_type = "/datasets/" if self.repo_type == "dataset" else "/"
39+
print(
40+
f"Directory is uploaded to https://huggingface.co{middle_type}{self.info['repo_id']}"
41+
)
42+
43+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright 2023 The OpenRL Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
""""""
18+
19+
from huggingface_hub import HfApi
20+
from huggingface_tool.uploaders.base_api_uploader import BaseAPIUploader
21+
22+
23+
class FileUploader(BaseAPIUploader):
24+
def _push(self) -> bool:
25+
try:
26+
api = HfApi()
27+
api.upload_file(
28+
path_or_fileobj=self.file_or_dir,
29+
path_in_repo=self.info["file_or_directory"],
30+
repo_id=self.info["repo_id"],
31+
repo_type=self.repo_type, )
32+
except:
33+
print("Cannot upload to hub")
34+
return False
35+
return True
36+
37+
def _success_message(self):
38+
middle_type = "/datasets/" if self.repo_type == "dataset" else "/"
39+
print(
40+
f"File is uploaded to https://huggingface.co{middle_type}{self.info['repo_id']}"
41+
)
42+

0 commit comments

Comments
 (0)