python diffusers StableDiffusionXLPipeline 离线使用

  

下载sd_xl_base_1.0.safetensors

https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main

python diffusers StableDiffusionXLPipeline 离线使用_第1张图片

我这下载后放到项目 models 里

python diffusers StableDiffusionXLPipeline 离线使用_第2张图片 

model_path = "./models/v1-5-pruned-emaonly.safetensors"
# model_path = "./models/v1-5-pruned.safetensors"
# modelId = "runwayml/stable-diffusion-v1-5"
if Util.isMac():
    # from_single_file
    pipe = StableDiffusionPipeline.from_single_file(model_path, original_config_file='./models/v1-inference.yaml', cache_dir='./cache/', use_safetensors=True)
    pipe = pipe.to("mps")
    pipe.enable_attention_slicing()
else:
    pipe = StableDiffusionPipeline.from_single_file(model_path, torch_dtype=torch.float16, use_safetensors=True)
    pipe.to("cuda")
    pipe.enable_model_cpu_offload()
    pipe.enable_attention_slicing()

修改package

Diffusers 里代码写死了会访问 huggingface,需要手工改掉



import sysconfig; 

def repalceStringInFile(srcStr, dstStr, filePath):
    # Read in the file
    with open(filePath, 'r') as file:
        filedata = file.read()

    # Replace the target string
    filedata = filedata.replace(srcStr, dstStr)

    # Write the file out again
    with open(filePath, 'w') as file:
        file.write(filedata)

sitedir=sysconfig.get_paths()["purelib"]
ckPtFilePath=f"{sitedir}/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py"
print(ckPtFilePath)

srcStr="https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"
dstStr="http://xxxxxx/github/CompVis_stable-diffusion_main_configs_stable-diffusion_v1-inference.yaml"

repalceStringInFile(srcStr, dstStr, ckPtFilePath)

srcStr="https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference-v.yaml"
dstStr="http://xxxxxx/github/Stability-AI_stablediffusion_main_configs_stable-diffusion_v2-inference-v.yaml"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)


srcStr="https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_base.yaml"
dstStr="http://xxxxxx/github/Stability-AI_generative-models_main_configs_inference_sd_xl_base.yaml"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)


srcStr="https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_refiner.yaml"
dstStr="http://xxxxxx/github/Stability-AI_generative-models_main_configs_inference_sd_xl_refiner.yaml"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)


ckPtFilePath=f"{sitedir}/diffusers/loaders.py"
print(ckPtFilePath)
srcStr="text_encoder=text_encoder,\n            vae=vae,"
dstStr="text_encoder=text_encoder,\n            local_files_only=True,\n            vae=vae,"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)


ckPtFilePath=f"{sitedir}/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py"
print(ckPtFilePath)
srcStr="has_projection=True, **config_kwargs"
dstStr="has_projection=True, local_files_only=local_files_only, **config_kwargs"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)

http://xxxxxx 需要自己下载github里的文件,找地方放,然后替换链接

local_files_only是让取本地缓存,这个缓存在当前用户文件夹下

ubuntu

/home/fxbox/.cache/huggingface/hub

mac

/Users/linzhiji/.cache/huggingface/hub

把能的机器里的缓存,拷贝到不能的机器里

你可能感兴趣的:(python,人工智能,深度学习)