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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

    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, then install this version of Gradio. A later version fixes a
    bug that is needed for the menus to work properly.

cd C:\Diffusers && .venv\Scripts\activate.bat && pip install https://gradio-builds.s3.amazonaws.com/fce80ac804dce1b1b22a8b5c575a35cf0356b82a/gradio-4.8.0-py3-none-any.whl

    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)

    That command is described here:

        https://www.gradio.app/main/docs/interface

    When a later version is eventually called, "gradio" will eventually be added
    to the previous command rather than having to specify the version number and
    where to install from.

    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, model data will then have been downloaded. This could be 20
    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.

    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 HuggingFace:
        https://huggingface.co/docs/huggingface_hub/package_reference/environment_variables#hfhuboffline

    In regard to HuggingFace 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