ArtemisAI's picture
Upload folder using huggingface_hub
0c9b99b verified
Raw History Blame Contribute Delete
2.01 kB
import os
from huggingface_hub import HfApi, HfFolder
def upload_project_to_hub():
"""
Uploads the complete project to a specified Hugging Face Hub repository.
"""
# --- Configuration ---
# NOTE: Ensure you are logged in via `huggingface-cli login` in your terminal first.
# The token will be read automatically from the local cache.
repo_id = "ArtemisAI/Level-1-Project-2-Text-Summarization"
project_directory = "."
print(f"Preparing to upload project to Hugging Face Hub repository: {repo_id}")
api = HfApi()
# 1. Create the repository on the Hub (if it doesn't exist)
try:
api.create_repo(
repo_id=repo_id,
repo_type="space", # This is a Gradio Space
space_sdk="gradio",
exist_ok=True,
)
print(f"Repository '{repo_id}' created or already exists.")
except Exception as e:
print(f"Error creating repository: {e}")
return
# 2. Upload the entire project folder
# The HfApi().upload_folder function will handle all files recursively.
try:
print(f"Uploading contents of '{project_directory}' to '{repo_id}'...")
api.upload_folder(
folder_path=project_directory,
repo_id=repo_id,
repo_type="space",
# Ignore git files, pycache, and other local artifacts
allow_patterns=["*.py", "*.md", "*.txt"],
ignore_patterns=["*.git*", "*__pycache__*"],
)
print("✅ Upload complete!")
print(f"Check out your Space here: https://huggingface.co/spaces/{repo_id}")
except Exception as e:
print(f"An error occurred during the upload: {e}")
if __name__ == "__main__":
# Ensure a token is available for authentication
if HfFolder.get_token() is None:
print("Hugging Face token not found.")
print("Please log in using the 'huggingface-cli login' command in your terminal.")
else:
upload_project_to_hub()