liuganghuggingface commited on
Commit
d1baa81
1 Parent(s): 1a01805

Upload 2_upload_single.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. 2_upload_single.py +116 -0
2_upload_single.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from huggingface_hub import HfApi
2
+ # import os
3
+ # import sys
4
+
5
+ # def verify_authentication():
6
+ # api = HfApi()
7
+ # try:
8
+ # user_info = api.whoami()
9
+ # print(f"Authenticated as: {user_info['name']}")
10
+ # return True
11
+ # except Exception as e:
12
+ # print("Authentication failed. Please ensure you're logged in to Hugging Face.")
13
+ # print(f"Error: {e}")
14
+ # return False
15
+
16
+ # def upload_to_hf_space(api, local_path, repo_id, path_in_repo):
17
+ # try:
18
+ # url = api.upload_file(
19
+ # path_or_fileobj=local_path,
20
+ # path_in_repo=path_in_repo,
21
+ # repo_id=repo_id,
22
+ # token=os.environ.get("HF_TOKEN"),
23
+ # repo_type="space",
24
+ # )
25
+ # print(f"File uploaded successfully: {local_path} -> {url}")
26
+ # except Exception as e:
27
+ # print(f"An error occurred while uploading {local_path}: {e}")
28
+
29
+ # def upload_folder(api, repo_id, local_dir, folder_name):
30
+ # folder_path = os.path.join(local_dir, folder_name)
31
+ # for root, dirs, files in os.walk(folder_path):
32
+ # for file in files:
33
+ # local_path = os.path.join(root, file)
34
+ # relative_path = os.path.relpath(local_path, local_dir)
35
+ # upload_to_hf_space(api, local_path, repo_id, relative_path)
36
+
37
+ # def upload_items(repo_id, local_dir, items):
38
+ # if not verify_authentication():
39
+ # sys.exit(1)
40
+
41
+ # api = HfApi()
42
+
43
+ # for item in items:
44
+ # full_path = os.path.join(local_dir, item)
45
+ # if os.path.isfile(full_path):
46
+ # upload_to_hf_space(api, full_path, repo_id, item)
47
+ # elif os.path.isdir(full_path):
48
+ # upload_folder(api, repo_id, local_dir, item)
49
+ # else:
50
+ # print(f"The item {item} does not exist in {local_dir}.")
51
+
52
+ # if __name__ == "__main__":
53
+ # repo_id = "liuganghuggingface/polymer-design"
54
+ # local_dir = "./" # Use the current directory
55
+ # items_to_upload = ["app.py", "requirements.txt", "model_labeled", "model_all"] # List of files and folders to upload
56
+
57
+ # # Ensure the HF_TOKEN is set
58
+ # if "HF_TOKEN" not in os.environ:
59
+ # print("Please set the HF_TOKEN environment variable with your Hugging Face token.")
60
+ # sys.exit(1)
61
+
62
+ # upload_items(repo_id, local_dir, items_to_upload)
63
+
64
+
65
+
66
+ from huggingface_hub import HfApi
67
+ import os
68
+ import sys
69
+
70
+ def upload_single_file(api, repo_id, local_path, remote_path):
71
+ print(f"Attempting to upload: {local_path} to {remote_path}")
72
+ try:
73
+ if not os.path.exists(local_path):
74
+ print(f"Error: Local file does not exist: {local_path}")
75
+ return
76
+
77
+ url = api.upload_file(
78
+ path_or_fileobj=local_path,
79
+ path_in_repo=remote_path,
80
+ repo_id=repo_id,
81
+ token=os.environ.get("HF_TOKEN"),
82
+ repo_type="space",
83
+ )
84
+ print(f"File uploaded successfully: {local_path} -> {url}")
85
+
86
+ # Verify the upload
87
+ files = api.list_repo_files(repo_id=repo_id, repo_type="space")
88
+ if remote_path in files:
89
+ print(f"Verified: {remote_path} is present in the repository.")
90
+ else:
91
+ print(f"Warning: {remote_path} was uploaded but not found in the repository listing.")
92
+ except Exception as e:
93
+ print(f"An error occurred while uploading {local_path}: {e}")
94
+
95
+ def main():
96
+ repo_id = "liuganghuggingface/polymer-design"
97
+ local_dir = "./" # Use the current directory
98
+
99
+ files_to_upload = [
100
+ ("model_labeled/data.meta.json", "model_labeled/data.meta.json"),
101
+ ("model_all/config.yaml", "model_all/config.yaml"),
102
+ ("model_all/model.pt", "model_all/model.pt")
103
+ ]
104
+
105
+ api = HfApi()
106
+
107
+ for local_path, remote_path in files_to_upload:
108
+ full_local_path = os.path.join(local_dir, local_path)
109
+ upload_single_file(api, repo_id, full_local_path, remote_path)
110
+
111
+ if __name__ == "__main__":
112
+ if "HF_TOKEN" not in os.environ:
113
+ print("Please set the HF_TOKEN environment variable with your Hugging Face token.")
114
+ sys.exit(1)
115
+
116
+ main()