|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from fnmatch import fnmatch |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any, Dict, List, Optional, Union |
| 6 | + |
| 7 | +from huggingface_hub.utils import filter_repo_objects |
| 8 | + |
| 9 | +from pycsghub.commit_ops import build_payload, CommitOperationAdd, CommitOperationDelete |
| 10 | +from pycsghub.csghub_api import CsgHubApi |
| 11 | +from pycsghub.file_download import file_download as csghub_file_download |
| 12 | +from pycsghub.snapshot_download import snapshot_download as csghub_snapshot_download |
| 13 | +from pycsghub.utils import get_endpoint, get_repo_info |
| 14 | +from pycsghub.cmd import repo |
| 15 | + |
| 16 | +class CsghubApi: |
| 17 | + def __init__(self, token: Optional[str] = None, endpoint: Optional[str] = None, user_name: Optional[str] = None): |
| 18 | + self._api = CsgHubApi(token=token, endpoint=endpoint) |
| 19 | + self._token = token |
| 20 | + self._endpoint = endpoint |
| 21 | + self._user_name = user_name |
| 22 | + |
| 23 | + def hf_hub_download( |
| 24 | + self, |
| 25 | + repo_id: str, |
| 26 | + filename: str, |
| 27 | + subfolder: Optional[str] = None, |
| 28 | + repo_type: Optional[str] = None, |
| 29 | + revision: Optional[str] = None, |
| 30 | + library_name: Optional[str] = None, |
| 31 | + library_version: Optional[str] = None, |
| 32 | + cache_dir: Optional[Union[str, Path]] = None, |
| 33 | + local_dir: Optional[Union[str, Path]] = None, |
| 34 | + user_agent: Optional[Union[Dict, str]] = None, |
| 35 | + force_download: bool = False, |
| 36 | + force_filename: Optional[str] = None, |
| 37 | + proxies: Optional[Dict] = None, |
| 38 | + etag_timeout: float = 10, |
| 39 | + resume_download: bool = False, |
| 40 | + token: Optional[Union[bool, str]] = None, |
| 41 | + local_files_only: bool = False, |
| 42 | + headers: Optional[Dict[str, str]] = None, |
| 43 | + endpoint: Optional[str] = None, |
| 44 | + **kwargs |
| 45 | + ) -> Union[str, Path]: |
| 46 | + # Adapt to csghub_file_download signature |
| 47 | + # Note: subfolder handling in csghub might be different, usually path in filename |
| 48 | + final_filename = f"{subfolder}/{filename}" if subfolder else filename |
| 49 | + |
| 50 | + # Extract dry_run/quiet/source from kwargs if present |
| 51 | + dry_run = kwargs.get('dry_run', False) |
| 52 | + quiet = kwargs.get('quiet', False) |
| 53 | + source = kwargs.get('source', None) |
| 54 | + |
| 55 | + return csghub_file_download( |
| 56 | + repo_id=repo_id, |
| 57 | + file_name=final_filename, |
| 58 | + revision=revision, |
| 59 | + repo_type=repo_type, |
| 60 | + cache_dir=cache_dir, |
| 61 | + local_dir=local_dir, |
| 62 | + local_files_only=local_files_only, |
| 63 | + headers=headers, |
| 64 | + endpoint=endpoint or self._endpoint, |
| 65 | + token=token or self._token, |
| 66 | + force_download=force_download, |
| 67 | + dry_run=dry_run, |
| 68 | + quiet=quiet, |
| 69 | + source=source |
| 70 | + ) |
| 71 | + |
| 72 | + def snapshot_download( |
| 73 | + self, |
| 74 | + repo_id: str, |
| 75 | + revision: Optional[str] = None, |
| 76 | + repo_type: Optional[str] = None, |
| 77 | + cache_dir: Optional[Union[str, Path]] = None, |
| 78 | + local_dir: Optional[Union[str, Path]] = None, |
| 79 | + library_name: Optional[str] = None, |
| 80 | + library_version: Optional[str] = None, |
| 81 | + user_agent: Optional[Union[Dict, str]] = None, |
| 82 | + proxies: Optional[Dict] = None, |
| 83 | + etag_timeout: float = 10, |
| 84 | + resume_download: bool = False, |
| 85 | + token: Optional[Union[bool, str]] = None, |
| 86 | + local_files_only: bool = False, |
| 87 | + allow_patterns: Optional[Union[List[str], str]] = None, |
| 88 | + ignore_patterns: Optional[Union[List[str], str]] = None, |
| 89 | + max_workers: int = 8, |
| 90 | + tqdm_class: Optional[Any] = None, |
| 91 | + # Extra args for CSGHub compatibility or enhancements |
| 92 | + endpoint: Optional[str] = None, |
| 93 | + source: Optional[str] = None, |
| 94 | + quiet: Optional[bool] = False, |
| 95 | + dry_run: Optional[bool] = False, |
| 96 | + force_download: Optional[bool] = False, |
| 97 | + ): |
| 98 | + # Delegate to pycsghub.snapshot_download |
| 99 | + return csghub_snapshot_download( |
| 100 | + repo_id=repo_id, |
| 101 | + repo_type=repo_type, |
| 102 | + revision=revision, |
| 103 | + cache_dir=cache_dir, |
| 104 | + local_dir=local_dir, |
| 105 | + local_files_only=local_files_only, |
| 106 | + allow_patterns=allow_patterns, |
| 107 | + ignore_patterns=ignore_patterns, |
| 108 | + endpoint=endpoint or self._endpoint, |
| 109 | + token=token or self._token, |
| 110 | + source=source, |
| 111 | + dry_run=dry_run, |
| 112 | + force_download=force_download, |
| 113 | + max_workers=max_workers, |
| 114 | + quiet=quiet |
| 115 | + ) |
| 116 | + |
| 117 | + def create_repo( |
| 118 | + self, |
| 119 | + repo_id: str, |
| 120 | + repo_type: Optional[str] = None, |
| 121 | + exist_ok: bool = False, |
| 122 | + private: bool = False, |
| 123 | + **kwargs |
| 124 | + ): |
| 125 | + # Reusing Repository logic for creation |
| 126 | + from pycsghub.repository import Repository |
| 127 | + repo = Repository( |
| 128 | + repo_id=repo_id, |
| 129 | + upload_path="..", |
| 130 | + repo_type=repo_type, |
| 131 | + token=self._token, |
| 132 | + endpoint=self._endpoint, |
| 133 | + auto_create=False |
| 134 | + ) |
| 135 | + try: |
| 136 | + repo.create_new_repo() |
| 137 | + except Exception: |
| 138 | + if not exist_ok: |
| 139 | + raise |
| 140 | + return repo |
| 141 | + |
| 142 | + def repo_info( |
| 143 | + self, |
| 144 | + repo_id: str, |
| 145 | + revision: Optional[str] = None, |
| 146 | + repo_type: Optional[str] = None, |
| 147 | + token: Optional[str] = None, |
| 148 | + ): |
| 149 | + return get_repo_info( |
| 150 | + repo_id=repo_id, |
| 151 | + revision=revision, |
| 152 | + repo_type=repo_type, |
| 153 | + token=token or self._token, |
| 154 | + endpoint=self._endpoint |
| 155 | + ) |
| 156 | + |
| 157 | + def create_branch( |
| 158 | + self, |
| 159 | + repo_id: str, |
| 160 | + branch: str, |
| 161 | + repo_type: Optional[str] = None, |
| 162 | + exist_ok: bool = False |
| 163 | + ): |
| 164 | + try: |
| 165 | + # Reusing Repository logic until CsgHubApi supports create_branch directly or via new endpoint |
| 166 | + from pycsghub.repository import Repository |
| 167 | + repo = Repository( |
| 168 | + repo_id=repo_id, |
| 169 | + upload_path="..", |
| 170 | + repo_type=repo_type, |
| 171 | + branch_name=branch, |
| 172 | + token=self._token, |
| 173 | + endpoint=self._endpoint |
| 174 | + ) |
| 175 | + return repo.create_new_branch() |
| 176 | + except Exception: |
| 177 | + if not exist_ok: |
| 178 | + raise |
| 179 | + |
| 180 | + def upload_file( |
| 181 | + self, |
| 182 | + *, |
| 183 | + path_or_fileobj: Union[str, Path, bytes], |
| 184 | + path_in_repo: str, |
| 185 | + repo_id: str, |
| 186 | + repo_type: Optional[str] = None, |
| 187 | + revision: Optional[str] = None, |
| 188 | + commit_message: Optional[str] = None, |
| 189 | + commit_description: Optional[str] = None, |
| 190 | + create_pr: Optional[bool] = None, |
| 191 | + parent_commit: Optional[str] = None, |
| 192 | + ) -> dict: |
| 193 | + return repo.upload_files( |
| 194 | + repo_id=repo_id, |
| 195 | + repo_type=repo_type, |
| 196 | + repo_file=path_or_fileobj, |
| 197 | + path_in_repo=path_in_repo, |
| 198 | + revision=revision, |
| 199 | + endpoint=self._endpoint, |
| 200 | + token=self._token, |
| 201 | + ) |
| 202 | + |
| 203 | + def upload_folder( |
| 204 | + self, |
| 205 | + *, |
| 206 | + repo_id: str, |
| 207 | + folder_path: Union[str, Path], |
| 208 | + path_in_repo: Optional[str] = None, |
| 209 | + repo_type: Optional[str] = None, |
| 210 | + revision: Optional[str] = None, |
| 211 | + commit_message: Optional[str] = None, |
| 212 | + commit_description: Optional[str] = None, |
| 213 | + allow_patterns: Optional[Union[List[str], str]] = None, |
| 214 | + ignore_patterns: Optional[Union[List[str], str]] = None, |
| 215 | + delete_patterns: Optional[Union[List[str], str]] = None, |
| 216 | + create_pr: Optional[bool] = None, |
| 217 | + parent_commit: Optional[str] = None, |
| 218 | + ) -> dict: |
| 219 | + return repo.upload_folder( |
| 220 | + repo_id=repo_id, |
| 221 | + repo_type=repo_type, |
| 222 | + local_path=folder_path, |
| 223 | + path_in_repo=path_in_repo, |
| 224 | + revision=revision, |
| 225 | + endpoint=self._endpoint, |
| 226 | + token=self._token, |
| 227 | + user_name=self._user_name, |
| 228 | + ) |
0 commit comments