File size: 1,280 Bytes
c009d4f
f7edee4
c009d4f
f7edee4
b7d4bc8
c009d4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7d4bc8
c009d4f
b7d4bc8
c009d4f
b7d4bc8
c009d4f
b7d4bc8
c009d4f
b7d4bc8
c009d4f
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
import gc
from typing import List
import gradio as gr
from utils.app_utils import _ensure_model_downloaded
from core.settings import ALL_MODEL_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, display_name in enumerate(required_models):
            if progress and hasattr(progress, '__call__'):
                progress(i / max(len(required_models), 1), desc=f"Checking file: {display_name}")
            try:
                _ensure_model_downloaded(display_name, progress)
            except Exception as e:
                raise gr.Error(f"Failed to download model '{display_name}'. Reason: {e}")
        print(f"--- [ModelManager] ✅ All required models are present on disk. ---")
    
model_manager = ModelManager()