2023-08-14 11:46:07 -03:00
|
|
|
import copy
|
2023-03-15 12:33:26 -03:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-01-22 00:02:46 -03:00
|
|
|
import gradio as gr
|
2023-04-14 11:07:28 -03:00
|
|
|
import torch
|
2023-08-14 11:46:07 -03:00
|
|
|
import yaml
|
2023-10-27 08:09:51 +05:30
|
|
|
from transformers import is_torch_xpu_available
|
2023-01-22 00:02:46 -03:00
|
|
|
|
2024-01-04 20:33:32 -08:00
|
|
|
import extensions
|
2023-04-12 10:27:06 -03:00
|
|
|
from modules import shared
|
|
|
|
|
2023-09-13 13:29:00 -07:00
|
|
|
with open(Path(__file__).resolve().parent / '../css/NotoSans/stylesheet.css', 'r') as f:
|
2023-03-15 12:33:26 -03:00
|
|
|
css = f.read()
|
2023-09-13 13:29:00 -07:00
|
|
|
with open(Path(__file__).resolve().parent / '../css/main.css', 'r') as f:
|
|
|
|
css += f.read()
|
2024-04-26 10:13:11 -03:00
|
|
|
with open(Path(__file__).resolve().parent / '../css/katex/katex.min.css', 'r') as f:
|
|
|
|
css += f.read()
|
|
|
|
with open(Path(__file__).resolve().parent / '../css/highlightjs/highlightjs-copy.min.css', 'r') as f:
|
|
|
|
css += f.read()
|
2023-08-06 21:49:27 -03:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/main.js', 'r') as f:
|
2023-08-13 01:12:15 -03:00
|
|
|
js = f.read()
|
2023-08-06 21:49:27 -03:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/save_files.js', 'r') as f:
|
2023-08-02 18:50:13 -07:00
|
|
|
save_files_js = f.read()
|
2023-08-13 18:14:09 -07:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/switch_tabs.js', 'r') as f:
|
|
|
|
switch_tabs_js = f.read()
|
2023-08-16 02:39:58 -03:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/show_controls.js', 'r') as f:
|
|
|
|
show_controls_js = f.read()
|
2023-11-19 02:05:17 -03:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/update_big_picture.js', 'r') as f:
|
|
|
|
update_big_picture_js = f.read()
|
2024-07-20 20:32:57 -07:00
|
|
|
with open(Path(__file__).resolve().parent / '../js/dark_theme.js', 'r') as f:
|
|
|
|
dark_theme_js = f.read()
|
2023-03-15 12:01:32 -03:00
|
|
|
|
2023-07-04 00:03:30 -03:00
|
|
|
refresh_symbol = '🔄'
|
2023-05-20 17:48:45 -07:00
|
|
|
delete_symbol = '🗑️'
|
|
|
|
save_symbol = '💾'
|
|
|
|
|
2023-04-18 23:36:23 -03:00
|
|
|
theme = gr.themes.Default(
|
2023-09-13 13:29:00 -07:00
|
|
|
font=['Noto Sans', 'Helvetica', 'ui-sans-serif', 'system-ui', 'sans-serif'],
|
2023-04-18 23:36:23 -03:00
|
|
|
font_mono=['IBM Plex Mono', 'ui-monospace', 'Consolas', 'monospace'],
|
|
|
|
).set(
|
|
|
|
border_color_primary='#c5c5d2',
|
2023-04-21 00:20:33 -03:00
|
|
|
button_large_padding='6px 12px',
|
2023-04-21 02:47:18 -03:00
|
|
|
body_text_color_subdued='#484848',
|
2024-03-13 08:18:49 -07:00
|
|
|
background_fill_secondary='#eaeaea',
|
2024-04-26 10:13:11 -03:00
|
|
|
background_fill_primary='var(--neutral-50)',
|
2024-06-27 20:47:42 -07:00
|
|
|
body_background_fill="white",
|
|
|
|
block_background_fill="#f4f4f4",
|
|
|
|
body_text_color="#333",
|
|
|
|
button_secondary_background_fill="#f4f4f4",
|
|
|
|
button_secondary_border_color="var(--border-color-primary)"
|
2023-04-18 23:36:23 -03:00
|
|
|
)
|
2023-04-07 00:15:45 -03:00
|
|
|
|
2024-12-17 00:47:41 -03:00
|
|
|
if not shared.args.old_colors:
|
|
|
|
theme = theme.set(
|
|
|
|
# General Colors
|
|
|
|
border_color_primary='#c5c5d2',
|
|
|
|
body_text_color_subdued='#484848',
|
|
|
|
background_fill_secondary='#eaeaea',
|
2024-12-17 07:59:55 -08:00
|
|
|
background_fill_secondary_dark='var(--selected-item-color-dark)',
|
2024-12-17 00:47:41 -03:00
|
|
|
background_fill_primary='var(--neutral-50)',
|
|
|
|
background_fill_primary_dark='var(--darker-gray)',
|
|
|
|
body_background_fill="white",
|
|
|
|
block_background_fill="transparent",
|
|
|
|
body_text_color="#333",
|
|
|
|
button_secondary_background_fill="#f4f4f4",
|
|
|
|
button_secondary_border_color="var(--border-color-primary)",
|
|
|
|
|
|
|
|
# Dark Mode Colors
|
|
|
|
input_background_fill_dark='var(--darker-gray)',
|
|
|
|
checkbox_background_color_dark='var(--darker-gray)',
|
|
|
|
block_background_fill_dark='transparent',
|
|
|
|
block_border_color_dark='transparent',
|
|
|
|
input_border_color_dark='var(--border-color-dark)',
|
|
|
|
checkbox_border_color_dark='var(--border-color-dark)',
|
|
|
|
border_color_primary_dark='var(--border-color-dark)',
|
|
|
|
button_secondary_border_color_dark='var(--border-color-dark)',
|
|
|
|
body_background_fill_dark='var(--dark-gray)',
|
|
|
|
button_primary_background_fill_dark='transparent',
|
|
|
|
button_secondary_background_fill_dark='transparent',
|
|
|
|
checkbox_label_background_fill_dark='transparent',
|
|
|
|
button_cancel_background_fill_dark='transparent',
|
|
|
|
button_secondary_background_fill_hover_dark='var(--selected-item-color-dark)',
|
|
|
|
checkbox_label_background_fill_hover_dark='var(--selected-item-color-dark)',
|
|
|
|
table_even_background_fill_dark='var(--darker-gray)',
|
2024-12-17 07:59:55 -08:00
|
|
|
table_odd_background_fill_dark='var(--selected-item-color-dark)',
|
2024-12-17 19:58:09 -08:00
|
|
|
code_background_fill_dark='var(--darker-gray)',
|
2024-12-17 00:47:41 -03:00
|
|
|
|
|
|
|
# Shadows and Radius
|
|
|
|
checkbox_label_shadow='none',
|
|
|
|
block_shadow='none',
|
|
|
|
block_shadow_dark='none',
|
|
|
|
button_large_radius='0.375rem',
|
|
|
|
button_large_padding='6px 12px',
|
|
|
|
input_radius='0.375rem',
|
|
|
|
)
|
|
|
|
|
2023-08-06 21:49:27 -03:00
|
|
|
if Path("notification.mp3").exists():
|
|
|
|
audio_notification_js = "document.querySelector('#audio_notification audio')?.play();"
|
|
|
|
else:
|
|
|
|
audio_notification_js = ""
|
|
|
|
|
2023-05-03 21:43:17 -03:00
|
|
|
|
2023-04-14 11:07:28 -03:00
|
|
|
def list_model_elements():
|
2023-07-04 00:03:30 -03:00
|
|
|
elements = [
|
|
|
|
'loader',
|
2023-09-25 10:28:35 -07:00
|
|
|
'filter_by_loader',
|
2023-07-04 00:03:30 -03:00
|
|
|
'cpu_memory',
|
|
|
|
'auto_devices',
|
|
|
|
'disk',
|
|
|
|
'cpu',
|
|
|
|
'bf16',
|
|
|
|
'load_in_8bit',
|
|
|
|
'trust_remote_code',
|
2023-11-16 19:45:05 -08:00
|
|
|
'no_use_fast',
|
2023-11-05 00:59:33 +08:00
|
|
|
'use_flash_attention_2',
|
2024-07-01 23:08:08 +08:00
|
|
|
'use_eager_attention',
|
2023-07-04 00:03:30 -03:00
|
|
|
'load_in_4bit',
|
|
|
|
'compute_dtype',
|
|
|
|
'quant_type',
|
|
|
|
'use_double_quant',
|
|
|
|
'wbits',
|
|
|
|
'groupsize',
|
|
|
|
'triton',
|
|
|
|
'desc_act',
|
|
|
|
'no_inject_fused_mlp',
|
|
|
|
'no_use_cuda_fp16',
|
2023-08-12 02:26:58 -04:00
|
|
|
'disable_exllama',
|
2023-12-15 06:46:13 -08:00
|
|
|
'disable_exllamav2',
|
2023-08-24 16:27:36 -03:00
|
|
|
'cfg_cache',
|
2023-11-02 11:23:04 -07:00
|
|
|
'no_flash_attn',
|
2024-07-11 15:47:37 -07:00
|
|
|
'no_xformers',
|
|
|
|
'no_sdpa',
|
2023-12-17 12:08:33 -03:00
|
|
|
'num_experts_per_token',
|
2024-12-17 15:43:48 -05:00
|
|
|
'cache_type',
|
2024-02-16 15:26:10 -03:00
|
|
|
'autosplit',
|
2024-10-01 11:16:15 -07:00
|
|
|
'enable_tp',
|
2023-07-04 00:03:30 -03:00
|
|
|
'threads',
|
2023-10-01 21:27:04 -07:00
|
|
|
'threads_batch',
|
2023-07-04 00:03:30 -03:00
|
|
|
'n_batch',
|
|
|
|
'no_mmap',
|
|
|
|
'mlock',
|
2023-10-22 12:22:06 -07:00
|
|
|
'no_mul_mat_q',
|
2023-07-04 00:03:30 -03:00
|
|
|
'n_gpu_layers',
|
2023-08-18 12:03:34 -03:00
|
|
|
'tensor_split',
|
2023-07-04 00:03:30 -03:00
|
|
|
'n_ctx',
|
|
|
|
'gpu_split',
|
|
|
|
'max_seq_len',
|
2023-07-04 00:13:16 -04:00
|
|
|
'compress_pos_emb',
|
2023-08-25 06:53:37 -07:00
|
|
|
'alpha_value',
|
2023-09-27 03:05:00 +02:00
|
|
|
'rope_freq_base',
|
|
|
|
'numa',
|
2023-11-07 14:35:48 -08:00
|
|
|
'logits_all',
|
2023-12-19 15:22:21 -03:00
|
|
|
'no_offload_kqv',
|
2024-02-05 02:36:40 +00:00
|
|
|
'row_split',
|
2024-07-22 18:05:11 -07:00
|
|
|
'tensorcores',
|
2024-06-13 03:34:54 +00:00
|
|
|
'flash_attn',
|
2024-03-09 00:25:33 -03:00
|
|
|
'streaming_llm',
|
|
|
|
'attention_sink_size',
|
2023-12-18 19:23:16 -05:00
|
|
|
'hqq_backend',
|
2024-06-24 02:30:03 -03:00
|
|
|
'cpp_runner',
|
2023-07-04 00:03:30 -03:00
|
|
|
]
|
2024-07-21 22:06:49 -07:00
|
|
|
|
2023-10-27 08:09:51 +05:30
|
|
|
if is_torch_xpu_available():
|
|
|
|
for i in range(torch.xpu.device_count()):
|
|
|
|
elements.append(f'gpu_memory_{i}')
|
|
|
|
else:
|
|
|
|
for i in range(torch.cuda.device_count()):
|
|
|
|
elements.append(f'gpu_memory_{i}')
|
2023-05-25 01:14:13 -03:00
|
|
|
|
2023-04-14 11:07:28 -03:00
|
|
|
return elements
|
|
|
|
|
|
|
|
|
2023-07-04 00:03:30 -03:00
|
|
|
def list_interface_input_elements():
|
|
|
|
elements = [
|
|
|
|
'max_new_tokens',
|
2023-08-02 14:52:20 -03:00
|
|
|
'auto_max_new_tokens',
|
2023-08-29 17:44:31 -03:00
|
|
|
'max_tokens_second',
|
2024-04-26 10:14:51 -03:00
|
|
|
'max_updates_second',
|
2024-01-17 17:09:36 -03:00
|
|
|
'prompt_lookup_num_tokens',
|
2023-07-04 00:03:30 -03:00
|
|
|
'seed',
|
|
|
|
'temperature',
|
2023-11-04 13:09:07 -03:00
|
|
|
'temperature_last',
|
2024-01-07 17:03:47 -03:00
|
|
|
'dynamic_temperature',
|
2024-01-08 23:28:35 -03:00
|
|
|
'dynatemp_low',
|
|
|
|
'dynatemp_high',
|
|
|
|
'dynatemp_exponent',
|
2024-02-03 21:20:02 -06:00
|
|
|
'smoothing_factor',
|
2024-03-03 10:22:21 -06:00
|
|
|
'smoothing_curve',
|
2023-07-04 00:03:30 -03:00
|
|
|
'top_p',
|
2023-11-02 14:32:51 -05:00
|
|
|
'min_p',
|
2023-07-04 00:03:30 -03:00
|
|
|
'top_k',
|
|
|
|
'typical_p',
|
|
|
|
'epsilon_cutoff',
|
|
|
|
'eta_cutoff',
|
|
|
|
'repetition_penalty',
|
2023-10-25 10:10:28 -05:00
|
|
|
'presence_penalty',
|
|
|
|
'frequency_penalty',
|
2023-07-04 00:03:30 -03:00
|
|
|
'repetition_penalty_range',
|
|
|
|
'encoder_repetition_penalty',
|
|
|
|
'no_repeat_ngram_size',
|
2024-05-20 08:23:47 +05:30
|
|
|
'dry_multiplier',
|
|
|
|
'dry_base',
|
|
|
|
'dry_allowed_length',
|
|
|
|
'dry_sequence_breakers',
|
2024-09-28 07:20:12 +05:30
|
|
|
'xtc_threshold',
|
|
|
|
'xtc_probability',
|
2023-07-04 00:03:30 -03:00
|
|
|
'do_sample',
|
|
|
|
'penalty_alpha',
|
|
|
|
'mirostat_mode',
|
|
|
|
'mirostat_tau',
|
|
|
|
'mirostat_eta',
|
2023-09-24 18:05:24 -03:00
|
|
|
'grammar_string',
|
2023-08-06 17:22:48 -03:00
|
|
|
'negative_prompt',
|
|
|
|
'guidance_scale',
|
2023-07-04 00:03:30 -03:00
|
|
|
'add_bos_token',
|
|
|
|
'ban_eos_token',
|
2023-09-15 14:27:27 -07:00
|
|
|
'custom_token_bans',
|
2024-02-06 11:20:10 -03:00
|
|
|
'sampler_priority',
|
2023-07-04 00:03:30 -03:00
|
|
|
'truncation_length',
|
|
|
|
'custom_stopping_strings',
|
|
|
|
'skip_special_tokens',
|
|
|
|
'stream',
|
|
|
|
'tfs',
|
|
|
|
'top_a',
|
|
|
|
]
|
|
|
|
|
2023-08-13 01:12:15 -03:00
|
|
|
# Chat elements
|
|
|
|
elements += [
|
|
|
|
'textbox',
|
2023-08-14 11:46:07 -03:00
|
|
|
'start_with',
|
2023-08-13 01:12:15 -03:00
|
|
|
'character_menu',
|
|
|
|
'history',
|
2025-01-02 18:46:40 -08:00
|
|
|
'search_chat',
|
2024-07-21 00:01:42 -03:00
|
|
|
'unique_id',
|
2023-08-13 01:12:15 -03:00
|
|
|
'name1',
|
2024-03-11 23:41:57 -03:00
|
|
|
'user_bio',
|
2023-08-13 01:12:15 -03:00
|
|
|
'name2',
|
|
|
|
'greeting',
|
|
|
|
'context',
|
|
|
|
'mode',
|
2023-11-07 20:02:58 -03:00
|
|
|
'custom_system_message',
|
2023-12-12 17:23:14 -03:00
|
|
|
'instruction_template_str',
|
|
|
|
'chat_template_str',
|
2023-08-13 01:12:15 -03:00
|
|
|
'chat_style',
|
|
|
|
'chat-instruct_command',
|
|
|
|
]
|
2023-04-24 03:05:47 -03:00
|
|
|
|
2023-08-13 01:12:15 -03:00
|
|
|
# Notebook/default elements
|
|
|
|
elements += [
|
|
|
|
'textbox-notebook',
|
|
|
|
'textbox-default',
|
2023-08-14 11:46:07 -03:00
|
|
|
'output_textbox',
|
|
|
|
'prompt_menu-default',
|
|
|
|
'prompt_menu-notebook',
|
2023-08-13 01:12:15 -03:00
|
|
|
]
|
|
|
|
|
|
|
|
# Model elements
|
2023-04-14 11:07:28 -03:00
|
|
|
elements += list_model_elements()
|
2023-08-13 01:12:15 -03:00
|
|
|
|
2023-04-12 10:27:06 -03:00
|
|
|
return elements
|
|
|
|
|
|
|
|
|
|
|
|
def gather_interface_values(*args):
|
2024-07-21 22:06:49 -07:00
|
|
|
interface_elements = list_interface_input_elements()
|
|
|
|
|
2023-04-12 10:27:06 -03:00
|
|
|
output = {}
|
2024-07-21 22:06:49 -07:00
|
|
|
for element, value in zip(interface_elements, args):
|
|
|
|
output[element] = value
|
2023-04-24 03:05:47 -03:00
|
|
|
|
2023-07-04 00:03:30 -03:00
|
|
|
if not shared.args.multi_user:
|
|
|
|
shared.persistent_interface_state = output
|
|
|
|
|
2023-04-12 10:27:06 -03:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2023-04-24 03:05:47 -03:00
|
|
|
def apply_interface_values(state, use_persistent=False):
|
|
|
|
if use_persistent:
|
|
|
|
state = shared.persistent_interface_state
|
2024-12-17 00:47:41 -03:00
|
|
|
if 'textbox-default' in state and 'prompt_menu-default' in state:
|
2024-07-22 11:05:40 -07:00
|
|
|
state.pop('prompt_menu-default')
|
|
|
|
|
2024-12-17 00:47:41 -03:00
|
|
|
if 'textbox-notebook' and 'prompt_menu-notebook' in state:
|
2024-07-22 11:05:40 -07:00
|
|
|
state.pop('prompt_menu-notebook')
|
2023-04-24 03:05:47 -03:00
|
|
|
|
2023-07-04 00:03:30 -03:00
|
|
|
elements = list_interface_input_elements()
|
2024-07-22 11:05:40 -07:00
|
|
|
|
2023-04-24 03:05:47 -03:00
|
|
|
if len(state) == 0:
|
|
|
|
return [gr.update() for k in elements] # Dummy, do nothing
|
|
|
|
else:
|
2023-07-07 09:09:14 -07:00
|
|
|
return [state[k] if k in state else gr.update() for k in elements]
|
2023-04-14 11:07:28 -03:00
|
|
|
|
|
|
|
|
2024-01-09 04:20:10 -08:00
|
|
|
def save_settings(state, preset, extensions_list, show_controls, theme_state):
|
2023-08-14 11:46:07 -03:00
|
|
|
output = copy.deepcopy(shared.settings)
|
2024-07-21 10:45:01 -07:00
|
|
|
exclude = ['name2', 'greeting', 'context', 'truncation_length', 'instruction_template_str']
|
2023-08-14 11:46:07 -03:00
|
|
|
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']
|
2024-01-04 20:33:32 -08:00
|
|
|
output['default_extensions'] = extensions_list
|
2023-08-14 11:46:07 -03:00
|
|
|
output['seed'] = int(output['seed'])
|
2023-08-16 07:03:53 -07:00
|
|
|
output['show_controls'] = show_controls
|
2024-01-09 04:20:10 -08:00
|
|
|
output['dark_theme'] = True if theme_state == 'dark' else False
|
2023-08-14 11:46:07 -03:00
|
|
|
|
2024-01-04 20:33:32 -08:00
|
|
|
# Save extension values in the UI
|
|
|
|
for extension_name in extensions_list:
|
2024-04-11 23:19:16 +02:00
|
|
|
extension = getattr(extensions, extension_name, None)
|
|
|
|
if extension:
|
|
|
|
extension = extension.script
|
|
|
|
if hasattr(extension, 'params'):
|
|
|
|
params = getattr(extension, 'params')
|
|
|
|
for param in params:
|
|
|
|
_id = f"{extension_name}-{param}"
|
|
|
|
# Only save if different from default value
|
|
|
|
if param not in shared.default_settings or params[param] != shared.default_settings[param]:
|
|
|
|
output[_id] = params[param]
|
2024-01-04 20:33:32 -08:00
|
|
|
|
2024-01-09 18:59:04 -08:00
|
|
|
# Do not save unchanged settings
|
|
|
|
for key in list(output.keys()):
|
|
|
|
if key in shared.default_settings and output[key] == shared.default_settings[key]:
|
|
|
|
output.pop(key)
|
|
|
|
|
2024-07-21 10:45:01 -07:00
|
|
|
return yaml.dump(output, sort_keys=False, width=float("inf"), allow_unicode=True)
|
2023-08-14 11:46:07 -03:00
|
|
|
|
|
|
|
|
2023-09-26 05:44:04 -07:00
|
|
|
def create_refresh_button(refresh_component, refresh_method, refreshed_args, elem_class, interactive=True):
|
2023-07-25 15:49:04 -07:00
|
|
|
"""
|
|
|
|
Copied from https://github.com/AUTOMATIC1111/stable-diffusion-webui
|
|
|
|
"""
|
2023-01-22 00:02:46 -03:00
|
|
|
def refresh():
|
|
|
|
refresh_method()
|
|
|
|
args = refreshed_args() if callable(refreshed_args) else refreshed_args
|
|
|
|
|
|
|
|
return gr.update(**(args or {}))
|
|
|
|
|
2023-10-10 22:20:49 -03:00
|
|
|
refresh_button = gr.Button(refresh_symbol, elem_classes=elem_class, interactive=interactive)
|
2023-01-22 00:02:46 -03:00
|
|
|
refresh_button.click(
|
2024-01-08 17:49:54 -08:00
|
|
|
fn=lambda: {k: tuple(v) if type(k) is list else v for k, v in refresh().items()},
|
2023-01-22 00:02:46 -03:00
|
|
|
inputs=[],
|
|
|
|
outputs=[refresh_component]
|
|
|
|
)
|
2023-07-04 00:03:30 -03:00
|
|
|
|
2023-01-22 00:02:46 -03:00
|
|
|
return refresh_button
|