Skip to content

Commit 6affd9c

Browse files
authored
[PushToHub] Make it possible to upload folders (#23920)
Add first draft
1 parent 4aa1322 commit 6affd9c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/transformers/utils/hub.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,31 @@ def _upload_modified_files(
701701
for f in os.listdir(working_dir)
702702
if f not in files_timestamps or os.path.getmtime(os.path.join(working_dir, f)) > files_timestamps[f]
703703
]
704+
705+
# filter for actual files + folders at the root level
706+
modified_files = [
707+
f
708+
for f in modified_files
709+
if os.path.isfile(os.path.join(working_dir, f)) or os.path.isdir(os.path.join(working_dir, f))
710+
]
711+
704712
operations = []
713+
# upload standalone files
705714
for file in modified_files:
706715
operations.append(CommitOperationAdd(path_or_fileobj=os.path.join(working_dir, file), path_in_repo=file))
716+
if os.path.isdir(os.path.join(working_dir, file)):
717+
# go over individual files of folder
718+
for f in os.listdir(os.path.join(working_dir, file)):
719+
operations.append(
720+
CommitOperationAdd(
721+
path_or_fileobj=os.path.join(working_dir, file, f), path_in_repo=os.path.join(file, f)
722+
)
723+
)
724+
else:
725+
operations.append(
726+
CommitOperationAdd(path_or_fileobj=os.path.join(working_dir, file), path_in_repo=file)
727+
)
728+
707729
logger.info(f"Uploading the following files to {repo_id}: {','.join(modified_files)}")
708730
return create_commit(
709731
repo_id=repo_id, operations=operations, commit_message=commit_message, token=token, create_pr=create_pr

0 commit comments

Comments
 (0)