2023-03-15 11:33:26 -04:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-01-21 22:02:46 -05:00
|
|
|
import gradio as gr
|
2023-04-14 10:07:28 -04:00
|
|
|
import torch
|
2023-01-21 22:02:46 -05:00
|
|
|
|
2023-04-12 09:27:06 -04:00
|
|
|
from modules import shared
|
|
|
|
|
2023-03-15 11:33:26 -04:00
|
|
|
with open(Path(__file__).resolve().parent / '../css/main.css', 'r') as f:
|
|
|
|
css = f.read()
|
|
|
|
with open(Path(__file__).resolve().parent / '../css/chat.css', 'r') as f:
|
|
|
|
chat_css = f.read()
|
|
|
|
with open(Path(__file__).resolve().parent / '../css/main.js', 'r') as f:
|
2023-03-15 12:24:54 -04:00
|
|
|
main_js = f.read()
|
|
|
|
with open(Path(__file__).resolve().parent / '../css/chat.js', 'r') as f:
|
|
|
|
chat_js = f.read()
|
2023-03-15 11:01:32 -04:00
|
|
|
|
2023-04-18 22:36:23 -04:00
|
|
|
refresh_symbol = '\U0001f504' # 🔄
|
2023-05-20 20:48:45 -04:00
|
|
|
delete_symbol = '🗑️'
|
|
|
|
save_symbol = '💾'
|
|
|
|
|
2023-04-18 22:36:23 -04: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-20 23:20:33 -04:00
|
|
|
button_large_padding='6px 12px',
|
2023-04-21 01:47:18 -04:00
|
|
|
body_text_color_subdued='#484848',
|
|
|
|
background_fill_secondary='#eaeaea'
|
2023-04-18 22:36:23 -04:00
|
|
|
)
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-05-03 20:43:17 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
def list_model_elements():
|
2023-06-25 21:49:26 -04: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', 'threads', 'n_batch', 'no_mmap', 'mlock', 'n_gpu_layers', 'n_ctx', 'llama_cpp_seed', 'gpu_split', 'max_seq_len', 'compress_pos_emb']
|
2023-04-14 10:07:28 -04:00
|
|
|
for i in range(torch.cuda.device_count()):
|
|
|
|
elements.append(f'gpu_memory_{i}')
|
2023-05-25 00:14:13 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
return elements
|
|
|
|
|
|
|
|
|
2023-04-12 09:27:06 -04:00
|
|
|
def list_interface_input_elements(chat=False):
|
2023-06-29 12:40:13 -04:00
|
|
|
elements = ['max_new_tokens', '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', 'add_bos_token', 'ban_eos_token', 'truncation_length', 'custom_stopping_strings', 'skip_special_tokens', 'preset_menu', 'stream', 'tfs', 'top_a']
|
2023-04-12 09:27:06 -04:00
|
|
|
if chat:
|
2023-06-25 21:49:26 -04:00
|
|
|
elements += ['name1', 'name2', 'greeting', 'context', 'chat_generation_attempts', 'stop_at_newline', 'mode', 'instruction_template', 'character_menu', 'name1_instruct', 'name2_instruct', 'context_instruct', 'turn_template', 'chat_style', 'chat-instruct_command']
|
2023-04-24 02:05:47 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
elements += list_model_elements()
|
2023-04-12 09:27:06 -04:00
|
|
|
return elements
|
|
|
|
|
|
|
|
|
|
|
|
def gather_interface_values(*args):
|
|
|
|
output = {}
|
|
|
|
for i, element in enumerate(shared.input_elements):
|
|
|
|
output[element] = args[i]
|
2023-04-24 02:05:47 -04:00
|
|
|
|
|
|
|
shared.persistent_interface_state = output
|
2023-04-12 09:27:06 -04:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2023-04-24 02:05:47 -04:00
|
|
|
def apply_interface_values(state, use_persistent=False):
|
|
|
|
if use_persistent:
|
|
|
|
state = shared.persistent_interface_state
|
|
|
|
|
|
|
|
elements = list_interface_input_elements(chat=shared.is_chat())
|
|
|
|
if len(state) == 0:
|
|
|
|
return [gr.update() for k in elements] # Dummy, do nothing
|
|
|
|
else:
|
2023-05-14 09:43:55 -04:00
|
|
|
return [state[k] if k in state else gr.update() for k in elements]
|
2023-04-14 10:07:28 -04:00
|
|
|
|
|
|
|
|
2023-01-21 22:02:46 -05:00
|
|
|
class ToolButton(gr.Button, gr.components.FormComponent):
|
|
|
|
"""Small button with single emoji as text, fits inside gradio forms"""
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2023-06-11 13:20:16 -04:00
|
|
|
super().__init__(**kwargs)
|
2023-01-21 22:02:46 -05:00
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "button"
|
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-06-11 13:20:16 -04:00
|
|
|
def create_refresh_button(refresh_component, refresh_method, refreshed_args, elem_class):
|
2023-01-21 22:02:46 -05: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 13:20:16 -04:00
|
|
|
refresh_button = ToolButton(value=refresh_symbol, elem_classes=elem_class)
|
2023-01-21 22:02:46 -05:00
|
|
|
refresh_button.click(
|
|
|
|
fn=refresh,
|
|
|
|
inputs=[],
|
|
|
|
outputs=[refresh_component]
|
|
|
|
)
|
|
|
|
return refresh_button
|
2023-05-20 20:48:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
def create_delete_button(**kwargs):
|
|
|
|
return ToolButton(value=delete_symbol, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def create_save_button(**kwargs):
|
|
|
|
return ToolButton(value=save_symbol, **kwargs)
|