import os import time from tqdm import tqdm from huggingface_hub import HfApi # 本地保存的图片路径 local_dir = "./images" # Hugging Face 数据集名称 dataset_name = "jamesqijingsong/zidian" # 初始化 API api = HfApi() # 上传图片到 Hugging Face 数据集 for root, _, files in os.walk(local_dir): for file in tqdm(files, desc="Uploading images"): file_path = os.path.join(root, file) repo_path = f"image/{file}" # 数据集中的路径 try: api.upload_file( path_or_fileobj=file_path, path_in_repo=repo_path, repo_id=dataset_name, repo_type="dataset", ) # 防止上传过快,暂停一小段时间 time.sleep(1) # 每次上传后暂停 0.5 秒 except Exception as e: print(f"Failed to upload {file}: {e}")