From 7d2d4ffe9a0f1a8369120e86c995fe2ba8365e60 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Fri, 31 Jan 2025 18:31:00 -0500 Subject: [PATCH] simplify file wildcard matching Signed-off-by: Vladimir Mandic --- CHANGELOG.md | 1 + modules/styles.py | 51 ++++++++++++++++++++++++++++------------------- wiki | 2 +- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c366d12de..68c45cb77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - ipex device wrapper with adetailer - openvino error handling - relax python version checks for rocm + - simplify and improve file wildcard matching ## Update for 2025-01-29 diff --git a/modules/styles.py b/modules/styles.py index de395c705..bfbe7470e 100644 --- a/modules/styles.py +++ b/modules/styles.py @@ -43,37 +43,46 @@ def apply_styles_to_prompt(prompt, styles): def apply_file_wildcards(prompt, replaced = [], not_found = [], recursion=0, seed=-1): - def check_files(prompt, wildcard, files): + def check_wildcard_files(prompt, wildcard, files, file_only=True): + trimmed = wildcard.replace('\\', '').replace('/', '').strip().lower() for file in files: - if wildcard == os.path.splitext(os.path.basename(file))[0] if os.path.sep not in wildcard else wildcard in file: - with open(file, 'r', encoding='utf-8') as f: - lines = f.readlines() - if len(lines) > 0: - choice = random.choice(lines).strip(' \n') - if '|' in choice: - choice = random.choice(choice.split('|')).strip(' []{}\n') - prompt = prompt.replace(f"__{wildcard}__", choice, 1) - shared.log.debug(f'Wildcards apply: wildcard="{wildcard}" choice="{choice}" file="{file}" choices={len(lines)}') - replaced.append(wildcard) - return prompt, True - return prompt, False + if file_only: + paths = [os.path.splitext(os.path.basename(file).lower())[0]] + else: + paths = [os.path.splitext(p.lower())[0] for p in os.path.normpath(file).split(os.path.sep)] + if trimmed in paths: + try: + with open(file, 'r', encoding='utf-8') as f: + lines = f.readlines() + if len(lines) > 0: + choice = random.choice(lines).strip(' \n') + if '|' in choice: + choice = random.choice(choice.split('|')).strip(' []{}\n') + prompt = prompt.replace(f"__{wildcard}__", choice, 1) + shared.log.debug(f'Wildcards apply: wildcard="{wildcard}" choice="{choice}" file="{file}" choices={len(lines)}') + replaced.append(wildcard) + return prompt, True + except Exception as e: + shared.log.error(f'Wildcards: wildcard={wildcard} file={file} {e}') + if not file_only: + return prompt, False + return check_wildcard_files(prompt, wildcard, files, file_only=False) recursion += 1 if not shared.opts.wildcards_enabled or recursion >= 10: return prompt, replaced, not_found matches = re.findall(r'__(.*?)__', prompt, re.DOTALL) matches = [m for m in matches if m not in not_found] - matches = [m.replace('\\', os.path.sep) for m in matches if m not in replaced] - matches = [m.replace('/', os.path.sep) for m in matches if m not in replaced] + matches = [m for m in matches if m not in replaced] if len(matches) == 0: return prompt, replaced, not_found files = list(files_cache.list_files(shared.opts.wildcards_dir, ext_filter=[".txt"], recursive=True)) - for m in matches: - prompt, found = check_files(prompt, m, files) - if found and m in not_found: - not_found.remove(m) - elif not found and m not in not_found: - not_found.append(m) + for wildcard in matches: + prompt, found = check_wildcard_files(prompt, wildcard, files) + if found and wildcard in not_found: + not_found.remove(wildcard) + elif not found and wildcard not in not_found: + not_found.append(wildcard) prompt, replaced, not_found = apply_file_wildcards(prompt, replaced, not_found, recursion, seed) # recursive until we get early return return prompt, replaced, not_found diff --git a/wiki b/wiki index feb33b2e7..1318ce640 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit feb33b2e79a4ca11d3c1fac373afec4673b9377b +Subproject commit 1318ce640a5c04053d13022233cb3ae55c40cef9