simplify file wildcard matching

Signed-off-by: Vladimir Mandic <mandic00@live.com>
This commit is contained in:
Vladimir Mandic
2025-01-31 18:31:00 -05:00
parent e28b8cd920
commit 7d2d4ffe9a
3 changed files with 32 additions and 22 deletions
+1
View File
@@ -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
+30 -21
View File
@@ -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
+1 -1
Submodule wiki updated: feb33b2e79...1318ce640a