File size: 2,432 Bytes
ef20d79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from constants import DEVICE, LCM_DEFAULT_MODEL_OPENVINO
from backend.tiny_decoder import get_tiny_decoder_vae_model
from typing import Any
from backend.device import is_openvino_device
from paths import get_base_folder_name

if is_openvino_device():
    from huggingface_hub import snapshot_download
    from optimum.intel.openvino.modeling_diffusion import OVBaseModel

    from optimum.intel.openvino.modeling_diffusion import (
        OVStableDiffusionPipeline,
        OVStableDiffusionImg2ImgPipeline,
        OVStableDiffusionXLPipeline,
        OVStableDiffusionXLImg2ImgPipeline,
    )
    from backend.openvino.custom_ov_model_vae_decoder import CustomOVModelVaeDecoder


def ov_load_taesd(
    pipeline: Any,
    use_local_model: bool = False,
):
    taesd_dir = snapshot_download(
        repo_id=get_tiny_decoder_vae_model(pipeline.__class__.__name__),
        local_files_only=use_local_model,
    )
    pipeline.vae_decoder = CustomOVModelVaeDecoder(
        model=OVBaseModel.load_model(f"{taesd_dir}/vae_decoder/openvino_model.xml"),
        parent_model=pipeline,
        model_dir=taesd_dir,
    )


def get_ov_text_to_image_pipeline(
    model_id: str = LCM_DEFAULT_MODEL_OPENVINO,
    use_local_model: bool = False,
) -> Any:
    if "xl" in get_base_folder_name(model_id).lower():
        pipeline = OVStableDiffusionXLPipeline.from_pretrained(
            model_id,
            local_files_only=use_local_model,
            ov_config={"CACHE_DIR": ""},
            device=DEVICE.upper(),
        )
    else:
        pipeline = OVStableDiffusionPipeline.from_pretrained(
            model_id,
            local_files_only=use_local_model,
            ov_config={"CACHE_DIR": ""},
            device=DEVICE.upper(),
        )

    return pipeline


def get_ov_image_to_image_pipeline(
    model_id: str = LCM_DEFAULT_MODEL_OPENVINO,
    use_local_model: bool = False,
) -> Any:
    if "xl" in get_base_folder_name(model_id).lower():
        pipeline = OVStableDiffusionXLImg2ImgPipeline.from_pretrained(
            model_id,
            local_files_only=use_local_model,
            ov_config={"CACHE_DIR": ""},
            device=DEVICE.upper(),
        )
    else:
        pipeline = OVStableDiffusionImg2ImgPipeline.from_pretrained(
            model_id,
            local_files_only=use_local_model,
            ov_config={"CACHE_DIR": ""},
            device=DEVICE.upper(),
        )
    return pipeline