7/19/2024

Download specific model from hugging face

 refer to code

.

import os
import shutil
from huggingface_hub import hf_hub_download

# Repository name
repo_id = "h94/IP-Adapter"
# Directory to save the downloaded files
local_directory = "./models/image_encoder"

# Ensure the local directory exists
os.makedirs(local_directory, exist_ok=True)

# List of files to download
files_to_download = [
"models/image_encoder/config.json",
"models/image_encoder/model.safetensors",
"models/image_encoder/pytorch_model.bin"
]

# Download each file and move it to the desired directory
for file in files_to_download:
file_path = hf_hub_download(repo_id=repo_id, filename=file)
# Construct the destination path
dest_path = os.path.join(local_directory, os.path.basename(file))
# Move the file to the destination path
shutil.move(file_path, dest_path)
print(f"Downloaded and moved to {dest_path}")

..


Thank you.


other option is

file_path = hf_hub_download(repo_id=repo_id, filename=file, cache_dir=local_directory, force_download=True)


No comments:

Post a Comment