BoxDreamer / app.py
LittleFrog's picture
Update app.py
9475268 verified
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 start_gradio_demo():
"""
Start the Gradio demo.
"""
try:
print("Starting Gradio demo...")
subprocess.run(["python", "-m", "src.demo.gradio_demo", "--hf", "--downsample"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error starting Gradio demo: {e}")
raise
def modify_file(file_path):
"""
Modify specific lines in a file.
Replace lines 107 and 108 with a valid `raise ImportError` statement.
"""
try:
# Read the file contents
with open(file_path, "r") as file:
lines = file.readlines()
# Modify the specified lines
lines[106] = " raise ImportError(\"Custom import error message\")\n" # Line 107 (index starts at 0)
lines[107] = "" # Line 108, replace with an empty line or remove it
# Write the modified lines back to the file
with open(file_path, "w") as file:
file.writelines(lines)
print(f"File {file_path} modified successfully: lines 107 and 108 replaced.")
except FileNotFoundError:
print(f"Error: File {file_path} not found.")
raise
except Exception as e:
print(f"Error modifying file {file_path}: {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"
# Define the file to be modified
file_to_modify = "./three/dust3r/croco/models/pos_embed.py"
try:
# Clone the repository and initialize submodules
clone_and_initialize_repo(repo_url, target_dir)
# Modify the specified file
# modify_file(file_to_modify)
# Log in to Hugging Face
huggingface_login()
# Start the Gradio demo
start_gradio_demo()
print("All operations completed successfully.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()