Skip to content

Instantly share code, notes, and snippets.

@MalikKillian
Last active July 29, 2023 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MalikKillian/2be8af358111049848139d95abddee64 to your computer and use it in GitHub Desktop.
Save MalikKillian/2be8af358111049848139d95abddee64 to your computer and use it in GitHub Desktop.
Stable Diffusion model downloader

This is just a basic script I made up to download Stable Diffusion models. I suppose I wanted a way to quickly recreate my setup if I decided to tear it all down and start fresh. Uh, I guess it sounded like a good idea before I wrote it then I just decided to keep at it until it was done.

This script assumes you're running within an existing AUTOMATIC1111 installation and you've gone through the setup (it requires libraries installed by the venv). This also assumes you're running it from the AUTOMATIC1111/stable-diffusion-webui-ALT/models folder (in order to place downloads into the right folder).

Here's an example of it executing (forgive the use of Windows):

PS C:\Users\Malik\Documents\AUTOMATIC1111\stable-diffusion-webui-ALT> & c:/Users/Malik/Documents/AUTOMATIC1111/stable-diffusion-webui-ALT/venv/Scripts/Activate.ps1
(venv) PS C:\Users\Malik\Documents\AUTOMATIC1111\stable-diffusion-webui-ALT> & c:/Users/Malik/Documents/AUTOMATIC1111/stable-diffusion-webui-ALT/venv/Scripts/python.exe c:/Users/Malik/Documents/AUTOMATIC1111/stable-diffusion-webui-ALT/models/add-models.py
sd-v1-5-inpainting.ckpt -- SKIPPED (preexisting file, checksum confirmed)
westernAnimation_v1.safetensors: 100%|███████████████████████████████████████| 1.99G/1.99G [01:47<00:00, 19.8MB/s]
(venv) PS C:\Users\Malik\Documents\AUTOMATIC1111\stable-diffusion-webui-ALT>
import os
from typing import Callable
import urllib.request
from urllib.error import HTTPError, URLError
import shutil
def generate_stub_validator(hash_value):
def is_checksum_valid(filepath):
"""
If this wasn't a stub we would compare the contents of `filepath` against `hash_value`, but this IS a stub.
Returns `True` if `filepath` exists; `False` otherwise.
"""
return True if os.path.exists(filepath) else False
return is_checksum_valid
def create_request(url):
return urllib.request.Request(
url,
data=None,
headers={
# Some sites reject the default Python agent... be nice!
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
def get_filename_from_cd(cd, default_filename='unknown_model.safetensors'):
"""
Get filename from content-disposition
"""
import re
cd_sanitized = cd or ''
fname = re.findall('filename=[\'"]?([\w\-\.]+)', cd_sanitized)
return fname[0].replace('"', '') if len(fname) > 0 else default_filename
def get_model_w_progress(url: str, is_file_checksum_correct: Callable[[str], bool]):
"""
url: URL of the model to be downloaded.
is_file_checksum_correct: Function accepting a file path and returning True if the file has the correct hash value.
"""
from tqdm import tqdm
req = create_request(url)
try:
response = urllib.request.urlopen(req)
filename = get_filename_from_cd(
response.getheader('content-disposition')
, None
)
except:
print("-- SKIPPED (could not determine filename)")
return
file_path = os.path.join(os.getcwd(), 'models/Stable-diffusion', filename)
if is_file_checksum_correct(file_path):
print(f'{os.path.basename(file_path)} -- SKIPPED (preexisting file, checksum confirmed)')
response.close()
return
with tqdm.wrapattr(
open(file_path, "wb"),
"write",
miniters=1,
desc=os.path.basename(file_path),
total=getattr(response, 'length', None)) as fout:
for chunk in response:
fout.write(chunk)
dummy_validator = generate_stub_validator("this param is ignored")
"""
Start adding your models!
Let me know if you implement a hash validator. I've been too lazy to do it myself.
"""
# sd-v1-5-inpainting.ckpt
get_model_w_progress("https://huggingface.co/runwayml/stable-diffusion-inpainting/resolve/main/sd-v1-5-inpainting.ckpt", dummy_validator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment