起步
安裝依賴
- torch/tensorflow根據(jù)不同項目需要安裝
# 安裝
pip install huggingface_hub
# (按項目需要)安裝PyTorch的GPU版颤绕,及huggingface輔助特性
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
pip install 'huggingface_hub[torch]'
# (按項目需要)安裝tensorflow躏嚎,及huggingface輔助特性
pip install tensorflow
pip install 'huggingface_hub[tensorflow]'
# 其他常用依賴
pip install transformers accelerate
# 項目可能有各自需要的依賴纺裁,看項目介紹去安裝
注冊/登錄
- 目前注冊/登錄必須有"科學"上網環(huán)境
- 很多倉庫下載需要權限诫肠,所以先配置token
- 注冊HuggingFace賬號,并登錄
- 創(chuàng)建AccessToken路徑:右上角用戶頭像 > "Settings" > "Access Tokens" > "Create new token" > "Token Type"切換到"Read" > 隨意填個名字 > "Create token" > 復制下來欺缘,格式"hf_***"
- 如下代碼登錄
- 登錄后栋豫,token存儲在"~/.cache/huggingface/token"
- 僅需登錄(運行)一次,除非token失效
from huggingface_hub import login
token = 'hf_***'
login(token)
下載模型
使用cli
# 安裝工具
pip install huggingface_hub
# 設置鏡像地址
export HF_ENDPOINT=https://hf-mirror.com
# 下載整個庫
huggingface-cli download 'stabilityai/stable-diffusion-3-medium-diffusers'
huggingface-cli download 'stabilityai/stable-diffusion-3-medium-diffusers' --local-dir 'sd'
huggingface-cli download 'stabilityai/stable-diffusion-3-medium-diffusers' --local-dir 'sd' --token 'hf_****'
# 下載特定文件
huggingface-cli download 'stabilityai/stable-diffusion-3-medium-diffusers' 'xxx.safetensors'
huggingface-cli download 'stabilityai/stable-diffusion-3-medium-diffusers' 'xxx.safetensors' --local-dir 'sd'
huggingface-cli download 'stabilityai/stable-diffusion-3-medium-diffusers' 'xxx.safetensors' --local-dir 'sd' --token 'hf_****'
使用python
- 下載
- 未指定local_dir參數(shù)時丛肢,默認下載路徑:~/.cache/huggingface/
import os
from huggingface_hub import hf_hub_download, snapshot_download
# 設置鏡像地址
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
# 下載整個倉庫
snapshot_download(repo_id="stabilityai/stable-diffusion-3-medium-diffusers")
snapshot_download(repo_id="stabilityai/stable-diffusion-3-medium-diffusers", local_dir="sd")
snapshot_download(repo_id="stabilityai/stable-diffusion-3-medium-diffusers", local_dir="sd", token="hd_***")
# 下載特定文件
hf_hub_download(repo_id="stabilityai/stable-diffusion-3-medium-diffusers", filename="xxx.safetensors")
hf_hub_download(repo_id="stabilityai/stable-diffusion-3-medium-diffusers", filename="xxx.safetensors", local_dir="sd")
hf_hub_download(repo_id="stabilityai/stable-diffusion-3-medium-diffusers", filename="xxx.safetensors", local_dir="sd", token="hd_***")
示例
- 每個項目運行都有差別,注意看項目的README
- 一部分要加載項目中的"model_index.json"配置運行
- 一部分使用ComfyUI運行
文生圖(emilianJR/epiCRealism)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
pip install 'huggingface_hub[torch]'
pip install transformers accelerate diffusers
- 下載模型
- 默認下載路徑:~/.cache/huggingface/
# 設置鏡像
export HF_ENDPOINT=https://hf-mirror.com
# 下載模型
huggingface-cli download 'emilianJR/epiCRealism'
# 默認空間不夠的剿干,--local-dir參數(shù)指定下載路徑蜂怎,使用時替換成該路徑即可
huggingface-cli download 'emilianJR/epiCRealism' --local-dir 'models/emilianJR/epiCRealism'
from diffusers import StableDiffusionPipeline
import torch
model_id = "emilianJR/epiCRealism"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
prompt = "提示詞,要用英文置尔,這個模型中文效果差"
image = pipe(prompt).images[0]
image.save("image.png")
pip install gradio
from diffusers import StableDiffusionPipeline
import torch
import gradio as gr
model_id = "emilianJR/epiCRealism"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
def generate(prompt: str):
image = pipe(prompt).images[0]
return image
demo = gr.Interface(fn=generate,
inputs=gr.Textbox(label="提示詞杠步,使用英文,中文兼容性不好"),
outputs=gr.Image(),
examples=["A girl smiling", "A boy smiling", "A dog running"])
demo.launch()
文生視頻(ByteDance/AnimateDiff-Lightning)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
pip install 'huggingface_hub[torch]'
pip install transformers accelerate diffusers
- 輕量文生視頻榜轿,需要借助文生圖模型幽歼,再生成視頻
- 文生圖模型使用上述emilianJR/epiCRealism
# 設置鏡像
export HF_ENDPOINT=https://hf-mirror.com
# 基礎文生圖模型
huggingface-cli download 'emilianJR/epiCRealism'
# 下載文生視頻模型
huggingface-cli download 'ByteDance/AnimateDiff-Lightning' 'animatediff_lightning_4step_diffusers.safetensors'
# 也可以下載整個倉庫,倉庫包含多個級別的模型文件
huggingface-cli download 'ByteDance/AnimateDiff-Lightning'
import torch
from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
from diffusers.utils import export_to_gif
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
device = "cuda"
dtype = torch.float16
step = 4 # Options: [1,2,4,8]
repo = "ByteDance/AnimateDiff-Lightning"
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
base = "emilianJR/epiCRealism" # Choose to your favorite base model.
adapter = MotionAdapter().to(device, dtype)
adapter.load_state_dict(load_file(hf_hub_download(repo ,ckpt), device=device))
pipe = AnimateDiffPipeline.from_pretrained(base, motion_adapter=adapter, torch_dtype=dtype).to(device)
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
output = pipe(prompt="A girl smiling", guidance_scale=1.0, num_inference_steps=step)
export_to_gif(output.frames[0], "animation.gif")
文生圖(stable-diffusion-3)
pip install huggingface_hub
# 安裝PyTorch的CUDA(GPU)版颠蕴,安裝命令參考PyTorch官網:https://pytorch.org/
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
# 或者Torch的CPU版泣刹,建議上GPU,CPU根本跑不動
pip install 'huggingface_hub[torch]'
# 其他依賴
pip install transformers
pip install accelerate
pip install diffusers
pip install sentencepiece
pip install protobuf
- 下載模型
- 默認下載路徑:~/.cache/huggingface/
- --local-dir指定下載目錄犀被,運行代碼時將模型設置為相同目錄即可
# 設置環(huán)境變了
export HF_ENDPOINT=https://hf-mirror.com
# 下載整個庫
huggingface-cli download 'stabilityai/stable-diffusion-3-medium-diffusers'
huggingface-cli download 'stabilityai/stable-diffusion-3-medium-diffusers' --local-dir 'sd'
import torch
from diffusers import StableDiffusion3Pipeline
pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
# pipe = StableDiffusion3Pipeline.from_pretrained("sd", torch_dtype=torch.float16) # 指定目錄
pipe = pipe.to("cuda")
image = pipe("A cat holding a sign that says hello world",
negative_prompt="",
num_inference_steps=28,
guidance_scale=7.0).images[0]
# 保存成圖片
image.save('cat.jpg')
torch.OutOfMemoryError: CUDA out of memory.
Tried to allocate 512.00 MiB. GPU 0 has a total capacity of 11.00 GiB of which 0 bytes is free.
Of the allocated memory 16.80 GiB is allocated by PyTorch, and 574.00 MiB is reserved by PyTorch but unallocated.
If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.
See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)
可能遇到問題
報錯缺失'fbgemm.dll'
OSError: [WinError 126] 找不到指定的模塊。 Error loading "...\site-packages\torch\lib\fbgemm.dll" or one of its dependencies.