ai_image_creation / Instructions.txt
magicfixeseverything's picture
Upload 2 files
cf6ac90
raw
history blame
No virus
16 kB
AI Image Creation Offline on Windows using NVIDIA GPU
About installing Diffusers in a virtual environment:
https://huggingface.co/docs/diffusers/installation
But I include the instructions on what you need to do below.
---
I am still working on this. (note added December 7th, 2023) I am very new to
this, so I'm sure there are many ways this could be done better.
---
You must have a NVIDIA graphics card with Cuda installed. I already had Cuda
installed. If you need Cuda, it's in a step below. This script is written to
be used on my Windows computer using the GPU. This script will not work
using a CPU. While there is code in the script for CPU usage, it doesn't
work on a Windows computer. It might be related to the Triton module not
being available in Windows. There seem to be something that might work, but
it would have meant installing something from elsewhere that I was not
willing to try.
The version of CUDA might matter. If you have one that doesn't work, you
might have to install a different version of CUDA.
If you have installed Automatic1111, you don't need to do step 1 and 2 as
that program required Python and Git. You don't need Automatic1111 installed
to use this program.
About Automatic1111:
https://github.com/AUTOMATIC1111/stable-diffusion-webui#installation-and-running
Your Antivirus/Firewall software will need to be set to allow the command
prompt to download and install these programs. What you allow or don't allow
is up to you. You may need to reinstall some of it if you don't allow it
through the first time and it doesn't work for you. I don't know what the
minimum you need to allow would be, or if some or all of it can operate in a
sandboxed environment.
---
Step 1 (if you haven't done already):
Install Python 3.10.6:
https://www.python.org/ftp/python/3.10.6/python-3.10.6-amd64.exe
Make sure to check "Add Python to PATH"
---
Step 2 (if you haven't done already):
Install git
https://git-scm.com/download/win
---
Step 3:
Create directory manually:
C:/Diffusers
This can be anywhere, but if you change it here you need to change it in all
the other places in these instructions and in some of the files you
download.
---
Step 4:
You need to open a regular Command Prompt. You can press Win key + R to get
"Run" window. In that, enter:
cmd
In Command Prompt, copy this line and press Enter:
cd C:\Diffusers && py -m venv .venv && .venv\Scripts\activate.bat
This will change the directory of the command prompt to the directory you
created. Next, it creates a virtual environment for Python as described
here:
https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
Finally, it activates that virtual environment as also described there.
The command prompt will now look like this:
(.venv) C:\Diffusers>
You don't need to reopen the Command Prompt each time. Wait until a task
finishes and then you will be able to enter the next command. It's best you
do not close it.
If you do close it, you will need to go to the directory again each time and
activate the virtual environment like this, by copying this and pressing
Enter:
cd C:\Diffusers && .venv\Scripts\activate.bat
You do not want to install Python packages outside of the virtual
environment as it would replace the packages in your main installation. That
could cause conflicts in other programs.
Always make sure you have:
(.venv) C:\Diffusers>
In future steps. (though I have added the code to activate it every time as
a precaution)
---
Step 5 (optional):
pip is the package installer for Python.
If you want to make sure pip is up-to-date, in Command Prompt (that is doing
things in your virtual environment), copy this line and press Enter:
cd C:\Diffusers && .venv\Scripts\activate.bat && py -m pip install --upgrade pip
If you want to then verify it updated, you could optionally do this, in
Command Prompt, copy this line and press Enter:
cd C:\Diffusers && .venv\Scripts\activate.bat && py -m pip --version
Since you are using a virtual environment, packages that are installed will
be installed into this folder rather than into Python's main program:
C:/Diffusers/.venv/Lib/site-packages
That way you don't cause issues updating things you may not want to have
updated.
---
Step 6 (if needed; unsure of this step, I didn't do it):
If you don't have Cuda and need to get it, it's here:
https://developer.nvidia.com/cuda-toolkit-archive
I have 12.2 installed. Some things mention needing 12.1. In Step 8, as of
writing this, it specifically mentions Cuda 12.1. You may want to check the
PyTorch website first in Step 8 if you need to install Cuda to see what
version it is compatible with or you will not get past that step.
That link above has a list of prior versions. As packages update, you might
need something even newer.
To see what version of Cuda you have, in Command Prompt, copy this
line and press Enter:
nvidia-smi
In the top right corner of the text output that is displayed, your Cuda
version will be displayed.
An older page on this command is here:
https://developer.nvidia.com/nvidia-system-management-interface
The text output now looks different.
---
Step 7:
You need to install PyTorch.
You should visit their website and see what version they recommend based on
the version of Cuda you have.
https://pytorch.org/
As of writing this, in the "INSTALL PYTORCH" section I selected the Stable
PyTorch build, Windows, Pip, Python and Cuda 12.1.
That generated in the "Run this Command" section the command you see below.
(with the added code to do this in the virtual environment)
To install PyTorch in Command Prompt (that is doing things in your virtual
environment), copy this line and press Enter:
cd C:\Diffusers && .venv\Scripts\activate.bat && pip3 install -U xformers --index-url https://download.pytorch.org/whl/cu121
A lot of things will download and install, including any packages needed to
run this. When complete, move on to the next step. (when the command prompt
returns to where you can paste into it again)
The torch file alone is over 2 gigabytes.
---
Step 8:
This step installs the rest of what you need.
You can read about them if you want:
https://huggingface.co/docs/diffusers/index
https://huggingface.co/docs/transformers/index
https://huggingface.co/docs/accelerate/index
https://pypi.org/project/ftfy/
https://github.com/modin-project/modin
https://pypi.org/project/invisible-watermark/
https://github.com/gradio-app/gradio
To install these, in Command Prompt (that is doing things in your virtual
environment), all you need to do is copy this line and press Enter:
cd C:\Diffusers && .venv\Scripts\activate.bat && pip install diffusers transformers accelerate ftfy modin[all] invisible_watermark
A whole bunch of things, what might be 100 packages or more, will download
and install, including any packages needed to run these.
When complete, you might need to install a later version of Gradio. A later
version fixes a bug that is needed for the menus to work properly. This
worked for me:
cd C:\Diffusers && .venv\Scripts\activate.bat && pip install https://gradio-builds.s3.amazonaws.com/6b1401c514c2ec012b0a50c72a6ec81cb673bf1d/gradio-4.8.0-py3-none-any.whl
That was found here:
https://www.gradio.app/docs/blocks
After selecting "main" from the version number dropdown in the left column.
The link changes. If you don't want to doit that way, just do this instead
to see if the dropdown menus work:
cd C:\Diffusers && .venv\Scripts\activate.bat && pip install gradio
The gallery feature doesn't allow images to be downloaded using the download
button in the current version above. Hopefully that will work in a later
version. (so eventually you will need to try another Gradio version)
When a later version is eventually called, "gradio" will eventually be added
to the original command rather than having to specify it separately.
When complete, move on to the next step.
---
Step 9:
Download the following file:
https://huggingface.co/spaces/magicfixeseverything/ai_image_creation/raw/main/app.py
Add it into a folder you create here:
C:/Diffusers/.venv/
Like this:
C:/Diffusers/.venv/ai_image_creation/
Add then add the file here:
C:/Diffusers/.venv/ai_image_creation/app.py
There are some things you will need to change in that file, most notably the
directory specified in "main_dir"
---
Step 10:
You're ready to run the script that runs the web interface.
The first time the models download it will take time as it's gigabytes of
data that you need to download. An individual model file isn't downloaded
until you actually try creating an image for that model. For all four
models, the total size might be about 20 gigabytes if running on a GPU. It
could be twice that size if on a CPU.
In Command Prompt (that is doing things in your virtual environment), copy
this line and press Enter:
cd C:\Diffusers && .venv\Scripts\activate.bat && py .venv\ai_image_creation\app.py
That will launch the web interface.
Model files are downloaded to:
C:\Diffusers\model_data
You should know that this folder can grow considerably. You need to manually
clean it out when you need to get rid of old models. Occasionally, new
versions will be downloaded when you create an image. When that happens, you
may want to remove the old version if the new version works. Otherwise, you
could have 10 to 20 gigabytes of space added each time.
You should also know that temporary images created are likely stored here:
%USERPROFILE%/AppData/Local/Temp/gradio
Those however are likely cleaned out automatically by your computer.
A package called Triton apparently can't be installed on Windows so you will
see an error in the Command Prompt go by about that every time you launch
the web interface. It doesn't seem to impact anything when using a GPU on
Windows.
https://github.com/openai/triton/issues/1057
https://github.com/openai/triton/issues/1640
Someone offers something there that might work, but I didn't want to install
something from someone randomly. Again, it seems to work without it when
using a GPU. If trying to use a CPU using Windows, then it might mean you
can't use this script.
When done, you will get a message like this:
Running on local URL: http://127.0.0.1:7860
If your browser did not open a web browser, visit the link that was in the
command prompt. If it was not the one above, note it. However, if you have
something like Automatic1111 open already, it will use a different port,
like "7861".
It might be this:
http://127.0.0.1:7860
---
Step 11:
When you open the link, you'll see the display to create the imagery. You
must leave the command prompt open. When you process an image, the command
prompt will tell you the progress of what you are creating and approximately
how long it will take for the step it is on. It will take longer as there
will be things that happen for which there is no progress indicator.
---
Step 12:
To create a shortcut to the command prompt that needs to be launched every
time, download the following file:
https://huggingface.co/spaces/magicfixeseverything/ai_image_creation/raw/main/ai_image_creation.bat
Add it into the folder you created:
C:/Diffusers/.venv/ai_image_creation/ai_image_creation.bat
The content of that file is the following:
@echo off
cd C:\Diffusers
call .venv\Scripts\activate.bat
timeout /t 0 >nul
py .venv\ai_image_creation\app.py
cmd /k
For whatever reason, sometimes a shortcut from the desktop doesn't execute.
I am seeing if a timeout for zero seconds works. (>nul makes the delay
message not appear)
Next, we will create a shortcut to that, which you could do on the desktop.
You can follow the instructions below or download this file instead:
https://huggingface.co/spaces/magicfixeseverything/ai_image_creation/resolve/main/AI%20Image%20Creation.lnk
Right click on your desktop and then click on "New > Shortcut".
For "Type the location of the item", enter:
cmd
Then click "Next". You can name the shortcut whatever you want, like
"AI Image Creation".
Then click "Finish".
Then right click on the shortcut you created and click "Properties".
In "Target", replace what is there with the following:
C:\Windows\System32\cmd.exe /k C:/Diffusers/.venv/ai_image_creation/ai_image_creation.bat
That will execute that file each time you click on it.
If for some reason that doesn't work sometimes, and rather than get the
program to launch it simply opens the command prompt, then you must enter
this each time in the command prompt:
cd C:\Diffusers && .venv\Scripts\activate.bat && py .venv\ai_image_creation\app.py
You might also not be able to use a shortcut. If so, just copy
"ai_image_creation.bat" and put it on your desktop and name it what you
want.
---
Step 13 (Important):
I feel this is a very important step. After you have created model data for
each base model, as well as have used the refiner and upscaler, data will
then have been downloaded. This could be 30 gigabytes or more of data. I
strongly recommend that you then disable the script from downloading updates
to the model data. It will not automatically delete old data. If you didn't
manually go through and delete the older data, eventually the model data
would use all of the space on your computer.
There is something that is very important to note however. If you do this,
other installations that use Hugging Face for example, like Automatic1111,
will not be able to download data and will not work properly.
Once you have downloaded the model data for each model, you can disable
updating by doing the following in a command prompt:
setx HF_HUB_OFFLINE "1"
It doesn't matter if it is in your virtual environment or not as it will be
stored with all your environment variables.
To view where this variable goes, in the search bar in Windows you can look
for:
edit environment variables
And it will be listed.
If you need to once again download model data, you can delete it there or
enter the following in the command prompt:
setx HF_HUB_OFFLINE "0" && REG DELETE HKEY_CURRENT_USER\Environment /v HF_HUB_OFFLINE /f
In my testing, setting it to 0 and then deleting it made it immediately take
effect, once you restart the script, to be able to download model data
again.
You can read about environment variables here at Hugging Face:
https://huggingface.co/docs/huggingface_hub/package_reference/environment_variables#hfhuboffline
In regard to Hugging Face caching things, you can learn more on this page:
https://huggingface.co/docs/huggingface_hub/how-to-cachehttps://huggingface.co/docs/huggingface_hub/how-to-cache#limitations
This is just my preferred way of handling it.
Optionally, you can disable telemetry by entering this in the command
prompt:
setx HF_HUB_DISABLE_TELEMETRY "1"
And to turn it back on:
setx HF_HUB_DISABLE_TELEMETRY "0" && REG DELETE HKEY_CURRENT_USER\Environment /v HF_HUB_DISABLE_TELEMETRY /f