Skip to content

Commit 00824e1

Browse files
committed
fix upload on batch
1 parent 17c5211 commit 00824e1

2 files changed

Lines changed: 72 additions & 74 deletions

File tree

src/cell2sentence4longevity/preprocess.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,17 @@ def upload(
244244
with start_action(action_type="cli_upload") as action:
245245
typer.echo("Uploading to HuggingFace...")
246246
action.log(message_type="upload_started", repo_id=repo_id, output_dir=str(output_dir))
247-
upload_to_huggingface(
247+
files_uploaded = upload_to_huggingface(
248248
data_splits_dir=output_dir,
249249
token=token,
250250
repo_id=repo_id,
251251
readme_path=readme_path
252252
)
253253
dataset_url = f"https://huggingface.co/datasets/{repo_id}"
254-
typer.secho("✓ Upload completed successfully", fg=typer.colors.GREEN)
254+
if files_uploaded:
255+
typer.secho("✓ Upload completed successfully", fg=typer.colors.GREEN)
255256
typer.echo(f"Dataset: {dataset_url}")
256-
action.log(message_type="upload_completed", repo_id=repo_id, dataset_url=dataset_url)
257+
action.log(message_type="upload_completed", repo_id=repo_id, dataset_url=dataset_url, files_uploaded=files_uploaded)
257258

258259

259260
def _process_single_file(
@@ -359,16 +360,19 @@ def _process_single_file(
359360
typer.echo("="*80)
360361
# Upload to same repository as subfolder (dataset_name creates subfolder in repo)
361362
action.log(message_type="upload_started", dataset_name=dataset_name, repo_id=repo_id, upload_dir=str(upload_dir))
362-
upload_to_huggingface(
363+
files_uploaded = upload_to_huggingface(
363364
data_splits_dir=upload_dir,
364365
token=token,
365366
repo_id=repo_id,
366367
dataset_name=dataset_name
367368
)
368369
dataset_url = f"https://huggingface.co/datasets/{repo_id}"
369-
typer.secho(f"✓ Upload complete for {dataset_name}\n", fg=typer.colors.GREEN)
370-
typer.echo(f"Dataset: {dataset_url} (subfolder: {dataset_name})")
371-
action.log(message_type="upload_completed", dataset_name=dataset_name, repo_id=repo_id, dataset_url=dataset_url)
370+
if files_uploaded:
371+
typer.secho(f"✓ Upload complete for {dataset_name}\n", fg=typer.colors.GREEN)
372+
typer.echo(f"Dataset: {dataset_url} (subfolder: {dataset_name})")
373+
else:
374+
typer.echo(f"Dataset: {dataset_url} (subfolder: {dataset_name})")
375+
action.log(message_type="upload_completed", dataset_name=dataset_name, repo_id=repo_id, dataset_url=dataset_url, files_uploaded=files_uploaded)
372376

373377
# Final garbage collection
374378
gc.collect()

src/cell2sentence4longevity/preprocessing/upload.py

Lines changed: 61 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from typing import List, Tuple
55

6+
import typer
67
from huggingface_hub import HfApi, login, CommitOperationAdd
78
from eliot import start_action
89
from 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

Comments
 (0)