File size: 1,592 Bytes
b5ac208
052cbd9
b5ac208
266f72f
 
b5ac208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266f72f
 
 
b5ac208
266f72f
b5ac208
266f72f
 
 
 
 
 
b5ac208
266f72f
b5ac208
5306acf
b5ac208
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
import gc
from typing import List
import gradio as gr
from utils.app_utils import _ensure_model_downloaded, ensure_file_downloaded
from core.settings import ALL_MODEL_MAP, ALL_FILE_DOWNLOAD_MAP

class ModelManager:
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(ModelManager, cls).__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self):
        if hasattr(self, 'initialized'):
            return
        self.initialized = True
        print("✅ ModelManager initialized.")

    def ensure_models_downloaded(self, required_models: List[str], progress):
        print(f"--- [ModelManager] Ensuring models are downloaded: {required_models} ---")
        for i, item in enumerate(required_models):
            if not item:
                continue
            if progress and hasattr(progress, '__call__'):
                progress(i / max(len(required_models), 1), desc=f"Checking file: {item}")
            try:
                if item in ALL_MODEL_MAP:
                    _ensure_model_downloaded(item, progress)
                elif item in ALL_FILE_DOWNLOAD_MAP:
                    ensure_file_downloaded(item, progress)
                else:
                    _ensure_model_downloaded(item, progress)
            except Exception as e:
                raise gr.Error(f"Failed to download model '{item}'. Reason: {e}")
        print(f"--- [ModelManager] ✅ All required models are present on disk. ---")
    
model_manager = ModelManager()