CNN2 / deploy_to_hf.py
d-e-e-k-11's picture
Upload folder using huggingface_hub
294928d verified
from huggingface_hub import HfApi, login
import os
import sys
def deploy():
# Use the token provided by the user in the next step
# For now, this script will try to use the stored token or prompt
api = HfApi()
try:
user_info = api.whoami()
print(f"Logged in as: {user_info['name']}")
except Exception as e:
print(f"Authentication failed: {e}")
print("Please provide a valid WRITE token from https://huggingface.co/settings/tokens")
token = input("Enter your Hugging Face Write Token: ")
try:
login(token=token, add_to_git_credential=True)
user_info = api.whoami()
print(f"Successfully logged in as: {user_info['name']}")
except Exception as login_e:
print(f"Login failed even with token: {login_e}")
return
repo_id = f"{user_info['name']}/CNN2"
print(f"Creating/Checking Space: {repo_id}")
try:
api.create_repo(
repo_id=repo_id,
repo_type="space",
space_sdk="docker",
exist_ok=True
)
print(f"Space {repo_id} is ready.")
print("Uploading files...")
# Uploading the files from the current directory
# We upload Dockerfile, requirements.txt and everything in web_app/
# Upload everything in the directory
api.upload_folder(
folder_path=".",
repo_id=repo_id,
repo_type="space",
ignore_patterns=[".git*", "data*", "__pycache__*", "venv*", "*.pyc", "web_app*"]
)
print(f"\nDeployment Successful!")
print(f"View your Space at: https://huggingface.co/spaces/{repo_id}")
except Exception as e:
print(f"Deployment failed: {e}")
if __name__ == "__main__":
deploy()