File size: 1,906 Bytes
dc72d06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
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")