2023-08-14 16:46:07 +02:00
|
|
|
import copy
|
2023-03-15 16:33:26 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-01-22 04:02:46 +01:00
|
|
|
import gradio as gr
|
2023-04-14 16:07:28 +02:00
|
|
|
import torch
|
2023-08-14 16:46:07 +02:00
|
|
|
import yaml
|
2023-01-22 04:02:46 +01:00
|
|
|
|
2023-04-12 15:27:06 +02:00
|
|
|
from modules import shared
|
|
|
|
|
2023-07-04 05:03:30 +02:00
|
|
|
|
2023-03-15 16:33:26 +01:00
|
|
|
with open(Path(__file__).resolve().parent / '../css/main.css', 'r') as f:
|
|
|
|
css = f.read()
|
2023-08-07 02:49:27 +02:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/main.js', 'r') as f:
|
2023-08-13 06:12:15 +02:00
|
|
|
js = f.read()
|
2023-08-07 02:49:27 +02:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/save_files.js', 'r') as f:
|
2023-08-03 03:50:13 +02:00
|
|
|
save_files_js = f.read()
|
2023-08-14 03:14:09 +02:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/switch_tabs.js', 'r') as f:
|
|
|
|
switch_tabs_js = f.read()
|
2023-08-16 07:39:58 +02:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/show_controls.js', 'r') as f:
|
|
|
|
show_controls_js = f.read()
|
2023-03-15 16:01:32 +01:00
|
|
|
|
2023-07-04 05:03:30 +02:00
|
|
|
refresh_symbol = '🔄'
|
2023-05-21 02:48:45 +02:00
|
|
|
delete_symbol = '🗑️'
|
|
|
|
save_symbol = '💾'
|
|
|
|
|
2023-04-19 04:36:23 +02:00
|
|
|
theme = gr.themes.Default(
|
|
|
|
font=['Helvetica', 'ui-sans-serif', 'system-ui', 'sans-serif'],
|
|
|
|
font_mono=['IBM Plex Mono', 'ui-monospace', 'Consolas', 'monospace'],
|
|
|
|
).set(
|
|
|
|
border_color_primary='#c5c5d2',
|
2023-04-21 05:20:33 +02:00
|
|
|
button_large_padding='6px 12px',
|
2023-04-21 07:47:18 +02:00
|
|
|
body_text_color_subdued='#484848',
|
|
|
|
background_fill_secondary='#eaeaea'
|
2023-04-19 04:36:23 +02:00
|
|
|
)
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-08-07 02:49:27 +02:00
|
|
|
if Path("notification.mp3").exists():
|
|
|
|
audio_notification_js = "document.querySelector('#audio_notification audio')?.play();"
|
|
|
|
else:
|
|
|
|
audio_notification_js = ""
|
|
|
|
|
2023-05-04 02:43:17 +02:00
|
|
|
|
2023-04-14 16:07:28 +02:00
|
|
|
def list_model_elements():
|
2023-07-04 05:03:30 +02:00
|
|
|
elements = [
|
|
|
|
'loader',
|
|
|
|
'cpu_memory',
|
|
|
|
'auto_devices',
|
|
|
|
'disk',
|
|
|
|
'cpu',
|
|
|
|
'bf16',
|
|
|
|
'load_in_8bit',
|
|
|
|
'trust_remote_code',
|
|
|
|
'load_in_4bit',
|
|
|
|
'compute_dtype',
|
|
|
|
'quant_type',
|
|
|
|
'use_double_quant',
|
|
|
|
'wbits',
|
|
|
|
'groupsize',
|
|
|
|
'model_type',
|
|
|
|
'pre_layer',
|
|
|
|
'triton',
|
|
|
|
'desc_act',
|
|
|
|
'no_inject_fused_attention',
|
|
|
|
'no_inject_fused_mlp',
|
|
|
|
'no_use_cuda_fp16',
|
2023-08-12 08:26:58 +02:00
|
|
|
'disable_exllama',
|
2023-08-24 21:27:36 +02:00
|
|
|
'cfg_cache',
|
2023-07-04 05:03:30 +02:00
|
|
|
'threads',
|
|
|
|
'n_batch',
|
|
|
|
'no_mmap',
|
2023-07-12 16:05:13 +02:00
|
|
|
'low_vram',
|
2023-07-04 05:03:30 +02:00
|
|
|
'mlock',
|
2023-08-18 17:03:34 +02:00
|
|
|
'mul_mat_q',
|
2023-07-04 05:03:30 +02:00
|
|
|
'n_gpu_layers',
|
2023-08-18 17:03:34 +02:00
|
|
|
'tensor_split',
|
2023-07-04 05:03:30 +02:00
|
|
|
'n_ctx',
|
|
|
|
'llama_cpp_seed',
|
|
|
|
'gpu_split',
|
|
|
|
'max_seq_len',
|
2023-07-04 06:13:16 +02:00
|
|
|
'compress_pos_emb',
|
2023-08-25 15:53:37 +02:00
|
|
|
'alpha_value',
|
|
|
|
'rope_freq_base'
|
2023-07-04 05:03:30 +02:00
|
|
|
]
|
|
|
|
|
2023-04-14 16:07:28 +02:00
|
|
|
for i in range(torch.cuda.device_count()):
|
|
|
|
elements.append(f'gpu_memory_{i}')
|
2023-05-25 06:14:13 +02:00
|
|
|
|
2023-04-14 16:07:28 +02:00
|
|
|
return elements
|
|
|
|
|
|
|
|
|
2023-07-04 05:03:30 +02:00
|
|
|
def list_interface_input_elements():
|
|
|
|
elements = [
|
|
|
|
'max_new_tokens',
|
2023-08-02 19:52:20 +02:00
|
|
|
'auto_max_new_tokens',
|
2023-08-29 22:44:31 +02:00
|
|
|
'max_tokens_second',
|
2023-07-04 05:03:30 +02:00
|
|
|
'seed',
|
|
|
|
'temperature',
|
|
|
|
'top_p',
|
|
|
|
'top_k',
|
|
|
|
'typical_p',
|
|
|
|
'epsilon_cutoff',
|
|
|
|
'eta_cutoff',
|
|
|
|
'repetition_penalty',
|
|
|
|
'repetition_penalty_range',
|
|
|
|
'encoder_repetition_penalty',
|
|
|
|
'no_repeat_ngram_size',
|
|
|
|
'min_length',
|
|
|
|
'do_sample',
|
|
|
|
'penalty_alpha',
|
|
|
|
'num_beams',
|
|
|
|
'length_penalty',
|
|
|
|
'early_stopping',
|
|
|
|
'mirostat_mode',
|
|
|
|
'mirostat_tau',
|
|
|
|
'mirostat_eta',
|
2023-08-06 22:22:48 +02:00
|
|
|
'negative_prompt',
|
|
|
|
'guidance_scale',
|
2023-07-04 05:03:30 +02:00
|
|
|
'add_bos_token',
|
|
|
|
'ban_eos_token',
|
|
|
|
'truncation_length',
|
|
|
|
'custom_stopping_strings',
|
|
|
|
'skip_special_tokens',
|
|
|
|
'stream',
|
|
|
|
'tfs',
|
|
|
|
'top_a',
|
|
|
|
]
|
|
|
|
|
2023-08-13 06:12:15 +02:00
|
|
|
# Chat elements
|
|
|
|
elements += [
|
|
|
|
'textbox',
|
2023-08-14 16:46:07 +02:00
|
|
|
'start_with',
|
2023-08-13 06:12:15 +02:00
|
|
|
'character_menu',
|
|
|
|
'history',
|
|
|
|
'name1',
|
|
|
|
'name2',
|
|
|
|
'greeting',
|
|
|
|
'context',
|
|
|
|
'mode',
|
|
|
|
'instruction_template',
|
|
|
|
'name1_instruct',
|
|
|
|
'name2_instruct',
|
|
|
|
'context_instruct',
|
|
|
|
'turn_template',
|
|
|
|
'chat_style',
|
|
|
|
'chat-instruct_command',
|
|
|
|
]
|
2023-04-24 08:05:47 +02:00
|
|
|
|
2023-08-13 06:12:15 +02:00
|
|
|
# Notebook/default elements
|
|
|
|
elements += [
|
|
|
|
'textbox-notebook',
|
|
|
|
'textbox-default',
|
2023-08-14 16:46:07 +02:00
|
|
|
'output_textbox',
|
|
|
|
'prompt_menu-default',
|
|
|
|
'prompt_menu-notebook',
|
2023-08-13 06:12:15 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
# Model elements
|
2023-04-14 16:07:28 +02:00
|
|
|
elements += list_model_elements()
|
2023-08-13 06:12:15 +02:00
|
|
|
|
2023-04-12 15:27:06 +02:00
|
|
|
return elements
|
|
|
|
|
|
|
|
|
|
|
|
def gather_interface_values(*args):
|
|
|
|
output = {}
|
2023-07-04 05:03:30 +02:00
|
|
|
for i, element in enumerate(list_interface_input_elements()):
|
2023-04-12 15:27:06 +02:00
|
|
|
output[element] = args[i]
|
2023-04-24 08:05:47 +02:00
|
|
|
|
2023-07-04 05:03:30 +02:00
|
|
|
if not shared.args.multi_user:
|
|
|
|
shared.persistent_interface_state = output
|
|
|
|
|
2023-04-12 15:27:06 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2023-04-24 08:05:47 +02:00
|
|
|
def apply_interface_values(state, use_persistent=False):
|
|
|
|
if use_persistent:
|
|
|
|
state = shared.persistent_interface_state
|
|
|
|
|
2023-07-04 05:03:30 +02:00
|
|
|
elements = list_interface_input_elements()
|
2023-04-24 08:05:47 +02:00
|
|
|
if len(state) == 0:
|
|
|
|
return [gr.update() for k in elements] # Dummy, do nothing
|
|
|
|
else:
|
2023-07-07 18:09:14 +02:00
|
|
|
return [state[k] if k in state else gr.update() for k in elements]
|
2023-04-14 16:07:28 +02:00
|
|
|
|
|
|
|
|
2023-08-16 16:03:53 +02:00
|
|
|
def save_settings(state, preset, instruction_template, extensions, show_controls):
|
2023-08-14 16:46:07 +02:00
|
|
|
output = copy.deepcopy(shared.settings)
|
|
|
|
exclude = ['name1', 'name2', 'greeting', 'context', 'turn_template']
|
|
|
|
for k in state:
|
|
|
|
if k in shared.settings and k not in exclude:
|
|
|
|
output[k] = state[k]
|
|
|
|
|
|
|
|
output['preset'] = preset
|
|
|
|
output['prompt-default'] = state['prompt_menu-default']
|
|
|
|
output['prompt-notebook'] = state['prompt_menu-notebook']
|
|
|
|
output['character'] = state['character_menu']
|
|
|
|
output['instruction_template'] = instruction_template
|
|
|
|
output['default_extensions'] = extensions
|
|
|
|
output['seed'] = int(output['seed'])
|
2023-08-16 16:03:53 +02:00
|
|
|
output['show_controls'] = show_controls
|
2023-08-14 16:46:07 +02:00
|
|
|
|
|
|
|
return yaml.dump(output, sort_keys=False, width=float("inf"))
|
|
|
|
|
|
|
|
|
2023-07-11 23:52:16 +02:00
|
|
|
class ToolButton(gr.Button, gr.components.IOComponent):
|
2023-07-26 00:49:04 +02:00
|
|
|
"""
|
|
|
|
Small button with single emoji as text, fits inside gradio forms
|
|
|
|
Copied from https://github.com/AUTOMATIC1111/stable-diffusion-webui
|
|
|
|
"""
|
2023-01-22 04:02:46 +01:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2023-06-11 19:20:16 +02:00
|
|
|
super().__init__(**kwargs)
|
2023-01-22 04:02:46 +01:00
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "button"
|
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-06-11 19:20:16 +02:00
|
|
|
def create_refresh_button(refresh_component, refresh_method, refreshed_args, elem_class):
|
2023-07-26 00:49:04 +02:00
|
|
|
"""
|
|
|
|
Copied from https://github.com/AUTOMATIC1111/stable-diffusion-webui
|
|
|
|
"""
|
2023-01-22 04:02:46 +01:00
|
|
|
def refresh():
|
|
|
|
refresh_method()
|
|
|
|
args = refreshed_args() if callable(refreshed_args) else refreshed_args
|
|
|
|
|
|
|
|
for k, v in args.items():
|
|
|
|
setattr(refresh_component, k, v)
|
|
|
|
|
|
|
|
return gr.update(**(args or {}))
|
|
|
|
|
2023-06-11 19:20:16 +02:00
|
|
|
refresh_button = ToolButton(value=refresh_symbol, elem_classes=elem_class)
|
2023-01-22 04:02:46 +01:00
|
|
|
refresh_button.click(
|
|
|
|
fn=refresh,
|
|
|
|
inputs=[],
|
|
|
|
outputs=[refresh_component]
|
|
|
|
)
|
2023-07-04 05:03:30 +02:00
|
|
|
|
2023-01-22 04:02:46 +01:00
|
|
|
return refresh_button
|
2023-05-21 02:48:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_delete_button(**kwargs):
|
|
|
|
return ToolButton(value=delete_symbol, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def create_save_button(**kwargs):
|
|
|
|
return ToolButton(value=save_symbol, **kwargs)
|