Add files using upload-large-folder tool
Browse files- metadata.csv +0 -0
- upload_huggingface.py +61 -7
metadata.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
upload_huggingface.py
CHANGED
|
@@ -13,10 +13,12 @@ or fall back to an interactive login prompt from huggingface_hub.
|
|
| 13 |
"""
|
| 14 |
|
| 15 |
import argparse
|
|
|
|
| 16 |
import os
|
| 17 |
import sys
|
|
|
|
| 18 |
|
| 19 |
-
from huggingface_hub import login, upload_folder
|
| 20 |
|
| 21 |
|
| 22 |
def parse_args() -> argparse.Namespace:
|
|
@@ -42,6 +44,21 @@ def parse_args() -> argparse.Namespace:
|
|
| 42 |
default=None,
|
| 43 |
help="Hugging Face token. If omitted, uses HF_TOKEN env or interactive login.",
|
| 44 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return parser.parse_args()
|
| 46 |
|
| 47 |
|
|
@@ -55,12 +72,49 @@ def main() -> None:
|
|
| 55 |
else:
|
| 56 |
login()
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
|
|
|
| 13 |
"""
|
| 14 |
|
| 15 |
import argparse
|
| 16 |
+
import csv
|
| 17 |
import os
|
| 18 |
import sys
|
| 19 |
+
from pathlib import Path
|
| 20 |
|
| 21 |
+
from huggingface_hub import HfApi, login, upload_folder
|
| 22 |
|
| 23 |
|
| 24 |
def parse_args() -> argparse.Namespace:
|
|
|
|
| 44 |
default=None,
|
| 45 |
help="Hugging Face token. If omitted, uses HF_TOKEN env or interactive login.",
|
| 46 |
)
|
| 47 |
+
parser.add_argument(
|
| 48 |
+
"--labels",
|
| 49 |
+
default="label.txt",
|
| 50 |
+
help="Path to label file (TSV: image_path<TAB>label). Default: label.txt",
|
| 51 |
+
)
|
| 52 |
+
parser.add_argument(
|
| 53 |
+
"--metadata",
|
| 54 |
+
default="metadata.csv",
|
| 55 |
+
help="Output metadata CSV (file_name,text) for imagefolder. Default: metadata.csv",
|
| 56 |
+
)
|
| 57 |
+
parser.add_argument(
|
| 58 |
+
"--use-large-upload",
|
| 59 |
+
action="store_true",
|
| 60 |
+
help="Use upload_large_folder to handle big datasets (recommended for large folders).",
|
| 61 |
+
)
|
| 62 |
return parser.parse_args()
|
| 63 |
|
| 64 |
|
|
|
|
| 72 |
else:
|
| 73 |
login()
|
| 74 |
|
| 75 |
+
# Build metadata.csv from label.txt so Hugging Face uses explicit labels
|
| 76 |
+
labels_path = Path(args.labels)
|
| 77 |
+
metadata_path = Path(args.metadata)
|
| 78 |
+
if labels_path.exists():
|
| 79 |
+
base = Path(args.folder).resolve()
|
| 80 |
+
with labels_path.open("r", encoding="utf-8") as fin, metadata_path.open(
|
| 81 |
+
"w", encoding="utf-8", newline=""
|
| 82 |
+
) as fout:
|
| 83 |
+
writer = csv.writer(fout)
|
| 84 |
+
writer.writerow(["file_name", "text"])
|
| 85 |
+
for line in fin:
|
| 86 |
+
line = line.rstrip("\n")
|
| 87 |
+
if not line:
|
| 88 |
+
continue
|
| 89 |
+
try:
|
| 90 |
+
rel_path, text = line.split("\t", 1)
|
| 91 |
+
except ValueError:
|
| 92 |
+
print(f"Skipping malformed label line: {line}")
|
| 93 |
+
continue
|
| 94 |
+
# Ensure path relative to the uploaded folder
|
| 95 |
+
rel_path = str(Path(rel_path))
|
| 96 |
+
writer.writerow([rel_path, text])
|
| 97 |
+
print(f"Wrote metadata to {metadata_path} using labels from {labels_path}")
|
| 98 |
+
else:
|
| 99 |
+
print(f"Label file not found at {labels_path}; skipping metadata build.")
|
| 100 |
+
|
| 101 |
+
if args.use_large_upload:
|
| 102 |
+
api = HfApi()
|
| 103 |
+
api.upload_large_folder(
|
| 104 |
+
folder_path=args.folder,
|
| 105 |
+
repo_id=args.repo_id,
|
| 106 |
+
repo_type=args.repo_type,
|
| 107 |
+
)
|
| 108 |
+
print(
|
| 109 |
+
f"Uploaded '{args.folder}' via upload_large_folder to {args.repo_type} repo '{args.repo_id}'."
|
| 110 |
+
)
|
| 111 |
+
else:
|
| 112 |
+
upload_folder(
|
| 113 |
+
folder_path=args.folder,
|
| 114 |
+
repo_id=args.repo_id,
|
| 115 |
+
repo_type=args.repo_type,
|
| 116 |
+
)
|
| 117 |
+
print(f"Uploaded '{args.folder}' to {args.repo_type} repo '{args.repo_id}'.")
|
| 118 |
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|