Spaces:
Sleeping
Sleeping
Upload upl.py with huggingface_hub
Browse files
upl.py
CHANGED
@@ -2,52 +2,34 @@ import os
|
|
2 |
from huggingface_hub import HfApi, HfFolder
|
3 |
|
4 |
def upload_folder_to_hf_space(repo_id, folder_path, token=None):
|
5 |
-
"""
|
6 |
-
Uploads all files from a folder to a Hugging Face Space while skipping unnecessary files.
|
7 |
-
|
8 |
-
Args:
|
9 |
-
repo_id (str): The Hugging Face Space repository ID (e.g., "username/space_name").
|
10 |
-
folder_path (str): Path to the local folder to upload.
|
11 |
-
token (str, optional): Hugging Face API token. Defaults to None.
|
12 |
-
|
13 |
-
Returns:
|
14 |
-
None
|
15 |
-
"""
|
16 |
api = HfApi()
|
17 |
token = token or HfFolder.get_token()
|
18 |
|
19 |
if not token:
|
20 |
raise ValueError("Hugging Face token not found. Log in or provide a token.")
|
21 |
|
22 |
-
# Walk through the folder and upload files
|
23 |
for root, _, files in os.walk(folder_path):
|
24 |
for file in files:
|
25 |
-
# Skip hidden files and unnecessary directories
|
26 |
if file.startswith('.') or file.endswith('~'):
|
27 |
continue
|
28 |
|
29 |
local_path = os.path.join(root, file)
|
30 |
-
# Compute the relative path for upload
|
31 |
remote_path = os.path.relpath(local_path, folder_path)
|
32 |
|
33 |
print(f"Uploading {local_path} to {repo_id}/{remote_path}...")
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
)
|
42 |
-
except Exception as e:
|
43 |
-
print(f"Failed to upload {local_path}: {e}")
|
44 |
|
45 |
print("Upload completed successfully!")
|
46 |
|
47 |
-
#
|
48 |
-
repo_id = "OzoneAsai/CnJaFlashC-1"
|
49 |
-
folder_path = "./"
|
50 |
-
token = os.getenv("
|
51 |
|
52 |
-
# Upload the folder
|
53 |
upload_folder_to_hf_space(repo_id, folder_path, token)
|
|
|
2 |
from huggingface_hub import HfApi, HfFolder
|
3 |
|
4 |
def upload_folder_to_hf_space(repo_id, folder_path, token=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
api = HfApi()
|
6 |
token = token or HfFolder.get_token()
|
7 |
|
8 |
if not token:
|
9 |
raise ValueError("Hugging Face token not found. Log in or provide a token.")
|
10 |
|
|
|
11 |
for root, _, files in os.walk(folder_path):
|
12 |
for file in files:
|
|
|
13 |
if file.startswith('.') or file.endswith('~'):
|
14 |
continue
|
15 |
|
16 |
local_path = os.path.join(root, file)
|
|
|
17 |
remote_path = os.path.relpath(local_path, folder_path)
|
18 |
|
19 |
print(f"Uploading {local_path} to {repo_id}/{remote_path}...")
|
20 |
+
api.upload_file(
|
21 |
+
path_or_fileobj=local_path,
|
22 |
+
path_in_repo=remote_path,
|
23 |
+
repo_id=repo_id,
|
24 |
+
repo_type="space",
|
25 |
+
token=token
|
26 |
+
)
|
|
|
|
|
|
|
27 |
|
28 |
print("Upload completed successfully!")
|
29 |
|
30 |
+
# 使用例
|
31 |
+
repo_id = "OzoneAsai/CnJaFlashC-1"
|
32 |
+
folder_path = "./"
|
33 |
+
token = os.getenv("HF_TOKEN") # 環境変数に設定したトークンを使用
|
34 |
|
|
|
35 |
upload_folder_to_hf_space(repo_id, folder_path, token)
|