Skip to content

Commit

Permalink
simplify file wildcard matching
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Mandic <[email protected]>
  • Loading branch information
vladmandic committed Jan 31, 2025
1 parent e28b8cd commit 7d2d4ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 30 additions & 21 deletions modules/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion wiki
Submodule wiki updated from feb33b to 1318ce

0 comments on commit 7d2d4ff

Please sign in to comment.