Spaces:
Running
on
Zero
Running
on
Zero
import os | |
import subprocess | |
from huggingface_hub import login | |
def clone_and_initialize_repo(repo_url, target_dir): | |
""" | |
Clone a GitHub repository using a personal access token and initialize submodules. | |
""" | |
github_token = os.getenv("GITHUB_TOKEN") | |
if not github_token: | |
raise ValueError("Error: GITHUB_TOKEN is not set in environment variables.") | |
# Construct the authenticated repository URL | |
authenticated_repo_url = repo_url.replace( | |
"https://", f"https://{github_token}@" | |
) | |
try: | |
# Clone the repository | |
print(f"Cloning repository from {repo_url} into {target_dir}...") | |
subprocess.run( | |
["git", "clone", "--recurse-submodules", authenticated_repo_url, target_dir], | |
check=True | |
) | |
# Navigate to the repository directory | |
os.chdir(target_dir) | |
# Initialize and update submodules | |
print("Initializing and updating submodules...") | |
subprocess.run(["git", "submodule", "update", "--init", "--recursive"], check=True) | |
print("Repository cloned and submodules initialized successfully.") | |
except subprocess.CalledProcessError as e: | |
print(f"Error during repository cloning or submodule initialization: {e}") | |
raise | |
def huggingface_login(): | |
""" | |
Log in to Hugging Face using the API token from environment variables. | |
""" | |
hf_token = os.getenv("HF_TOKEN") | |
if not hf_token: | |
raise ValueError("Error: HF_TOKEN is not set in environment variables.") | |
try: | |
print("Logging in to Hugging Face...") | |
login(token=hf_token, add_to_git_credential=False) | |
print("Hugging Face login successful.") | |
except Exception as e: | |
print(f"Error during Hugging Face login: {e}") | |
raise | |
def main(): | |
""" | |
Main function to perform all operations. | |
""" | |
# Define the repository URL and target directory | |
repo_url = "https://github.com/dadwadw233/BoxDreamer.git" | |
target_dir = "./BoxDreamer" | |
try: | |
# Clone the repository and initialize submodules | |
clone_and_initialize_repo(repo_url, target_dir) | |
# Log in to Hugging Face | |
huggingface_login() | |
print("All operations completed successfully.") | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
if __name__ == "__main__": | |
main() |