-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_downloader.py
78 lines (68 loc) · 2.6 KB
/
model_downloader.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import logging
from typing import List, Tuple, Dict
from diffusers import PixArtAlphaPipeline, StableDiffusionUpscalePipeline, ControlNetModel, DiffusionPipeline
import os
from config import MODEL_MID_RES, MODEL_HIGH_RES, UPSCALER_MODEL, CONTROLNET_MODEL, FREESTYLE_MODEL
logger = logging.getLogger(__name__)
ModelInfo = Tuple[type, str]
def get_model_list() -> List[ModelInfo]:
"""
Get a list of models to be downloaded.
Returns:
List[ModelInfo]: A list of tuples containing the model class and model name.
"""
return [
(PixArtAlphaPipeline, MODEL_MID_RES),
(PixArtAlphaPipeline, MODEL_HIGH_RES),
(StableDiffusionUpscalePipeline, UPSCALER_MODEL),
(ControlNetModel, CONTROLNET_MODEL),
(DiffusionPipeline, FREESTYLE_MODEL)
]
def download_model(model_class: type, model_name: str) -> None:
"""
Download a specific model.
Args:
model_class (type): The class of the model to be downloaded.
model_name (str): The name of the model to be downloaded.
"""
logger.info(f"Checking/downloading {model_name}")
try:
if model_class == DiffusionPipeline and model_name == FREESTYLE_MODEL:
model_class.from_pretrained(model_name, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
else:
model_class.from_pretrained(model_name)
logger.info(f"Model {model_name} is ready")
except Exception as e:
logger.error(f"Error downloading {model_name}: {str(e)}")
def download_models() -> None:
"""
Download all required models.
"""
models = get_model_list()
for model_class, model_name in models:
download_model(model_class, model_name)
def get_model_status() -> Dict[str, str]:
"""
Get the download status of all required models.
Returns:
Dict[str, str]: A dictionary containing the status of each model.
"""
status = {}
models = get_model_list()
for _, model_name in models:
model_path = os.path.join(os.getcwd(), model_name)
if os.path.exists(model_path):
status[model_name] = "Downloaded"
else:
status[model_name] = "Not Downloaded"
return status
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
logger.info("Starting model download process...")
try:
download_models()
logger.info("Model download process completed successfully.")
except Exception as e:
logger.error(f"An error occurred during the model download process: {str(e)}", exc_info=True)
finally:
logger.info("Model download process ended.")