File size: 4,077 Bytes
d1baa81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# from huggingface_hub import HfApi
# import os
# import sys

# def verify_authentication():
#     api = HfApi()
#     try:
#         user_info = api.whoami()
#         print(f"Authenticated as: {user_info['name']}")
#         return True
#     except Exception as e:
#         print("Authentication failed. Please ensure you're logged in to Hugging Face.")
#         print(f"Error: {e}")
#         return False

# def upload_to_hf_space(api, local_path, repo_id, path_in_repo):
#     try:
#         url = api.upload_file(
#             path_or_fileobj=local_path,
#             path_in_repo=path_in_repo,
#             repo_id=repo_id,
#             token=os.environ.get("HF_TOKEN"),
#             repo_type="space",
#         )
#         print(f"File uploaded successfully: {local_path} -> {url}")
#     except Exception as e:
#         print(f"An error occurred while uploading {local_path}: {e}")

# def upload_folder(api, repo_id, local_dir, folder_name):
#     folder_path = os.path.join(local_dir, folder_name)
#     for root, dirs, files in os.walk(folder_path):
#         for file in files:
#             local_path = os.path.join(root, file)
#             relative_path = os.path.relpath(local_path, local_dir)
#             upload_to_hf_space(api, local_path, repo_id, relative_path)

# def upload_items(repo_id, local_dir, items):
#     if not verify_authentication():
#         sys.exit(1)

#     api = HfApi()

#     for item in items:
#         full_path = os.path.join(local_dir, item)
#         if os.path.isfile(full_path):
#             upload_to_hf_space(api, full_path, repo_id, item)
#         elif os.path.isdir(full_path):
#             upload_folder(api, repo_id, local_dir, item)
#         else:
#             print(f"The item {item} does not exist in {local_dir}.")

# if __name__ == "__main__":
#     repo_id = "liuganghuggingface/polymer-design"
#     local_dir = "./"  # Use the current directory
#     items_to_upload = ["app.py", "requirements.txt", "model_labeled", "model_all"]  # List of files and folders to upload

#     # Ensure the HF_TOKEN is set
#     if "HF_TOKEN" not in os.environ:
#         print("Please set the HF_TOKEN environment variable with your Hugging Face token.")
#         sys.exit(1)

#     upload_items(repo_id, local_dir, items_to_upload)



from huggingface_hub import HfApi
import os
import sys

def upload_single_file(api, repo_id, local_path, remote_path):
    print(f"Attempting to upload: {local_path} to {remote_path}")
    try:
        if not os.path.exists(local_path):
            print(f"Error: Local file does not exist: {local_path}")
            return

        url = api.upload_file(
            path_or_fileobj=local_path,
            path_in_repo=remote_path,
            repo_id=repo_id,
            token=os.environ.get("HF_TOKEN"),
            repo_type="space",
        )
        print(f"File uploaded successfully: {local_path} -> {url}")
        
        # Verify the upload
        files = api.list_repo_files(repo_id=repo_id, repo_type="space")
        if remote_path in files:
            print(f"Verified: {remote_path} is present in the repository.")
        else:
            print(f"Warning: {remote_path} was uploaded but not found in the repository listing.")
    except Exception as e:
        print(f"An error occurred while uploading {local_path}: {e}")

def main():
    repo_id = "liuganghuggingface/polymer-design"
    local_dir = "./"  # Use the current directory
    
    files_to_upload = [
        ("model_labeled/data.meta.json", "model_labeled/data.meta.json"),
        ("model_all/config.yaml", "model_all/config.yaml"),
        ("model_all/model.pt", "model_all/model.pt")
    ]

    api = HfApi()

    for local_path, remote_path in files_to_upload:
        full_local_path = os.path.join(local_dir, local_path)
        upload_single_file(api, repo_id, full_local_path, remote_path)

if __name__ == "__main__":
    if "HF_TOKEN" not in os.environ:
        print("Please set the HF_TOKEN environment variable with your Hugging Face token.")
        sys.exit(1)

    main()