|
| 1 | +""" |
| 2 | +HuggingFace Repository Duplication Module |
| 3 | +
|
| 4 | +This module provides functionality for duplicating repositories on HuggingFace Hub via command line interface. |
| 5 | +It includes commands and utilities to copy repositories while preserving their content and structure. |
| 6 | +""" |
| 7 | + |
| 8 | +import click |
| 9 | +from huggingface_hub import configure_http_backend |
| 10 | + |
| 11 | +from .base import CONTEXT_SETTINGS, command_wrap |
| 12 | +from ..operate import hf_repo_duplicate |
| 13 | +from ..operate.base import REPO_TYPES, RepoTypeTyping |
| 14 | +from ..utils import get_requests_session |
| 15 | + |
| 16 | + |
| 17 | +def _add_duplicate_subcommand(cli: click.Group) -> click.Group: |
| 18 | + """ |
| 19 | + Add the duplicate subcommand to the CLI group. |
| 20 | +
|
| 21 | + This function adds a 'duplicate' command to the provided Click CLI group that enables |
| 22 | + repository duplication on HuggingFace Hub. It configures all necessary options and |
| 23 | + handles the execution flow. |
| 24 | +
|
| 25 | + :param cli: The Click command group to add the duplicate command to |
| 26 | + :type cli: click.Group |
| 27 | +
|
| 28 | + :return: The modified CLI group with the duplicate command added |
| 29 | + :rtype: click.Group |
| 30 | + """ |
| 31 | + |
| 32 | + @cli.command('duplicate', help='Duplicate Repository on HuggingFace.\n\n' |
| 33 | + 'Set environment $HF_TOKEN to use your own access token.', |
| 34 | + context_settings=CONTEXT_SETTINGS) |
| 35 | + @click.option('-r', '--src_repo', 'src_repo_id', type=str, required=True, |
| 36 | + help='Repository to be duplicated.') |
| 37 | + @click.option('-R', '--dst_repo', 'dst_repo_id', type=str, required=True, |
| 38 | + help='Repository to duplicate to.') |
| 39 | + @click.option('-t', '--type', 'repo_type', type=click.Choice(REPO_TYPES), default='dataset', |
| 40 | + help='Type of the HuggingFace repository.', show_default=True) |
| 41 | + @click.option('--private', 'private', type=bool, is_flag=True, default=False, |
| 42 | + help='Duplicate as a private repository.') |
| 43 | + @command_wrap() |
| 44 | + def duplicate(src_repo_id: str, dst_repo_id: str, repo_type: RepoTypeTyping, private: bool): |
| 45 | + """ |
| 46 | + Duplicate a HuggingFace repository to a new location. |
| 47 | +
|
| 48 | + This command duplicates an existing HuggingFace repository to a new destination, |
| 49 | + with options to specify the repository type and visibility settings. |
| 50 | +
|
| 51 | + :param src_repo_id: The source repository ID to duplicate from |
| 52 | + :type src_repo_id: str |
| 53 | + :param dst_repo_id: The destination repository ID to duplicate to |
| 54 | + :type dst_repo_id: str |
| 55 | + :param repo_type: The type of repository (dataset, model, etc.) |
| 56 | + :type repo_type: RepoTypeTyping |
| 57 | + :param private: Whether the duplicated repository should be private |
| 58 | + :type private: bool |
| 59 | +
|
| 60 | + :raises: Various exceptions from huggingface_hub based on operation status |
| 61 | + """ |
| 62 | + configure_http_backend(get_requests_session) |
| 63 | + hf_repo_duplicate( |
| 64 | + src_repo_id=src_repo_id, |
| 65 | + dst_repo_id=dst_repo_id, |
| 66 | + repo_type=repo_type, |
| 67 | + private=private, |
| 68 | + ) |
| 69 | + |
| 70 | + return cli |
0 commit comments