11import os
22import tempfile
3- from concurrent .futures import Future
43from functools import partial
54from pathlib import Path
65from typing import List , Optional , Union
1312logger = logging .get_logger (__name__ )
1413
1514
16- class PushToMsHubMixin :
15+ def create_repo (repo_id : str , * , token : Union [str , bool , None ] = None , private : bool = False , ** kwargs ) -> RepoUrl :
16+ from modelscope .hub .repository import Repository
17+ hub_model_id = PushToMsHubMixin .create_ms_repo (repo_id , token , private )
18+ PushToMsHubMixin .ms_token = token
19+ with tempfile .TemporaryDirectory () as temp_cache_dir :
20+ repo = Repository (temp_cache_dir , hub_model_id )
21+ PushToMsHubMixin .add_patterns_to_gitattributes (repo , ['*.safetensors' , '*.bin' , '*.pt' ])
22+ # Add 'runs/' to .gitignore, ignore tensorboard files
23+ PushToMsHubMixin .add_patterns_to_gitignore (repo , ['runs/' , 'images/' ])
24+ PushToMsHubMixin .add_patterns_to_file (
25+ repo ,
26+ 'configuration.json' , ['{"framework": "pytorch", "task": "text-generation", "allow_remote": true}' ],
27+ ignore_push_error = True )
28+ # Add '*.sagemaker' to .gitignore if using SageMaker
29+ if os .environ .get ('SM_TRAINING_ENV' ):
30+ PushToMsHubMixin .add_patterns_to_gitignore (repo , ['*.sagemaker-uploading' , '*.sagemaker-uploaded' ],
31+ 'Add `*.sagemaker` patterns to .gitignore' )
32+ return RepoUrl (url = hub_model_id , )
33+
34+
35+ @future_compatible
36+ def upload_folder (
37+ self ,
38+ * ,
39+ repo_id : str ,
40+ folder_path : Union [str , Path ],
41+ path_in_repo : Optional [str ] = None ,
42+ commit_message : Optional [str ] = None ,
43+ commit_description : Optional [str ] = None ,
44+ token : Union [str , bool , None ] = None ,
45+ revision : Optional [str ] = 'master' ,
46+ ignore_patterns : Optional [Union [List [str ], str ]] = None ,
47+ run_as_future : bool = False ,
48+ ** kwargs ,
49+ ):
50+ from modelscope import push_to_hub
51+ commit_message = commit_message or 'Upload folder using api'
52+ if commit_description :
53+ commit_message = commit_message + '\n ' + commit_description
54+ if not os .path .exists (os .path .join (folder_path , 'configuration.json' )):
55+ with open (os .path .join (folder_path , 'configuration.json' ), 'w' ) as f :
56+ f .write ('{"framework": "pytorch", "task": "text-generation", "allow_remote": true}' )
57+ if ignore_patterns :
58+ ignore_patterns = [p for p in ignore_patterns if p != '_*' ]
59+ if path_in_repo :
60+ # We don't support part submit for now
61+ path_in_repo = os .path .basename (folder_path )
62+ folder_path = os .path .dirname (folder_path )
63+ ignore_patterns = []
64+ push_to_hub (
65+ repo_id ,
66+ folder_path ,
67+ token or PushToMsHubMixin .ms_token ,
68+ commit_message = commit_message ,
69+ ignore_file_pattern = ignore_patterns ,
70+ revision = revision ,
71+ tag = path_in_repo )
72+ return CommitInfo (
73+ commit_url = f'https://www.modelscope.cn/models/{ repo_id } /files' ,
74+ commit_message = commit_message ,
75+ commit_description = commit_description ,
76+ oid = None ,
77+ )
1778
18- _use_hf_hub = strtobool (os .environ .get ('USE_HF' , 'False' ))
19- _token = None
2079
21- @staticmethod
22- def create_repo (repo_id : str , * , token : Union [str , bool , None ] = None , private : bool = False , ** kwargs ) -> RepoUrl :
23- from modelscope .hub .repository import Repository
24- hub_model_id = PushToMsHubMixin ._create_ms_repo (repo_id , token , private )
25- PushToMsHubMixin ._token = token
26- with tempfile .TemporaryDirectory () as temp_cache_dir :
27- repo = Repository (temp_cache_dir , hub_model_id )
28- PushToMsHubMixin ._add_patterns_to_gitattributes (repo , ['*.safetensors' , '*.bin' , '*.pt' ])
29- # Add 'runs/' to .gitignore, ignore tensorboard files
30- PushToMsHubMixin ._add_patterns_to_gitignore (repo , ['runs/' , 'images/' ])
31- PushToMsHubMixin ._add_patterns_to_file (
32- repo ,
33- 'configuration.json' , ['{"framework": "pytorch", "task": "text-generation", "allow_remote": true}' ],
34- ignore_push_error = True )
35- # Add '*.sagemaker' to .gitignore if using SageMaker
36- if os .environ .get ('SM_TRAINING_ENV' ):
37- PushToMsHubMixin ._add_patterns_to_gitignore (repo , ['*.sagemaker-uploading' , '*.sagemaker-uploaded' ],
38- 'Add `*.sagemaker` patterns to .gitignore' )
39- return RepoUrl (url = hub_model_id , )
80+ class PushToMsHubMixin :
4081
41- @staticmethod
42- @future_compatible
43- def upload_folder (
44- self ,
45- * ,
46- repo_id : str ,
47- folder_path : Union [str , Path ],
48- path_in_repo : Optional [str ] = None ,
49- commit_message : Optional [str ] = None ,
50- commit_description : Optional [str ] = None ,
51- token : Union [str , bool , None ] = None ,
52- revision : Optional [str ] = 'master' ,
53- ignore_patterns : Optional [Union [List [str ], str ]] = None ,
54- run_as_future : bool = False ,
55- ** kwargs ,
56- ) -> Union [CommitInfo , str , Future [CommitInfo ], Future [str ]]:
57- from modelscope import push_to_hub
58- commit_message = commit_message or 'Upload folder using api'
59- if commit_description :
60- commit_message = commit_message + '\n ' + commit_description
61- if not os .path .exists (os .path .join (folder_path , 'configuration.json' )):
62- with open (os .path .join (folder_path , 'configuration.json' ), 'w' ) as f :
63- f .write ('{"framework": "pytorch", "task": "text-generation", "allow_remote": true}' )
64- if ignore_patterns :
65- ignore_patterns = [p for p in ignore_patterns if p != '_*' ]
66- if path_in_repo :
67- # We don't support part submit for now
68- path_in_repo = os .path .basename (folder_path )
69- folder_path = os .path .dirname (folder_path )
70- ignore_patterns = []
71- push_to_hub (
72- repo_id ,
73- folder_path ,
74- token or PushToMsHubMixin ._token ,
75- commit_message = commit_message ,
76- ignore_file_pattern = ignore_patterns ,
77- revision = revision ,
78- tag = path_in_repo )
79- return CommitInfo (
80- commit_url = f'https://www.modelscope.cn/models/{ repo_id } /files' ,
81- commit_message = commit_message ,
82- commit_description = commit_description ,
83- oid = None ,
84- )
82+ _use_hf_hub = strtobool (os .environ .get ('USE_HF' , 'False' ))
83+ ms_token = None
8584
8685 if not _use_hf_hub :
8786 import huggingface_hub
@@ -93,7 +92,7 @@ def upload_folder(
9392 trainer .upload_folder = partial (upload_folder , api )
9493
9594 @staticmethod
96- def _create_ms_repo (hub_model_id : str , hub_token : Optional [str ] = None , hub_private_repo : bool = False ) -> str :
95+ def create_ms_repo (hub_model_id : str , hub_token : Optional [str ] = None , hub_private_repo : bool = False ) -> str :
9796 from modelscope import HubApi
9897 from modelscope .hub .api import ModelScopeConfig
9998 from modelscope .hub .constants import ModelVisibility
@@ -121,11 +120,11 @@ def _create_ms_repo(hub_model_id: str, hub_token: Optional[str] = None, hub_priv
121120 return hub_model_id
122121
123122 @staticmethod
124- def _add_patterns_to_file (repo ,
125- file_name : str ,
126- patterns : List [str ],
127- commit_message : Optional [str ] = None ,
128- ignore_push_error = False ) -> None :
123+ def add_patterns_to_file (repo ,
124+ file_name : str ,
125+ patterns : List [str ],
126+ commit_message : Optional [str ] = None ,
127+ ignore_push_error = False ) -> None :
129128 if isinstance (patterns , str ):
130129 patterns = [patterns ]
131130 if commit_message is None :
@@ -161,11 +160,11 @@ def _add_patterns_to_file(repo,
161160 raise e
162161
163162 @staticmethod
164- def _add_patterns_to_gitignore (repo , patterns : List [str ], commit_message : Optional [str ] = None ) -> None :
165- PushToMsHubMixin ._add_patterns_to_file (repo , '.gitignore' , patterns , commit_message , ignore_push_error = True )
163+ def add_patterns_to_gitignore (repo , patterns : List [str ], commit_message : Optional [str ] = None ) -> None :
164+ PushToMsHubMixin .add_patterns_to_file (repo , '.gitignore' , patterns , commit_message , ignore_push_error = True )
166165
167166 @staticmethod
168- def _add_patterns_to_gitattributes (repo , patterns : List [str ], commit_message : Optional [str ] = None ) -> None :
167+ def add_patterns_to_gitattributes (repo , patterns : List [str ], commit_message : Optional [str ] = None ) -> None :
169168 new_patterns = []
170169 suffix = 'filter=lfs diff=lfs merge=lfs -text'
171170 for pattern in patterns :
@@ -175,4 +174,4 @@ def _add_patterns_to_gitattributes(repo, patterns: List[str], commit_message: Op
175174 file_name = '.gitattributes'
176175 if commit_message is None :
177176 commit_message = f'Add `{ patterns [0 ]} ` patterns to { file_name } '
178- PushToMsHubMixin ._add_patterns_to_file (repo , file_name , new_patterns , commit_message , ignore_push_error = True )
177+ PushToMsHubMixin .add_patterns_to_file (repo , file_name , new_patterns , commit_message , ignore_push_error = True )
0 commit comments