2023-05-05 22:14:56 -04:00
|
|
|
import os
|
|
|
|
import re
|
2023-06-13 19:34:35 -04:00
|
|
|
from datetime import datetime
|
2023-05-05 22:14:56 -04:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-09-25 22:48:30 -04:00
|
|
|
from modules import github, shared
|
2023-06-11 11:19:18 -04:00
|
|
|
from modules.logging_colors import logger
|
|
|
|
|
|
|
|
|
2023-07-03 23:03:30 -04:00
|
|
|
# Helper function to get multiple values from shared.gradio
|
|
|
|
def gradio(*keys):
|
2023-08-13 00:12:15 -04:00
|
|
|
if len(keys) == 1 and type(keys[0]) in [list, tuple]:
|
2023-07-03 23:03:30 -04:00
|
|
|
keys = keys[0]
|
|
|
|
|
|
|
|
return [shared.gradio[k] for k in keys]
|
|
|
|
|
|
|
|
|
2023-06-11 11:19:18 -04:00
|
|
|
def save_file(fname, contents):
|
|
|
|
if fname == '':
|
|
|
|
logger.error('File name is empty!')
|
|
|
|
return
|
|
|
|
|
|
|
|
root_folder = Path(__file__).resolve().parent.parent
|
2023-12-13 16:08:21 -05:00
|
|
|
abs_path_str = os.path.abspath(fname)
|
|
|
|
rel_path_str = os.path.relpath(abs_path_str, root_folder)
|
|
|
|
rel_path = Path(rel_path_str)
|
2023-06-11 11:19:18 -04:00
|
|
|
if rel_path.parts[0] == '..':
|
|
|
|
logger.error(f'Invalid file path: {fname}')
|
|
|
|
return
|
|
|
|
|
2023-12-13 16:08:21 -05:00
|
|
|
with open(abs_path_str, 'w', encoding='utf-8') as f:
|
2023-06-11 11:19:18 -04:00
|
|
|
f.write(contents)
|
|
|
|
|
2023-12-13 16:08:21 -05:00
|
|
|
logger.info(f'Saved {abs_path_str}.')
|
2023-06-11 11:19:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
def delete_file(fname):
|
|
|
|
if fname == '':
|
|
|
|
logger.error('File name is empty!')
|
|
|
|
return
|
|
|
|
|
|
|
|
root_folder = Path(__file__).resolve().parent.parent
|
2023-12-13 16:08:21 -05:00
|
|
|
abs_path_str = os.path.abspath(fname)
|
|
|
|
rel_path_str = os.path.relpath(abs_path_str, root_folder)
|
|
|
|
rel_path = Path(rel_path_str)
|
2023-06-11 11:19:18 -04:00
|
|
|
if rel_path.parts[0] == '..':
|
|
|
|
logger.error(f'Invalid file path: {fname}')
|
|
|
|
return
|
|
|
|
|
2023-12-13 19:54:43 -05:00
|
|
|
if rel_path.exists():
|
|
|
|
rel_path.unlink()
|
2023-06-11 11:19:18 -04:00
|
|
|
logger.info(f'Deleted {fname}.')
|
2023-05-05 22:14:56 -04:00
|
|
|
|
|
|
|
|
2023-06-13 19:34:35 -04:00
|
|
|
def current_time():
|
|
|
|
return f"{datetime.now().strftime('%Y-%m-%d-%H%M%S')}"
|
|
|
|
|
|
|
|
|
2023-05-05 22:14:56 -04:00
|
|
|
def atoi(text):
|
|
|
|
return int(text) if text.isdigit() else text.lower()
|
|
|
|
|
|
|
|
|
2023-05-10 00:34:04 -04:00
|
|
|
# Replace multiple string pairs in a string
|
|
|
|
def replace_all(text, dic):
|
|
|
|
for i, j in dic.items():
|
|
|
|
text = text.replace(i, j)
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2023-05-05 22:14:56 -04:00
|
|
|
def natural_keys(text):
|
|
|
|
return [atoi(c) for c in re.split(r'(\d+)', text)]
|
|
|
|
|
|
|
|
|
|
|
|
def get_available_models():
|
2023-11-07 22:59:27 -05:00
|
|
|
model_list = []
|
2023-08-10 13:01:12 -04:00
|
|
|
for item in list(Path(f'{shared.args.model_dir}/').glob('*')):
|
|
|
|
if not item.name.endswith(('.txt', '-np', '.pt', '.json', '.yaml', '.py')) and 'llama-tokenizer' not in item.name:
|
|
|
|
model_list.append(re.sub('.pth$', '', item.name))
|
|
|
|
|
2023-11-07 22:59:27 -05:00
|
|
|
return ['None'] + sorted(model_list, key=natural_keys)
|
2023-05-05 22:14:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_available_presets():
|
2023-05-28 21:34:12 -04:00
|
|
|
return sorted(set((k.stem for k in Path('presets').glob('*.yaml'))), key=natural_keys)
|
2023-05-05 22:14:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_available_prompts():
|
|
|
|
prompts = []
|
2023-05-10 01:24:09 -04:00
|
|
|
files = set((k.stem for k in Path('prompts').glob('*.txt')))
|
|
|
|
prompts += sorted([k for k in files if re.match('^[0-9]', k)], key=natural_keys, reverse=True)
|
|
|
|
prompts += sorted([k for k in files if re.match('^[^0-9]', k)], key=natural_keys)
|
2023-05-05 22:14:56 -04:00
|
|
|
prompts += ['None']
|
|
|
|
return prompts
|
|
|
|
|
|
|
|
|
|
|
|
def get_available_characters():
|
|
|
|
paths = (x for x in Path('characters').iterdir() if x.suffix in ('.json', '.yaml', '.yml'))
|
2023-09-21 16:19:32 -04:00
|
|
|
return sorted(set((k.stem for k in paths)), key=natural_keys)
|
2023-05-05 22:14:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_available_instruction_templates():
|
2023-08-06 20:50:07 -04:00
|
|
|
path = "instruction-templates"
|
2023-05-05 22:14:56 -04:00
|
|
|
paths = []
|
|
|
|
if os.path.exists(path):
|
|
|
|
paths = (x for x in Path(path).iterdir() if x.suffix in ('.json', '.yaml', '.yml'))
|
2023-05-10 00:34:04 -04:00
|
|
|
|
2023-12-18 00:19:06 -05:00
|
|
|
return ['Select template to load...'] + sorted(set((k.stem for k in paths)), key=natural_keys)
|
2023-05-05 22:14:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_available_extensions():
|
2023-09-25 22:48:30 -04:00
|
|
|
extensions = sorted(set(map(lambda x: x.parts[1], Path('extensions').glob('*/script.py'))), key=natural_keys)
|
|
|
|
extensions = [v for v in extensions if v not in github.new_extensions]
|
|
|
|
return extensions
|
2023-05-05 22:14:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_available_loras():
|
2023-10-10 21:20:49 -04:00
|
|
|
return ['None'] + sorted([item.name for item in list(Path(shared.args.lora_dir).glob('*')) if not item.name.endswith(('.txt', '-np', '.pt', '.json'))], key=natural_keys)
|
2023-05-05 22:14:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_datasets(path: str, ext: str):
|
2023-07-12 10:44:30 -04:00
|
|
|
# include subdirectories for raw txt files to allow training from a subdirectory of txt files
|
|
|
|
if ext == "txt":
|
2023-11-07 17:41:11 -05:00
|
|
|
return ['None'] + sorted(set([k.stem for k in list(Path(path).glob('*.txt')) + list(Path(path).glob('*/')) if k.stem != 'put-trainer-datasets-here']), key=natural_keys)
|
2023-07-12 10:44:30 -04:00
|
|
|
|
2023-05-05 22:14:56 -04:00
|
|
|
return ['None'] + sorted(set([k.stem for k in Path(path).glob(f'*.{ext}') if k.stem != 'put-trainer-datasets-here']), key=natural_keys)
|
2023-05-08 11:35:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_available_chat_styles():
|
|
|
|
return sorted(set(('-'.join(k.stem.split('-')[1:]) for k in Path('css').glob('chat_style*.css'))), key=natural_keys)
|
2023-09-24 10:08:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_available_grammars():
|
|
|
|
return ['None'] + sorted([item.name for item in list(Path('grammars').glob('*.gbnf'))], key=natural_keys)
|