Spaces:
Sleeping
Sleeping
Upload upl.py with huggingface_hub
Browse files
upl.py
CHANGED
|
@@ -1,30 +1,53 @@
|
|
| 1 |
import os
|
| 2 |
from huggingface_hub import HfApi, HfFolder
|
| 3 |
|
| 4 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
api = HfApi()
|
| 6 |
token = token or HfFolder.get_token()
|
| 7 |
|
| 8 |
if not token:
|
| 9 |
-
raise ValueError("Hugging Face token
|
| 10 |
|
|
|
|
| 11 |
for root, _, files in os.walk(folder_path):
|
| 12 |
for file in files:
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
local_path = os.path.join(root, file)
|
|
|
|
| 16 |
remote_path = os.path.relpath(local_path, folder_path)
|
| 17 |
|
| 18 |
print(f"Uploading {local_path} to {repo_id}/{remote_path}...")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
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 |
+
try:
|
| 35 |
+
api.upload_file(
|
| 36 |
+
path_or_fileobj=local_path,
|
| 37 |
+
path_in_repo=remote_path,
|
| 38 |
+
repo_id=repo_id,
|
| 39 |
+
repo_type="space",
|
| 40 |
+
token=token
|
| 41 |
+
)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Failed to upload {local_path}: {e}")
|
| 44 |
+
|
| 45 |
+
print("Upload completed successfully!")
|
| 46 |
+
|
| 47 |
+
# Replace these with your details
|
| 48 |
+
repo_id = "OzoneAsai/CnJaFlashC-1" # Replace with your space repository
|
| 49 |
+
folder_path = "./" # Path to the folder to upload
|
| 50 |
+
token = os.getenv("HF_Token") # Use an environment variable or set directly
|
| 51 |
+
|
| 52 |
+
# Upload the folder
|
| 53 |
+
upload_folder_to_hf_space(repo_id, folder_path, token)
|