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

Create upload_response.py

Browse files
Files changed (1) hide show
  1. upload_response.py +82 -0
upload_response.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 main():
27
+ parser = argparse.ArgumentParser(description='Upload JSON file to Hugging Face')
28
+
29
+ # Add arguments
30
+ parser.add_argument(
31
+ '--token',
32
+ type=str,
33
+ help='Hugging Face access token (or set HUGGINGFACE_TOKEN env variable)',
34
+ default=os.getenv('HUGGINGFACE_TOKEN')
35
+ )
36
+
37
+ parser.add_argument(
38
+ '--repo-id',
39
+ type=str,
40
+ required=True,
41
+ help='Repository ID (format: username/repo-name)'
42
+ )
43
+
44
+ parser.add_argument(
45
+ '--file-path',
46
+ type=str,
47
+ required=True,
48
+ help='Path to the JSON file to upload'
49
+ )
50
+
51
+ parser.add_argument(
52
+ '--file-name',
53
+ type=str,
54
+ help='Name to save the file as in the repository (defaults to the input filename)',
55
+ )
56
+
57
+ # Parse arguments
58
+ args = parser.parse_args()
59
+
60
+ # Validate token
61
+ if not args.token:
62
+ raise ValueError("Please provide a token either via --token or HUGGINGFACE_TOKEN environment variable")
63
+
64
+ # If file_name is not provided, use the basename of file_path
65
+ if not args.file_name:
66
+ args.file_name = os.path.basename(args.file_path)
67
+
68
+ # Validate file exists and is JSON
69
+ if not os.path.exists(args.file_path):
70
+ raise FileNotFoundError(f"File not found: {args.file_path}")
71
+
72
+ try:
73
+ with open(args.file_path, 'r') as f:
74
+ json.load(f) # Validate JSON format
75
+ except json.JSONDecodeError:
76
+ raise ValueError(f"File is not valid JSON: {args.file_path}")
77
+
78
+ # Upload file
79
+ upload_json_to_hf(args.token, args.repo_id, args.file_path, args.file_name)
80
+
81
+ if __name__ == "__main__":
82
+ main()