| import shutil |
| import requests |
| from pathlib import Path |
| from zipfile import ZipFile |
| from io import BytesIO |
|
|
| def download_file(url: str, destination: Path) -> None: |
| """Download a file from URL to the specified destination.""" |
| response = requests.get(url) |
| response.raise_for_status() |
| destination.write_bytes(response.content) |
|
|
| def download_github_folder(repo_owner: str, repo_name: str, folder_path: str, destination: Path) -> None: |
| """Download a specific folder from a GitHub repository using the ZIP download feature.""" |
| |
| zip_url = f"https://github.com/{repo_owner}/{repo_name}/archive/refs/heads/main.zip" |
| response = requests.get(zip_url) |
| response.raise_for_status() |
|
|
| |
| with ZipFile(BytesIO(response.content)) as zip_file: |
| folder_prefix = f"{repo_name}-main/{folder_path}" |
| |
| for file in zip_file.namelist(): |
| if file.startswith(folder_prefix): |
| |
| relative_path = file.replace(f"{repo_name}-main/", "", 1) |
| if relative_path.endswith('/'): |
| continue |
| |
| |
| content = zip_file.read(file) |
| |
| |
| output_path = destination / relative_path |
| output_path.parent.mkdir(parents=True, exist_ok=True) |
| |
| |
| output_path.write_bytes(content) |
|
|
| def setup_imagebind(): |
| """Setup ImageBind by downloading only required files.""" |
| |
| imagebind_dir = Path("imagebind") |
| if imagebind_dir.exists(): |
| shutil.rmtree(imagebind_dir) |
| |
| |
| download_github_folder( |
| repo_owner="facebookresearch", |
| repo_name="ImageBind", |
| folder_path="imagebind", |
| destination=Path(".") |
| ) |
| |
| |
| setup_py_url = "https://raw.githubusercontent.com/facebookresearch/ImageBind/main/setup.py" |
| setup_py_path = Path("setup.py") |
| download_file(setup_py_url, setup_py_path) |
|
|
| if __name__ == "__main__": |
| setup_imagebind() |