diff --git a/.gitignore b/.gitignore index bcd008c..033a576 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -model-keyword-user.txt \ No newline at end of file +custom-mappings.txt +custom-mappings-backup.txt diff --git a/README.md b/README.md index 91023f5..b49799f 100644 --- a/README.md +++ b/README.md @@ -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. * 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. -* 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. * 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. diff --git a/model-keyword-user.txt b/model-keyword-user.txt index 34fb901..abdf16e 100644 --- a/model-keyword-user.txt +++ b/model-keyword-user.txt @@ -1,12 +1,3 @@ -# csv file for adding custom model_hash to keyword mapping -# line starting with # is ignored. -# -# 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 +# This file is no long used. +# User settings are now saved in custom-mappings.txt +# Instead of manually editing, use the UI to add custom mappings. diff --git a/scripts/model_keyword.py b/scripts/model_keyword.py index 887e3f3..81dcab0 100644 --- a/scripts/model_keyword.py +++ b/scripts/model_keyword.py @@ -53,29 +53,33 @@ class Script(scripts.Script): insert_line = f'{model_hash}, {txt}, {model_ckpt}' global scripts_dir - user_file = f'{scripts_dir}/model-keyword-user.txt' - user_backup_file = f'{scripts_dir}/model-keyword-user-backup.txt' + user_file = f'{scripts_dir}/custom-mappings.txt' + user_backup_file = f'{scripts_dir}/custom-mappings-backup.txt' lines = [] - with open(user_file, newline='') as csvfile: - csvreader = csv.reader(csvfile) - for row in csvreader: - try: - mhash = row[0] - if mhash.startswith('#'): + if os.path.exists(user_file): + with open(user_file, newline='') as csvfile: + csvreader = csv.reader(csvfile) + for row in csvreader: + try: + 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)) - 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)) - except: - pass + except: + pass lines.append(insert_line) csvtxt = '\n'.join(lines) + '\n' 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) return 'added: ' + insert_line @@ -83,18 +87,17 @@ class Script(scripts.Script): with gr.Group(): with gr.Accordion('Model Keyword', open=False): is_enabled = gr.Checkbox(label='Model Keyword Enabled', value=True) - info = gr.HTML("
You can edit extensions/model-keyword/model-keyword-user.txt to add custom mappings
") keyword_placement = gr.Dropdown(choices=["keyword prompt", "prompt keyword", "keyword, prompt", "prompt, keyword"], value='keyword prompt', label='Keyword placement:') multiple_keywords = gr.Dropdown(choices=["keyword1, keyword2", "random", "iterate", "keyword1", "keyword2"], - value='keyword1, keyword2', - label='Multiple keywords:') + value='keyword1, keyword2', + label='Multiple keywords:') with gr.Accordion('Add Custom Mappings', open=False): - info = gr.HTML("Add custom keyword(trigger word) mapping for current model. Custom mappings are saved to extensions/model-keyword/model-keyword-user.txt
") + info = gr.HTML("Add custom keyword(trigger word) mapping for current model. Custom mappings are saved to extensions/model-keyword/custom-mappings.txt
") text_input = gr.Textbox(placeholder="Keyword or keywords separated by |", label="Keyword(trigger word)") add_custom_mappings = gr.Button(value='Set Keyword for Model') 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) - return [is_enabled, info, keyword_placement, multiple_keywords] + return [is_enabled, keyword_placement, multiple_keywords] def load_hash_dict(self): global hash_dict, hash_dict_modified, scripts_dir 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) if hash_dict is None or hash_dict_modified != modified: hash_dict = defaultdict(list) def parse_file(path): - with open(path, newline='') as csvfile: - csvreader = csv.reader(csvfile) - for row in csvreader: - try: - mhash = row[0].strip(' ') - kw = row[1].strip(' ') - if mhash.startswith('#'): - continue - ckptname = 'default' if len(row)<=2 else row[2].strip(' ') - hash_dict[mhash].append((kw, ckptname)) - except: - pass + if os.path.exists(path): + with open(path, newline='') as csvfile: + csvreader = csv.reader(csvfile) + for row in csvreader: + try: + mhash = row[0].strip(' ') + kw = row[1].strip(' ') + if mhash.startswith('#'): + continue + ckptname = 'default' if len(row)<=2 else row[2].strip(' ') + hash_dict[mhash].append((kw, ckptname)) + except: + pass parse_file(default_file) parse_file(user_file) @@ -134,7 +138,7 @@ class Script(scripts.Script): 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: global hash_dict