-
Notifications
You must be signed in to change notification settings - Fork 87
/
main.py
330 lines (238 loc) · 15.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
import sys, os
import random
import uuid
import re
from datetime import datetime
import time
sys.path.append(os.path.abspath(".."))
from call_txt2img import *
from call_img2img import *
from build_dynamic_prompt import *
from call_extras import *
from model_lists import *
def generateimages(amount = 1, size = "all",model = "currently selected model",samplingsteps = "40",cfg= "7",hiresfix = True,hiressteps ="0",denoisestrength="0.6",samplingmethod="DPM++ SDE Karras", upscaler="R-ESRGAN 4x+", hiresscale="2",apiurl="http://127.0.0.1:7860",qualitygate=False,quality="7.6",runs="5",insanitylevel="5",subject="all", artist="all", imagetype="all",silentmode=False, workprompt="", antistring="",prefixprompt="", suffixprompt="", negativeprompt="",promptcompounderlevel = "1", seperator="comma", img2imgbatch = "1", img2imgsamplingsteps = "20", img2imgcfg = "7", img2imgsamplingmethod = "DPM++ SDE Karras", img2imgupscaler = "R-ESRGAN 4x+", img2imgmodel = "currently selected model", img2imgactivate = False, img2imgscale = "2", img2imgpadding = "64",img2imgdenoisestrength="0.3",ultimatesdupscale=False,usdutilewidth = "512", usdutileheight = "0", usdumaskblur = "8", usduredraw ="Linear", usduSeamsfix = "None", usdusdenoise = "0.35", usduswidth = "64", usduspadding ="32", usdusmaskblur = "8",controlnetenabled=False, controlnetmodel="",img2imgdenoisestrengthmod="-0.05",enableextraupscale = False,controlnetblockymode = False,extrasupscaler1 = "all",extrasupscaler2 ="all",extrasupscaler2visiblity="0.5",extrasupscaler2gfpgan="0",extrasupscaler2codeformer="0.15",extrasupscaler2codeformerweight="0.1",extrasresize="2",onlyupscale="false",givensubject="",smartsubject=True,giventypeofimage="",imagemodechance=20, gender="all", chosensubjectsubtypeobject="all", chosensubjectsubtypehumanoid="all", chosensubjectsubtypeconcept="all", increasestability = False, qualityhiresfix = False, qualitymode = "highest", qualitykeep="keep used", basesize = "512", promptvariantinsanitylevel = 0, givenoutfit = "", autonegativeprompt = True, autonegativepromptstrength = 0, autonegativepromptenhance = False, base_model = "SD1.5", OBP_preset = "", amountoffluff = "none", promptenhancer = "none", presetprefix = "", presetsuffix = ""):
loops = int(amount) # amount of images to generate
steps = 0
upscalefilelist=[]
originalimage = ""
originalpnginfo =""
randomprompt = ""
filename=""
continuewithnextpart = True
randomsubject = ""
originalmodel = model
originalsamplingmethod = samplingmethod
originalnegativeprompt = negativeprompt
originalimg2imgmodel = img2imgmodel
originalimg2imgsamplingmethod = img2imgsamplingmethod
originalimg2imgupscaler = img2imgupscaler
originalupscaler = upscaler
insanitylevel = int(insanitylevel)
originalimg2imgdenoisestrength = img2imgdenoisestrength
originalimg2imgpadding = img2imgpadding
currentlyselectedmodel = ""
modellist=get_models()
samplerlist=get_samplers()
upscalerlist=get_upscalers()
img2imgupscalerlist=get_upscalers_for_img2img()
img2imgsamplerlist=get_samplers_for_img2img()
tempmodel = "v1-5-pruned-emaonly.safetensors [6ce0161689]"
optionsresponse = requests.get(url=f'{apiurl}/sdapi/v1/options')
optionsresponsejson = optionsresponse.json()
currentlyselectedmodelhash = optionsresponsejson["sd_checkpoint_hash"]
sdmodelsrespone = requests.get(url=f'{apiurl}/sdapi/v1/sd-models')
sdmodelsresponsejson = sdmodelsrespone.json()
for item in sdmodelsresponsejson:
if(item['sha256'] == currentlyselectedmodelhash):
currentlyselectedmodel = item['title']
break
# Print the 'title' if found
if currentlyselectedmodel != "":
print("current selected model is:")
print(currentlyselectedmodel)
else:
print("Cannot find current model.")
currentlyselectedmodel = tempmodel
while(currentlyselectedmodel == tempmodel or tempmodel not in modellist):
tempmodel = random.choice(modellist)
if(onlyupscale==True):
script_dir = os.path.dirname(os.path.abspath(__file__)) # Script directory
inputupscalemefolder = os.path.join(script_dir, "./automated_outputs/upscale_me/" )
for upscalefilename in os.listdir(inputupscalemefolder):
f = os.path.join(inputupscalemefolder, upscalefilename)
# checking if it is a file
if os.path.isfile(f):
if(f[-3:]!="txt"):
upscalefilelist.append(f)
loops = len(upscalefilelist)
if(loops==0):
print('No files to upscale found! Please place images in //upscale_me// folder')
else:
print("")
print("Found and upscaling files")
print("")
if(ultimatesdupscale==False):
upscalescript="SD upscale"
else:
upscalescript="Ultimate SD upscale"
while steps < loops:
# load the base model as a workaround
if(steps > 0 and increasestability == True):
print("")
print("Increase Stability has been turned on.")
print("To prevent a memory issue, we are going to unload and then load the checkpoint back in.")
print("This helps with a memory leak issue. However A1111 is bad with memory management.")
print("")
response = requests.post(url=f'{apiurl}/sdapi/v1/unload-checkpoint')
print("model unloaded")
response = requests.post(url=f'{apiurl}/sdapi/v1/reload-checkpoint')
print("model reloaded")
# build prompt
if(silentmode==True and workprompt == ""):
print("Trying to use provided workflow prompt, but is empty. Generating a random prompt instead.")
if(onlyupscale==False): # only do txt2img when onlyupscale is False
if(silentmode==True and workprompt != ""):
randomprompt = createpromptvariant(workprompt, promptvariantinsanitylevel)
print("Using provided workflow prompt")
print(randomprompt)
else:
randompromptlist = build_dynamic_prompt(insanitylevel,subject,artist,imagetype, False,antistring,prefixprompt,suffixprompt,promptcompounderlevel, seperator,givensubject,smartsubject,giventypeofimage,imagemodechance, gender, chosensubjectsubtypeobject, chosensubjectsubtypehumanoid, chosensubjectsubtypeconcept,True,False,-1,givenoutfit, prompt_g_and_l=True, base_model=base_model, OBP_preset=OBP_preset, prompt_enhancer=promptenhancer, preset_prefix=presetprefix, preset_suffix=presetsuffix)
randomprompt = randompromptlist[0]
randomsubject = randompromptlist[1]
if(autonegativeprompt):
negativeprompt = build_dynamic_negative(positive_prompt=randomprompt, insanitylevel=autonegativepromptstrength,enhance=autonegativepromptenhance, existing_negative_prompt=originalnegativeprompt, base_model=base_model)
randomprompt = flufferizer(prompt=randomprompt, amountoffluff=amountoffluff)
if(randomsubject == ""):
# make the filename, from from a to the first comma
# find the index of the first comma after "of a" or end of the prompt
if(randomprompt.find("of a ") != -1):
start_index = randomprompt.find("of a ") + len("of a ")
end_index = randomprompt.find(",", start_index)
if(end_index == -1):
end_index=len(randomprompt)
else:
start_index = 0
end_index = 128
# extract the desired substring using slicing
filename = randomprompt[start_index:end_index]
else:
filename = randomsubject[0:128] # Fix for too long filenames
# cleanup some unsafe things in the filename
filename = filename.replace("\"", "")
filename = filename.replace("[", "")
filename = filename.replace("|", "")
filename = filename.replace("]", "")
filename = filename.replace("<", "")
filename = filename.replace(">", "")
filename = filename.replace(":", "_")
filename = filename.replace(".", "")
filename = re.sub(r'[0-9]+', '', filename)
safe_characters = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.")
# Use regular expression to filter out any characters not in the whitelist
filename = re.sub(r"[^{}]+".format(re.escape(''.join(safe_characters))), '', filename)
if(filename==""):
filename = str(uuid.uuid4())
# create a datetime object for the current date and time
now = datetime.now()
filenamecomplete = now.strftime("%Y%m%d%H%M%S") + "_" + filename.replace(" ", "_").strip()
#Check if there is any random value we have to choose or not
if(originalmodel=="all"):
model = random.choice(modellist)
#lets not do inpainting models
while "inpaint" in model:
model = random.choice(modellist)
print("Going to run with model " + model)
if(originalmodel=="currently selected model"):
model = currentlyselectedmodel
# set the model here
#if(originalmodel!="currently selected model"):
option_payload = {
"sd_model_checkpoint": model
}
response = requests.post(url=f'{apiurl}/sdapi/v1/options', json=option_payload)
if(originalsamplingmethod=="all"):
samplingmethod = random.choice(samplerlist)
print ("Going to run with sampling method " + samplingmethod)
if(originalupscaler=="all" and hiresfix == True):
upscaler = random.choice(upscalerlist)
print ("Going to run with upscaler " + upscaler)
# WebUI fix for PLMS and UniPC and hiresfix
if(samplingmethod in ['PLMS', 'UniPC']): # PLMS/UniPC do not support hirefix so we just silently switch to DDIM
samplingmethod = 'DDIM'
txt2img = call_txt2img(randomprompt, size ,hiresfix, 0, filenamecomplete,model ,samplingsteps,cfg, hiressteps, denoisestrength,samplingmethod, upscaler,hiresscale,apiurl,qualitygate,quality,runs,negativeprompt, qualityhiresfix, qualitymode, qualitykeep, basesize)
originalimage = txt2img[0] #Set this for later use
originalpnginfo = txt2img[1] #Sort of hacky way of bringing this forward. But if it works, it works
continuewithnextpart = txt2img[2]
image = txt2img[0]
else:
if(filename==""):
filename = str(uuid.uuid4())
# create a datetime object for the current date and time
now = datetime.now()
filenamecomplete = now.strftime("%Y%m%d%H%M%S") + "_" + filename.replace(" ", "_").strip()
image = upscalefilelist[steps] # else we get the image from the upscale file list
originalimage = image # this is also the original image file
# upscale via img2img
img2imgloops = int(img2imgbatch)
if(img2imgactivate == False or continuewithnextpart == False): # If we dont want to run, turn it off
img2imgloops = 0
else:
#Check if there is any random value we have to choose or not
if(originalimg2imgmodel=="all"):
img2imgmodel = random.choice(modellist)
#lets not do inpainting models
while "inpaint" in model:
img2imgmodel = random.choice(modellist)
print("Going to upscale with model " + img2imgmodel)
if(originalimg2imgmodel=="currently selected model"):
img2imgmodel = currentlyselectedmodel
# set the model here
#if(originalimg2imgmodel!="currently selected model"):
option_payload = {
"sd_model_checkpoint": img2imgmodel
}
response = requests.post(url=f'{apiurl}/sdapi/v1/options', json=option_payload)
if(originalimg2imgsamplingmethod=="all"):
img2imgsamplingmethod = random.choice(img2imgsamplerlist)
print ("Going to upscale with sampling method " + img2imgsamplingmethod)
if(originalimg2imgupscaler=="all"):
img2imgupscaler = random.choice(img2imgupscalerlist)
print ("Going to run with upscaler " + img2imgupscaler)
# WebUI fix for PLMS and UniPC and img2img
if(img2imgsamplingmethod in ['PLMS', 'UniPC']): # PLMS/UniPC do not support img2img so we just silently switch to DDIM
img2imgsamplingmethod = 'DDIM'
img2imgsteps = 0
# start the batching!
img2imgdenoisestrength = originalimg2imgdenoisestrength
img2imgpadding = originalimg2imgpadding
while img2imgsteps < img2imgloops:
#filenamecomplete = originalfilenamecomplete + "_" + str(img2imgsteps)
#print(filenamecomplete)
img2img = call_img2img(image, originalimage, originalpnginfo, apiurl, filenamecomplete, randomprompt,negativeprompt,img2imgsamplingsteps, img2imgcfg, img2imgsamplingmethod, img2imgupscaler, img2imgmodel, img2imgdenoisestrength, img2imgscale, img2imgpadding,upscalescript,usdutilewidth, usdutileheight, usdumaskblur, usduredraw, usduSeamsfix, usdusdenoise, usduswidth, usduspadding, usdusmaskblur,controlnetenabled, controlnetmodel,controlnetblockymode)
image = img2img[0]
if(originalpnginfo==""):
originalpnginfo = img2img[1]
img2imgdenoisestrength = str(round(float(img2imgdenoisestrength) + float(img2imgdenoisestrengthmod),2)) # lower or increase the denoise strength for each batch
img2imgpadding = str(int(int(img2imgpadding) * float(img2imgscale))) # also increase padding by scale
if(int(img2imgpadding)>256): # but not overdo it :D
img2imgpadding="256"
# Sometimes, we are too quick to do another call, causing memory issues. So we wait a bit to let the system settle done a bit.
# Its stupid but it works. Sometimes....
time.sleep(5)
img2imgsteps += 1
# upscale via extras upscaler next
if(enableextraupscale==True and continuewithnextpart == True):
if(extrasupscaler1=="all"):
extrasupscaler1 = random.choice(img2imgupscalerlist)
print ("Going to upscale with upscaler 1 " + extrasupscaler1)
if(extrasupscaler2=="all"):
extrasupscaler2 = random.choice(img2imgupscalerlist)
print ("Going to upscale with upscaler 2 " + extrasupscaler2)
image = call_extras(image, originalimage, originalpnginfo, apiurl, filenamecomplete,extrasupscaler1,extrasupscaler2 ,extrasupscaler2visiblity,extrasupscaler2gfpgan,extrasupscaler2codeformer,extrasupscaler2codeformerweight,extrasresize)
if(continuewithnextpart == True):
# only count images we actually fully processed
steps += 1
print("")
print("All done!")
def tryinterrupt(apiurl="http://127.0.0.1:7860"):
response = requests.post(url=f'{apiurl}/sdapi/v1/interrupt')