ai_image_creation / Instructions.txt
magicfixeseverything's picture
Upload 2 files
638e2b5
raw
history blame
18.7 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.
You should make sure that you have at least 10 gigabytes of space,
perhaps more, before installing this. You should make sure you have at
least 50 gigabytes more of space before downloading model data. For
information about the space this installation will use, go to the end
of the file first.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Instructions
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
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://pypi.org/project/torchsde/
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 gradio torchsde
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 different version of Gradio.
I used version 4.11.0 in testing. In some versions before that there was
a bug that made dropdown menus not work properly. Another bug was that
an image couldn't be downloaded using the download button when viewing a
gallery. This worked for me:
cd C:\Diffusers && .venv\Scripts\activate.bat && pip install https://gradio-builds.s3.amazonaws.com/d4068557953746662235d595ec435c42ceb24414/gradio-4.11.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 when there is a new version. A version history
of Gradio is here:
https://www.gradio.app/changelog
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
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.
Please note that if you don't use "ai_image_creation.bat" you will need
to set "HF_HUB_OFFLINE" manually if you want to change it. (this is
discussed in next step)
--------------------
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 40 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.
If you use "ai_image_creation.bat" to launch the script, you can set
"HF_HUB_OFFLINE" in that file to 1 to not download data again.
That is the easiest way that doesn't impact other installations. If you
wanted to permanently store that environment variable in Windows, which
I don't recommend, it would impact other installations that use Hugging
Face. This only has it apply when using the script.
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
The first time you use the script you will need to set HF_HUB_OFFLINE to
0 like this there:
set HF_HUB_OFFLINE=0
Do not include any spaces before or after the equals sign.
The script will not work otherwise. Because of the importance of this
variable, I prefer to not have the script work until someone changes
that value themselves.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Other Information
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
File Sizes
For perspective, on my computer the ".venv" folder has just over 50,000
files, and nearly 6,000 folders, and is nearly 6 gigabytes. The model
data folder ("model_data") has the following folders and these are their
sizes:
5.10 GB models--circulus--canvers-realistic-v3.6
5.10 GB models--circulus--canvers-real-v3.7.5
3.97 GB models--circulus--canvers-real-v3.8
3.97 GB models--circulus--canvers-real-v3.8.1
1.98 GB models--runwayml--stable-diffusion-v1-5
2.15 GB models--stabilityai--sd-x2-latent-upscaler
12.50 GB models--stabilityai--sdxl-turbo
6.62 GB models--stabilityai--stable-diffusion-xl-base-1.0
5.81 GB models--stabilityai--stable-diffusion-xl-refiner-1.0