Rudra Rahul Chothe
Upload folder using huggingface_hub
5143659 verified
from dotenv import load_dotenv
import os
from huggingface_hub import HfApi, upload_folder, login
from pathlib import Path
import logging
def setup_logging():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
return logging.getLogger(__name__)
def validate_environment():
load_dotenv()
token = os.getenv("HF_ACCESS_TOKEN")
if not token:
raise ValueError("HF_ACCESS_TOKEN environment variable is not set")
return token
def validate_directory(dir_path: str) -> Path:
path = Path(dir_path)
if not path.exists():
raise FileNotFoundError(f"Directory not found: {dir_path}")
if not path.is_dir():
raise NotADirectoryError(f"Path is not a directory: {dir_path}")
return path
def create_and_upload_space(
local_dir: str,
repo_id: str,
space_sdk: str = "streamlit",
token: str = None
) -> None:
logger = setup_logging()
try:
# Login
token = token or validate_environment()
login(token=token)
# Validate directory
dir_path = validate_directory(local_dir)
# Initialize API
api = HfApi()
# Create space
logger.info(f"Creating space repository: {repo_id}")
api.create_repo(
repo_id=repo_id,
repo_type="space",
space_sdk=space_sdk,
exist_ok=True
)
# Upload files
logger.info(f"Uploading files from {dir_path}")
upload_folder(
folder_path=str(dir_path),
repo_id=repo_id,
repo_type="space"
)
logger.info("Upload completed successfully")
except Exception as e:
logger.error(f"Operation failed: {str(e)}")
raise
if __name__ == "__main__":
create_and_upload_space(
local_dir=r"C:\Users\rudra\Documents\GitHub\image-search-engine",
repo_id="rudra0410/image-search-engine-fashion-space"
)