33from pathlib import Path
44from typing import List , Tuple
55
6+ import typer
67from huggingface_hub import HfApi , login , CommitOperationAdd
78from eliot import start_action
89from tqdm import tqdm
@@ -17,7 +18,7 @@ def upload_to_huggingface(
1718 repo_id : str = DEFAULT_REPO_ID ,
1819 dataset_name : str | None = None ,
1920 readme_path : Path | None = None
20- ) -> None :
21+ ) -> bool :
2122 """Upload data to HuggingFace hub in a single commit.
2223
2324 Handles two cases:
@@ -140,16 +141,11 @@ def upload_to_huggingface(
140141 )
141142 action .log (message_type = "repo_ready" )
142143
143- # Check existing files
144- action .log (message_type = "checking_existing_files" )
145- existing_files = set (api .list_repo_files (repo_id , repo_type = 'dataset' ))
146- action .log (message_type = "found_existing_files" , count = len (existing_files ))
147-
148144 # Prepare operations list for batch commit
149145 operations : List [CommitOperationAdd ] = []
150146
151- # Add README if provided and not exists
152- if readme_path is not None and readme_path .exists () and 'README.md' not in existing_files :
147+ # Add README if provided
148+ if readme_path is not None and readme_path .exists ():
153149 action .log (message_type = "adding_readme_to_commit" )
154150 operations .append (
155151 CommitOperationAdd (
@@ -160,101 +156,99 @@ def upload_to_huggingface(
160156
161157 if has_train_test_split :
162158 # Handle train/test split case
163- # Prepare train files
159+ # Prepare train files - upload all files regardless of existing status
164160 train_files = sorted (list (train_chunks_dir .glob ("chunk_*.parquet" ))) if train_chunks_dir .exists () else []
165- train_to_upload = []
166161 for filepath in train_files :
167162 repo_path = f'{ dataset_name } /train/{ filepath .name } '
168- if repo_path not in existing_files :
169- train_to_upload .append (filepath )
170- operations .append (
171- CommitOperationAdd (
172- path_in_repo = repo_path ,
173- path_or_fileobj = str (filepath )
174- )
163+ operations .append (
164+ CommitOperationAdd (
165+ path_in_repo = repo_path ,
166+ path_or_fileobj = str (filepath )
175167 )
168+ )
176169
177170 action .log (
178171 message_type = "train_files_prepared" ,
179172 total = len (train_files ),
180- to_upload = len (train_to_upload ),
173+ to_upload = len (train_files ),
181174 train_chunks_dir = str (train_chunks_dir )
182175 )
183176
184- # Prepare test files
177+ # Prepare test files - upload all files regardless of existing status
185178 test_files = sorted (list (test_chunks_dir .glob ("chunk_*.parquet" ))) if test_chunks_dir .exists () else []
186- test_to_upload = []
187179 for filepath in test_files :
188180 repo_path = f'{ dataset_name } /test/{ filepath .name } '
189- if repo_path not in existing_files :
190- test_to_upload .append (filepath )
191- operations .append (
192- CommitOperationAdd (
193- path_in_repo = repo_path ,
194- path_or_fileobj = str (filepath )
195- )
181+ operations .append (
182+ CommitOperationAdd (
183+ path_in_repo = repo_path ,
184+ path_or_fileobj = str (filepath )
196185 )
186+ )
197187
198188 action .log (
199189 message_type = "test_files_prepared" ,
200190 total = len (test_files ),
201- to_upload = len (test_to_upload ),
191+ to_upload = len (test_files ),
202192 test_chunks_dir = str (test_chunks_dir )
203193 )
204194
205- total_files = len (train_to_upload ) + len (test_to_upload )
195+ total_files = len (train_files ) + len (test_files )
206196 else :
207- # Handle single dataset case (no split)
197+ # Handle single dataset case (no split) - upload all files regardless of existing status
208198 single_files = sorted (list (single_chunks_dir .glob ("chunk_*.parquet" ))) if single_chunks_dir .exists () else []
209- files_to_upload = []
210199 for filepath in single_files :
211200 repo_path = f'{ dataset_name } /{ filepath .name } '
212- if repo_path not in existing_files :
213- files_to_upload .append (filepath )
214- operations .append (
215- CommitOperationAdd (
216- path_in_repo = repo_path ,
217- path_or_fileobj = str (filepath )
218- )
201+ operations .append (
202+ CommitOperationAdd (
203+ path_in_repo = repo_path ,
204+ path_or_fileobj = str (filepath )
219205 )
206+ )
220207
221208 action .log (
222209 message_type = "single_dataset_files_prepared" ,
223210 total = len (single_files ),
224- to_upload = len (files_to_upload ),
211+ to_upload = len (single_files ),
225212 single_chunks_dir = str (single_chunks_dir )
226213 )
227214
228- total_files = len (files_to_upload )
215+ total_files = len (single_files )
229216
230- # Upload all files in a single commit
217+ # Upload all files in a single commit (always upload, even if files exist)
231218 if len (operations ) == 0 :
232- action .log (message_type = "no_files_to_upload" )
233- else :
234- # Log the paths that will be used in the repo
235- repo_paths = [op .path_in_repo for op in operations ]
236- action .log (
237- message_type = "starting_batch_upload" ,
238- total_operations = len (operations ),
239- total_files = total_files ,
240- repo_paths = repo_paths [:10 ] # Log first 10 paths as sample
241- )
242-
243- # Create a single commit with all operations
244- with tqdm (total = 1 , desc = 'Uploading batch' ) as pbar :
245- commit_info = api .create_commit (
246- repo_id = repo_id ,
247- repo_type = 'dataset' ,
248- operations = operations ,
249- commit_message = f'Upload { total_files } data files for { dataset_name } '
250- )
251- pbar .update (1 )
252-
253- action .log (
254- message_type = "upload_complete" ,
255- commit_url = commit_info .commit_url ,
256- total_operations = len (operations ),
219+ action .log (message_type = "no_files_to_upload" , dataset_name = dataset_name , repo_id = repo_id )
220+ typer .echo (f"⚠ No files found to upload for { dataset_name } " )
221+ return False
222+
223+ # Log the paths that will be used in the repo
224+ repo_paths = [op .path_in_repo for op in operations ]
225+ action .log (
226+ message_type = "starting_batch_upload" ,
227+ total_operations = len (operations ),
228+ total_files = total_files ,
229+ repo_paths = repo_paths [:10 ] # Log first 10 paths as sample
230+ )
231+
232+ typer .echo (f"Uploading { total_files } file(s) to { repo_id } /{ dataset_name } /..." )
233+
234+ # Create a single commit with all operations (will overwrite existing files)
235+ with tqdm (total = 1 , desc = 'Uploading batch' ) as pbar :
236+ commit_info = api .create_commit (
257237 repo_id = repo_id ,
258- dataset_name = dataset_name
238+ repo_type = 'dataset' ,
239+ operations = operations ,
240+ commit_message = f'Upload { total_files } data files for { dataset_name } '
259241 )
242+ pbar .update (1 )
243+
244+ action .log (
245+ message_type = "upload_complete" ,
246+ commit_url = commit_info .commit_url ,
247+ total_operations = len (operations ),
248+ repo_id = repo_id ,
249+ dataset_name = dataset_name
250+ )
251+ typer .echo (f"✓ Successfully uploaded { total_files } file(s)" )
252+ typer .echo (f" Commit: { commit_info .commit_url } " )
253+ return True
260254
0 commit comments