2023-04-05 19:05:50 +02:00
import os
2023-04-21 22:20:59 +02:00
import warnings
2023-05-22 03:42:34 +02:00
from modules . logging_colors import logger
2023-07-07 07:24:52 +02:00
from modules . block_requests import OpenMonkeyPatch , RequestBlocker
2023-04-21 22:18:34 +02:00
2023-04-05 19:05:50 +02:00
os . environ [ ' GRADIO_ANALYTICS_ENABLED ' ] = ' False '
2023-04-13 17:04:39 +02:00
os . environ [ ' BITSANDBYTES_NOWELCOME ' ] = ' 1 '
2023-04-13 17:09:42 +02:00
warnings . filterwarnings ( ' ignore ' , category = UserWarning , message = ' TypedStorage is deprecated ' )
2023-05-22 03:42:34 +02:00
2023-06-18 21:31:14 +02:00
with RequestBlocker ( ) :
import gradio as gr
2023-04-05 19:05:50 +02:00
2023-04-19 06:43:10 +02:00
import matplotlib
2023-05-04 02:43:17 +02:00
matplotlib . use ( ' Agg ' ) # This fixes LaTeX rendering on some systems
2023-04-19 06:43:10 +02:00
2023-04-10 16:36:39 +02:00
import importlib
2023-02-10 19:40:03 +01:00
import json
2023-04-12 22:09:56 +02:00
import math
2023-04-10 16:36:39 +02:00
import os
2023-02-10 19:40:03 +01:00
import re
2023-04-13 02:24:26 +02:00
import sys
2023-02-10 19:40:03 +01:00
import time
2023-04-10 16:36:39 +02:00
import traceback
2023-04-24 08:05:47 +02:00
from functools import partial
2023-01-07 20:33:43 +01:00
from pathlib import Path
2023-05-24 14:38:20 +02:00
from threading import Lock
2023-04-10 16:36:39 +02:00
2023-04-12 22:09:56 +02:00
import psutil
import torch
2023-04-15 17:54:02 +02:00
import yaml
2023-04-05 03:28:49 +02:00
from PIL import Image
2023-05-08 04:47:02 +02:00
2023-02-23 16:05:25 +01:00
import modules . extensions as extensions_module
2023-06-17 00:00:37 +02:00
from modules import chat , loaders , presets , shared , training , ui , utils
2023-05-17 05:03:39 +02:00
from modules . extensions import apply_extensions
2023-06-11 22:11:06 +02:00
from modules . github import clone_or_pull_repository
2023-04-05 16:49:59 +02:00
from modules . html_generator import chat_html_wrapper
2023-03-17 15:42:25 +01:00
from modules . LoRA import add_lora_to_model
2023-06-06 12:42:23 +02:00
from modules . models import load_model , unload_model
2023-06-25 06:44:36 +02:00
from modules . models_settings import (
apply_model_settings_to_state ,
get_model_settings_from_yamls ,
save_model_settings ,
update_model_parameters
)
from modules . text_generation import (
generate_reply_wrapper ,
get_encoded_length ,
stop_everything_event
)
2023-07-04 05:03:30 +02:00
from modules . utils import gradio
2023-03-17 01:31:39 +01:00
2023-04-07 05:15:45 +02:00
2023-06-17 00:00:37 +02:00
def load_model_wrapper ( selected_model , loader , autoload = False ) :
2023-05-09 20:52:35 +02:00
if not autoload :
2023-07-12 03:29:20 +02:00
yield f " The settings for { selected_model } have been updated. \n Click on \" Load \" to load it. "
2023-05-09 20:52:35 +02:00
return
2023-02-08 02:08:21 +01:00
2023-05-09 20:52:35 +02:00
if selected_model == ' None ' :
yield " No model selected "
else :
try :
yield f " Loading { selected_model } ... "
shared . model_name = selected_model
unload_model ( )
if selected_model != ' ' :
2023-06-17 00:00:37 +02:00
shared . model , shared . tokenizer = load_model ( shared . model_name , loader )
2023-05-09 20:52:35 +02:00
2023-06-17 00:00:37 +02:00
if shared . model is not None :
yield f " Successfully loaded { selected_model } "
else :
yield f " Failed to load { selected_model } . "
2023-05-09 20:52:35 +02:00
except :
2023-06-17 01:35:38 +02:00
exc = traceback . format_exc ( )
logger . error ( ' Failed to load the model. ' )
print ( exc )
yield exc
2023-02-12 13:36:27 +01:00
2023-04-07 05:15:45 +02:00
2023-04-14 19:52:06 +02:00
def load_lora_wrapper ( selected_loras ) :
yield ( " Applying the following LoRAs to {} : \n \n {} " . format ( shared . model_name , ' \n ' . join ( selected_loras ) ) )
add_lora_to_model ( selected_loras )
yield ( " Successfuly applied the LoRAs " )
2023-03-17 01:31:39 +01:00
2023-04-07 05:15:45 +02:00
2023-03-27 17:16:37 +02:00
def load_prompt ( fname ) :
if fname in [ ' None ' , ' ' ] :
return ' '
2023-05-10 06:34:04 +02:00
elif fname . startswith ( ' Instruct- ' ) :
fname = re . sub ( ' ^Instruct- ' , ' ' , fname )
2023-06-17 22:57:56 +02:00
file_path = Path ( f ' characters/instruction-following/ { fname } .yaml ' )
if not file_path . exists ( ) :
return ' '
with open ( file_path , ' r ' , encoding = ' utf-8 ' ) as f :
2023-05-10 06:34:04 +02:00
data = yaml . safe_load ( f )
output = ' '
if ' context ' in data :
output + = data [ ' context ' ]
replacements = {
' <|user|> ' : data [ ' user ' ] ,
' <|bot|> ' : data [ ' bot ' ] ,
' <|user-message|> ' : ' Input ' ,
}
output + = utils . replace_all ( data [ ' turn_template ' ] . split ( ' <|bot-message|> ' ) [ 0 ] , replacements )
2023-05-12 19:19:55 +02:00
return output . rstrip ( ' ' )
2023-03-27 17:16:37 +02:00
else :
2023-06-17 22:57:56 +02:00
file_path = Path ( f ' prompts/ { fname } .txt ' )
if not file_path . exists ( ) :
return ' '
with open ( file_path , ' r ' , encoding = ' utf-8 ' ) as f :
2023-03-30 03:40:04 +02:00
text = f . read ( )
if text [ - 1 ] == ' \n ' :
text = text [ : - 1 ]
2023-05-12 19:19:55 +02:00
2023-03-30 03:40:04 +02:00
return text
2023-04-05 16:49:59 +02:00
2023-04-07 05:15:45 +02:00
2023-04-21 22:18:34 +02:00
def count_tokens ( text ) :
2023-06-11 17:19:18 +02:00
try :
tokens = get_encoded_length ( text )
return f ' { tokens } tokens in the input. '
except :
return ' Couldn \' t count the number of tokens. Is a tokenizer loaded? '
2023-04-21 22:18:34 +02:00
2023-06-21 03:59:14 +02:00
def download_model_wrapper ( repo_id , progress = gr . Progress ( ) ) :
2023-04-10 16:36:39 +02:00
try :
2023-06-01 05:11:21 +02:00
downloader_module = importlib . import_module ( " download-model " )
downloader = downloader_module . ModelDownloader ( )
2023-05-05 18:59:01 +02:00
repo_id_parts = repo_id . split ( " : " )
model = repo_id_parts [ 0 ] if len ( repo_id_parts ) > 0 else repo_id
branch = repo_id_parts [ 1 ] if len ( repo_id_parts ) > 1 else " main "
2023-04-10 16:36:39 +02:00
check = False
2023-06-21 03:59:14 +02:00
progress ( 0.0 )
2023-04-11 16:46:30 +02:00
yield ( " Cleaning up the model/branch names " )
2023-04-10 16:36:39 +02:00
model , branch = downloader . sanitize_model_and_branch_names ( model , branch )
2023-04-11 16:46:30 +02:00
yield ( " Getting the download links from Hugging Face " )
2023-04-10 16:36:39 +02:00
links , sha256 , is_lora = downloader . get_download_links_from_huggingface ( model , branch , text_only = False )
2023-04-11 16:46:30 +02:00
yield ( " Getting the output folder " )
2023-07-12 23:19:33 +02:00
base_folder = shared . args . lora_dir if is_lora else shared . args . model_dir
output_folder = downloader . get_output_folder ( model , branch , is_lora , base_folder = base_folder )
2023-04-10 16:36:39 +02:00
if check :
2023-06-21 03:59:14 +02:00
progress ( 0.5 )
2023-04-11 16:46:30 +02:00
yield ( " Checking previously downloaded files " )
2023-04-10 16:36:39 +02:00
downloader . check_model_files ( model , branch , links , sha256 , output_folder )
2023-06-21 03:59:14 +02:00
progress ( 1.0 )
2023-04-10 16:36:39 +02:00
else :
2023-04-11 16:46:30 +02:00
yield ( f " Downloading files to { output_folder } " )
2023-06-21 03:59:14 +02:00
downloader . download_model_files ( model , branch , links , sha256 , output_folder , progress_bar = progress , threads = 1 )
2023-04-11 16:46:30 +02:00
yield ( " Done! " )
2023-04-10 16:36:39 +02:00
except :
2023-06-21 03:59:14 +02:00
progress ( 1.0 )
2023-04-10 16:36:39 +02:00
yield traceback . format_exc ( )
2023-04-09 13:11:43 +02:00
2023-04-12 23:26:15 +02:00
def create_model_menus ( ) :
2023-04-12 22:09:56 +02:00
# Finding the default values for the GPU and CPU memories
2023-04-12 23:21:14 +02:00
total_mem = [ ]
for i in range ( torch . cuda . device_count ( ) ) :
2023-04-13 16:44:10 +02:00
total_mem . append ( math . floor ( torch . cuda . get_device_properties ( i ) . total_memory / ( 1024 * 1024 ) ) )
2023-04-12 23:21:14 +02:00
default_gpu_mem = [ ]
2023-04-12 22:09:56 +02:00
if shared . args . gpu_memory is not None and len ( shared . args . gpu_memory ) > 0 :
2023-04-12 23:21:14 +02:00
for i in shared . args . gpu_memory :
if ' mib ' in i . lower ( ) :
default_gpu_mem . append ( int ( re . sub ( ' [a-zA-Z ] ' , ' ' , i ) ) )
else :
2023-04-13 16:44:10 +02:00
default_gpu_mem . append ( int ( re . sub ( ' [a-zA-Z ] ' , ' ' , i ) ) * 1000 )
2023-04-12 23:21:14 +02:00
while len ( default_gpu_mem ) < len ( total_mem ) :
default_gpu_mem . append ( 0 )
2023-04-13 16:44:10 +02:00
total_cpu_mem = math . floor ( psutil . virtual_memory ( ) . total / ( 1024 * 1024 ) )
2023-04-12 22:09:56 +02:00
if shared . args . cpu_memory is not None :
default_cpu_mem = re . sub ( ' [a-zA-Z ] ' , ' ' , shared . args . cpu_memory )
else :
default_cpu_mem = 0
2023-04-06 06:54:05 +02:00
with gr . Row ( ) :
with gr . Column ( ) :
with gr . Row ( ) :
2023-04-12 22:09:56 +02:00
with gr . Column ( ) :
with gr . Row ( ) :
2023-06-11 19:20:16 +02:00
shared . gradio [ ' model_menu ' ] = gr . Dropdown ( choices = utils . get_available_models ( ) , value = shared . model_name , label = ' Model ' , elem_classes = ' slim-dropdown ' )
2023-05-06 04:14:56 +02:00
ui . create_refresh_button ( shared . gradio [ ' model_menu ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_models ( ) } , ' refresh-button ' )
2023-06-12 06:24:44 +02:00
load = gr . Button ( " Load " , visible = not shared . settings [ ' autoload_model ' ] , elem_classes = ' refresh-button ' )
2023-06-11 19:20:16 +02:00
unload = gr . Button ( " Unload " , elem_classes = ' refresh-button ' )
reload = gr . Button ( " Reload " , elem_classes = ' refresh-button ' )
save_settings = gr . Button ( " Save settings " , elem_classes = ' refresh-button ' )
2023-04-12 22:09:56 +02:00
with gr . Column ( ) :
with gr . Row ( ) :
2023-06-11 19:20:16 +02:00
shared . gradio [ ' lora_menu ' ] = gr . Dropdown ( multiselect = True , choices = utils . get_available_loras ( ) , value = shared . lora_names , label = ' LoRA(s) ' , elem_classes = ' slim-dropdown ' )
2023-05-06 04:14:56 +02:00
ui . create_refresh_button ( shared . gradio [ ' lora_menu ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_loras ( ) , ' value ' : shared . lora_names } , ' refresh-button ' )
2023-06-11 19:20:16 +02:00
shared . gradio [ ' lora_menu_apply ' ] = gr . Button ( value = ' Apply LoRAs ' , elem_classes = ' refresh-button ' )
2023-04-12 22:09:56 +02:00
2023-04-09 13:11:43 +02:00
with gr . Row ( ) :
2023-04-10 16:36:39 +02:00
with gr . Column ( ) :
2023-07-16 07:29:23 +02:00
shared . gradio [ ' loader ' ] = gr . Dropdown ( label = " Model loader " , choices = [ " Transformers " , " ExLlama_HF " , " ExLlama " , " AutoGPTQ " , " GPTQ-for-LLaMa " , " llama.cpp " , " llamacpp_HF " ] , value = None )
2023-04-12 22:09:56 +02:00
with gr . Box ( ) :
with gr . Row ( ) :
with gr . Column ( ) :
2023-04-12 23:21:14 +02:00
for i in range ( len ( total_mem ) ) :
2023-04-14 16:07:28 +02:00
shared . gradio [ f ' gpu_memory_ { i } ' ] = gr . Slider ( label = f " gpu-memory in MiB for device : { i } " , maximum = total_mem [ i ] , value = default_gpu_mem [ i ] )
2023-05-25 06:14:13 +02:00
2023-04-14 16:07:28 +02:00
shared . gradio [ ' cpu_memory ' ] = gr . Slider ( label = " cpu-memory in MiB " , maximum = total_cpu_mem , value = default_cpu_mem )
2023-06-17 00:07:16 +02:00
shared . gradio [ ' transformers_info ' ] = gr . Markdown ( ' load-in-4bit params: ' )
2023-05-25 06:14:13 +02:00
shared . gradio [ ' compute_dtype ' ] = gr . Dropdown ( label = " compute_dtype " , choices = [ " bfloat16 " , " float16 " , " float32 " ] , value = shared . args . compute_dtype )
shared . gradio [ ' quant_type ' ] = gr . Dropdown ( label = " quant_type " , choices = [ " nf4 " , " fp4 " ] , value = shared . args . quant_type )
2023-07-26 05:45:20 +02:00
2023-05-16 00:51:23 +02:00
shared . gradio [ ' n_gpu_layers ' ] = gr . Slider ( label = " n-gpu-layers " , minimum = 0 , maximum = 128 , value = shared . args . n_gpu_layers )
2023-06-26 03:49:26 +02:00
shared . gradio [ ' n_ctx ' ] = gr . Slider ( minimum = 0 , maximum = 16384 , step = 256 , label = " n_ctx " , value = shared . args . n_ctx )
2023-07-26 05:45:20 +02:00
shared . gradio [ ' threads ' ] = gr . Slider ( label = " threads " , minimum = 0 , step = 1 , maximum = 32 , value = shared . args . threads )
shared . gradio [ ' n_batch ' ] = gr . Slider ( label = " n_batch " , minimum = 1 , maximum = 2048 , value = shared . args . n_batch )
2023-08-01 15:10:09 +02:00
shared . gradio [ ' n_gqa ' ] = gr . Slider ( minimum = 0 , maximum = 16 , step = 1 , label = " n_gqa " , value = shared . args . n_gqa , info = ' grouped-query attention. Must be 8 for llama-2 70b. ' )
shared . gradio [ ' rms_norm_eps ' ] = gr . Slider ( minimum = 0 , maximum = 1e-5 , step = 1e-6 , label = " rms_norm_eps " , value = shared . args . n_gqa , info = ' 5e-6 is a good value for llama-2 models. ' )
2023-07-26 05:45:20 +02:00
2023-07-12 04:27:38 +02:00
shared . gradio [ ' wbits ' ] = gr . Dropdown ( label = " wbits " , choices = [ " None " , 1 , 2 , 3 , 4 , 8 ] , value = str ( shared . args . wbits ) if shared . args . wbits > 0 else " None " )
shared . gradio [ ' groupsize ' ] = gr . Dropdown ( label = " groupsize " , choices = [ " None " , 32 , 64 , 128 , 1024 ] , value = str ( shared . args . groupsize ) if shared . args . groupsize > 0 else " None " )
2023-06-17 00:00:37 +02:00
shared . gradio [ ' model_type ' ] = gr . Dropdown ( label = " model_type " , choices = [ " None " , " llama " , " opt " , " gptj " ] , value = shared . args . model_type or " None " )
shared . gradio [ ' pre_layer ' ] = gr . Slider ( label = " pre_layer " , minimum = 0 , maximum = 100 , value = shared . args . pre_layer [ 0 ] if shared . args . pre_layer is not None else 0 )
2023-07-16 07:29:23 +02:00
shared . gradio [ ' autogptq_info ' ] = gr . Markdown ( ' * ExLlama_HF is recommended over AutoGPTQ for models derived from LLaMA. ' )
2023-06-17 02:01:57 +02:00
shared . gradio [ ' gpu_split ' ] = gr . Textbox ( label = ' gpu-split ' , info = ' Comma-separated list of VRAM (in GB) to use per GPU. Example: 20,7,7 ' )
2023-06-26 03:55:43 +02:00
shared . gradio [ ' max_seq_len ' ] = gr . Slider ( label = ' max_seq_len ' , minimum = 2048 , maximum = 16384 , step = 256 , info = ' Maximum sequence length. ' , value = shared . args . max_seq_len )
shared . gradio [ ' compress_pos_emb ' ] = gr . Slider ( label = ' compress_pos_emb ' , minimum = 1 , maximum = 8 , step = 1 , info = ' Positional embeddings compression factor. Should typically be set to max_seq_len / 2048. ' , value = shared . args . compress_pos_emb )
2023-07-16 06:56:04 +02:00
shared . gradio [ ' alpha_value ' ] = gr . Slider ( label = ' alpha_value ' , minimum = 1 , maximum = 32 , step = 1 , info = ' Positional embeddings alpha factor for NTK RoPE scaling. Scaling is not identical to embedding compression. Use either this or compress_pos_emb, not both. ' , value = shared . args . alpha_value )
2023-05-16 00:51:23 +02:00
with gr . Column ( ) :
2023-06-17 00:00:37 +02:00
shared . gradio [ ' triton ' ] = gr . Checkbox ( label = " triton " , value = shared . args . triton )
shared . gradio [ ' no_inject_fused_attention ' ] = gr . Checkbox ( label = " no_inject_fused_attention " , value = shared . args . no_inject_fused_attention , info = ' Disable fused attention. Fused attention improves inference performance but uses more VRAM. Disable if running low on VRAM. ' )
shared . gradio [ ' no_inject_fused_mlp ' ] = gr . Checkbox ( label = " no_inject_fused_mlp " , value = shared . args . no_inject_fused_mlp , info = ' Affects Triton only. Disable fused MLP. Fused MLP improves performance but uses more VRAM. Disable if running low on VRAM. ' )
2023-06-23 17:22:56 +02:00
shared . gradio [ ' no_use_cuda_fp16 ' ] = gr . Checkbox ( label = " no_use_cuda_fp16 " , value = shared . args . no_use_cuda_fp16 , info = ' This can make models faster on some systems. ' )
2023-06-17 00:00:37 +02:00
shared . gradio [ ' desc_act ' ] = gr . Checkbox ( label = " desc_act " , value = shared . args . desc_act , info = ' \' desc_act \' , \' wbits \' , and \' groupsize \' are used for old models without a quantize_config.json. ' )
2023-06-17 00:07:16 +02:00
shared . gradio [ ' cpu ' ] = gr . Checkbox ( label = " cpu " , value = shared . args . cpu )
2023-06-17 00:00:37 +02:00
shared . gradio [ ' load_in_8bit ' ] = gr . Checkbox ( label = " load-in-8bit " , value = shared . args . load_in_8bit )
shared . gradio [ ' bf16 ' ] = gr . Checkbox ( label = " bf16 " , value = shared . args . bf16 )
shared . gradio [ ' auto_devices ' ] = gr . Checkbox ( label = " auto-devices " , value = shared . args . auto_devices )
shared . gradio [ ' disk ' ] = gr . Checkbox ( label = " disk " , value = shared . args . disk )
2023-06-17 22:57:56 +02:00
shared . gradio [ ' load_in_4bit ' ] = gr . Checkbox ( label = " load-in-4bit " , value = shared . args . load_in_4bit )
2023-06-17 00:00:37 +02:00
shared . gradio [ ' use_double_quant ' ] = gr . Checkbox ( label = " use_double_quant " , value = shared . args . use_double_quant )
2023-05-16 22:35:49 +02:00
shared . gradio [ ' no_mmap ' ] = gr . Checkbox ( label = " no-mmap " , value = shared . args . no_mmap )
2023-07-12 16:05:13 +02:00
shared . gradio [ ' low_vram ' ] = gr . Checkbox ( label = " low-vram " , value = shared . args . low_vram )
2023-05-16 00:51:23 +02:00
shared . gradio [ ' mlock ' ] = gr . Checkbox ( label = " mlock " , value = shared . args . mlock )
2023-05-25 15:29:31 +02:00
shared . gradio [ ' llama_cpp_seed ' ] = gr . Number ( label = ' Seed (0 for random) ' , value = shared . args . llama_cpp_seed )
2023-06-17 00:00:37 +02:00
shared . gradio [ ' trust_remote_code ' ] = gr . Checkbox ( label = " trust-remote-code " , value = shared . args . trust_remote_code , info = ' Make sure to inspect the .py files inside the model folder before loading it with this option enabled. ' )
shared . gradio [ ' gptq_for_llama_info ' ] = gr . Markdown ( ' GPTQ-for-LLaMa is currently 2x faster than AutoGPTQ on some systems. It is installed by default with the one-click installers. Otherwise, it has to be installed manually following the instructions here: [instructions](https://github.com/oobabooga/text-generation-webui/blob/main/docs/GPTQ-models-(4-bit-mode).md#installation-1). ' )
2023-06-25 01:24:17 +02:00
shared . gradio [ ' exllama_info ' ] = gr . Markdown ( ' For more information, consult the [docs](https://github.com/oobabooga/text-generation-webui/blob/main/docs/ExLlama.md). ' )
2023-06-26 05:10:13 +02:00
shared . gradio [ ' exllama_HF_info ' ] = gr . Markdown ( ' ExLlama_HF is a wrapper that lets you use ExLlama like a Transformers model, which means it can use the Transformers samplers. It \' s a bit slower than the regular ExLlama. ' )
2023-07-16 07:21:13 +02:00
shared . gradio [ ' llamacpp_HF_info ' ] = gr . Markdown ( ' llamacpp_HF is a wrapper that lets you use llama.cpp like a Transformers model, which means it can use the Transformers samplers. It works, but it \' s experimental and slow. Contributions are welcome. \n \n To use it, make sure to first download oobabooga/llama-tokenizer under " Download custom model or LoRA " . ' )
2023-06-17 00:00:37 +02:00
with gr . Column ( ) :
with gr . Row ( ) :
shared . gradio [ ' autoload_model ' ] = gr . Checkbox ( value = shared . settings [ ' autoload_model ' ] , label = ' Autoload the model ' , info = ' Whether to load the model as soon as it is selected in the Model dropdown. ' )
shared . gradio [ ' custom_model_menu ' ] = gr . Textbox ( label = " Download custom model or LoRA " , info = " Enter the Hugging Face username/model path, for instance: facebook/galactica-125m. To specify a branch, add it at the end after a \" : \" character like this: facebook/galactica-125m:main " )
shared . gradio [ ' download_model_button ' ] = gr . Button ( " Download " )
2023-05-16 00:51:23 +02:00
2023-05-22 03:42:34 +02:00
with gr . Row ( ) :
2023-05-16 00:51:23 +02:00
shared . gradio [ ' model_status ' ] = gr . Markdown ( ' No model is loaded ' if shared . model_name == ' None ' else ' Ready ' )
2023-04-12 22:09:56 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' loader ' ] . change ( loaders . make_loader_params_visible , gradio ( ' loader ' ) , gradio ( loaders . get_all_params ( ) ) )
2023-06-17 00:00:37 +02:00
2023-04-14 16:07:28 +02:00
# In this event handler, the interface state is read and updated
# with the model defaults (if any), and then the model is loaded
2023-05-09 20:52:35 +02:00
# unless "autoload_model" is unchecked
2023-04-12 22:09:56 +02:00
shared . gradio [ ' model_menu ' ] . change (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
apply_model_settings_to_state , gradio ( ' model_menu ' , ' interface_state ' ) , gradio ( ' interface_state ' ) ) . then (
ui . apply_interface_values , gradio ( ' interface_state ' ) , gradio ( ui . list_interface_input_elements ( ) ) , show_progress = False ) . then (
update_model_parameters , gradio ( ' interface_state ' ) , None ) . then (
load_model_wrapper , gradio ( ' model_menu ' , ' loader ' , ' autoload_model ' ) , gradio ( ' model_status ' ) , show_progress = False )
2023-05-09 20:52:35 +02:00
load . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
update_model_parameters , gradio ( ' interface_state ' ) , None ) . then (
2023-07-17 21:50:25 +02:00
partial ( load_model_wrapper , autoload = True ) , gradio ( ' model_menu ' , ' loader ' ) , gradio ( ' model_status ' ) , show_progress = False )
2023-04-12 22:09:56 +02:00
unload . click (
unload_model , None , None ) . then (
2023-07-17 21:50:25 +02:00
lambda : " Model unloaded " , None , gradio ( ' model_status ' ) )
2023-04-12 22:09:56 +02:00
reload . click (
unload_model , None , None ) . then (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
update_model_parameters , gradio ( ' interface_state ' ) , None ) . then (
2023-07-17 21:50:25 +02:00
partial ( load_model_wrapper , autoload = True ) , gradio ( ' model_menu ' , ' loader ' ) , gradio ( ' model_status ' ) , show_progress = False )
2023-04-15 17:54:02 +02:00
save_settings . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
save_model_settings , gradio ( ' model_menu ' , ' interface_state ' ) , gradio ( ' model_status ' ) , show_progress = False )
2023-04-06 06:54:05 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' lora_menu_apply ' ] . click ( load_lora_wrapper , gradio ( ' lora_menu ' ) , gradio ( ' model_status ' ) , show_progress = False )
shared . gradio [ ' download_model_button ' ] . click ( download_model_wrapper , gradio ( ' custom_model_menu ' ) , gradio ( ' model_status ' ) , show_progress = True )
shared . gradio [ ' autoload_model ' ] . change ( lambda x : gr . update ( visible = not x ) , gradio ( ' autoload_model ' ) , load )
2023-04-06 06:54:05 +02:00
2023-04-07 05:15:45 +02:00
2023-06-06 12:46:25 +02:00
def create_chat_settings_menus ( ) :
if not shared . is_chat ( ) :
return
with gr . Box ( ) :
gr . Markdown ( " Chat parameters " )
with gr . Row ( ) :
with gr . Column ( ) :
shared . gradio [ ' max_new_tokens ' ] = gr . Slider ( minimum = shared . settings [ ' max_new_tokens_min ' ] , maximum = shared . settings [ ' max_new_tokens_max ' ] , step = 1 , label = ' max_new_tokens ' , value = shared . settings [ ' max_new_tokens ' ] )
2023-06-26 03:49:26 +02:00
shared . gradio [ ' chat_generation_attempts ' ] = gr . Slider ( minimum = shared . settings [ ' chat_generation_attempts_min ' ] , maximum = shared . settings [ ' chat_generation_attempts_max ' ] , value = shared . settings [ ' chat_generation_attempts ' ] , step = 1 , label = ' Generation attempts (for longer replies) ' , info = ' New generations will be called until either this number is reached or no new content is generated between two iterations. ' )
2023-06-06 12:46:25 +02:00
with gr . Column ( ) :
shared . gradio [ ' stop_at_newline ' ] = gr . Checkbox ( value = shared . settings [ ' stop_at_newline ' ] , label = ' Stop generating at new line character ' )
2023-04-11 17:30:06 +02:00
2023-03-15 16:01:32 +01:00
2023-06-06 12:46:25 +02:00
def create_settings_menus ( default_preset ) :
2023-06-14 01:34:35 +02:00
generate_params = presets . load_preset ( default_preset )
2023-03-27 17:16:37 +02:00
with gr . Row ( ) :
with gr . Column ( ) :
2023-04-06 06:54:05 +02:00
with gr . Row ( ) :
2023-07-26 00:15:29 +02:00
shared . gradio [ ' preset_menu ' ] = gr . Dropdown ( choices = utils . get_available_presets ( ) , value = default_preset , label = ' Generation parameters preset ' , elem_classes = ' slim-dropdown ' )
2023-06-11 17:19:18 +02:00
ui . create_refresh_button ( shared . gradio [ ' preset_menu ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_presets ( ) } , ' refresh-button ' )
2023-06-11 19:20:16 +02:00
shared . gradio [ ' save_preset ' ] = gr . Button ( ' 💾 ' , elem_classes = ' refresh-button ' )
shared . gradio [ ' delete_preset ' ] = gr . Button ( ' 🗑️ ' , elem_classes = ' refresh-button ' )
2023-03-27 17:16:37 +02:00
2023-06-12 00:50:20 +02:00
with gr . Column ( ) :
2023-08-01 03:44:00 +02:00
filter_by_loader = gr . Dropdown ( label = " Filter by loader " , choices = [ " All " , " Transformers " , " ExLlama_HF " , " ExLlama " , " AutoGPTQ " , " GPTQ-for-LLaMa " , " llama.cpp " , " llamacpp_HF " ] , value = " All " , elem_classes = ' slim-dropdown ' )
2023-06-12 00:50:20 +02:00
with gr . Row ( ) :
with gr . Column ( ) :
2023-03-15 17:24:54 +01:00
with gr . Box ( ) :
with gr . Row ( ) :
with gr . Column ( ) :
2023-06-29 19:33:47 +02:00
shared . gradio [ ' temperature ' ] = gr . Slider ( 0.01 , 1.99 , value = generate_params [ ' temperature ' ] , step = 0.01 , label = ' temperature ' )
shared . gradio [ ' top_p ' ] = gr . Slider ( 0.0 , 1.0 , value = generate_params [ ' top_p ' ] , step = 0.01 , label = ' top_p ' )
shared . gradio [ ' top_k ' ] = gr . Slider ( 0 , 200 , value = generate_params [ ' top_k ' ] , step = 1 , label = ' top_k ' )
shared . gradio [ ' typical_p ' ] = gr . Slider ( 0.0 , 1.0 , value = generate_params [ ' typical_p ' ] , step = 0.01 , label = ' typical_p ' )
shared . gradio [ ' epsilon_cutoff ' ] = gr . Slider ( 0 , 9 , value = generate_params [ ' epsilon_cutoff ' ] , step = 0.01 , label = ' epsilon_cutoff ' )
shared . gradio [ ' eta_cutoff ' ] = gr . Slider ( 0 , 20 , value = generate_params [ ' eta_cutoff ' ] , step = 0.01 , label = ' eta_cutoff ' )
2023-08-01 03:44:00 +02:00
shared . gradio [ ' tfs ' ] = gr . Slider ( 0.0 , 1.0 , value = generate_params [ ' tfs ' ] , step = 0.01 , label = ' tfs ' )
shared . gradio [ ' top_a ' ] = gr . Slider ( 0.0 , 1.0 , value = generate_params [ ' top_a ' ] , step = 0.01 , label = ' top_a ' )
2023-05-23 00:37:24 +02:00
2023-03-15 17:24:54 +01:00
with gr . Column ( ) :
2023-06-29 19:33:47 +02:00
shared . gradio [ ' repetition_penalty ' ] = gr . Slider ( 1.0 , 1.5 , value = generate_params [ ' repetition_penalty ' ] , step = 0.01 , label = ' repetition_penalty ' )
shared . gradio [ ' repetition_penalty_range ' ] = gr . Slider ( 0 , 4096 , step = 64 , value = generate_params [ ' repetition_penalty_range ' ] , label = ' repetition_penalty_range ' )
shared . gradio [ ' encoder_repetition_penalty ' ] = gr . Slider ( 0.8 , 1.5 , value = generate_params [ ' encoder_repetition_penalty ' ] , step = 0.01 , label = ' encoder_repetition_penalty ' )
shared . gradio [ ' no_repeat_ngram_size ' ] = gr . Slider ( 0 , 20 , step = 1 , value = generate_params [ ' no_repeat_ngram_size ' ] , label = ' no_repeat_ngram_size ' )
shared . gradio [ ' min_length ' ] = gr . Slider ( 0 , 2000 , step = 1 , value = generate_params [ ' min_length ' ] , label = ' min_length ' )
2023-08-01 03:44:00 +02:00
shared . gradio [ ' seed ' ] = gr . Number ( value = shared . settings [ ' seed ' ] , label = ' Seed (-1 for random) ' )
2023-06-11 17:19:18 +02:00
shared . gradio [ ' do_sample ' ] = gr . Checkbox ( value = generate_params [ ' do_sample ' ] , label = ' do_sample ' )
2023-06-29 19:33:47 +02:00
with gr . Accordion ( " Learn more " , open = False ) :
gr . Markdown ( """
For a technical description of the parameters , the [ transformers documentation ] ( https : / / huggingface . co / docs / transformers / main_classes / text_generation #transformers.GenerationConfig) is a good reference.
2023-07-04 07:39:01 +02:00
The best presets , according to the [ Preset Arena ] ( https : / / github . com / oobabooga / oobabooga . github . io / blob / main / arena / results . md ) experiment , are :
* Instruction following :
1 ) Divine Intellect
2 ) Big O
3 ) simple - 1
4 ) Space Alien
5 ) StarChat
6 ) Titanic
7 ) tfs - with - top - a
8 ) Asterism
9 ) Contrastive Search
* Chat :
1 ) Midnight Enigma
2 ) Yara
3 ) Shortwave
2023-06-29 19:33:47 +02:00
### Temperature
Primary factor to control randomness of outputs . 0 = deterministic ( only the most likely token is used ) . Higher value = more randomness .
### top_p
If not set to 1 , select tokens with probabilities adding up to less than this number . Higher value = higher range of possible random results .
### top_k
Similar to top_p , but select instead only the top_k most likely tokens . Higher value = higher range of possible random results .
### typical_p
If not set to 1 , select only tokens that are at least this much more likely to appear than random tokens , given the prior text .
### epsilon_cutoff
In units of 1e-4 ; a reasonable value is 3. This sets a probability floor below which tokens are excluded from being sampled . Should be used with top_p , top_k , and eta_cutoff set to 0.
### eta_cutoff
In units of 1e-4 ; a reasonable value is 3. Should be used with top_p , top_k , and epsilon_cutoff set to 0.
### repetition_penalty
Exponential penalty factor for repeating prior tokens . 1 means no penalty , higher value = less repetition , lower value = more repetition .
### repetition_penalty_range
The number of most recent tokens to consider for repetition penalty . 0 makes all tokens be used .
### encoder_repetition_penalty
Also known as the " Hallucinations filter " . Used to penalize tokens that are * not * in the prior text . Higher value = more likely to stay in context , lower value = more likely to diverge .
### no_repeat_ngram_size
If not set to 0 , specifies the length of token sets that are completely blocked from repeating at all . Higher values = blocks larger phrases , lower values = blocks words or letters from repeating . Only 0 or high values are a good idea in most cases .
### min_length
Minimum generation length in tokens .
### penalty_alpha
Contrastive Search is enabled by setting this to greater than zero and unchecking " do_sample " . It should be used with a low value of top_k , for instance , top_k = 4.
""" , elem_classes= " markdown " )
2023-05-23 00:37:24 +02:00
2023-03-15 17:24:54 +01:00
with gr . Column ( ) :
2023-06-06 12:46:25 +02:00
create_chat_settings_menus ( )
2023-03-15 17:24:54 +01:00
with gr . Box ( ) :
with gr . Row ( ) :
with gr . Column ( ) :
2023-08-01 04:55:11 +02:00
shared . gradio [ ' mirostat_mode ' ] = gr . Slider ( 0 , 2 , step = 1 , value = generate_params [ ' mirostat_mode ' ] , label = ' mirostat_mode ' , info = ' mode=1 is for llama.cpp only. ' )
shared . gradio [ ' mirostat_tau ' ] = gr . Slider ( 0 , 10 , step = 0.01 , value = generate_params [ ' mirostat_tau ' ] , label = ' mirostat_tau ' )
shared . gradio [ ' mirostat_eta ' ] = gr . Slider ( 0 , 1 , step = 0.01 , value = generate_params [ ' mirostat_eta ' ] , label = ' mirostat_eta ' )
with gr . Column ( ) :
shared . gradio [ ' penalty_alpha ' ] = gr . Slider ( 0 , 5 , value = generate_params [ ' penalty_alpha ' ] , label = ' penalty_alpha ' , info = ' For Contrastive Search. do_sample must be unchecked. ' )
2023-05-21 20:11:57 +02:00
2023-08-01 05:06:10 +02:00
shared . gradio [ ' num_beams ' ] = gr . Slider ( 1 , 20 , step = 1 , value = generate_params [ ' num_beams ' ] , label = ' num_beams ' , info = ' For Beam Search, along with length_penalty and early_stopping. ' )
2023-03-15 17:24:54 +01:00
shared . gradio [ ' length_penalty ' ] = gr . Slider ( - 5 , 5 , value = generate_params [ ' length_penalty ' ] , label = ' length_penalty ' )
2023-04-19 04:36:23 +02:00
shared . gradio [ ' early_stopping ' ] = gr . Checkbox ( value = generate_params [ ' early_stopping ' ] , label = ' early_stopping ' )
2023-05-23 00:37:24 +02:00
2023-04-17 05:33:22 +02:00
with gr . Box ( ) :
2023-04-11 23:46:06 +02:00
with gr . Row ( ) :
2023-04-17 05:33:22 +02:00
with gr . Column ( ) :
2023-06-26 03:49:26 +02:00
shared . gradio [ ' truncation_length ' ] = gr . Slider ( value = shared . settings [ ' truncation_length ' ] , minimum = shared . settings [ ' truncation_length_min ' ] , maximum = shared . settings [ ' truncation_length_max ' ] , step = 256 , label = ' Truncate the prompt up to this length ' , info = ' The leftmost tokens are removed if the prompt exceeds this length. Most models require this to be at most 2048. ' )
2023-04-17 05:33:22 +02:00
shared . gradio [ ' custom_stopping_strings ' ] = gr . Textbox ( lines = 1 , value = shared . settings [ " custom_stopping_strings " ] or None , label = ' Custom stopping strings ' , info = ' In addition to the defaults. Written between " " and separated by commas. For instance: " \\ nYour Assistant: " , " \\ nThe assistant: " ' )
with gr . Column ( ) :
shared . gradio [ ' ban_eos_token ' ] = gr . Checkbox ( value = shared . settings [ ' ban_eos_token ' ] , label = ' Ban the eos_token ' , info = ' Forces the model to never end the generation prematurely. ' )
2023-05-17 00:52:22 +02:00
shared . gradio [ ' add_bos_token ' ] = gr . Checkbox ( value = shared . settings [ ' add_bos_token ' ] , label = ' Add the bos_token to the beginning of prompts ' , info = ' Disabling this can make the replies more creative. ' )
2023-04-16 19:24:49 +02:00
2023-04-17 05:33:22 +02:00
shared . gradio [ ' skip_special_tokens ' ] = gr . Checkbox ( value = shared . settings [ ' skip_special_tokens ' ] , label = ' Skip special tokens ' , info = ' Some specific models need this unset. ' )
2023-05-05 23:53:03 +02:00
shared . gradio [ ' stream ' ] = gr . Checkbox ( value = not shared . args . no_stream , label = ' Activate text streaming ' )
2023-03-15 17:24:54 +01:00
2023-08-01 03:44:00 +02:00
filter_by_loader . change ( loaders . blacklist_samplers , filter_by_loader , gradio ( loaders . list_all_samplers ( ) ) , show_progress = False )
2023-07-04 05:03:30 +02:00
shared . gradio [ ' preset_menu ' ] . change ( presets . load_preset_for_ui , gradio ( ' preset_menu ' , ' interface_state ' ) , gradio ( ' interface_state ' , ' do_sample ' , ' temperature ' , ' top_p ' , ' typical_p ' , ' epsilon_cutoff ' , ' eta_cutoff ' , ' repetition_penalty ' , ' repetition_penalty_range ' , ' encoder_repetition_penalty ' , ' top_k ' , ' min_length ' , ' no_repeat_ngram_size ' , ' num_beams ' , ' penalty_alpha ' , ' length_penalty ' , ' early_stopping ' , ' mirostat_mode ' , ' mirostat_tau ' , ' mirostat_eta ' , ' tfs ' , ' top_a ' ) )
2023-02-08 02:08:21 +01:00
2023-04-07 05:15:45 +02:00
2023-06-11 17:19:18 +02:00
def create_file_saving_menus ( ) :
# Text file saver
with gr . Box ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' file_saver ' ] :
shared . gradio [ ' save_filename ' ] = gr . Textbox ( lines = 1 , label = ' File name ' )
shared . gradio [ ' save_root ' ] = gr . Textbox ( lines = 1 , label = ' File folder ' , info = ' For reference. Unchangeable. ' , interactive = False )
shared . gradio [ ' save_contents ' ] = gr . Textbox ( lines = 10 , label = ' File contents ' )
with gr . Row ( ) :
shared . gradio [ ' save_confirm ' ] = gr . Button ( ' Save ' , elem_classes = " small-button " )
shared . gradio [ ' save_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
# Text file deleter
with gr . Box ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' file_deleter ' ] :
shared . gradio [ ' delete_filename ' ] = gr . Textbox ( lines = 1 , label = ' File name ' )
shared . gradio [ ' delete_root ' ] = gr . Textbox ( lines = 1 , label = ' File folder ' , info = ' For reference. Unchangeable. ' , interactive = False )
with gr . Row ( ) :
shared . gradio [ ' delete_confirm ' ] = gr . Button ( ' Delete ' , elem_classes = " small-button " , variant = ' stop ' )
shared . gradio [ ' delete_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
# Character saver/deleter
if shared . is_chat ( ) :
with gr . Box ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' character_saver ' ] :
shared . gradio [ ' save_character_filename ' ] = gr . Textbox ( lines = 1 , label = ' File name ' , info = ' The character will be saved to your characters/ folder with this base filename. ' )
with gr . Row ( ) :
shared . gradio [ ' save_character_confirm ' ] = gr . Button ( ' Save ' , elem_classes = " small-button " )
shared . gradio [ ' save_character_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
with gr . Box ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' character_deleter ' ] :
gr . Markdown ( ' Confirm the character deletion? ' )
with gr . Row ( ) :
shared . gradio [ ' delete_character_confirm ' ] = gr . Button ( ' Delete ' , elem_classes = " small-button " , variant = ' stop ' )
shared . gradio [ ' delete_character_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
2023-06-12 02:20:51 +02:00
def create_file_saving_event_handlers ( ) :
shared . gradio [ ' save_confirm ' ] . click (
2023-07-04 05:03:30 +02:00
lambda x , y , z : utils . save_file ( x + y , z ) , gradio ( ' save_root ' , ' save_filename ' , ' save_contents ' ) , None ) . then (
lambda : gr . update ( visible = False ) , None , gradio ( ' file_saver ' ) )
2023-06-12 02:20:51 +02:00
shared . gradio [ ' delete_confirm ' ] . click (
2023-07-04 05:03:30 +02:00
lambda x , y : utils . delete_file ( x + y ) , gradio ( ' delete_root ' , ' delete_filename ' ) , None ) . then (
lambda : gr . update ( visible = False ) , None , gradio ( ' file_deleter ' ) )
2023-06-12 02:20:51 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' delete_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' file_deleter ' ) )
shared . gradio [ ' save_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' file_saver ' ) )
2023-06-12 02:20:51 +02:00
if shared . is_chat ( ) :
2023-06-11 17:19:18 +02:00
shared . gradio [ ' save_character_confirm ' ] . click (
2023-07-04 05:03:30 +02:00
chat . save_character , gradio ( ' name2 ' , ' greeting ' , ' context ' , ' character_picture ' , ' save_character_filename ' ) , None ) . then (
lambda : gr . update ( visible = False ) , None , gradio ( ' character_saver ' ) )
2023-06-11 17:19:18 +02:00
shared . gradio [ ' delete_character_confirm ' ] . click (
2023-07-04 05:03:30 +02:00
chat . delete_character , gradio ( ' character_menu ' ) , None ) . then (
lambda : gr . update ( visible = False ) , None , gradio ( ' character_deleter ' ) ) . then (
lambda : gr . update ( choices = utils . get_available_characters ( ) ) , None , gradio ( ' character_menu ' ) )
2023-06-11 17:19:18 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' save_character_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' character_saver ' ) )
shared . gradio [ ' delete_character_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' character_deleter ' ) )
2023-06-11 17:19:18 +02:00
2023-06-12 02:20:51 +02:00
shared . gradio [ ' save_preset ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
presets . generate_preset_yaml , gradio ( ' interface_state ' ) , gradio ( ' save_contents ' ) ) . then (
lambda : ' presets/ ' , None , gradio ( ' save_root ' ) ) . then (
lambda : ' My Preset.yaml ' , None , gradio ( ' save_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_saver ' ) )
2023-06-12 02:20:51 +02:00
shared . gradio [ ' delete_preset ' ] . click (
2023-07-04 05:03:30 +02:00
lambda x : f ' { x } .yaml ' , gradio ( ' preset_menu ' ) , gradio ( ' delete_filename ' ) ) . then (
lambda : ' presets/ ' , None , gradio ( ' delete_root ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_deleter ' ) )
if not shared . args . multi_user :
def load_session ( session , state ) :
with open ( Path ( f ' logs/ { session } .json ' ) , ' r ' ) as f :
state . update ( json . loads ( f . read ( ) ) )
if shared . is_chat ( ) :
chat . save_persistent_history ( state [ ' history ' ] , state [ ' character_menu ' ] , state [ ' mode ' ] )
return state
if shared . is_chat ( ) :
shared . gradio [ ' save_session ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
lambda x : json . dumps ( x , indent = 4 ) , gradio ( ' interface_state ' ) , gradio ( ' save_contents ' ) ) . then (
lambda : ' logs/ ' , None , gradio ( ' save_root ' ) ) . then (
lambda x : f ' session_ { shared . get_mode ( ) } _ { x + " _ " if x not in [ " None " , None , " " ] else " " } { utils . current_time ( ) } .json ' , gradio ( ' character_menu ' ) , gradio ( ' save_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_saver ' ) )
shared . gradio [ ' session_menu ' ] . change (
load_session , gradio ( ' session_menu ' , ' interface_state ' ) , gradio ( ' interface_state ' ) ) . then (
ui . apply_interface_values , gradio ( ' interface_state ' ) , gradio ( ui . list_interface_input_elements ( ) ) , show_progress = False ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) )
else :
shared . gradio [ ' save_session ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
lambda x : json . dumps ( x , indent = 4 ) , gradio ( ' interface_state ' ) , gradio ( ' save_contents ' ) ) . then (
lambda : ' logs/ ' , None , gradio ( ' save_root ' ) ) . then (
lambda : f ' session_ { shared . get_mode ( ) } _ { utils . current_time ( ) } .json ' , None , gradio ( ' save_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_saver ' ) )
shared . gradio [ ' session_menu ' ] . change (
load_session , gradio ( ' session_menu ' , ' interface_state ' ) , gradio ( ' interface_state ' ) ) . then (
ui . apply_interface_values , gradio ( ' interface_state ' ) , gradio ( ui . list_interface_input_elements ( ) ) , show_progress = False )
shared . gradio [ ' delete_session ' ] . click (
lambda x : f ' { x } .json ' , gradio ( ' session_menu ' ) , gradio ( ' delete_filename ' ) ) . then (
lambda : ' logs/ ' , None , gradio ( ' delete_root ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_deleter ' ) )
2023-06-12 02:20:51 +02:00
2023-06-11 17:19:18 +02:00
2023-04-01 04:41:10 +02:00
def set_interface_arguments ( interface_mode , extensions , bool_active ) :
2023-03-16 16:44:03 +01:00
modes = [ " default " , " notebook " , " chat " , " cai_chat " ]
cmd_list = vars ( shared . args )
2023-04-01 04:41:10 +02:00
bool_list = [ k for k in cmd_list if type ( cmd_list [ k ] ) is bool and k not in modes ]
2023-03-16 16:44:03 +01:00
shared . args . extensions = extensions
for k in modes [ 1 : ] :
2023-04-16 06:36:50 +02:00
setattr ( shared . args , k , False )
2023-03-16 16:44:03 +01:00
if interface_mode != " default " :
2023-04-16 06:36:50 +02:00
setattr ( shared . args , interface_mode , True )
2023-03-16 16:44:03 +01:00
2023-04-01 04:41:10 +02:00
for k in bool_list :
2023-04-16 06:36:50 +02:00
setattr ( shared . args , k , False )
2023-04-01 04:41:10 +02:00
for k in bool_active :
2023-04-16 06:36:50 +02:00
setattr ( shared . args , k , True )
2023-03-16 16:44:03 +01:00
shared . need_restart = True
2023-04-07 05:15:45 +02:00
2023-04-14 16:07:28 +02:00
def create_interface ( ) :
# Defining some variables
gen_events = [ ]
2023-05-29 03:34:12 +02:00
default_preset = shared . settings [ ' preset ' ]
default_text = load_prompt ( shared . settings [ ' prompt ' ] )
2023-04-14 16:07:28 +02:00
title = ' Text generation web UI '
2023-01-19 18:03:47 +01:00
2023-04-14 16:07:28 +02:00
# Authentication variables
auth = None
2023-05-24 01:39:26 +02:00
gradio_auth_creds = [ ]
if shared . args . gradio_auth :
gradio_auth_creds + = [ x . strip ( ) for x in shared . args . gradio_auth . strip ( ' " ' ) . replace ( ' \n ' , ' ' ) . split ( ' , ' ) if x . strip ( ) ]
2023-04-14 16:07:28 +02:00
if shared . args . gradio_auth_path is not None :
with open ( shared . args . gradio_auth_path , ' r ' , encoding = " utf8 " ) as file :
for line in file . readlines ( ) :
gradio_auth_creds + = [ x . strip ( ) for x in line . split ( ' , ' ) if x . strip ( ) ]
2023-05-24 01:39:26 +02:00
if gradio_auth_creds :
2023-04-14 16:07:28 +02:00
auth = [ tuple ( cred . split ( ' : ' ) ) for cred in gradio_auth_creds ]
2023-03-15 16:01:32 +01:00
2023-04-14 16:07:28 +02:00
# Importing the extension files and executing their setup() functions
2023-03-16 03:29:56 +01:00
if shared . args . extensions is not None and len ( shared . args . extensions ) > 0 :
extensions_module . load_extensions ( )
2023-03-15 16:01:32 +01:00
2023-07-26 05:37:01 +02:00
# Forcing some events to be triggered on page load
shared . persistent_interface_state . update ( {
' loader ' : shared . args . loader or ' Transformers ' ,
} )
if shared . is_chat ( ) :
shared . persistent_interface_state . update ( {
' mode ' : shared . settings [ ' mode ' ] ,
' character_menu ' : shared . args . character or shared . settings [ ' character ' ] ,
' instruction_template ' : shared . settings [ ' instruction_template ' ]
} )
if Path ( " cache/pfp_character.png " ) . exists ( ) :
Path ( " cache/pfp_character.png " ) . unlink ( )
2023-05-17 05:03:39 +02:00
# css/js strings
css = ui . css if not shared . is_chat ( ) else ui . css + ui . chat_css
js = ui . main_js if not shared . is_chat ( ) else ui . main_js + ui . chat_js
css + = apply_extensions ( ' css ' )
js + = apply_extensions ( ' js ' )
with gr . Blocks ( css = css , analytics_enabled = False , title = title , theme = ui . theme ) as shared . gradio [ ' interface ' ] :
2023-05-20 04:16:06 +02:00
if Path ( " notification.mp3 " ) . exists ( ) :
shared . gradio [ ' audio_notification ' ] = gr . Audio ( interactive = False , value = " notification.mp3 " , elem_id = " audio_notification " , visible = False )
audio_notification_js = " document.querySelector( ' #audio_notification audio ' )?.play(); "
else :
audio_notification_js = " "
2023-04-11 16:46:30 +02:00
2023-06-12 02:20:51 +02:00
# Floating menus for saving/deleting files
create_file_saving_menus ( )
2023-04-14 16:07:28 +02:00
# Create chat mode interface
if shared . is_chat ( ) :
2023-07-04 05:03:30 +02:00
shared . input_elements = ui . list_interface_input_elements ( )
shared . gradio . update ( {
' interface_state ' : gr . State ( { k : None for k in shared . input_elements } ) ,
' Chat input ' : gr . State ( ) ,
' dummy ' : gr . State ( ) ,
' history ' : gr . State ( { ' internal ' : [ ] , ' visible ' : [ ] } ) ,
} )
2023-04-11 16:46:30 +02:00
2023-04-12 22:09:56 +02:00
with gr . Tab ( ' Text generation ' , elem_id = ' main ' ) :
2023-07-04 05:03:30 +02:00
shared . gradio [ ' display ' ] = gr . HTML ( value = chat_html_wrapper ( { ' internal ' : [ ] , ' visible ' : [ ] } , shared . settings [ ' name1 ' ] , shared . settings [ ' name2 ' ] , ' chat ' , ' cai-chat ' ) )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' textbox ' ] = gr . Textbox ( label = ' Input ' )
with gr . Row ( ) :
2023-04-12 22:09:56 +02:00
shared . gradio [ ' Stop ' ] = gr . Button ( ' Stop ' , elem_id = ' stop ' )
2023-04-19 05:56:24 +02:00
shared . gradio [ ' Generate ' ] = gr . Button ( ' Generate ' , elem_id = ' Generate ' , variant = ' primary ' )
2023-04-15 04:17:15 +02:00
shared . gradio [ ' Continue ' ] = gr . Button ( ' Continue ' )
2023-04-14 16:07:28 +02:00
2023-03-16 03:29:56 +01:00
with gr . Row ( ) :
2023-05-30 05:17:31 +02:00
shared . gradio [ ' Impersonate ' ] = gr . Button ( ' Impersonate ' )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' Regenerate ' ] = gr . Button ( ' Regenerate ' )
2023-05-30 05:17:31 +02:00
shared . gradio [ ' Remove last ' ] = gr . Button ( ' Remove last ' )
2023-04-14 16:07:28 +02:00
2023-03-16 03:29:56 +01:00
with gr . Row ( ) :
2023-05-30 05:17:31 +02:00
shared . gradio [ ' Copy last reply ' ] = gr . Button ( ' Copy last reply ' )
shared . gradio [ ' Replace last reply ' ] = gr . Button ( ' Replace last reply ' )
2023-04-12 03:21:41 +02:00
shared . gradio [ ' Send dummy message ' ] = gr . Button ( ' Send dummy message ' )
shared . gradio [ ' Send dummy reply ' ] = gr . Button ( ' Send dummy reply ' )
2023-04-14 16:07:28 +02:00
2023-04-12 03:21:41 +02:00
with gr . Row ( ) :
2023-03-16 03:29:56 +01:00
shared . gradio [ ' Clear history ' ] = gr . Button ( ' Clear history ' )
2023-04-12 22:09:56 +02:00
shared . gradio [ ' Clear history-confirm ' ] = gr . Button ( ' Confirm ' , variant = ' stop ' , visible = False )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' Clear history-cancel ' ] = gr . Button ( ' Cancel ' , visible = False )
2023-01-15 22:16:46 +01:00
2023-06-02 18:58:08 +02:00
with gr . Row ( ) :
shared . gradio [ ' start_with ' ] = gr . Textbox ( label = ' Start reply with ' , placeholder = ' Sure thing! ' , value = shared . settings [ ' start_with ' ] )
with gr . Row ( ) :
shared . gradio [ ' mode ' ] = gr . Radio ( choices = [ ' chat ' , ' chat-instruct ' , ' instruct ' ] , value = shared . settings [ ' mode ' ] if shared . settings [ ' mode ' ] in [ ' chat ' , ' instruct ' , ' chat-instruct ' ] else ' chat ' , label = ' Mode ' , info = ' Defines how the chat prompt is generated. In instruct and chat-instruct modes, the instruction template selected under " Chat settings " must match the current model. ' )
shared . gradio [ ' chat_style ' ] = gr . Dropdown ( choices = utils . get_available_chat_styles ( ) , label = ' Chat style ' , value = shared . settings [ ' chat_style ' ] , visible = shared . settings [ ' mode ' ] != ' instruct ' )
2023-04-05 16:49:59 +02:00
2023-05-14 15:43:55 +02:00
with gr . Tab ( ' Chat settings ' , elem_id = ' chat-settings ' ) :
2023-05-21 02:48:45 +02:00
2023-06-25 06:10:20 +02:00
with gr . Tab ( " Character " ) :
with gr . Row ( ) :
with gr . Column ( scale = 8 ) :
with gr . Row ( ) :
2023-07-04 05:03:30 +02:00
shared . gradio [ ' character_menu ' ] = gr . Dropdown ( value = ' None ' , choices = utils . get_available_characters ( ) , label = ' Character ' , elem_id = ' character-menu ' , info = ' Used in chat and chat-instruct modes. ' , elem_classes = ' slim-dropdown ' )
2023-06-25 06:10:20 +02:00
ui . create_refresh_button ( shared . gradio [ ' character_menu ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_characters ( ) } , ' refresh-button ' )
shared . gradio [ ' save_character ' ] = gr . Button ( ' 💾 ' , elem_classes = ' refresh-button ' )
shared . gradio [ ' delete_character ' ] = gr . Button ( ' 🗑️ ' , elem_classes = ' refresh-button ' )
2023-05-16 22:22:59 +02:00
2023-06-25 06:10:20 +02:00
shared . gradio [ ' name1 ' ] = gr . Textbox ( value = shared . settings [ ' name1 ' ] , lines = 1 , label = ' Your name ' )
shared . gradio [ ' name2 ' ] = gr . Textbox ( value = shared . settings [ ' name2 ' ] , lines = 1 , label = ' Character \' s name ' )
shared . gradio [ ' context ' ] = gr . Textbox ( value = shared . settings [ ' context ' ] , lines = 4 , label = ' Context ' )
shared . gradio [ ' greeting ' ] = gr . Textbox ( value = shared . settings [ ' greeting ' ] , lines = 4 , label = ' Greeting ' )
2023-05-16 22:22:59 +02:00
2023-06-25 06:10:20 +02:00
with gr . Column ( scale = 1 ) :
shared . gradio [ ' character_picture ' ] = gr . Image ( label = ' Character picture ' , type = ' pil ' )
shared . gradio [ ' your_picture ' ] = gr . Image ( label = ' Your picture ' , type = ' pil ' , value = Image . open ( Path ( ' cache/pfp_me.png ' ) ) if Path ( ' cache/pfp_me.png ' ) . exists ( ) else None )
2023-03-16 03:29:56 +01:00
2023-06-25 06:10:20 +02:00
with gr . Tab ( " Instruction template " ) :
with gr . Row ( ) :
2023-03-16 03:29:56 +01:00
with gr . Row ( ) :
2023-06-25 06:10:20 +02:00
shared . gradio [ ' instruction_template ' ] = gr . Dropdown ( choices = utils . get_available_instruction_templates ( ) , label = ' Instruction template ' , value = ' None ' , info = ' Change this according to the model/LoRA that you are using. Used in instruct and chat-instruct modes. ' , elem_classes = ' slim-dropdown ' )
ui . create_refresh_button ( shared . gradio [ ' instruction_template ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_instruction_templates ( ) } , ' refresh-button ' )
shared . gradio [ ' save_template ' ] = gr . Button ( ' 💾 ' , elem_classes = ' refresh-button ' )
shared . gradio [ ' delete_template ' ] = gr . Button ( ' 🗑️ ' , elem_classes = ' refresh-button ' )
shared . gradio [ ' name1_instruct ' ] = gr . Textbox ( value = ' ' , lines = 2 , label = ' User string ' )
shared . gradio [ ' name2_instruct ' ] = gr . Textbox ( value = ' ' , lines = 1 , label = ' Bot string ' )
shared . gradio [ ' context_instruct ' ] = gr . Textbox ( value = ' ' , lines = 4 , label = ' Context ' )
shared . gradio [ ' turn_template ' ] = gr . Textbox ( value = shared . settings [ ' turn_template ' ] , lines = 1 , label = ' Turn template ' , info = ' Used to precisely define the placement of spaces and new line characters in instruction prompts. ' )
with gr . Row ( ) :
shared . gradio [ ' chat-instruct_command ' ] = gr . Textbox ( value = shared . settings [ ' chat-instruct_command ' ] , lines = 4 , label = ' Command for chat-instruct mode ' , info = ' <|character|> gets replaced by the bot name, and <|prompt|> gets replaced by the regular chat prompt. ' )
with gr . Tab ( ' Chat history ' ) :
with gr . Row ( ) :
with gr . Column ( ) :
shared . gradio [ ' download ' ] = gr . File ( label = " Download " )
shared . gradio [ ' download_button ' ] = gr . Button ( value = ' Refresh ' )
2023-04-14 16:07:28 +02:00
2023-06-25 06:10:20 +02:00
with gr . Column ( ) :
shared . gradio [ ' upload_chat_history ' ] = gr . File ( type = ' binary ' , file_types = [ ' .json ' , ' .txt ' ] , label = " Upload " )
2023-04-14 16:07:28 +02:00
2023-06-25 06:10:20 +02:00
with gr . Tab ( ' Upload character ' ) :
2023-07-30 20:25:38 +02:00
with gr . Tab ( ' YAML or JSON ' ) :
2023-06-25 06:10:20 +02:00
with gr . Row ( ) :
2023-07-30 20:25:38 +02:00
shared . gradio [ ' upload_json ' ] = gr . File ( type = ' binary ' , file_types = [ ' .json ' , ' .yaml ' ] , label = ' JSON or YAML File ' )
2023-06-25 06:10:20 +02:00
shared . gradio [ ' upload_img_bot ' ] = gr . Image ( type = ' pil ' , label = ' Profile Picture (optional) ' )
2023-06-29 19:56:25 +02:00
shared . gradio [ ' Submit character ' ] = gr . Button ( value = ' Submit ' , interactive = False )
2023-04-14 16:07:28 +02:00
2023-07-30 20:42:30 +02:00
with gr . Tab ( ' TavernAI PNG ' ) :
2023-06-29 19:56:25 +02:00
with gr . Row ( ) :
with gr . Column ( ) :
shared . gradio [ ' upload_img_tavern ' ] = gr . Image ( type = ' pil ' , label = ' TavernAI PNG File ' , elem_id = " upload_img_tavern " )
shared . gradio [ ' tavern_json ' ] = gr . State ( )
with gr . Column ( ) :
shared . gradio [ ' tavern_name ' ] = gr . Textbox ( value = ' ' , lines = 1 , label = ' Name ' , interactive = False )
shared . gradio [ ' tavern_desc ' ] = gr . Textbox ( value = ' ' , lines = 4 , max_lines = 4 , label = ' Description ' , interactive = False )
shared . gradio [ ' Submit tavern character ' ] = gr . Button ( value = ' Submit ' , interactive = False )
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Parameters " , elem_id = " parameters " ) :
create_settings_menus ( default_preset )
2023-04-14 16:07:28 +02:00
# Create notebook mode interface
2023-03-16 03:29:56 +01:00
elif shared . args . notebook :
2023-07-04 05:03:30 +02:00
shared . input_elements = ui . list_interface_input_elements ( )
2023-04-11 16:46:30 +02:00
shared . gradio [ ' interface_state ' ] = gr . State ( { k : None for k in shared . input_elements } )
2023-04-14 19:34:56 +02:00
shared . gradio [ ' last_input ' ] = gr . State ( ' ' )
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Text generation " , elem_id = " main " ) :
2023-03-15 22:56:26 +01:00
with gr . Row ( ) :
2023-03-27 03:20:30 +02:00
with gr . Column ( scale = 4 ) :
with gr . Tab ( ' Raw ' ) :
2023-04-19 04:36:23 +02:00
shared . gradio [ ' textbox ' ] = gr . Textbox ( value = default_text , elem_classes = " textbox " , lines = 27 )
2023-04-14 16:07:28 +02:00
2023-03-27 03:20:30 +02:00
with gr . Tab ( ' Markdown ' ) :
2023-05-25 16:59:27 +02:00
shared . gradio [ ' markdown_render ' ] = gr . Button ( ' Render ' )
2023-03-27 03:20:30 +02:00
shared . gradio [ ' markdown ' ] = gr . Markdown ( )
2023-04-14 16:07:28 +02:00
2023-03-27 03:20:30 +02:00
with gr . Tab ( ' HTML ' ) :
shared . gradio [ ' html ' ] = gr . HTML ( )
2023-03-15 16:01:32 +01:00
2023-03-27 03:20:30 +02:00
with gr . Row ( ) :
2023-04-21 07:35:28 +02:00
shared . gradio [ ' Generate ' ] = gr . Button ( ' Generate ' , variant = ' primary ' , elem_classes = " small-button " )
shared . gradio [ ' Stop ' ] = gr . Button ( ' Stop ' , elem_classes = " small-button " )
shared . gradio [ ' Undo ' ] = gr . Button ( ' Undo ' , elem_classes = " small-button " )
shared . gradio [ ' Regenerate ' ] = gr . Button ( ' Regenerate ' , elem_classes = " small-button " )
2023-03-27 17:16:37 +02:00
2023-03-27 03:20:30 +02:00
with gr . Column ( scale = 1 ) :
2023-03-27 18:52:12 +02:00
gr . HTML ( ' <div style= " padding-bottom: 13px " ></div> ' )
2023-03-27 03:20:30 +02:00
shared . gradio [ ' max_new_tokens ' ] = gr . Slider ( minimum = shared . settings [ ' max_new_tokens_min ' ] , maximum = shared . settings [ ' max_new_tokens_max ' ] , step = 1 , label = ' max_new_tokens ' , value = shared . settings [ ' max_new_tokens ' ] )
2023-04-13 16:05:47 +02:00
with gr . Row ( ) :
2023-06-11 19:20:16 +02:00
shared . gradio [ ' prompt_menu ' ] = gr . Dropdown ( choices = utils . get_available_prompts ( ) , value = ' None ' , label = ' Prompt ' , elem_classes = ' slim-dropdown ' )
ui . create_refresh_button ( shared . gradio [ ' prompt_menu ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_prompts ( ) } , [ ' refresh-button ' , ' refresh-button-small ' ] )
shared . gradio [ ' save_prompt ' ] = gr . Button ( ' 💾 ' , elem_classes = [ ' refresh-button ' , ' refresh-button-small ' ] )
shared . gradio [ ' delete_prompt ' ] = gr . Button ( ' 🗑️ ' , elem_classes = [ ' refresh-button ' , ' refresh-button-small ' ] )
2023-06-11 17:19:18 +02:00
2023-04-21 22:18:34 +02:00
shared . gradio [ ' count_tokens ' ] = gr . Button ( ' Count tokens ' )
2023-04-13 16:05:47 +02:00
shared . gradio [ ' status ' ] = gr . Markdown ( ' ' )
2023-03-15 16:01:32 +01:00
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Parameters " , elem_id = " parameters " ) :
create_settings_menus ( default_preset )
2023-03-15 16:01:32 +01:00
2023-04-14 16:07:28 +02:00
# Create default mode interface
2023-03-16 03:29:56 +01:00
else :
2023-07-04 05:03:30 +02:00
shared . input_elements = ui . list_interface_input_elements ( )
2023-04-11 16:46:30 +02:00
shared . gradio [ ' interface_state ' ] = gr . State ( { k : None for k in shared . input_elements } )
2023-04-14 19:34:56 +02:00
shared . gradio [ ' last_input ' ] = gr . State ( ' ' )
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Text generation " , elem_id = " main " ) :
with gr . Row ( ) :
with gr . Column ( ) :
2023-04-19 04:36:23 +02:00
shared . gradio [ ' textbox ' ] = gr . Textbox ( value = default_text , elem_classes = " textbox_default " , lines = 27 , label = ' Input ' )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' max_new_tokens ' ] = gr . Slider ( minimum = shared . settings [ ' max_new_tokens_min ' ] , maximum = shared . settings [ ' max_new_tokens_max ' ] , step = 1 , label = ' max_new_tokens ' , value = shared . settings [ ' max_new_tokens ' ] )
with gr . Row ( ) :
2023-06-11 17:19:18 +02:00
shared . gradio [ ' Generate ' ] = gr . Button ( ' Generate ' , variant = ' primary ' )
shared . gradio [ ' Stop ' ] = gr . Button ( ' Stop ' )
shared . gradio [ ' Continue ' ] = gr . Button ( ' Continue ' )
shared . gradio [ ' count_tokens ' ] = gr . Button ( ' Count tokens ' )
2023-01-29 13:48:18 +01:00
2023-04-13 16:05:47 +02:00
with gr . Row ( ) :
2023-06-11 19:20:16 +02:00
shared . gradio [ ' prompt_menu ' ] = gr . Dropdown ( choices = utils . get_available_prompts ( ) , value = ' None ' , label = ' Prompt ' , elem_classes = ' slim-dropdown ' )
2023-06-11 17:19:18 +02:00
ui . create_refresh_button ( shared . gradio [ ' prompt_menu ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_prompts ( ) } , ' refresh-button ' )
2023-06-11 19:20:16 +02:00
shared . gradio [ ' save_prompt ' ] = gr . Button ( ' 💾 ' , elem_classes = ' refresh-button ' )
shared . gradio [ ' delete_prompt ' ] = gr . Button ( ' 🗑️ ' , elem_classes = ' refresh-button ' )
2023-04-13 16:05:47 +02:00
2023-06-11 17:19:18 +02:00
shared . gradio [ ' status ' ] = gr . Markdown ( ' ' )
2023-01-19 02:44:47 +01:00
2023-03-16 03:29:56 +01:00
with gr . Column ( ) :
with gr . Tab ( ' Raw ' ) :
2023-04-19 04:36:23 +02:00
shared . gradio [ ' output_textbox ' ] = gr . Textbox ( elem_classes = " textbox_default_output " , lines = 27 , label = ' Output ' )
2023-04-13 16:05:47 +02:00
2023-03-16 03:29:56 +01:00
with gr . Tab ( ' Markdown ' ) :
2023-05-25 16:59:27 +02:00
shared . gradio [ ' markdown_render ' ] = gr . Button ( ' Render ' )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' markdown ' ] = gr . Markdown ( )
2023-04-13 16:05:47 +02:00
2023-03-16 03:29:56 +01:00
with gr . Tab ( ' HTML ' ) :
shared . gradio [ ' html ' ] = gr . HTML ( )
2023-03-27 17:16:37 +02:00
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Parameters " , elem_id = " parameters " ) :
create_settings_menus ( default_preset )
2023-04-14 16:07:28 +02:00
# Model tab
2023-04-06 06:54:05 +02:00
with gr . Tab ( " Model " , elem_id = " model-tab " ) :
create_model_menus ( )
2023-04-14 16:07:28 +02:00
# Training tab
2023-03-27 17:32:32 +02:00
with gr . Tab ( " Training " , elem_id = " training-tab " ) :
training . create_train_interface ( )
2023-07-04 05:03:30 +02:00
# Session tab
with gr . Tab ( " Session " , elem_id = " session-tab " ) :
2023-05-08 17:35:03 +02:00
modes = [ " default " , " notebook " , " chat " ]
2023-03-16 03:29:56 +01:00
current_mode = " default "
2023-03-16 16:44:03 +01:00
for mode in modes [ 1 : ] :
2023-04-16 06:36:50 +02:00
if getattr ( shared . args , mode ) :
2023-03-16 03:29:56 +01:00
current_mode = mode
2023-03-16 16:44:03 +01:00
break
2023-05-08 17:35:03 +02:00
2023-03-16 16:44:03 +01:00
cmd_list = vars ( shared . args )
2023-05-17 06:57:51 +02:00
bool_list = sorted ( [ k for k in cmd_list if type ( cmd_list [ k ] ) is bool and k not in modes + ui . list_model_elements ( ) ] )
2023-04-01 04:41:10 +02:00
bool_active = [ k for k in bool_list if vars ( shared . args ) [ k ] ]
2023-03-16 03:29:56 +01:00
2023-05-22 08:11:44 +02:00
with gr . Row ( ) :
2023-06-11 20:29:45 +02:00
with gr . Column ( ) :
2023-07-04 05:03:30 +02:00
with gr . Row ( ) :
shared . gradio [ ' interface_modes_menu ' ] = gr . Dropdown ( choices = modes , value = current_mode , label = " Mode " , elem_classes = ' slim-dropdown ' )
shared . gradio [ ' reset_interface ' ] = gr . Button ( " Apply and restart " , elem_classes = " small-button " , variant = " primary " )
shared . gradio [ ' toggle_dark_mode ' ] = gr . Button ( ' Toggle 💡 ' , elem_classes = " small-button " )
2023-06-11 22:11:06 +02:00
2023-07-04 05:03:30 +02:00
with gr . Row ( ) :
with gr . Column ( ) :
shared . gradio [ ' extensions_menu ' ] = gr . CheckboxGroup ( choices = utils . get_available_extensions ( ) , value = shared . args . extensions , label = " Available extensions " , info = ' Note that some of these extensions may require manually installing Python requirements through the command: pip install -r extensions/extension_name/requirements.txt ' , elem_classes = ' checkboxgroup-table ' )
2023-06-11 20:29:45 +02:00
2023-07-04 05:03:30 +02:00
with gr . Column ( ) :
shared . gradio [ ' bool_menu ' ] = gr . CheckboxGroup ( choices = bool_list , value = bool_active , label = " Boolean command-line flags " , elem_classes = ' checkboxgroup-table ' )
2023-06-11 22:11:06 +02:00
2023-07-04 05:03:30 +02:00
with gr . Column ( ) :
if not shared . args . multi_user :
with gr . Row ( ) :
shared . gradio [ ' session_menu ' ] = gr . Dropdown ( choices = utils . get_available_sessions ( ) , value = ' None ' , label = ' Session ' , elem_classes = ' slim-dropdown ' , info = ' When saving a session, make sure to keep the initial part of the filename (session_chat, session_notebook, or session_default), otherwise it will not appear on this list afterwards. ' )
ui . create_refresh_button ( shared . gradio [ ' session_menu ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_sessions ( ) } , [ ' refresh-button ' ] )
shared . gradio [ ' save_session ' ] = gr . Button ( ' 💾 ' , elem_classes = [ ' refresh-button ' ] )
shared . gradio [ ' delete_session ' ] = gr . Button ( ' 🗑️ ' , elem_classes = [ ' refresh-button ' ] )
2023-06-11 22:11:06 +02:00
2023-07-04 05:03:30 +02:00
extension_name = gr . Textbox ( lines = 1 , label = ' Install or update an extension ' , info = ' Enter the GitHub URL below and press Enter. For a list of extensions, see: https://github.com/oobabooga/text-generation-webui-extensions ⚠️ WARNING ⚠️ : extensions can execute arbitrary code. Make sure to inspect their source code before activating them. ' )
extension_status = gr . Markdown ( )
extension_name . submit (
2023-06-11 22:11:06 +02:00
clone_or_pull_repository , extension_name , extension_status , show_progress = False ) . then (
2023-07-04 05:03:30 +02:00
lambda : gr . update ( choices = utils . get_available_extensions ( ) , value = shared . args . extensions ) , None , gradio ( ' extensions_menu ' ) )
2023-03-16 14:19:00 +01:00
2023-04-08 22:25:21 +02:00
# Reset interface event
shared . gradio [ ' reset_interface ' ] . click (
2023-07-04 05:03:30 +02:00
set_interface_arguments , gradio ( ' interface_modes_menu ' , ' extensions_menu ' , ' bool_menu ' ) , None ) . then (
2023-07-16 07:39:39 +02:00
lambda : None , None , None , _js = ' () => { document.body.innerHTML= \' <h1 style= " font-family:monospace;padding-top:20 % ;margin:0;height:100vh;color:lightgray;text-align:center;background:var(--body-background-fill) " >Reloading...</h1> \' ; setTimeout(function() { location.reload()},2500); return []} ' )
2023-03-27 18:41:06 +02:00
2023-05-22 08:19:01 +02:00
shared . gradio [ ' toggle_dark_mode ' ] . click ( lambda : None , None , None , _js = ' () => { document.getElementsByTagName( " body " )[0].classList.toggle( " dark " )} ' )
2023-05-22 08:11:44 +02:00
2023-04-14 16:07:28 +02:00
# chat mode event handlers
if shared . is_chat ( ) :
2023-07-04 05:03:30 +02:00
shared . input_params = gradio ( ' Chat input ' , ' start_with ' , ' interface_state ' )
clear_arr = gradio ( ' Clear history-confirm ' , ' Clear history ' , ' Clear history-cancel ' )
shared . reload_inputs = gradio ( ' history ' , ' name1 ' , ' name2 ' , ' mode ' , ' chat_style ' )
2023-04-14 16:07:28 +02:00
gen_events . append ( shared . gradio [ ' Generate ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
lambda x : ( x , ' ' ) , gradio ( ' textbox ' ) , gradio ( ' Chat input ' , ' textbox ' ) , show_progress = False ) . then (
chat . generate_chat_reply_wrapper , shared . input_params , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
2023-04-14 16:07:28 +02:00
)
gen_events . append ( shared . gradio [ ' textbox ' ] . submit (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
lambda x : ( x , ' ' ) , gradio ( ' textbox ' ) , gradio ( ' Chat input ' , ' textbox ' ) , show_progress = False ) . then (
chat . generate_chat_reply_wrapper , shared . input_params , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
2023-04-14 16:07:28 +02:00
)
gen_events . append ( shared . gradio [ ' Regenerate ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
partial ( chat . generate_chat_reply_wrapper , regenerate = True ) , shared . input_params , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
2023-04-14 16:07:28 +02:00
)
gen_events . append ( shared . gradio [ ' Continue ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
partial ( chat . generate_chat_reply_wrapper , _continue = True ) , shared . input_params , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
2023-04-14 16:07:28 +02:00
)
gen_events . append ( shared . gradio [ ' Impersonate ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
lambda x : x , gradio ( ' textbox ' ) , gradio ( ' Chat input ' ) , show_progress = False ) . then (
chat . impersonate_wrapper , shared . input_params , gradio ( ' textbox ' ) , show_progress = False ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
2023-04-14 16:07:28 +02:00
)
shared . gradio [ ' Replace last reply ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . replace_last_reply , gradio ( ' textbox ' , ' interface_state ' ) , gradio ( ' history ' ) ) . then (
lambda : ' ' , None , gradio ( ' textbox ' ) , show_progress = False ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None )
2023-04-14 16:07:28 +02:00
shared . gradio [ ' Send dummy message ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . send_dummy_message , gradio ( ' textbox ' , ' interface_state ' ) , gradio ( ' history ' ) ) . then (
lambda : ' ' , None , gradio ( ' textbox ' ) , show_progress = False ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None )
2023-04-14 16:07:28 +02:00
shared . gradio [ ' Send dummy reply ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . send_dummy_reply , gradio ( ' textbox ' , ' interface_state ' ) , gradio ( ' history ' ) ) . then (
lambda : ' ' , None , gradio ( ' textbox ' ) , show_progress = False ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None )
2023-04-14 16:07:28 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' Clear history ' ] . click ( lambda : [ gr . update ( visible = True ) , gr . update ( visible = False ) , gr . update ( visible = True ) ] , None , clear_arr )
shared . gradio [ ' Clear history-cancel ' ] . click ( lambda : [ gr . update ( visible = False ) , gr . update ( visible = True ) , gr . update ( visible = False ) ] , None , clear_arr )
2023-04-14 16:07:28 +02:00
shared . gradio [ ' Clear history-confirm ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-04-14 16:07:28 +02:00
lambda : [ gr . update ( visible = False ) , gr . update ( visible = True ) , gr . update ( visible = False ) ] , None , clear_arr ) . then (
2023-07-04 05:03:30 +02:00
chat . clear_chat_log , gradio ( ' interface_state ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None )
shared . gradio [ ' Remove last ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . remove_last_message , gradio ( ' history ' ) , gradio ( ' textbox ' , ' history ' ) , show_progress = False ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) ) . then (
chat . save_persistent_history , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , None )
shared . gradio [ ' character_menu ' ] . change (
partial ( chat . load_character , instruct = False ) , gradio ( ' character_menu ' , ' name1 ' , ' name2 ' ) , gradio ( ' name1 ' , ' name2 ' , ' character_picture ' , ' greeting ' , ' context ' , ' dummy ' ) ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . load_persistent_history , gradio ( ' interface_state ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) )
2023-04-14 16:07:28 +02:00
shared . gradio [ ' Stop ' ] . click (
stop_everything_event , None , None , queue = False , cancels = gen_events if shared . args . no_stream else None ) . then (
2023-07-04 05:03:30 +02:00
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) )
2023-04-14 16:07:28 +02:00
shared . gradio [ ' mode ' ] . change (
2023-07-04 05:03:30 +02:00
lambda x : gr . update ( visible = x != ' instruct ' ) , gradio ( ' mode ' ) , gradio ( ' chat_style ' ) , show_progress = False ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) )
2023-04-14 16:07:28 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' chat_style ' ] . change ( chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) )
2023-04-14 16:07:28 +02:00
shared . gradio [ ' instruction_template ' ] . change (
2023-07-04 05:03:30 +02:00
partial ( chat . load_character , instruct = True ) , gradio ( ' instruction_template ' , ' name1_instruct ' , ' name2_instruct ' ) , gradio ( ' name1_instruct ' , ' name2_instruct ' , ' dummy ' , ' dummy ' , ' context_instruct ' , ' turn_template ' ) )
2023-04-14 16:07:28 +02:00
shared . gradio [ ' upload_chat_history ' ] . upload (
2023-07-04 05:03:30 +02:00
chat . load_history , gradio ( ' upload_chat_history ' , ' history ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) )
2023-04-14 16:07:28 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' Copy last reply ' ] . click ( chat . send_last_reply_to_input , gradio ( ' history ' ) , gradio ( ' textbox ' ) , show_progress = False )
2023-05-11 20:37:04 +02:00
2023-05-21 02:48:45 +02:00
# Save/delete a character
shared . gradio [ ' save_character ' ] . click (
2023-07-04 05:03:30 +02:00
lambda x : x , gradio ( ' name2 ' ) , gradio ( ' save_character_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' character_saver ' ) )
2023-05-21 02:48:45 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' delete_character ' ] . click ( lambda : gr . update ( visible = True ) , None , gradio ( ' character_deleter ' ) )
2023-05-21 02:48:45 +02:00
2023-06-11 17:19:18 +02:00
shared . gradio [ ' save_template ' ] . click (
2023-07-04 05:03:30 +02:00
lambda : ' My Template.yaml ' , None , gradio ( ' save_filename ' ) ) . then (
lambda : ' characters/instruction-following/ ' , None , gradio ( ' save_root ' ) ) . then (
chat . generate_instruction_template_yaml , gradio ( ' name1_instruct ' , ' name2_instruct ' , ' context_instruct ' , ' turn_template ' ) , gradio ( ' save_contents ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_saver ' ) )
2023-05-21 02:48:45 +02:00
2023-06-11 17:19:18 +02:00
shared . gradio [ ' delete_template ' ] . click (
2023-07-04 05:03:30 +02:00
lambda x : f ' { x } .yaml ' , gradio ( ' instruction_template ' ) , gradio ( ' delete_filename ' ) ) . then (
lambda : ' characters/instruction-following/ ' , None , gradio ( ' delete_root ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_deleter ' ) )
2023-07-12 05:10:36 +02:00
shared . gradio [ ' download_button ' ] . click ( chat . save_history_at_user_request , gradio ( ' history ' , ' character_menu ' , ' mode ' ) , gradio ( ' download ' ) )
2023-07-04 05:03:30 +02:00
shared . gradio [ ' Submit character ' ] . click ( chat . upload_character , gradio ( ' upload_json ' , ' upload_img_bot ' ) , gradio ( ' character_menu ' ) )
shared . gradio [ ' upload_json ' ] . upload ( lambda : gr . update ( interactive = True ) , None , gradio ( ' Submit character ' ) )
shared . gradio [ ' upload_json ' ] . clear ( lambda : gr . update ( interactive = False ) , None , gradio ( ' Submit character ' ) )
shared . gradio [ ' Submit tavern character ' ] . click ( chat . upload_tavern_character , gradio ( ' upload_img_tavern ' , ' tavern_json ' ) , gradio ( ' character_menu ' ) )
shared . gradio [ ' upload_img_tavern ' ] . upload ( chat . check_tavern_character , gradio ( ' upload_img_tavern ' ) , gradio ( ' tavern_name ' , ' tavern_desc ' , ' tavern_json ' , ' Submit tavern character ' ) , show_progress = False )
shared . gradio [ ' upload_img_tavern ' ] . clear ( lambda : ( None , None , None , gr . update ( interactive = False ) ) , None , gradio ( ' tavern_name ' , ' tavern_desc ' , ' tavern_json ' , ' Submit tavern character ' ) , show_progress = False )
2023-05-11 20:37:04 +02:00
shared . gradio [ ' your_picture ' ] . change (
2023-07-04 05:03:30 +02:00
chat . upload_your_profile_picture , gradio ( ' your_picture ' ) , None ) . then (
partial ( chat . redraw_html , reset_cache = True ) , shared . reload_inputs , gradio ( ' display ' ) )
2023-05-11 20:37:04 +02:00
2023-04-14 16:07:28 +02:00
# notebook/default modes event handlers
else :
2023-07-04 05:03:30 +02:00
shared . input_params = gradio ( ' textbox ' , ' interface_state ' )
2023-04-14 16:07:28 +02:00
if shared . args . notebook :
2023-07-04 05:03:30 +02:00
output_params = gradio ( ' textbox ' , ' html ' )
2023-04-14 16:07:28 +02:00
else :
2023-07-04 05:03:30 +02:00
output_params = gradio ( ' output_textbox ' , ' html ' )
2023-04-14 16:07:28 +02:00
gen_events . append ( shared . gradio [ ' Generate ' ] . click (
2023-07-04 05:03:30 +02:00
lambda x : x , gradio ( ' textbox ' ) , gradio ( ' last_input ' ) ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-20 04:16:06 +02:00
generate_reply_wrapper , shared . input_params , output_params , show_progress = False ) . then (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
# lambda: None, None, None, _js="() => {element = document.getElementsByTagName('textarea')[0]; element.scrollTop = element.scrollHeight}")
2023-04-14 16:07:28 +02:00
)
gen_events . append ( shared . gradio [ ' textbox ' ] . submit (
2023-07-04 05:03:30 +02:00
lambda x : x , gradio ( ' textbox ' ) , gradio ( ' last_input ' ) ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-20 04:16:06 +02:00
generate_reply_wrapper , shared . input_params , output_params , show_progress = False ) . then (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
# lambda: None, None, None, _js="() => {element = document.getElementsByTagName('textarea')[0]; element.scrollTop = element.scrollHeight}")
2023-04-14 16:07:28 +02:00
)
2023-04-14 19:34:56 +02:00
if shared . args . notebook :
2023-07-04 05:03:30 +02:00
shared . gradio [ ' Undo ' ] . click ( lambda x : x , gradio ( ' last_input ' ) , gradio ( ' textbox ' ) , show_progress = False )
shared . gradio [ ' markdown_render ' ] . click ( lambda x : x , gradio ( ' textbox ' ) , gradio ( ' markdown ' ) , queue = False )
2023-04-14 19:34:56 +02:00
gen_events . append ( shared . gradio [ ' Regenerate ' ] . click (
2023-07-04 05:03:30 +02:00
lambda x : x , gradio ( ' last_input ' ) , gradio ( ' textbox ' ) , show_progress = False ) . then (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-20 04:16:06 +02:00
generate_reply_wrapper , shared . input_params , output_params , show_progress = False ) . then (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
# lambda: None, None, None, _js="() => {element = document.getElementsByTagName('textarea')[0]; element.scrollTop = element.scrollHeight}")
2023-04-14 19:34:56 +02:00
)
else :
2023-07-04 05:03:30 +02:00
shared . gradio [ ' markdown_render ' ] . click ( lambda x : x , gradio ( ' output_textbox ' ) , gradio ( ' markdown ' ) , queue = False )
2023-04-14 19:34:56 +02:00
gen_events . append ( shared . gradio [ ' Continue ' ] . click (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-20 04:16:06 +02:00
generate_reply_wrapper , [ shared . gradio [ ' output_textbox ' ] ] + shared . input_params [ 1 : ] , output_params , show_progress = False ) . then (
2023-07-04 05:03:30 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-05-22 05:02:59 +02:00
lambda : None , None , None , _js = f " () => {{ { audio_notification_js } }} " )
# lambda: None, None, None, _js="() => {element = document.getElementsByTagName('textarea')[1]; element.scrollTop = element.scrollHeight}")
2023-04-14 19:34:56 +02:00
)
2023-04-14 16:07:28 +02:00
shared . gradio [ ' Stop ' ] . click ( stop_everything_event , None , None , queue = False , cancels = gen_events if shared . args . no_stream else None )
2023-07-04 05:03:30 +02:00
shared . gradio [ ' prompt_menu ' ] . change ( load_prompt , gradio ( ' prompt_menu ' ) , gradio ( ' textbox ' ) , show_progress = False )
2023-06-11 17:19:18 +02:00
shared . gradio [ ' save_prompt ' ] . click (
2023-07-04 05:03:30 +02:00
lambda x : x , gradio ( ' textbox ' ) , gradio ( ' save_contents ' ) ) . then (
lambda : ' prompts/ ' , None , gradio ( ' save_root ' ) ) . then (
lambda : utils . current_time ( ) + ' .txt ' , None , gradio ( ' save_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_saver ' ) )
2023-06-11 17:19:18 +02:00
shared . gradio [ ' delete_prompt ' ] . click (
2023-07-04 05:03:30 +02:00
lambda : ' prompts/ ' , None , gradio ( ' delete_root ' ) ) . then (
lambda x : x + ' .txt ' , gradio ( ' prompt_menu ' ) , gradio ( ' delete_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_deleter ' ) )
2023-06-11 17:19:18 +02:00
2023-07-04 05:03:30 +02:00
shared . gradio [ ' count_tokens ' ] . click ( count_tokens , gradio ( ' textbox ' ) , gradio ( ' status ' ) , show_progress = False )
2023-03-28 04:29:52 +02:00
2023-06-12 02:20:51 +02:00
create_file_saving_event_handlers ( )
2023-07-04 05:03:30 +02:00
2023-05-23 00:45:11 +02:00
if shared . settings [ ' dark_theme ' ] :
2023-05-25 06:14:13 +02:00
shared . gradio [ ' interface ' ] . load ( lambda : None , None , None , _js = " () => document.getElementsByTagName( ' body ' )[0].classList.add( ' dark ' ) " )
2023-05-23 00:45:11 +02:00
2023-07-26 05:37:01 +02:00
shared . gradio [ ' interface ' ] . load ( lambda : None , None , None , _js = f " () => {{ { js } }} " )
shared . gradio [ ' interface ' ] . load ( partial ( ui . apply_interface_values , { } , use_persistent = True ) , None , gradio ( ui . list_interface_input_elements ( ) ) , show_progress = False )
2023-07-04 05:03:30 +02:00
if shared . is_chat ( ) :
shared . gradio [ ' interface ' ] . load ( chat . redraw_html , shared . reload_inputs , gradio ( ' display ' ) )
2023-05-23 00:45:11 +02:00
2023-05-17 06:25:01 +02:00
# Extensions tabs
extensions_module . create_extensions_tabs ( )
2023-04-24 08:30:35 +02:00
# Extensions block
2023-05-17 06:25:01 +02:00
extensions_module . create_extensions_block ( )
2023-04-24 08:05:47 +02:00
2023-03-16 03:29:56 +01:00
# Launch the interface
shared . gradio [ ' interface ' ] . queue ( )
2023-07-07 07:24:52 +02:00
with OpenMonkeyPatch ( ) :
if shared . args . listen :
shared . gradio [ ' interface ' ] . launch ( prevent_thread_lock = True , share = shared . args . share , server_name = shared . args . listen_host or ' 0.0.0.0 ' , server_port = shared . args . listen_port , inbrowser = shared . args . auto_launch , auth = auth )
else :
shared . gradio [ ' interface ' ] . launch ( prevent_thread_lock = True , share = shared . args . share , server_port = shared . args . listen_port , inbrowser = shared . args . auto_launch , auth = auth )
2023-03-16 03:29:56 +01:00
2023-04-07 05:15:45 +02:00
2023-04-14 16:07:28 +02:00
if __name__ == " __main__ " :
# Loading custom settings
settings_file = None
if shared . args . settings is not None and Path ( shared . args . settings ) . exists ( ) :
settings_file = Path ( shared . args . settings )
2023-05-29 03:34:12 +02:00
elif Path ( ' settings.yaml ' ) . exists ( ) :
settings_file = Path ( ' settings.yaml ' )
2023-04-14 16:07:28 +02:00
elif Path ( ' settings.json ' ) . exists ( ) :
settings_file = Path ( ' settings.json ' )
2023-05-12 11:09:45 +02:00
2023-04-14 16:07:28 +02:00
if settings_file is not None :
2023-05-22 03:42:34 +02:00
logger . info ( f " Loading settings from { settings_file } ... " )
2023-05-29 03:34:12 +02:00
file_contents = open ( settings_file , ' r ' , encoding = ' utf-8 ' ) . read ( )
new_settings = json . loads ( file_contents ) if settings_file . suffix == " json " else yaml . safe_load ( file_contents )
2023-04-14 16:07:28 +02:00
for item in new_settings :
shared . settings [ item ] = new_settings [ item ]
2023-05-29 03:34:12 +02:00
# Set default model settings based on settings file
2023-05-12 11:09:45 +02:00
shared . model_config [ ' .* ' ] = {
' wbits ' : ' None ' ,
' model_type ' : ' None ' ,
' groupsize ' : ' None ' ,
' pre_layer ' : 0 ,
' mode ' : shared . settings [ ' mode ' ] ,
' skip_special_tokens ' : shared . settings [ ' skip_special_tokens ' ] ,
' custom_stopping_strings ' : shared . settings [ ' custom_stopping_strings ' ] ,
2023-06-05 17:07:52 +02:00
' truncation_length ' : shared . settings [ ' truncation_length ' ] ,
2023-07-25 00:26:29 +02:00
' n_gqa ' : 0 ,
' rms_norm_eps ' : 0 ,
2023-05-12 11:09:45 +02:00
}
shared . model_config . move_to_end ( ' .* ' , last = False ) # Move to the beginning
2023-04-14 16:07:28 +02:00
# Default extensions
2023-05-06 04:14:56 +02:00
extensions_module . available_extensions = utils . get_available_extensions ( )
2023-04-14 16:07:28 +02:00
if shared . is_chat ( ) :
for extension in shared . settings [ ' chat_default_extensions ' ] :
shared . args . extensions = shared . args . extensions or [ ]
if extension not in shared . args . extensions :
shared . args . extensions . append ( extension )
else :
for extension in shared . settings [ ' default_extensions ' ] :
shared . args . extensions = shared . args . extensions or [ ]
if extension not in shared . args . extensions :
shared . args . extensions . append ( extension )
2023-05-06 04:14:56 +02:00
available_models = utils . get_available_models ( )
2023-04-14 16:07:28 +02:00
# Model defined through --model
if shared . args . model is not None :
shared . model_name = shared . args . model
# Select the model from a command-line menu
elif shared . args . model_menu :
if len ( available_models ) == 0 :
2023-05-22 03:42:34 +02:00
logger . error ( ' No models are available! Please download at least one. ' )
2023-04-14 16:07:28 +02:00
sys . exit ( 0 )
else :
print ( ' The following models are available: \n ' )
for i , model in enumerate ( available_models ) :
print ( f ' { i + 1 } . { model } ' )
2023-04-24 08:05:47 +02:00
2023-04-14 16:07:28 +02:00
print ( f ' \n Which one do you want to load? 1- { len ( available_models ) } \n ' )
i = int ( input ( ) ) - 1
print ( )
2023-04-24 08:05:47 +02:00
2023-04-14 16:07:28 +02:00
shared . model_name = available_models [ i ]
# If any model has been selected, load it
if shared . model_name != ' None ' :
2023-06-17 00:00:37 +02:00
model_settings = get_model_settings_from_yamls ( shared . model_name )
2023-04-14 16:07:28 +02:00
shared . settings . update ( model_settings ) # hijacking the interface defaults
2023-04-14 20:24:03 +02:00
update_model_parameters ( model_settings , initial = True ) # hijacking the command-line arguments
2023-04-14 16:07:28 +02:00
# Load the model
shared . model , shared . tokenizer = load_model ( shared . model_name )
if shared . args . lora :
2023-04-26 03:58:48 +02:00
add_lora_to_model ( shared . args . lora )
2023-04-14 16:07:28 +02:00
2023-05-24 14:38:20 +02:00
shared . generation_lock = Lock ( )
2023-07-04 05:03:30 +02:00
2023-04-14 16:07:28 +02:00
# Launch the web UI
create_interface ( )
while True :
time . sleep ( 0.5 )
if shared . need_restart :
shared . need_restart = False
2023-05-22 05:26:07 +02:00
time . sleep ( 0.5 )
2023-04-14 16:07:28 +02:00
shared . gradio [ ' interface ' ] . close ( )
2023-05-17 03:27:43 +02:00
time . sleep ( 0.5 )
2023-04-14 16:07:28 +02:00
create_interface ( )