JackyChunKit commited on
Commit
a22749b
·
verified ·
1 Parent(s): 6426005

Create upload_folder_response.py

Browse files
Files changed (1) hide show
  1. upload_folder_response.py +94 -0
upload_folder_response.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import login, HfApi
2
+ import json
3
+ import argparse
4
+ import os
5
+
6
+ def upload_json_to_hf(token, repo_id, file_path, file_name):
7
+ # Login to Hugging Face
8
+ login(token)
9
+
10
+ # Initialize the API
11
+ api = HfApi()
12
+
13
+ # Upload the file
14
+ try:
15
+ api.upload_file(
16
+ path_or_fileobj=file_path,
17
+ path_in_repo=file_name,
18
+ repo_id=repo_id,
19
+ repo_type="dataset"
20
+ )
21
+ print(f"Successfully uploaded {file_name} to {repo_id}")
22
+ except Exception as e:
23
+ print(f"Error uploading file: {str(e)}")
24
+ raise
25
+
26
+ def upload_folder_to_hf(token, repo_id, folder_path):
27
+ # Get all files in the folder
28
+ files = []
29
+ for root, _, filenames in os.walk(folder_path):
30
+ for filename in filenames:
31
+ if filename.endswith('.json'): # Only process JSON files
32
+ file_path = os.path.join(root, filename)
33
+ # Get relative path for the file in the repository
34
+ relative_path = os.path.relpath(file_path, folder_path)
35
+ files.append((file_path, relative_path))
36
+
37
+ # Upload each file
38
+ for file_path, relative_path in files:
39
+ try:
40
+ # Validate JSON format
41
+ with open(file_path, 'r') as f:
42
+ json.load(f)
43
+
44
+ # Upload file
45
+ upload_json_to_hf(token, repo_id, file_path, relative_path)
46
+ except json.JSONDecodeError:
47
+ print(f"Skipping invalid JSON file: {file_path}")
48
+ except Exception as e:
49
+ print(f"Error processing {file_path}: {str(e)}")
50
+
51
+ def main():
52
+ parser = argparse.ArgumentParser(description='Upload JSON files to Hugging Face')
53
+
54
+ # Add arguments
55
+ parser.add_argument(
56
+ '--token',
57
+ type=str,
58
+ help='Hugging Face access token (or set HUGGINGFACE_TOKEN env variable)',
59
+ default=os.getenv('HUGGINGFACE_TOKEN')
60
+ )
61
+
62
+ parser.add_argument(
63
+ '--repo-id',
64
+ type=str,
65
+ required=True,
66
+ help='Repository ID (format: username/repo-name)'
67
+ )
68
+
69
+ parser.add_argument(
70
+ '--folder-path',
71
+ type=str,
72
+ required=True,
73
+ help='Path to the folder containing JSON files'
74
+ )
75
+
76
+ # Parse arguments
77
+ args = parser.parse_args()
78
+
79
+ # Validate token
80
+ if not args.token:
81
+ raise ValueError("Please provide a token either via --token or HUGGINGFACE_TOKEN environment variable")
82
+
83
+ # Validate folder exists
84
+ if not os.path.exists(args.folder_path):
85
+ raise FileNotFoundError(f"Folder not found: {args.folder_path}")
86
+
87
+ if not os.path.isdir(args.folder_path):
88
+ raise NotADirectoryError(f"Path is not a directory: {args.folder_path}")
89
+
90
+ # Upload files
91
+ upload_folder_to_hf(args.token, args.repo_id, args.folder_path)
92
+
93
+ if __name__ == "__main__":
94
+ main()