#!/usr/bin/env python3 """ Pre-build script for Hugging Face Spaces This script handles the installation of GroundingDINO and downloads required weights """ import os import subprocess import sys from pathlib import Path def run_command(cmd, cwd=None): """Run a shell command and handle errors""" print(f"Running: {cmd}") try: result = subprocess.run( cmd, shell=True, check=True, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, ) print(result.stdout) return True except subprocess.CalledProcessError as e: print(f"Error running command: {cmd}") print(f"Error output: {e.stdout}") return False def setup_groundingdino(): """Setup GroundingDINO""" print("Setting up GroundingDINO...") # Check if GroundingDINO directory already exists # Install GroundingDINO if not run_command("pip install -v -e ."): return False return True def main(): """Main setup function""" print("Starting pre-build setup for Hugging Face Spaces...") # Setup GroundingDINO os.system("pip install -r requirements_hf.txt") if not setup_groundingdino(): print("Failed to setup GroundingDINO") sys.exit(1) print("Pre-build setup completed successfully!") if __name__ == "__main__": main()