use custom-mappings.txt instead of model-keyword-user.txt (updating through webui overwrote model-keyword-user.txt)

pull/33/head
ChunKoo Park 2023-01-17 15:52:41 +09:00
parent b77c198d61
commit 3f9c9c0fe5
4 changed files with 47 additions and 51 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
model-keyword-user.txt custom-mappings.txt
custom-mappings-backup.txt

View File

@ -43,9 +43,9 @@ When generating image, the extension will try to figure out which model is used
* Click "Set Keyword for Model" without filling 'Keyword' field -> outputs model name and model_hash in result. * Click "Set Keyword for Model" without filling 'Keyword' field -> outputs model name and model_hash in result.
* Fill keyword(trigger word) or keywords separated by pipe character |. * Fill keyword(trigger word) or keywords separated by pipe character |.
* Click "Set Keyword for Model" to save mapping. Mappings are saved in model-keyword-user.txt * Click "Set Keyword for Model" to save mapping. Mappings are saved in custom-mappings.txt
* If previous mapping is found, it overwrites the mapping. * If previous mapping is found, it overwrites the mapping.
* To delete an entry, edit model-keyword-user.txt in extensions/model-keyword. * To delete an entry, edit custom-mappings.txt in extensions/model-keyword.
* do NOT edit model-keyword.txt . It can be overwritten or cause conflict while upgrading. * do NOT edit model-keyword.txt . It can be overwritten or cause conflict while upgrading.
* hash value for model has been changed in webui(2023-01-14), this extension uses old hash value. Old hash value is no longer displayed in webui. * hash value for model has been changed in webui(2023-01-14), this extension uses old hash value. Old hash value is no longer displayed in webui.

View File

@ -1,12 +1,3 @@
# csv file for adding custom model_hash to keyword mapping # This file is no long used.
# line starting with # is ignored. # User settings are now saved in custom-mappings.txt
# # Instead of manually editing, use the UI to add custom mappings.
# model_hash, keyword
#41fef4bd, nousr robot
#
# model provides multiple keywords
#4d5cca44, TinyPlanet|TinyCityPlanet
#
# in case there is a hash collision. it will use the closest matching filename.ckpt
# model_hash, keyword, filename.ckpt
#a2a802b2, SKSKS app icon, App_Icons_V1_PublicPrompts.ckpt

View File

@ -53,29 +53,33 @@ class Script(scripts.Script):
insert_line = f'{model_hash}, {txt}, {model_ckpt}' insert_line = f'{model_hash}, {txt}, {model_ckpt}'
global scripts_dir global scripts_dir
user_file = f'{scripts_dir}/model-keyword-user.txt' user_file = f'{scripts_dir}/custom-mappings.txt'
user_backup_file = f'{scripts_dir}/model-keyword-user-backup.txt' user_backup_file = f'{scripts_dir}/custom-mappings-backup.txt'
lines = [] lines = []
with open(user_file, newline='') as csvfile: if os.path.exists(user_file):
csvreader = csv.reader(csvfile) with open(user_file, newline='') as csvfile:
for row in csvreader: csvreader = csv.reader(csvfile)
try: for row in csvreader:
mhash = row[0] try:
if mhash.startswith('#'): mhash = row[0]
if mhash.startswith('#'):
lines.append(','.join(row))
continue
# kw = row[1]
ckptname = None if len(row)<=2 else row[2].strip(' ')
if mhash==model_hash and ckptname==model_ckpt:
continue
lines.append(','.join(row)) lines.append(','.join(row))
continue except:
# kw = row[1] pass
ckptname = None if len(row)<=2 else row[2].strip(' ')
if mhash==model_hash and ckptname==model_ckpt:
continue
lines.append(','.join(row))
except:
pass
lines.append(insert_line) lines.append(insert_line)
csvtxt = '\n'.join(lines) + '\n' csvtxt = '\n'.join(lines) + '\n'
import shutil import shutil
shutil.copy(user_file, user_backup_file) try:
shutil.copy(user_file, user_backup_file)
except:
pass
open(user_file, 'w').write(csvtxt) open(user_file, 'w').write(csvtxt)
return 'added: ' + insert_line return 'added: ' + insert_line
@ -83,18 +87,17 @@ class Script(scripts.Script):
with gr.Group(): with gr.Group():
with gr.Accordion('Model Keyword', open=False): with gr.Accordion('Model Keyword', open=False):
is_enabled = gr.Checkbox(label='Model Keyword Enabled', value=True) is_enabled = gr.Checkbox(label='Model Keyword Enabled', value=True)
info = gr.HTML("<p style=\"margin-bottom:0.75em\">You can edit extensions/model-keyword/model-keyword-user.txt to add custom mappings</p>")
keyword_placement = gr.Dropdown(choices=["keyword prompt", "prompt keyword", "keyword, prompt", "prompt, keyword"], keyword_placement = gr.Dropdown(choices=["keyword prompt", "prompt keyword", "keyword, prompt", "prompt, keyword"],
value='keyword prompt', value='keyword prompt',
label='Keyword placement:') label='Keyword placement:')
multiple_keywords = gr.Dropdown(choices=["keyword1, keyword2", "random", "iterate", "keyword1", "keyword2"], multiple_keywords = gr.Dropdown(choices=["keyword1, keyword2", "random", "iterate", "keyword1", "keyword2"],
value='keyword1, keyword2', value='keyword1, keyword2',
label='Multiple keywords:') label='Multiple keywords:')
with gr.Accordion('Add Custom Mappings', open=False): with gr.Accordion('Add Custom Mappings', open=False):
info = gr.HTML("<p style=\"margin-bottom:0.75em\">Add custom keyword(trigger word) mapping for current model. Custom mappings are saved to extensions/model-keyword/model-keyword-user.txt</p>") info = gr.HTML("<p style=\"margin-bottom:0.75em\">Add custom keyword(trigger word) mapping for current model. Custom mappings are saved to extensions/model-keyword/custom-mappings.txt</p>")
text_input = gr.Textbox(placeholder="Keyword or keywords separated by |", label="Keyword(trigger word)") text_input = gr.Textbox(placeholder="Keyword or keywords separated by |", label="Keyword(trigger word)")
add_custom_mappings = gr.Button(value='Set Keyword for Model') add_custom_mappings = gr.Button(value='Set Keyword for Model')
text_output = gr.Textbox(interactive=False, label='result') text_output = gr.Textbox(interactive=False, label='result')
@ -102,30 +105,31 @@ class Script(scripts.Script):
add_custom_mappings.click(add_custom, inputs=text_input, outputs=text_output) add_custom_mappings.click(add_custom, inputs=text_input, outputs=text_output)
return [is_enabled, info, keyword_placement, multiple_keywords] return [is_enabled, keyword_placement, multiple_keywords]
def load_hash_dict(self): def load_hash_dict(self):
global hash_dict, hash_dict_modified, scripts_dir global hash_dict, hash_dict_modified, scripts_dir
default_file = f'{scripts_dir}/model-keyword.txt' default_file = f'{scripts_dir}/model-keyword.txt'
user_file = f'{scripts_dir}/model-keyword-user.txt' user_file = f'{scripts_dir}/custom-mappings.txt'
modified = str(os.stat(default_file).st_mtime) + '_' + str(os.stat(user_file).st_mtime) modified = str(os.stat(default_file).st_mtime) + '_' + str(os.stat(user_file).st_mtime)
if hash_dict is None or hash_dict_modified != modified: if hash_dict is None or hash_dict_modified != modified:
hash_dict = defaultdict(list) hash_dict = defaultdict(list)
def parse_file(path): def parse_file(path):
with open(path, newline='') as csvfile: if os.path.exists(path):
csvreader = csv.reader(csvfile) with open(path, newline='') as csvfile:
for row in csvreader: csvreader = csv.reader(csvfile)
try: for row in csvreader:
mhash = row[0].strip(' ') try:
kw = row[1].strip(' ') mhash = row[0].strip(' ')
if mhash.startswith('#'): kw = row[1].strip(' ')
continue if mhash.startswith('#'):
ckptname = 'default' if len(row)<=2 else row[2].strip(' ') continue
hash_dict[mhash].append((kw, ckptname)) ckptname = 'default' if len(row)<=2 else row[2].strip(' ')
except: hash_dict[mhash].append((kw, ckptname))
pass except:
pass
parse_file(default_file) parse_file(default_file)
parse_file(user_file) parse_file(user_file)
@ -134,7 +138,7 @@ class Script(scripts.Script):
return hash_dict return hash_dict
def process(self, p, is_enabled, _, keyword_placement, multiple_keywords): def process(self, p, is_enabled, keyword_placement, multiple_keywords):
if not is_enabled: if not is_enabled:
global hash_dict global hash_dict