Spaces:
Runtime error
Runtime error
| # 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() |