Spaces:
Runtime error
Runtime error
Update setup_imagebind.py
Browse files- setup_imagebind.py +22 -32
setup_imagebind.py
CHANGED
|
@@ -5,58 +5,48 @@ from zipfile import ZipFile
|
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
def download_file(url: str, destination: Path) -> None:
|
| 8 |
-
"""Download a file from URL to the specified destination."""
|
| 9 |
response = requests.get(url)
|
| 10 |
response.raise_for_status()
|
| 11 |
destination.write_bytes(response.content)
|
| 12 |
|
| 13 |
-
def download_github_folder(repo_owner: str, repo_name: str, folder_path: str,
|
| 14 |
-
"""Download a specific folder from a GitHub repository using the ZIP download feature."""
|
| 15 |
-
# Download the whole repository as a ZIP file
|
| 16 |
zip_url = f"https://github.com/{repo_owner}/{repo_name}/archive/refs/heads/main.zip"
|
| 17 |
response = requests.get(zip_url)
|
| 18 |
response.raise_for_status()
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
with ZipFile(BytesIO(response.content)) as zip_file:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Remove the repository name and branch prefix from the path
|
| 27 |
-
relative_path = file.replace(f"{repo_name}-main/", "", 1)
|
| 28 |
-
if relative_path.endswith('/'): # Skip directory entries
|
| 29 |
-
continue
|
| 30 |
-
|
| 31 |
-
# Read the file content from ZIP
|
| 32 |
-
content = zip_file.read(file)
|
| 33 |
-
|
| 34 |
-
# Create the file path
|
| 35 |
-
output_path = destination / relative_path
|
| 36 |
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 37 |
-
|
| 38 |
-
# Write the file
|
| 39 |
-
output_path.write_bytes(content)
|
| 40 |
|
| 41 |
def setup_imagebind():
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if
|
| 46 |
-
shutil.rmtree(
|
| 47 |
|
| 48 |
-
# Download the imagebind folder
|
| 49 |
download_github_folder(
|
| 50 |
repo_owner="facebookresearch",
|
| 51 |
repo_name="ImageBind",
|
| 52 |
folder_path="imagebind",
|
| 53 |
-
|
| 54 |
)
|
| 55 |
|
| 56 |
-
# Download setup.py file
|
| 57 |
setup_py_url = "https://raw.githubusercontent.com/facebookresearch/ImageBind/main/setup.py"
|
| 58 |
-
|
| 59 |
-
download_file(setup_py_url,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
setup_imagebind()
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
def download_file(url: str, destination: Path) -> None:
|
|
|
|
| 8 |
response = requests.get(url)
|
| 9 |
response.raise_for_status()
|
| 10 |
destination.write_bytes(response.content)
|
| 11 |
|
| 12 |
+
def download_github_folder(repo_owner: str, repo_name: str, folder_path: str, destination_root: Path) -> None:
|
|
|
|
|
|
|
| 13 |
zip_url = f"https://github.com/{repo_owner}/{repo_name}/archive/refs/heads/main.zip"
|
| 14 |
response = requests.get(zip_url)
|
| 15 |
response.raise_for_status()
|
| 16 |
|
| 17 |
+
target_folder_path_in_repo = f"{repo_name}-main/{folder_path}/"
|
| 18 |
+
local_target_folder = destination_root / folder_path
|
| 19 |
+
|
| 20 |
with ZipFile(BytesIO(response.content)) as zip_file:
|
| 21 |
+
for member_info in zip_file.infolist():
|
| 22 |
+
if member_info.filename.startswith(target_folder_path_in_repo) and not member_info.is_dir():
|
| 23 |
+
relative_path = member_info.filename.replace(target_folder_path_in_repo, "", 1)
|
| 24 |
+
output_path = local_target_folder / relative_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 26 |
+
output_path.write_bytes(zip_file.read(member_info.filename))
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def setup_imagebind():
|
| 29 |
+
current_dir = Path(".")
|
| 30 |
+
|
| 31 |
+
imagebind_code_dir = current_dir / "imagebind"
|
| 32 |
+
if imagebind_code_dir.exists():
|
| 33 |
+
shutil.rmtree(imagebind_code_dir)
|
| 34 |
|
|
|
|
| 35 |
download_github_folder(
|
| 36 |
repo_owner="facebookresearch",
|
| 37 |
repo_name="ImageBind",
|
| 38 |
folder_path="imagebind",
|
| 39 |
+
destination_root=current_dir
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
setup_py_url = "https://raw.githubusercontent.com/facebookresearch/ImageBind/main/setup.py"
|
| 43 |
+
setup_py_local_path = current_dir / "setup.py"
|
| 44 |
+
download_file(setup_py_url, setup_py_local_path)
|
| 45 |
+
|
| 46 |
+
readme_url = "https://raw.githubusercontent.com/facebookresearch/ImageBind/main/README.md"
|
| 47 |
+
readme_local_path = current_dir / "README.md" # This will overwrite your README.md in the build context
|
| 48 |
+
download_file(readme_url, readme_local_path)
|
| 49 |
+
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
setup_imagebind()
|