File size: 20,289 Bytes
f5cf8c0 54318a5 f5cf8c0 54318a5 f5cf8c0 73ad51c f5cf8c0 2da904c 402eb66 2da904c f5cf8c0 54318a5 f5cf8c0 54318a5 f5cf8c0 1abae11 f5cf8c0 3105f6b 402eb66 54318a5 f5cf8c0 54318a5 74b9136 f5cf8c0 3dee35f 482b7a6 f5cf8c0 2da904c 8a125e3 f5cf8c0 2da904c 74b9136 2da904c 74b9136 2da904c 482b7a6 f5cf8c0 74b9136 f5cf8c0 1d5295c |
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 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 |
import requests
import random
from PIL import Image, ImageOps, ImageTk
from datetime import datetime
import time
from pathlib import Path
import io, re, base64
import zipfile
BASE_URL="https://image.novelai.net"
def image_to_base64(image):
image_bytesIO = io.BytesIO()
image.save(image_bytesIO, format="PNG")
return base64.b64encode(image_bytesIO.getvalue()).decode()
def make_turbo_prompt(gen_request):
lines = gen_request['prompt']
result = {
"boys": False,
"girls": False,
"1girl": False,
"1boy": False,
"1other": False,
"others": False
}
state = {
"nude,": False,
"pov,": False,
"cum,": False,
"after ": False,
"pussy juice": False,
"barefoot": False,
"breasts": False,
"ejaculation": False,
}
def insert_spaces(source_list, reference_list):
modified_list = source_list.copy()
for index, keyword in enumerate(reference_list):
if keyword not in source_list:
space_count = len(keyword) # ํค์๋ ๊ธธ์ด๋งํผ์ ๊ณต๋ฐฑ ๋ฌธ์
modified_list.insert(index, ' ' * space_count)
return modified_list
keywords = gen_request['prompt'].split(', ')
filtered_keywords = []
removed_indices = []
positive0, positive1, positive2, positive3 = gen_request.copy(),gen_request.copy(),gen_request.copy(),gen_request.copy()
for word in result.keys():
if word in lines:
result[word] = True
for word in state.keys():
if word in gen_request['prompt']:
state[word] = True
key_index = int((len(keywords)/2)-1)
if(result["1boy"]) or (result["boys"]):
if(result["1girl"]):
if(', sex' in gen_request['prompt'] or 'group sex' in gen_request['prompt']):
sex_pos_keywords = ['stomach bulge','insertion', 'fucked silly', 'x-ray', 'orgasm', 'cross-section', 'uterus', 'overflow', 'rape', 'vaginal', 'anal']
facial_keywords = ['tongue','ahegao']
temp_sex_pos = []
temp_facial = []
cum_events = []
explicit_check = []
if 'open mouth' in keywords: keywords.remove('open mouth')
if 'closed mouth' in keywords: keywords.remove('closed mouth')
if 'after rape' in keywords:
keywords.remove('after rape')
explicit_check.append('after rape')
for keyword in keywords:
if ('sex' not in keyword and 'cum' not in keyword and 'ejaculation' not in keyword and 'vaginal' not in keyword and 'penetration' not in keyword) and all(sex_pos not in keyword for sex_pos in sex_pos_keywords) and all(facial not in keyword for facial in facial_keywords):
filtered_keywords.append(keyword)
elif 'sex' in keyword:
removed_indices.append(keyword)
elif 'penetration' in keyword:
removed_indices.append(keyword)
elif 'cum' in keyword and keyword != 'cum':
cum_events.append(keyword)
elif any(sex_pos in keyword for sex_pos in sex_pos_keywords):
for sex_pos in sex_pos_keywords:
if sex_pos in keyword:
temp_sex_pos.append(sex_pos)
elif any(facial not in keyword for facial in facial_keywords):
for facial in facial_keywords:
if facial in keyword:
temp_facial.append(facial)
filtered_keywords.insert(int((len(filtered_keywords)/2)-1), ' no penetration, imminent penetration')
filtered_keywords_positive0 = filtered_keywords.copy()
filtered_keywords.remove(' no penetration, imminent penetration')
#0 imminent penetration, imminent sex
for i, keyword in enumerate(filtered_keywords):
if 'pantyhose' in keyword:
filtered_keywords[i] = 'torn ' + filtered_keywords[i]
#1 default
key_index = int((len(filtered_keywords)/2)-1)
if 'pussy' in filtered_keywords: key_index = filtered_keywords.index('pussy')
if 'penis' in filtered_keywords: key_index = filtered_keywords.index('penis')
filtered_keywords[key_index:key_index] = ['motion lines', 'surprised']
for keyword in removed_indices:
if 'cum' not in keyword and 'ejaculation' not in keyword:
filtered_keywords.insert(key_index,keyword)
if(temp_sex_pos): filtered_keywords[key_index:key_index] = temp_sex_pos
if('clothed sex' in filtered_keywords and not 'bottomless' in filtered_keywords): filtered_keywords.insert(filtered_keywords.index('clothed sex')+1, 'bottomless')
pos1_copied_keywords = filtered_keywords.copy()
for i, keyword in enumerate(pos1_copied_keywords):
if 'closed eyes' in keyword:
rand_num = random.randint(0,2)
if(rand_num == 0): pos1_copied_keywords[i] = 'half-' + pos1_copied_keywords[i]
elif(rand_num == 1 and 'closed eyes' in pos1_copied_keywords):
pos1_copied_keywords.remove('closed eyes')
filtered_keywords[i] = 'half-closed eyes'
filtered_keywords_positive1 = pos1_copied_keywords.copy()
#2 ejaculation,cum in pussy
key_index = filtered_keywords.index('surprised')
filtered_keywords.remove('surprised')
filtered_keywords[key_index:key_index] = ["ejaculation","cum"]
for keyword in removed_indices:
if 'cum' in keyword:
filtered_keywords.insert(key_index,keyword)
if(temp_facial): filtered_keywords[key_index:key_index] =temp_facial
filtered_keywords_positive2 = filtered_keywords.copy()
#3 after sex, after ejaculation
for i, keyword in enumerate(filtered_keywords):
if 'closed eyes' in keyword:
rand_num = random.randint(0,2)
if(rand_num == 0 and filtered_keywords[i] != 'half-closed eyes'): filtered_keywords[i] = 'half-' + filtered_keywords[i]
elif(rand_num == 1): filtered_keywords[i] = 'empty eyes'
else: filtered_keywords[i] = 'empty eyes, half-closed eyes'
if 'sex' in filtered_keywords:
key_index = filtered_keywords.index('sex')
elif 'group sex' in filtered_keywords:
key_index = filtered_keywords.index('group sex')
filtered_keywords.remove('ejaculation')
filtered_keywords[key_index:key_index] = ['cum drip', 'erection'] + cum_events
if(explicit_check): filtered_keywords[key_index:key_index] = explicit_check
if 'sex' in filtered_keywords and 'group sex' not in filtered_keywords:
if('pussy' in filtered_keywords and not 'anal' in filtered_keywords): filtered_keywords.insert(filtered_keywords.index('sex')+1, 'after vaginal, spread pussy')
elif('anal' in filtered_keywords): filtered_keywords.insert(filtered_keywords.index('sex')+1, 'after anus, cum in ass')
filtered_keywords.insert(filtered_keywords.index('sex'), 'after sex')
filtered_keywords.remove('sex')
elif 'group sex' in filtered_keywords:
if('vaginal' in filtered_keywords and not 'anal' in filtered_keywords):
filtered_keywords.insert(filtered_keywords.index('group sex')+1, 'after vaginal, spread pussy')
if 'multiple penises' in filtered_keywords: filtered_keywords.insert(filtered_keywords.index('group sex')+3, 'cum on body, bukkake')
elif('anal' in filtered_keywords):
filtered_keywords.insert(filtered_keywords.index('group sex')+1, 'after anus, cum in ass')
if 'multiple penises' in filtered_keywords: filtered_keywords.insert(filtered_keywords.index('group sex')+3, 'cum on body, bukkake')
else: filtered_keywords.insert(filtered_keywords.index('group sex')+1, 'cum on body, {bukkake}')
temp_post_keyword = []
for keyword in sex_pos_keywords:
if not (keyword == 'orgasm' or keyword == 'overflow'):
if keyword in filtered_keywords:
temp_post_keyword.append(keyword)
for keyword in temp_post_keyword:
filtered_keywords.remove(keyword)
positive0['prompt'] = ', '.join(insert_spaces(filtered_keywords_positive0, filtered_keywords)).strip()
positive1['prompt'] = ', '.join(insert_spaces(filtered_keywords_positive1, filtered_keywords)).strip()
positive2['prompt'] = ', '.join(insert_spaces(filtered_keywords_positive2, filtered_keywords)).strip()
positive3['prompt'] = ', '.join(filtered_keywords).strip()
positive0["type"] = "turbo"
positive1["type"] = "turbo"
positive2["type"] = "turbo"
positive3["type"] = "turbo"
return positive0, positive1, positive2, positive3
def generate_image(access_token, prompt, model, action, parameters):
data = {
"input": prompt,
"model": model,
"action": action,
"parameters": parameters,
}
response = requests.post(f"{BASE_URL}/ai/generate-image", json=data, headers={ "Authorization": f"Bearer {access_token}" })
# catch any errors
return response.content
def generate(gen_request):
def parse_and_execute_commands(_prompt, negative, user_input, rating):
negative = negative.split(',')
negative = [neg.strip() for neg in negative]
prompt = _prompt.split(',')
prompt = [key.strip() for key in prompt]
commands = [cmd.strip() for cmd in user_input.split(',') if not cmd.strip().startswith('#')]
for command in commands:
condition, cmd = parse_conditional_command(command)
if check_condition(prompt, condition, rating):
negative = execute_command(negative, cmd)
return ', '.join(negative)
def parse_conditional_command(command):
match = re.match(r"\((.*?)\)\:(.*)", command)
if match:
return match.groups()
return '', command
def check_condition(prompt, condition, rating):
if not condition:
return True
sub_conditions = re.split(r'\)\s*&\s*\(', condition)
sub_conditions = [re.sub(r'^\(|\)$', '', cond) for cond in sub_conditions]
results = []
for sub_cond in sub_conditions:
if '&' in sub_cond:
results.append(all(check_condition(prompt, cond, rating) for cond in sub_cond.split('&')))
elif '|' in sub_cond:
results.append(any(check_condition(prompt, cond, rating) for cond in sub_cond.split('|')))
else:
if sub_cond in ['e', 'q', 's', 'g']:
results.append(sub_cond == rating)
elif sub_cond in ['~e', '~q', '~s', '~g']:
results.append(sub_cond != rating)
# PM
elif sub_cond.startswith('*'):
results.append(sub_cond[1:] in prompt)
# NOT IN
elif sub_cond.startswith('~!'):
results.append(sub_cond[2:] not in prompt)
elif sub_cond.startswith('~'):
results.append(any(sub_cond[1:] not in element for element in prompt))
# CONTAIN
else:
results.append(any(sub_cond in element for element in prompt))
return all(results)
def execute_command(negative, command):
if '+=' in command:
keyword, addition = command.split('+=', 1)
addition = addition.replace('^', ', ')
return insert_text_after_keyword(negative, keyword, addition)
elif command.startswith('add '):
keyword = command[4:]
keyword = keyword.replace('^', ', ')
keys = keyword.split(',')
keys = [key.strip() for key in keys]
for key in keys:
if key not in negative:
negative.append(key)
return negative
elif command.startswith('rem '):
keyword = command[4:]
keyword = keyword.replace('^', ', ')
keys = keyword.split(',')
keys = [key.strip() for key in keys]
for key in keys:
if key in negative:
negative.remove(key)
return negative
elif '=' in command:
keyword, replacement = command.split('=', 1)
if keyword in negative:
replacement = replacement.replace('^', ', ')
index = negative.index(keyword)
negative[index] = replacement
return negative
def insert_text_after_keyword(negative, user_keyword, user_additional_keyword):
if user_keyword in negative:
index = negative.index(user_keyword) + 1
negative.insert(index, user_additional_keyword)
return negative
params = {
"legacy": False,
"quality_toggle": True if gen_request["quality_toggle"] == 1 else False,
"width": int(gen_request["width"]),
"height": int(gen_request["height"]),
"n_samples": 1,
"seed": int(gen_request["seed"]),
"extra_noise_seed": random.randint(0,9999999999),
"sampler": gen_request["sampler"],
"steps": 28 if gen_request["type"]!="upper" else gen_request["steps"],
"scale": gen_request["scale"],
"uncond_scale": gen_request["uncond_scale"],
"negative_prompt": ', '.join([keyword.strip() for keyword in gen_request["negative"].split(',') if not keyword.strip().startswith('#')]),
"sm" : True if gen_request["sema"] == 1 else False,
"sm_dyn" : True if gen_request["sema_dyn"] == 1 else False,
"decrisper": False,
"controlnet_strength": 1.0,
"add_original_image": False,
"cfg_rescale": gen_request["cfg_rescale"],
"noise_schedule": "native"
}
if "image" not in gen_request and "reference_image" in gen_request:
params["reference_image"] = gen_request["reference_image"]
params["reference_information_extracted"] = gen_request["reference_information_extracted"]
params["reference_strength"] = gen_request["reference_strength"]
if gen_request["type"]=="inpaint":
if "mask" in gen_request:
params["mask"] = gen_request["mask"]
params['add_original_image'] = gen_request['add_original_image']
positive = gen_request["prompt"]
positive = re.sub(r'#.*?(\n|,|$)', '', positive)
keywords = [key.strip() for key in positive.split(',')]
if "cond_negative" in gen_request and gen_request["cond_negative"]:
user_input = gen_request["cond_negative"]
rating = gen_request["rating"]
params["negative_prompt"] = parse_and_execute_commands(positive, params["negative_prompt"], user_input, rating)
if "repeat" in gen_request:
max = gen_request["repeat_max"]
for i, key in enumerate(keywords):
if "->" in key:
instant_keyword = [k for k in key.split('->')]
if len(instant_keyword) > gen_request["repeat"]:
current_key = instant_keyword[gen_request["repeat"]]
else:
current_key = instant_keyword[gen_request["repeat"] % len(instant_keyword)]
keywords[i] = current_key
filename_rule = gen_request["png_rule"]
save_folder = gen_request["save_folder"]
access_token = gen_request["access_token"]
additional_folder = ""
request_type = "generate"
if "image" in gen_request:
params["image"] = gen_request["image"]
if "strength" in gen_request:
params["strength"] = gen_request["strength"]
params["noise"] = gen_request["noise"]
params["sm"] = False
params["sm_dyn"] = False
request_type = "img2img" if "mask" not in gen_request else "infill"
temp_del = []
for key in keywords:
if key.startswith('*'):
temp_del.append(key)
for key in temp_del:
if key in keywords:
keywords.remove(key)
positive = ', '.join(keywords)
def resize_and_fill(image, max_size=None):
if max_size is None:
max_size = gen_request["user_screen_size"]
original_width, original_height = image.size
if original_width > max_size or original_height > max_size:
# ๋น์จ์ ์ ์งํ๋ฉด์ ํฌ๊ธฐ ์กฐ์
image.thumbnail((max_size, max_size))
# ์ ์ด๋ฏธ์ง ํฌ๊ธฐ ๊ณ์ฐ
width, height = image.size
new_image = Image.new("RGB", (max_size, max_size), "black")
new_image.paste(image, ((max_size - width) // 2, (max_size - height) // 2))
return new_image
else:
return image
def log_error(e, output_file_path="output_file_path"):
# ํ์ฌ ์๊ฐ์ ์ป์ต๋๋ค
current_time = datetime.now().strftime("%m/%d %H:%M:%S")
# ์๋ฌ ๋ก๊ทธ ๋ฉ์์ง
error_message = f"#### Error occured at {current_time} ####\nError: {e}\n############################################\n"
# ์ง์ ๋ ์ถ๋ ฅ ํด๋์ error_log.txt ํ์ผ์ ์ฐ๊ธฐ
with open(f"error_log.txt", "a") as file:
file.write(error_message)
try:
zipped_bytes = generate_image(access_token, positive, "nai-diffusion-3" if "mask" not in params else "nai-diffusion-3-inpainting", request_type, params)
if gen_request["png_rule"] == "count":
additional_folder = "/" + gen_request["start_time"]
if gen_request["type"] == "turbo":
additional_folder += "/turbo"
d = Path(save_folder + additional_folder)
d.mkdir(parents=True, exist_ok=True)
zipped = zipfile.ZipFile(io.BytesIO(zipped_bytes))
image_bytes = zipped.read(zipped.infolist()[0])
if gen_request["png_rule"] == "count":
_count = gen_request["count"]
filename = (d / f"{_count:05}.png" )
else: filename = (d / f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.png" )
filename.write_bytes(image_bytes)
i = Image.open(io.BytesIO(image_bytes))
i = ImageOps.exif_transpose(i).convert("RGB")
i_resized = resize_and_fill(i)
#tk_image = ImageTk.PhotoImage(i_resized)
return i_resized, positive, params['seed'], i.info, str(filename)
except Exception as e:
try:
if zipped_bytes is None:
raise ValueError("Connection broken (Protocol Error)")
error_message = zipped_bytes.decode('utf-8')[2:-2]
except Exception as inner_exception:
error_message = str(inner_exception)
log_error(error_message, "path_to_output_folder")
return None, error_message, params['seed'], None, None |