SonicDiffusionClean / compatibility_patches.py
alpercagann's picture
Add torch to requirements
dc72d06
"""
Compatibility patches for huggingface_hub and diffusers
"""
import sys
import importlib
from functools import wraps
# Check if huggingface_hub is installed
try:
import huggingface_hub
# Add the cached_download function if it doesn't exist
if not hasattr(huggingface_hub, 'cached_download'):
def cached_download(*args, **kwargs):
"""Compatibility function to replace cached_download"""
# Use the newer hf_hub_download function
return huggingface_hub.hf_hub_download(*args, **kwargs)
# Add the missing function to the module
huggingface_hub.cached_download = cached_download
except ImportError:
print("huggingface_hub not found, skipping patch")
# Patch for diffusers dynamic_modules_utils.py
try:
import diffusers.utils.dynamic_modules_utils as dmu
# Store the original import statement
original_import = dmu.__import__
# Define a wrapper for __import__
@wraps(original_import)
def patched_import(name, *args, **kwargs):
try:
return original_import(name, *args, **kwargs)
except ImportError as e:
if 'cached_download' in str(e) and name == 'huggingface_hub':
# Import the module without the missing function
mod = importlib.import_module(name)
# Add the missing function
if not hasattr(mod, 'cached_download'):
def cached_download(*args, **kwargs):
return mod.hf_hub_download(*args, **kwargs)
mod.cached_download = cached_download
return mod
raise
# Apply the patch
dmu.__import__ = patched_import
except ImportError:
print("diffusers.utils.dynamic_modules_utils not found, skipping patch")