Unable to run Diffusers while offline

I am running into the error below when I try to run my Diffusers model completely offline (without any internet connection). However, my code works perfectly if I have internet.

I work in very remote locations for a few weeks out of every month and I don’t always have access to the internet while I am away. How can I avoid this error?

Traceback (most recent call last):
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\connection.py”, line 174, in _new_conn
conn = connection.create_connection(
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\util\connection.py”, line 72, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File “C:\Users\User\AppData\Local\Programs\Python\Python310\lib\socket.py”, line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\connectionpool.py”, line 703, in urlopen
httplib_response = self._make_request(
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\connectionpool.py”, line 386, in _make_request
self._validate_conn(conn)
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\connectionpool.py”, line 1042, in _validate_conn
conn.connect()
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\connection.py”, line 358, in connect
self.sock = conn = self._new_conn()
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\connection.py”, line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x00000257B6781EA0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\requests\adapters.py”, line 489, in send
resp = conn.urlopen(
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\connectionpool.py”, line 787, in urlopen
retries = retries.increment(
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\urllib3\util\retry.py”, line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host=‘raw.githubusercontent.com’, port=443): Max retries exceeded with url: /CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml (Caused by NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x00000257B6781EA0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed’))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\User\PycharmProjects\runinf\main.py”, line 8, in
import gen_img
File “C:\Users\User\PycharmProjects\runinf\gen_img.py”, line 17, in
pipe = StableDiffusionImg2ImgPipeline.from_single_file(pretrained_model_link_or_path=model_id_or_path, torch_dtype=torch.float16, local_files_only=True)
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\diffusers\loaders.py”, line 1924, in from_single_file
pipe = download_from_original_stable_diffusion_ckpt(
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\diffusers\pipelines\stable_diffusion\convert_from_ckpt.py”, line 1253, in download_from_original_stable_diffusion_ckpt
original_config_file = BytesIO(requests.get(config_url).content)
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\requests\api.py”, line 73, in get
return request(“get”, url, params=params, **kwargs)
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\requests\api.py”, line 59, in request
return session.request(method=method, url=url, **kwargs)
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\requests\sessions.py”, line 587, in request
resp = self.send(prep, **send_kwargs)
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\requests\sessions.py”, line 701, in send
r = adapter.send(request, **kwargs)
File “C:\Users\User\PycharmProjects\runinf\venv\lib\site-packages\requests\adapters.py”, line 565, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘raw.githubusercontent.com’, port=443): Max retries exceeded with url: /CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml (Caused by NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x00000257B6781EA0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed’))

Process finished with exit code 1

I am using the following code to establish my pipeline:

model_id_or_path = “./model/diff_model_3.safetensors”

pipe = StableDiffusionImg2ImgPipeline.from_single_file(pretrained_model_link_or_path=model_id_or_path, torch_dtype=torch.float16, local_files_only=True)
print(pipe.config.values())

pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)

pipe = pipe.to(device)

For anyone encountering the issue of the Huggingface diffusers library not working with local models when there’s no internet connection, I’ve identified a solution.

I initially intended to submit a Pull Request to GitHub but noticed that another contributor has already submitted a similar fix. I can confirm that the proposed changes effectively resolve the issue.

If you are currently having the same issue and want a quick fix, you can make the following changes to the loaders.py file:

  1. Modify from_single_file() function

Add a keyword argument to the function definition as follows:
original_config_file = kwargs.pop("original_config_file", None)

  1. Modify download_from_original_stable_diffusion_ckpt() function at the end of the ‘from_single_file()’ function

Pass the new keyword argument like this:
original_config_file=original_config_file

This will allow you to specify a local .yaml file for the original configuration of the model.

You can find the URLs for the .yaml files in the convert_from_ckpt.py file. This is what the library attempts to download, causing the error.

As of the time of writing, relevant URLs are located as follows:

Or, you can use the .yaml’s packaged in Automatic1111’s stable-diffusion-webui/configs if you are already using their GUI.

Hi @BoltMonkey! Is this the PR you were referring to? add config_file to from_single_file by zuojianghua · Pull Request #4614 · huggingface/diffusers · GitHub

If so, then I think the problem would be resolved, right?

Yes, that is the one.