Spaces:
Running
on
Zero
Running
on
Zero
File size: 3,858 Bytes
822dc53 f28af66 822dc53 9e5b5c8 43e60f6 9475268 43e60f6 f28af66 d45f7f4 f28af66 d45f7f4 f28af66 311b348 822dc53 f28af66 d08d66c f28af66 822dc53 f28af66 9475268 f28af66 822dc53 43e60f6 822dc53 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
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() |