-
Notifications
You must be signed in to change notification settings - Fork 109
Nodes will not function offline - a fix so groundingdino doesn't need to call huggingface #198
Description
Superb nodes - BUT - I've had to change some code to use offline. Is there a way you can resolve this, or are you at the whim of groundingdino here?
The problem
It is not possible to use many of your nodes offline. This seems to be a problem with using the python package 'groundingdino' which insists on making calls to huggingface then fails.
I feel the security-conscious will look to run AI models offline, particularly where trying out new code from others.
Previous issues
This was raised previously, of sorts in #102 by @axymeus, without resolution. It was closed as it looked to be a single user issue. A second user @marchcat69 recently commented with the same problem, and proposed a work-around which apparently worked for them. It is a very useful comment, but it includes only 1 of the 2 steps needed to work around the problem; plus the issues was already closed without a fix.
Please can I present the problem and my current work-around, and ask if you can see a way to resolving this?
Details
your SAM2 node AILab_SAM2Segment.py is a good example of the problem.
- The code downloads models to: models/SAM/
- The code downloads dino to: models/grounding-dino/
(great they are all cached for offline use now)
GroundingDINO is used to load bert-base-uncased... and sadly that doesn't cache the files.
To recreate the problem, startu ComfyUI without any internet connection.
Running the node give output:
got prompt
final text_encoder_type: bert-base-uncased
'(MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /bert-base-uncased/resolve/main/tokenizer_config.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at
..... and inevitable repeated futile attemts to keep trying for a minute or so
bert-base-uncased can be downloaded from https://huggingface.co/bert-base-uncased/
As commented in the above thread, it is possible to download models to: models/bert-base-uncased
Work-around (fix of sorts)
Unfortunately it is necessary to edit GroundingDINO to progress further
groundingdino.py - located in /your_path/ComfyUI/.venv/lib/python3.12/site-packages/groundingdino/models/GroundingDINO
lines 107 and 108 use code from 'get_tokenlizer.py'
self.tokenizer = get_tokenlizer.get_tokenlizer(text_encoder_type)
self.bert = get_tokenlizer.get_pretrained_language_model(text_encoder_type)
these call the 2 routines in get_tokenlizer - located in /yourpath/ComfyUI/.venv/lib/python3.12/site-packages/groundingdino/util
In the function "get_tokenlizer", I changed line 19 from
tokenizer = AutoTokenizer.from_pretrained(text_encoder_type)
to
tokenizer = AutoTokenizer.from_pretrained("/yourpath/ComfyUI/models/bert-base-uncased")
✅ line 107 in groundingdino.py could now run.
In the function "get_pretrained_language_model", I changed line 25 from
return BertModel.from_pretrained(text_encoder_type)
to
return BertModel.from_pretrained("/yourpath/ComfyUI/models/bert-base-uncased")
✅ line 108 in groundingdino.py was now successfully exectued.
...all your nodes now work offline!
As an aside. I've also seen comment on a very old thread about AutoTokenizer being an issue when using local models, and a suggestion using BertTokenizer.from_pretrained can help; that also worked, but that change didn't seem to be needed.
Anyhow, that fixes things - but can your node be changed so the edit isn't needed and all can run happily offline?
Thanks again for these and other nodes; always invaluable.