Spaces:
Sleeping
Sleeping
| import os | |
| import time | |
| from huggingface_hub import HfApi, HfFolder | |
| def upload_folder_to_hf_space(repo_id, folder_path, token=None): | |
| api = HfApi() | |
| token = token or HfFolder.get_token() | |
| if not token: | |
| raise ValueError("Hugging Face token not found. Log in or provide a token.") | |
| for root, _, files in os.walk(folder_path): | |
| for file in files: | |
| if file.startswith('.') or file.endswith('~'): | |
| continue | |
| local_path = os.path.join(root, file) | |
| remote_path = os.path.relpath(local_path, folder_path) | |
| print(f"Uploading {local_path} to {repo_id}/{remote_path}...") | |
| time.sleep(2) | |
| api.upload_file( | |
| path_or_fileobj=local_path, | |
| path_in_repo=remote_path, | |
| repo_id=repo_id, | |
| repo_type="space", | |
| token=token | |
| ) | |
| print("Upload completed successfully!") | |
| # 使用例 | |
| repo_id = "OzoneAsai/CnJaFlashC-1" | |
| folder_path = "./" | |
| token = os.getenv("HF_TOKEN") # 環境変数に設定したトークンを使用 | |
| upload_folder_to_hf_space(repo_id, folder_path, token) | |