Files
CalamitousFelicitousness f6607e8922 fix(hub): install the token header hijack at startup
Implicit tokens are disabled process-wide, so the hijack that re-attaches the
token explicitly is what keeps gated repos reachable. It was installed by the
checkpoint loader and the caption paths, so loading a video model into a fresh
session sent unauthenticated requests and gated repos answered 401.
2026-08-14 03:39:01 +01:00

102 lines
4.8 KiB
Python

import os
import time
import gradio as gr
from modules import sd_hijack_hfhub
from modules.logger import log
from modules.shared import opts
# initialize huggingface environment
def hf_init():
os.environ.setdefault('HF_HUB_DISABLE_EXPERIMENTAL_WARNING', '1')
os.environ.setdefault('HF_HUB_DISABLE_IMPLICIT_TOKEN', '1')
os.environ.setdefault('HF_HUB_DISABLE_SYMLINKS_WARNING', '1')
os.environ.setdefault('HF_HUB_DISABLE_TELEMETRY', '1')
os.environ.setdefault('HF_HUB_VERBOSITY', 'warning')
os.environ.setdefault('HF_HUB_DOWNLOAD_TIMEOUT', '60')
os.environ.setdefault('HF_HUB_ETAG_TIMEOUT', '10')
os.environ.setdefault('HF_ENABLE_PARALLEL_LOADING', 'true' if opts.sd_parallel_load else 'false')
os.environ.setdefault('HF_HUB_CACHE', opts.hfcache_dir)
os.environ.setdefault('HF_XET_CACHE', opts.xetcache_dir)
if opts.hf_transfer_mode == 'HTTP':
os.environ.setdefault('HF_XET_HIGH_PERFORMANCE', 'false')
os.environ.setdefault('HF_HUB_DISABLE_XET', 'true')
elif opts.hf_transfer_mode == 'XET':
os.environ.setdefault('HF_XET_HIGH_PERFORMANCE', 'false')
os.environ.setdefault('HF_HUB_DISABLE_XET', 'false')
elif opts.hf_transfer_mode == 'XET HighPerformance':
os.environ.setdefault('HF_XET_HIGH_PERFORMANCE', 'true')
os.environ.setdefault('HF_HUB_DISABLE_XET', 'false')
os.environ.setdefault('HF_XET_RECONSTRUCT_WRITE_SEQUENTIALLY', 'false')
elif opts.hf_transfer_mode == 'XET Sequential':
os.environ.setdefault('HF_XET_HIGH_PERFORMANCE', 'false')
os.environ.setdefault('HF_HUB_DISABLE_XET', 'false')
os.environ.setdefault('HF_XET_RECONSTRUCT_WRITE_SEQUENTIALLY', 'true')
obfuscated_token = None
if len(opts.huggingface_token) > 0 and opts.huggingface_token.startswith('hf_'):
obfuscated_token = 'hf_...' + opts.huggingface_token[-4:]
log.info(f'Huggingface: transfer={opts.hf_transfer_mode} parallel={opts.sd_parallel_load} direct={opts.diffusers_to_gpu} token="{obfuscated_token}" cache="{opts.hfcache_dir}"')
# the disable flag above drops the token from token=None requests, which is what diffusers and
# transformers send; the hijack re-adds it explicitly and has to precede the first download
sd_hijack_hfhub.init_hijack()
def hf_check_cache():
t0 = time.time()
from modules.modelstats import stat
prev_default = os.environ.get("SD_HFCACHEDIR", None) or os.path.join(os.path.expanduser('~'), '.cache', 'huggingface', 'hub')
if opts.hfcache_dir != prev_default:
size, _mtime = stat(prev_default)
if size // 1024 // 1024 > 99:
log.warning(f'Huggingface cache changed: type=huggingface unused="{prev_default}" size={size//1024//1024} MB')
prev_default = os.path.join(os.path.expanduser('~'), '.cache', 'huggingface', 'xet')
if opts.xetcache_dir != prev_default:
size, _mtime = stat(prev_default)
if size // 1024 // 1024 > 99:
log.warning(f'Huggingface cache changed: type=xet unused="{prev_default}" size={size//1024//1024} MB')
def check_thread():
hf_size, _mtime = stat(opts.hfcache_dir)
xet_size, _mtime = stat(opts.xetcache_dir)
t1 = time.time()
log.debug(f'Huggingface: cache="{opts.hfcache_dir}" size={hf_size//1024//1024} MB xet="{opts.xetcache_dir}" size={xet_size//1024//1024} MB time={t1-t0:.2f}')
from threading import Thread
Thread(target=check_thread, daemon=True).start()
def hf_search(keyword):
import huggingface_hub as hf
t0 = time.time()
hf_api = hf.HfApi()
models = hf_api.list_models(model_name=keyword, full=True, filter="diffusers", limit=50, sort="downloads")
data = []
for model in models:
tags = [t for t in model.tags if not t.startswith('diffusers') and not t.startswith('license') and not t.startswith('arxiv') and len(t) > 2]
data.append([model.id, model.pipeline_tag, tags, model.downloads, model.lastModified, f'https://huggingface.co/{model.id}'])
log.debug(f'Huggingface: search="{keyword}" results={len(data)} time={time.time()-t0:.2f}')
return data
def hf_select(evt: gr.SelectData, df):
row = list(df.iloc[evt.index[0]])
log.debug(f'Huggingface: selected={row} index={evt.index}')
return row[0] # repo_id only
def hf_download_model(hub_id: str, token, variant, revision, mirror, custom_pipeline):
from modules.modelloader import download_diffusers_model
download_diffusers_model(hub_id, cache_dir=opts.diffusers_dir, token=token, variant=variant, revision=revision, mirror=mirror, custom_pipeline=custom_pipeline)
from modules.sd_models import list_models # pylint: disable=W0621
list_models()
log.info(f'Huggingface: model="{hub_id}" downloaded')
return f'Diffuser model downloaded: model="{hub_id}"'
def hf_update_token(token):
log.debug('Huggingface: update token')
opts.huggingface_token = token
opts.save()