Skip to content
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

Implemented CLI upload functionality #1618

Merged
merged 19 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/source/en/guides/upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,60 @@ but before that, all previous logs on the repo on deleted. All of this in a sing
... )
```

## Upload from the CLI

You can use the `huggingface-cli upload` command from the terminal to directly upload files to the Hub. Internally
it uses the same [`upload_file`] and [`upload_folder`] helpers described above.

You can either upload a single file or an entire folder:

```bash
# Usage: huggingface-cli upload [repo_id] [local_path] [path_in_repo]
>>> huggingface-cli upload Wauplin/my-cool-model ./models/model.safetensors model.safetensors
https://huggingface.co/Wauplin/my-cool-model/blob/main/model.safetensors

>>> huggingface-cli upload Wauplin/my-cool-model ./models .
https://huggingface.co/Wauplin/my-cool-model/tree/main
```

`local_path` and `path_in_repo` are optional and can be implicitly inferred. If `local_path` is not set, the tool will
check if a local folder or file has the same name as the `repo_id`. If that's the case, its content will be uploaded.
Otherwise, an exception is raised asking the user to explicitly set `local_path`. In any case, if `path_in_repo` is not
set, files are uploaded at the root of the repo.

```bash
# Upload file at root
huggingface-cli upload my-cool-model model.safetensors

# Upload directory at root
huggingface-cli upload my-cool-model ./models

# Upload `my-cool-model/` directory if it exist, raise otherwise
huggingface-cli upload my-cool-model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this supposed to do exactly? Reading it I understand it to be the following: upload the my-cool-model folder and its content to the <user>/my-cool-model repository, the contents of the folder being at the root of the repository.

However, it doesn't seem to be the case: it seems to be uploading all folders and files in the current working directory to the root of the <user>/my-cool-model repository. Is this expected?

Reproducer:

mkdir hfh-test && cd hfh-test
mkdir folder-1
touch folder-1/file.txt
mkdir folder-2
touch folder-2/file.txt

# Current repo structure
# ./
# ../
# folder-1/
#     file.txt
# folder-2/
#     file.txt

huggingface-cli upload folder-1

I would expect folder-1 to contain the contents of folder-1, but it contains everything that was in the working directory: https://huggingface.co/lysandre/folder-1/tree/main

I would expect the command to do what it just did to be huggingface-cli upload folder-1 .

Copy link
Contributor

@Wauplin Wauplin Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @LysandreJik. My initial thought was that huggingface-cli upload my-cool-model would upload the current directory to the Hub (at root level). So huggingface-cli upload my-cool-model being equivalent to huggingface-cli upload my-cool-model . . . A bit as when you have a local repo and you do git add . && git commit -m "something" && git push.


That being said, the use case you've just described (e.g. repo_id == name of a local folder) is perfectly valid as well. I've updated the CLI in that sense. Now the behavior looks like this:

# Current repo structure
# ./
# ../
# folder-1/
#     file.txt
# folder-2/
#     file.txt

# Upload "./folder-1" content to "Wauplin/folder-1"
# On the hub: file.txt
>>> huggingface-cli upload folder-1

# Upload "./folder-1" content to "huggingface/folder-1"
# On the hub: file.txt
>>> huggingface-cli upload huggingface/folder-1

# Upload "./folder-1" and "./folder-2" to "Wauplin/my-cool-model"
# On the hub: folder-1/file.txt and folder-2/file.txt
>>> huggingface-cli upload my-cool-model .

# Upload "./folder-1" and "./folder-2" to "Wauplin/my-cool-model" under "./data"
# On the hub: data/folder-1/file.txt and data/folder-2/file.txt
>>> huggingface-cli upload my-cool-model . data/

# Raise exception => user must set local path explicitly 
>>> huggingface-cli upload folder-3

```

By default, the token saved locally (using `huggingface-cli login`) will be used. If you want to authenticate explicitly,
use the `--token` option:

```bash
huggingface-cli upload my-cool-model --token=hf_****
```

When uploading a folder, you can use the `--include` and `--exclude` arguments to filter the files to upload. You can
also use `--delete` to delete existing files on the Hub.

```bash
# Sync local Space with Hub (upload new files except from logs/, delete removed files)
huggingface-cli upload Wauplin/space-example --repo-type=space --exclude="/logs/*" --delete="*" --commit-message="Sync local Space with Hub"
```

Finally, you can also schedule a job that will upload your files regularly (see [scheduled uploads](#scheduled-uploads)).

```bash
# Upload new logs every 10 minutes
huggingface-cli upload training-model logs/ --every=10
Wauplin marked this conversation as resolved.
Show resolved Hide resolved
```

## Advanced features

In most cases, you won't need more than [`upload_file`] and [`upload_folder`] to upload your files to the Hub.
Expand Down
3 changes: 3 additions & 0 deletions src/huggingface_hub/commands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from argparse import Namespace, _SubParsersAction
from typing import List, Literal, Optional, Union

from huggingface_hub import logging
from huggingface_hub._snapshot_download import snapshot_download
from huggingface_hub.commands import BaseHuggingfaceCLICommand
from huggingface_hub.file_download import hf_hub_download
Expand Down Expand Up @@ -151,7 +152,9 @@ def run(self) -> None:
print(self._download()) # Print path to downloaded files
enable_progress_bars()
else:
logging.set_verbosity_info()
print(self._download()) # Print path to downloaded files
logging.set_verbosity_warning()

def _download(self) -> str:
# Warn user if patterns are ignored
Expand Down
4 changes: 3 additions & 1 deletion src/huggingface_hub/commands/huggingface_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from huggingface_hub.commands.env import EnvironmentCommand
from huggingface_hub.commands.lfs import LfsCommands
from huggingface_hub.commands.scan_cache import ScanCacheCommand
from huggingface_hub.commands.upload import UploadCommand
from huggingface_hub.commands.user import UserCommands


Expand All @@ -30,10 +31,11 @@ def main():
# Register commands
EnvironmentCommand.register_subcommand(commands_parser)
UserCommands.register_subcommand(commands_parser)
UploadCommand.register_subcommand(commands_parser)
DownloadCommand.register_subcommand(commands_parser)
LfsCommands.register_subcommand(commands_parser)
ScanCacheCommand.register_subcommand(commands_parser)
DeleteCacheCommand.register_subcommand(commands_parser)
DownloadCommand.register_subcommand(commands_parser)

# Let's go
args = parser.parse_args()
Expand Down
271 changes: 271 additions & 0 deletions src/huggingface_hub/commands/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
# coding=utf-8
# Copyright 2023-present, the HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Contains command to upload a repo or file with the CLI.

Usage:
# Upload file (implicit)
huggingface-cli upload my-cool-model ./my-cool-model.safetensors

# Upload file (explicit)
huggingface-cli upload my-cool-model ./my-cool-model.safetensors model.safetensors

# Upload directory (implicit). If `my-cool-model/` is a directory it will be uploaded, otherwise an exception is raised.
huggingface-cli upload my-cool-model

# Upload directory (explicit)
huggingface-cli upload my-cool-model ./models/my-cool-model .

# Upload filtered directory (example: tensorboard logs except for the last run)
huggingface-cli upload my-cool-model ./model/training /logs --include "*.tfevents.*" --exclude "*20230905*"

# Upload private dataset
huggingface-cli upload Wauplin/my-cool-dataset ./data . --repo-type=dataset --private

# Upload with token
huggingface-cli upload Wauplin/my-cool-model --token=hf_****

# Sync local Space with Hub (upload new files, delete removed files)
huggingface-cli upload Wauplin/space-example --repo-type=space --exclude="/logs/*" --delete="*" --commit-message="Sync local Space with Hub"

# Schedule commits every 30 minutes
huggingface-cli upload Wauplin/my-cool-model --every=30
"""
import os
import time
import warnings
from argparse import Namespace, _SubParsersAction
from typing import List, Optional

from huggingface_hub import logging
from huggingface_hub._commit_scheduler import CommitScheduler
from huggingface_hub.commands import BaseHuggingfaceCLICommand
from huggingface_hub.hf_api import create_repo, upload_file, upload_folder
from huggingface_hub.utils import disable_progress_bars, enable_progress_bars


class UploadCommand(BaseHuggingfaceCLICommand):
@staticmethod
def register_subcommand(parser: _SubParsersAction):
upload_parser = parser.add_parser("upload", help="Upload a file or a folder to a repo on the Hub")
upload_parser.add_argument(
"repo_id", type=str, help="The ID of the repo to upload to (e.g. `username/repo-name`)."
)
upload_parser.add_argument(
"local_path", nargs="?", help="Local path to the file or folder to upload. Defaults to current directory."
)
upload_parser.add_argument(
"path_in_repo",
nargs="?",
help="Path of the file or folder in the repo. Defaults to the relative path of the file or folder.",
)
upload_parser.add_argument(
"--repo-type",
choices=["model", "dataset", "space"],
default="model",
help="Type of the repo to upload to (e.g. `dataset`).",
)
upload_parser.add_argument(
"--revision",
type=str,
help="An optional Git revision id which can be a branch name, a tag, or a commit hash.",
)
upload_parser.add_argument(
"--private",
action="store_true",
help=(
"Whether to create a private repo if repo doesn't exist on the Hub. Ignored if the repo already"
" exists."
),
)
upload_parser.add_argument("--include", nargs="*", type=str, help="Glob patterns to match files to upload.")
upload_parser.add_argument(
"--exclude", nargs="*", type=str, help="Glob patterns to exclude from files to upload."
)
upload_parser.add_argument(
"--delete",
nargs="*",
type=str,
help="Glob patterns for file to be deleted from the repo while committing.",
)
upload_parser.add_argument(
"--commit-message", type=str, help="The summary / title / first line of the generated commit."
)
upload_parser.add_argument("--commit-description", type=str, help="The description of the generated commit.")
upload_parser.add_argument(
"--create-pr", action="store_true", help="Whether to upload content as a new Pull Request."
)
upload_parser.add_argument(
"--every",
type=float,
help="If set, a background job is scheduled to create commits every `every` minutes.",
)
upload_parser.add_argument(
"--token", type=str, help="A User Access Token generated from https://huggingface.co/settings/tokens"
)
upload_parser.add_argument(
"--quiet",
action="store_true",
help="If True, progress bars are disabled and only the path to the uploaded files is printed.",
)
upload_parser.set_defaults(func=UploadCommand)

def __init__(self, args: Namespace) -> None:
self.repo_id: str = args.repo_id
self.repo_type: Optional[str] = args.repo_type
self.revision: Optional[str] = args.revision
self.private: bool = args.private

self.include: Optional[List[str]] = args.include
self.exclude: Optional[List[str]] = args.exclude
self.delete: Optional[List[str]] = args.delete

self.commit_message: Optional[str] = args.commit_message
self.commit_description: Optional[str] = args.commit_description
self.create_pr: bool = args.create_pr
self.token: Optional[str] = args.token
self.quiet: bool = args.quiet # disable warnings and progress bars

# Check `--every` is valid
if args.every is not None and args.every <= 0:
raise ValueError(f"`every` must be a positive value (got '{args.every}')")
self.every: Optional[float] = args.every

# Resolve `local_path` and `path_in_repo`
repo_name: str = args.repo_id.split("/")[-1] # e.g. "Wauplin/my-cool-model" => "my-cool-model"
self.local_path: str
self.path_in_repo: str
if args.local_path is None and os.path.isfile(repo_name):
# Implicit case 1: user provided only a repo_id which happen to be a local file as well => upload it with same name
self.local_path = repo_name
self.path_in_repo = repo_name
elif args.local_path is None and os.path.isdir(repo_name):
# Implicit case 2: user provided only a repo_id which happen to be a local folder as well => upload it at root
self.local_path = repo_name
self.path_in_repo = "."
elif args.local_path is None:
# Implicit case 3: user provided only a repo_id that does not match a local file or folder
# => the user must explicitly provide a local_path => raise exception
raise ValueError(f"'{repo_name}' is not a local file or folder. Please set `local_path` explicitly.")
elif args.path_in_repo is None and os.path.isfile(args.local_path):
# Explicit local path to file, no path in repo => upload it at root with same name
self.local_path = args.local_path
self.path_in_repo = os.path.basename(args.local_path)
elif args.path_in_repo is None:
# Explicit local path to folder, no path in repo => upload at root
self.local_path = args.local_path
self.path_in_repo = "."
else:
# Finally, if both paths are explicit
self.local_path = args.local_path
self.path_in_repo = args.path_in_repo

def run(self) -> None:
if self.quiet:
disable_progress_bars()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
print(self._upload())
enable_progress_bars()
else:
logging.set_verbosity_info()
print(self._upload())
logging.set_verbosity_warning()

def _upload(self) -> str:
if os.path.isfile(self.local_path):
if self.include is not None and len(self.include) > 0:
warnings.warn("Ignoring `--include` since a single file is uploaded.")
if self.exclude is not None and len(self.exclude) > 0:
warnings.warn("Ignoring `--exclude` since a single file is uploaded.")
if self.delete is not None and len(self.delete) > 0:
warnings.warn("Ignoring `--delete` since a single file is uploaded.")

# Schedule commits if `every` is set
if self.every is not None:
if os.path.isfile(self.local_path):
# If file => watch entire folder + use allow_patterns
folder_path = os.path.dirname(self.local_path)
path_in_repo = (
self.path_in_repo[: -len(self.local_path)] # remove filename from path_in_repo
if self.path_in_repo.endswith(self.local_path)
else self.path_in_repo
)
allow_patterns = [self.local_path]
ignore_patterns = []
else:
folder_path = self.local_path
path_in_repo = self.path_in_repo
allow_patterns = self.include or []
ignore_patterns = self.exclude or []
if self.delete is not None and len(self.delete) > 0:
warnings.warn("Ignoring `--delete` when uploading with scheduled commits.")

scheduler = CommitScheduler(
folder_path=folder_path,
repo_id=self.repo_id,
repo_type=self.repo_type,
revision=self.revision,
allow_patterns=allow_patterns,
ignore_patterns=ignore_patterns,
path_in_repo=path_in_repo,
private=self.private,
every=self.every,
token=self.token,
)
print(f"Scheduling commits every {self.every} minutes to {scheduler.repo_id}.")
try: # Block main thread until KeyboardInterrupt
while True:
time.sleep(100)
except KeyboardInterrupt:
scheduler.stop()
return "Stopped scheduled commits."

# Otherwise, create repo and proceed with the upload
if not os.path.isfile(self.local_path) and not os.path.isdir(self.local_path):
raise FileNotFoundError(f"No such file or directory: '{self.local_path}'.")
repo_id = create_repo(
repo_id=self.repo_id, repo_type=self.repo_type, exist_ok=True, private=self.private, token=self.token
).repo_id

# File-based upload
if os.path.isfile(self.local_path):
return upload_file(
path_or_fileobj=self.local_path,
path_in_repo=self.path_in_repo,
repo_id=repo_id,
repo_type=self.repo_type,
revision=self.revision,
token=self.token,
commit_message=self.commit_message,
commit_description=self.commit_description,
create_pr=self.create_pr,
)

# Folder-based upload
else:
return upload_folder(
folder_path=self.local_path,
path_in_repo=self.path_in_repo,
repo_id=repo_id,
repo_type=self.repo_type,
revision=self.revision,
token=self.token,
commit_message=self.commit_message,
commit_description=self.commit_description,
create_pr=self.create_pr,
allow_patterns=self.include,
ignore_patterns=self.exclude,
delete_patterns=self.delete,
)
Loading
Loading