Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 44 additions & 47 deletions install.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import launch
from modules import paths_internal
import subprocess
import urllib.request
from tqdm import tqdm
import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict
from modules import paths_internal

# Define model URLs
MODEL_URLS = {
Expand All @@ -13,33 +11,31 @@
"video_model": "https://huggingface.co/spensercai/DeOldify/resolve/main/ColorizeVideo_gen.pth"
}

# Clean dependency list — compatible with Python 3.10 and modern SD envs
DEPS = [
'wandb',
'fastai==1.0.60',
'tensorboardX',
'ffmpeg',
'ffmpeg-python',
'yt-dlp',
'opencv-python',
'Pillow'
"fastai==1.0.60",
"tensorboardX",
"ffmpeg-python",
"yt-dlp",
"opencv-python",
"Pillow>=9.0.0,<10.0.0"
]

models_dir = os.path.join(paths_internal.models_path, "deoldify")

# Ensure models directory exists
os.makedirs(models_dir, exist_ok=True)


def download(url, path):
"""Download a file from a URL to a specific path, showing progress."""
"""Download a file from a URL to a specific path with progress bar."""
if os.path.exists(path):
return False # File already exists, no need to download
return False
try:
with urllib.request.urlopen(url) as request, open(path, 'wb') as f, tqdm(
with urllib.request.urlopen(url) as request, open(path, "wb") as f, tqdm(
desc=f"Downloading {os.path.basename(path)}",
total=int(request.headers.get('Content-Length', 0)),
unit='B',
total=int(request.headers.get("Content-Length", 0)),
unit="B",
unit_scale=True,
unit_divisor=1024
unit_divisor=1024,
) as progress:
for chunk in iter(lambda: request.read(4096), b""):
f.write(chunk)
Expand All @@ -49,41 +45,42 @@ def download(url, path):
print(f"Failed to download {os.path.basename(path)}: {e}")
return False


def download_models():
"""Download all models if they don't exist locally."""
models_already_downloaded = True
"""Ensure all model weights are present."""
all_downloaded = True
for name, url in MODEL_URLS.items():
path = os.path.join(models_dir, os.path.basename(url))
if download(url, path):
models_already_downloaded = False
if models_already_downloaded:
all_downloaded = False
if all_downloaded:
print("All models for DeOldify are already downloaded.")

def check_and_install_dependencies():
"""Install required dependencies for DeOldify, with version checks."""
dependencies_met = True

def install_dependencies():
"""Install required pip packages safely, ignoring conflicts."""
print("\nInstalling DeOldify dependencies...\n")
for dep in DEPS:
package_name, *version = dep.split('==')
version = version[0] if version else None
if not check_package_installed(package_name, version):
dependencies_met = False
print(f"Installing {dep} for DeOldify extension.")
launch.run_pip(f"install {dep}", package_name)
if dependencies_met:
print("All requirements for the DeOldify extension are already installed.")
print(f"Installing {dep} ...")
cmd = [
"python",
"-m",
"pip",
"install",
"--no-cache-dir",
"--prefer-binary",
"--upgrade",
"--quiet",
dep,
]
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
print(f"Warning: Failed to install {dep}, continuing anyway.")

print("\nDeOldify dependency setup complete.\n")

def check_package_installed(package_name, version=None):
"""Check if a package is installed with an optional version."""
if version:
package_spec = f"{package_name}=={version}"
else:
package_spec = package_name
try:
pkg_resources.require(package_spec)
return True
except (DistributionNotFound, VersionConflict):
return False

if __name__ == "__main__":
download_models()
check_and_install_dependencies()
install_dependencies()