in docker_images/diffusers/app/pipelines/image_to_image.py [0:0]
def __init__(self, model_id: str):
use_auth_token = os.getenv("HF_API_TOKEN")
self.use_auth_token = use_auth_token
# This should allow us to make the image work with private models when no token is provided, if the said model
# is already in local cache
self.offline_preferred = validation.str_to_bool(os.getenv("OFFLINE_PREFERRED"))
model_data = self._hub_model_info(model_id)
kwargs = (
{"safety_checker": None}
if model_id.startswith("hf-internal-testing/")
else {}
)
env_dtype = os.getenv("TORCH_DTYPE")
if env_dtype:
kwargs["torch_dtype"] = getattr(torch, env_dtype)
elif torch.cuda.is_available():
kwargs["torch_dtype"] = torch.float16
if model_id == "stabilityai/stable-diffusion-xl-refiner-1.0":
kwargs["variant"] = "fp16"
# check if is controlnet or SD/AD
config_file_name = None
for file_name in ("config.json", "model_index.json"):
if any(file.rfilename == file_name for file in model_data.siblings):
config_file_name = file_name
break
if config_file_name:
config_file = self._hub_repo_file(model_id, config_file_name)
with open(config_file, "r") as f:
config_dict = json.load(f)
model_type = config_dict.get("_class_name", None)
else:
raise ValueError("Model type not found")
# load according to model type
if model_type == "ControlNetModel":
model_to_load = (
model_data.cardData["base_model"]
if "base_model" in model_data.cardData
else "runwayml/stable-diffusion-v1-5"
)
controlnet = ControlNetModel.from_pretrained(
model_id, use_auth_token=use_auth_token, **kwargs
)
self.ldm = StableDiffusionControlNetPipeline.from_pretrained(
model_to_load,
controlnet=controlnet,
use_auth_token=use_auth_token,
**kwargs,
)
elif model_type in ["AltDiffusionPipeline", "AltDiffusionImg2ImgPipeline"]:
self.ldm = AltDiffusionImg2ImgPipeline.from_pretrained(
model_id, use_auth_token=use_auth_token, **kwargs
)
elif model_type in [
"StableDiffusionPipeline",
"StableDiffusionImg2ImgPipeline",
]:
self.ldm = StableDiffusionImg2ImgPipeline.from_pretrained(
model_id, use_auth_token=use_auth_token, **kwargs
)
elif model_type in ["StableUnCLIPPipeline", "StableUnCLIPImg2ImgPipeline"]:
self.ldm = StableUnCLIPImg2ImgPipeline.from_pretrained(
model_id, use_auth_token=use_auth_token, **kwargs
)
elif model_type in [
"StableDiffusionImageVariationPipeline",
"StableDiffusionInstructPix2PixPipeline",
"StableDiffusionUpscalePipeline",
"StableDiffusionLatentUpscalePipeline",
"StableDiffusionDepth2ImgPipeline",
]:
self.ldm = DiffusionPipeline.from_pretrained(
model_id, use_auth_token=use_auth_token, **kwargs
)
elif model_type in ["KandinskyImg2ImgPipeline", "KandinskyPipeline"]:
model_to_load = "kandinsky-community/kandinsky-2-1-prior"
self.ldm = KandinskyImg2ImgPipeline.from_pretrained(
model_id, use_auth_token=use_auth_token, **kwargs
)
self.prior = KandinskyPriorPipeline.from_pretrained(
model_to_load, use_auth_token=use_auth_token, **kwargs
)
else:
logger.debug("Falling back to generic auto pipeline loader")
self.ldm = AutoPipelineForImage2Image.from_pretrained(
model_id, use_auth_token=use_auth_token, **kwargs
)
if isinstance(
self.ldm,
(
StableUnCLIPImg2ImgPipeline,
StableUnCLIPPipeline,
StableDiffusionPipeline,
StableDiffusionImg2ImgPipeline,
AltDiffusionPipeline,
AltDiffusionImg2ImgPipeline,
StableDiffusionControlNetPipeline,
StableDiffusionInstructPix2PixPipeline,
StableDiffusionImageVariationPipeline,
StableDiffusionDepth2ImgPipeline,
),
):
self.ldm.scheduler = DPMSolverMultistepScheduler.from_config(
self.ldm.scheduler.config
)
if not idle.UNLOAD_IDLE:
self._model_to_gpu()