PoemForSmallFThings / NAIA_random_function_core.py
baqu2213's picture
Upload 3 files
9e9197e verified
raw
history blame
14.1 kB
import pandas as pd
import numpy as np
import re
def parse_and_execute_commands(processed, user_input, fix, after, popped_row):
commands = [cmd.strip() for cmd in user_input.split(',') if not cmd.strip().startswith('#')]
for command in commands:
condition, cmd = parse_conditional_command(command)
nfix = [item.translate(str.maketrans('', '', '{}[]')) for item in fix if not item.startswith('#')]
if check_condition(processed+nfix, condition, popped_row):
processed = execute_command(processed, cmd, fix, after)
return processed
def parse_conditional_command(command):
match = re.match(r"\((.*?)\)\:(.*)", command)
if match:
return match.groups()
return '', command
def check_condition(processed, condition, popped_row):
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(processed, cond, popped_row) for cond in sub_cond.split('&')))
elif '|' in sub_cond:
results.append(any(check_condition(processed, cond, popped_row) for cond in sub_cond.split('|')))
else:
if sub_cond in ['e', 'q', 's', 'g']:
results.append(sub_cond == popped_row['rating'])
elif sub_cond in ['~e', '~q', '~s', '~g']:
results.append(sub_cond != popped_row['rating'])
# PM
elif sub_cond.startswith('*'):
results.append(sub_cond[1:] in processed)
# NOT IN
elif sub_cond.startswith('~!'):
results.append(sub_cond[2:] not in processed)
elif sub_cond.startswith('~'):
results.append(any(sub_cond[1:] not in element for element in processed))
# CONTAIN
else:
results.append(any(sub_cond in element for element in processed))
return all(results)
def execute_command(processed, command, fix, after):
if '+=' in command:
keyword, addition = command.split('+=', 1)
addition = addition.replace('^', ', ')
#addiction split ํ•ด์„œ ํ”„๋กฌํ”„ํŠธ ์กด์žฌํ•˜๋Š”์ง€ ์ฒดํฌ ํ•„์š”
return insert_text_after_keyword(processed, keyword, addition, fix, after)
elif '=' in command:
keyword, replacement = command.split('=', 1)
if keyword in processed:
replacement = replacement.replace('^', ', ')
#replacement split ํ•ด์„œ ํ”„๋กฌํ”„ํŠธ ์กด์žฌํ•˜๋Š”์ง€ ์ฒดํฌ ํ•„์š”
index = processed.index(keyword)
processed[index] = replacement
return processed
def insert_text_after_keyword(processed, user_keyword, user_additional_keyword, fix, after):
if user_keyword == "prompt":
processed.append(user_additional_keyword)
elif user_keyword == "prefix":
fix.append(user_additional_keyword)
elif user_keyword == "postfix":
after.append(user_additional_keyword)
elif user_keyword in processed:
index = processed.index(user_keyword) + 1
processed.insert(index, user_additional_keyword)
return processed
def find_keyword_index(general):
# boys์™€ girls ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ๋ฐ ์ธ๋ฑ์Šค ์ €์žฅ
boys = ["1boy", "2boys", "3boys", "4boys", "5boys", "6+boys"]
girls = ["1girl", "2girls", "3girls", "4girls", "5girls", "6+girls"]
#others = ["1other", "2others", "3others", "4others", "5others", "6+others"]
boys_indices = [i for i, item in enumerate(general[:6]) if item in boys]
girls_indices = [i for i, item in enumerate(general[:6]) if item in girls]
# case 1๊ณผ case 2: girls ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ ์ฐพ๊ธฐ
if girls_indices:
return girls_indices[0]+1
# case 3: boys ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ ์ฐพ๊ธฐ
if boys_indices:
return boys_indices[0]+1
# case 4: ํ•ด๋‹น ์‚ฌํ•ญ ์—†์Œ
# 2์›” 12์ผ์— ํ•ด๋‹น ๋ถ€๋ถ„ return 2 -> return 0 ์œผ๋กœ ์ˆ˜์ •ํ•˜์˜€์œผ๋ฉฐ ์ด ๋ถ€๋ถ„ ํŠธ๋ž˜ํ‚น ํ•„์š”
return 0
def process_list(temp_fixed):
i = 0
while i < len(temp_fixed):
if ', ' in temp_fixed[i]:
# ํ•ด๋‹น ์›์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์Œ
processed_items = process_fix_prompt(temp_fixed[i])
# ์›๋ž˜ ์›์†Œ๋ฅผ ์ œ๊ฑฐ
temp_fixed.pop(i)
# ์ฒ˜๋ฆฌ๋œ ์›์†Œ๋“ค์„ ํ˜„์žฌ ์ธ๋ฑ์Šค์— ์‚ฝ์ž…
for item in reversed(processed_items):
temp_fixed.insert(i, item)
# ์‚ฝ์ž…๋œ ์›์†Œ๋“ค์„ ๊ฑด๋„ˆ๋›ฐ๊ธฐ ์œ„ํ•ด ์ธ๋ฑ์Šค ์กฐ์ •
i += len(processed_items)
else:
i += 1
return temp_fixed
def process_fix_prompt(fix_prompt):
fix = []
lt_count = 0 # '<' ๋ฌธ์ž ๊ฐœ์ˆ˜ ์ถ”์ 
current_item = "" # ํ˜„์žฌ ํ•ญ๋ชฉ์„ ๊ฒฐํ•ฉํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
# fix_prompt๋ฅผ ์‰ผํ‘œ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ ํ•ญ๋ชฉ์„ ์ˆœํšŒ
for item in fix_prompt.split(','):
item = item.strip() # ๊ณต๋ฐฑ ์ œ๊ฑฐ
if '<' in item:
lt_count += item.count('<') # '<' ๋ฐœ๊ฒฌ ์‹œ ์นด์šดํŠธ ์ฆ๊ฐ€
lt_count -= item.count('>')
if current_item: # ์ด๋ฏธ current_item์— ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์‰ผํ‘œ์™€ ๊ณต๋ฐฑ ์ถ”๊ฐ€
current_item += ", "
if lt_count <= 0: # lt_count๊ฐ€ 0์ด๋ฉด ์ผ๋ฐ˜ ํ•ญ๋ชฉ์œผ๋กœ ์ฒ˜๋ฆฌ
fix.append(item)
else:
current_item += item # ํ˜„์žฌ ํ•ญ๋ชฉ์— ์ถ”๊ฐ€
elif '>' in item:
lt_count -= item.count('>') # '>' ๋ฐœ๊ฒฌ ์‹œ ์นด์šดํŠธ ๊ฐ์†Œ
current_item += ", " + item # '>'๊ฐ€ ์žˆ๋Š” ํ•ญ๋ชฉ๋„ ํ˜„์žฌ ํ•ญ๋ชฉ์— ์ถ”๊ฐ€
if lt_count == 0: # '<'์™€ '>'์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งค์นญ๋˜๋ฉด fix์— ์ถ”๊ฐ€ํ•˜๊ณ  current_item ์ดˆ๊ธฐํ™”
fix.append(current_item)
current_item = ""
else:
if lt_count <= 0: # lt_count๊ฐ€ 0์ด๋ฉด ์ผ๋ฐ˜ ํ•ญ๋ชฉ์œผ๋กœ ์ฒ˜๋ฆฌ
fix.append(item)
else: # lt_count๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด ํ˜„์žฌ ํ•ญ๋ชฉ์— ๊ณ„์†ํ•ด์„œ ์ถ”๊ฐ€
current_item += ", " + item
# ๋งˆ์ง€๋ง‰์— ๋‚จ์€ current_item ์ฒ˜๋ฆฌ
if current_item:
fix.append(current_item)
return fix
def RFP(popped_row, fix_prompt, after_prompt, auto_hide_prompt, rm_a, rm_s, rm_c, rm_loc, rm_color, nsfw, data, magic_word):
boys = ["1boy", "2boys", "3boys", "4boys", "5boys", "6+boys"]
girls = ["1girl", "2girls", "3girls", "4girls", "5girls", "6+girls"]
general = [item.strip() for item in popped_row['general'].split(',')]
special_word_check = fix_prompt+', '+after_prompt
special_word_check = [item.strip() for item in special_word_check.split(',')]
special_word_check = [item for item in special_word_check if item.startswith('*')]
colors = ['black','white','blond','silver','gray','yellow','blue','purple','red','pink','brown','orange','green','aqua','gradient']
exc = []
for keyword in general:
if keyword == '!' or keyword == '!?' or keyword == '!!':
exc.append(keyword)
if exc:
for keyword in exc:
general.remove(keyword)
general[3:3] = exc
if nsfw == 1:
nsfw_word = []
for keyword in general:
if keyword in data.qe_word or keyword in data.bag_of_tags or "horns" in keyword or "(" in keyword or keyword in boys or keyword in girls:
nsfw_word.append(keyword)
nsfw_word = list(set(nsfw_word))
nsfw_word.sort()
general = nsfw_word
if rm_loc == 1:
temp_general = []
locations = ['airplane interior', 'airport', 'apartment', 'arena', 'armory', 'bar', 'barn', 'bathroom', 'bathtub', 'bedroom', 'bell tower', 'billiard room', 'book store', 'bowling alley', 'bunker', 'bus interior', 'butcher shop', 'cafe', 'cafeteria', 'car interior', 'casino', 'castle', 'catacomb', 'changing room', 'church', 'classroom', 'closet', 'construction site', 'convenience store', 'convention hall', 'court', 'dining room', 'drugstore', 'ferris wheel', 'flower shop', 'gym', 'hangar', 'hospital', 'hotel room', 'hotel', 'infirmary', 'izakaya', 'kitchen', 'laboratory', 'library', 'living room', 'locker room', 'mall', 'messy room', 'mosque', 'movie theater', 'museum', 'nightclub', 'office', 'onsen', 'ovservatory', 'phone booth', 'planetarium', 'pool', 'prison', 'refinery', 'restaurant', 'restroom', 'rural', 'salon', 'school', 'sex shop', 'shop', 'shower room', 'skating rink', 'snowboard shop', 'spacecraft interior', 'staff room', 'stage', 'supermarket', 'throne', 'train station', 'tunnel', 'airfield', 'alley', 'amphitheater', 'aqueduct', 'bamboo forest', 'beach', 'blizzard', 'bridge', 'bus stop', 'canal', 'canyon', 'carousel', 'cave', 'cliff', 'cockpit', 'conservatory', 'cross walk', 'desert', 'dust storm', 'flower field', 'forest', 'garden', 'gas staion', 'gazebo', 'geyser', 'glacier', 'graveyard', 'harbor', 'highway', 'hill', 'island', 'jungle', 'lake', 'market', 'meadow', 'nuclear powerplant', 'oasis', 'ocean bottom', 'ocean', 'pagoda', 'parking lot', 'playground', 'pond', 'poolside', 'railroad', 'rainforest', 'rice paddy', 'roller coster', 'rooftop', 'rope bridge', 'running track', 'savannah', 'shipyard', 'shirine', 'skyscraper', 'soccor field', 'space elevator', 'stair', 'starry sky', 'swamp', 'tidal flat', 'volcano', 'waterfall', 'waterpark', 'wheat field', 'zoo', 'white background', 'simple background', 'grey background', 'gradient background', 'blue background', 'black background', 'yellow background', 'pink background', 'red background', 'brown background', 'green background', 'purple background', 'orange background']
for keyword in general:
if keyword in locations:
temp_general.append(keyword)
for keyword in temp_general:
general.remove(keyword)
if rm_color == 1:
temp_general = []
for keyword in general:
if any(color in keyword for color in colors):
temp_general.append(keyword)
for keyword in temp_general:
general.remove(keyword)
if rm_c == 1:
temp_general = []
for keyword in general:
if keyword in data.bag_of_tags:
temp_general.append(keyword)
for keyword in temp_general:
general.remove(keyword)
#NAIA_random_function_core.process_fix_prompt()
fix = process_fix_prompt(fix_prompt)
at_first = []
for fp in fix:
if fp.startswith('*'):
at_first.append(fp[1:])
if at_first:
for af in at_first:
if '*'+af in fix:
fix.remove('*'+af)
if rm_a == 0:
if popped_row['artist']:
artists = [item.strip() for item in popped_row['artist'].split(',')]
artist = ["artist:" + _artist for _artist in artists]
fix = fix + artist
if rm_s == 0:
if popped_row['copyright']:
series = [item.strip() for item in popped_row['copyright'].split(',')]
else:
series = []
fix = fix + series
after = [item.strip() for item in after_prompt.split(',')]
auto_hide = [item.strip() for item in auto_hide_prompt.split(',') if not item.strip().startswith('#')] + ["| |", ":|", "\||/", "<|> <|>", "|| ||", ";|"]
fix_index = find_keyword_index(general)
processed = general.copy()
temp_hide_prompt = []
for keyword in processed:
if keyword in auto_hide:
temp_hide_prompt.append(keyword)
for keyword in temp_hide_prompt:
processed.remove(keyword)
#์—ฌ๊ธฐ์—์„œ ๋ฌธ์ œ์˜ magic word ์ฒ˜๋ฆฌ
if "cond" in magic_word and magic_word["cond"]:
user_input = magic_word["cond"]
processed = parse_and_execute_commands(processed, user_input, fix, after, popped_row)
processed[fix_index:fix_index] = fix
processed += after
if rm_c == 0 and '*(remove character name)' not in special_word_check and '*(set character name after prefix)' not in special_word_check and '*(set character name before prefix)' not in special_word_check:
if popped_row['character']:
character = [item.strip() for item in popped_row['character'].split(',')]
processed[fix_index:fix_index] = character
fix_index+=len(character)
elif rm_c == 0 and '*(set character name after prefix)' in special_word_check:
if popped_row['character']:
character = [item.strip() for item in popped_row['character'].split(',')]
processed[fix_index+len(fix):fix_index+len(fix)] = character
#fix_index+=len(character)
elif rm_c == 0 and '*(set character name before prefix)' in special_word_check:
if popped_row['character']:
character = [item.strip() for item in popped_row['character'].split(',')]
processed[fix_index+1:fix_index+1] = character
fix_index+=len(character)
if magic_word["random_artist"] == True:
processed.insert(fix_index, magic_word["random_artist_name"])
boy_in_processed = girl_in_processed = None
for boy in boys:
if boy in processed:
boy_in_processed = boy
break
for girl in girls:
if girl in processed:
girl_in_processed = girl
break
if boy_in_processed and girl_in_processed:
boy_index = processed.index(boy_in_processed)
girl_index = processed.index(girl_in_processed)
if boy_index > girl_index:
processed.pop(boy_index)
processed.insert(girl_index, boy_in_processed)
s_word = ['*(remove character name)', '*(set character name after prefix)', '*(set character name before prefix)']
for keyword in s_word:
if keyword in processed:
processed.remove(keyword)
if at_first:
processed = at_first + processed
for i, key in enumerate(processed):
if key.startswith('#'): processed[i] = '\n\n'+key+'\n'
return ', '.join(processed), popped_row['rating']