2023-04-05 19:05:50 +02:00
import os
os . environ [ ' GRADIO_ANALYTICS_ENABLED ' ] = ' False '
2023-01-28 23:16:37 +01:00
import io
2023-02-10 19:40:03 +01:00
import json
import re
import sys
import time
2023-02-13 19:25:16 +01:00
import zipfile
2023-03-27 17:16:37 +02:00
from datetime import datetime
2023-01-07 20:33:43 +01:00
from pathlib import Path
2023-02-10 19:40:03 +01:00
2022-12-21 17:27:31 +01:00
import gradio as gr
2023-04-05 03:28:49 +02:00
from PIL import Image
2023-02-10 19:40:03 +01:00
2023-02-23 16:05:25 +01:00
import modules . extensions as extensions_module
2023-04-06 06:22:15 +02:00
from modules import chat , shared , training , ui , api
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-02-23 18:41:42 +01:00
from modules . models import load_model , load_soft_prompt
2023-03-27 18:23:59 +02:00
from modules . text_generation import ( clear_torch_cache , generate_reply ,
stop_everything_event )
2022-12-21 17:27:31 +01:00
2023-02-23 18:31:28 +01:00
# Loading custom settings
2023-03-06 14:57:45 +01:00
settings_file = None
2023-02-23 16:05:25 +01:00
if shared . args . settings is not None and Path ( shared . args . settings ) . exists ( ) :
2023-03-06 14:57:45 +01:00
settings_file = Path ( shared . args . settings )
elif Path ( ' settings.json ' ) . exists ( ) :
settings_file = Path ( ' settings.json ' )
if settings_file is not None :
print ( f " Loading settings from { settings_file } ... " )
new_settings = json . loads ( open ( settings_file , ' r ' ) . read ( ) )
2023-01-16 20:35:45 +01:00
for item in new_settings :
2023-02-23 17:28:30 +01:00
shared . settings [ item ] = new_settings [ item ]
2023-02-13 19:25:16 +01:00
2023-02-23 18:31:28 +01:00
def get_available_models ( ) :
2023-03-02 16:03:57 +01:00
if shared . args . flexgen :
2023-03-25 01:36:31 +01:00
return sorted ( [ re . sub ( ' -np$ ' , ' ' , item . name ) for item in list ( Path ( f ' { shared . args . model_dir } / ' ) . glob ( ' * ' ) ) if item . name . endswith ( ' -np ' ) ] , key = str . lower )
2023-03-02 16:03:57 +01:00
else :
2023-03-25 01:36:31 +01:00
return sorted ( [ re . sub ( ' .pth$ ' , ' ' , item . name ) for item in list ( Path ( f ' { shared . args . model_dir } / ' ) . glob ( ' * ' ) ) if not item . name . endswith ( ( ' .txt ' , ' -np ' , ' .pt ' , ' .json ' ) ) ] , key = str . lower )
2023-02-23 18:31:28 +01:00
def get_available_presets ( ) :
2023-04-03 05:54:56 +02:00
return sorted ( set ( ( k . stem for k in Path ( ' presets ' ) . glob ( ' *.txt ' ) ) ) , key = str . lower )
2023-02-23 18:31:28 +01:00
2023-03-27 17:16:37 +02:00
def get_available_prompts ( ) :
prompts = [ ]
2023-04-03 05:54:56 +02:00
prompts + = sorted ( set ( ( k . stem for k in Path ( ' prompts ' ) . glob ( ' [0-9]*.txt ' ) ) ) , key = str . lower , reverse = True )
prompts + = sorted ( set ( ( k . stem for k in Path ( ' prompts ' ) . glob ( ' *.txt ' ) ) ) , key = str . lower )
2023-03-27 17:16:37 +02:00
prompts + = [ ' None ' ]
return prompts
2023-02-23 18:31:28 +01:00
def get_available_characters ( ) :
2023-04-03 01:34:25 +02:00
paths = ( x for x in Path ( ' characters ' ) . iterdir ( ) if x . suffix in ( ' .json ' , ' .yaml ' , ' .yml ' ) )
2023-04-05 16:49:59 +02:00
return [ ' None ' ] + sorted ( set ( ( k . stem for k in paths if k . stem != " instruction-following " ) ) , key = str . lower )
def get_available_instruction_templates ( ) :
2023-04-06 06:24:22 +02:00
path = " characters/instruction-following "
paths = [ ]
if os . path . exists ( path ) :
paths = ( x for x in Path ( path ) . iterdir ( ) if x . suffix in ( ' .json ' , ' .yaml ' , ' .yml ' ) )
2023-04-03 01:34:25 +02:00
return [ ' None ' ] + sorted ( set ( ( k . stem for k in paths ) ) , key = str . lower )
2023-02-23 18:31:28 +01:00
def get_available_extensions ( ) :
return sorted ( set ( map ( lambda x : x . parts [ 1 ] , Path ( ' extensions ' ) . glob ( ' */script.py ' ) ) ) , key = str . lower )
def get_available_softprompts ( ) :
2023-04-03 05:54:56 +02:00
return [ ' None ' ] + sorted ( set ( ( k . stem for k in Path ( ' softprompts ' ) . glob ( ' *.zip ' ) ) ) , key = str . lower )
2023-02-23 18:31:28 +01:00
2023-03-17 01:31:39 +01:00
def get_available_loras ( ) :
2023-03-28 07:16:40 +02:00
return [ ' None ' ] + sorted ( [ item . name for item in list ( Path ( shared . args . lora_dir ) . glob ( ' * ' ) ) if not item . name . endswith ( ( ' .txt ' , ' -np ' , ' .pt ' , ' .json ' ) ) ] , key = str . lower )
2023-03-17 01:31:39 +01:00
2023-03-27 04:47:29 +02:00
def unload_model ( ) :
shared . model = shared . tokenizer = None
clear_torch_cache ( )
2023-02-08 02:08:21 +01:00
def load_model_wrapper ( selected_model ) :
2023-02-23 16:05:25 +01:00
if selected_model != shared . model_name :
shared . model_name = selected_model
2023-02-12 13:36:27 +01:00
2023-03-27 04:47:29 +02:00
unload_model ( )
if selected_model != ' ' :
shared . model , shared . tokenizer = load_model ( shared . model_name )
2023-02-08 02:08:21 +01:00
2023-02-12 13:36:27 +01:00
return selected_model
2023-03-17 01:31:39 +01:00
def load_lora_wrapper ( selected_lora ) :
add_lora_to_model ( selected_lora )
2023-03-30 03:40:04 +02:00
return selected_lora
2023-03-17 01:31:39 +01:00
2023-04-06 06:22:15 +02:00
def load_preset_values ( preset_menu , state , return_dict = False ) :
2023-02-08 04:19:20 +01:00
generate_params = {
2023-02-08 02:08:21 +01:00
' do_sample ' : True ,
' temperature ' : 1 ,
' top_p ' : 1 ,
' typical_p ' : 1 ,
' repetition_penalty ' : 1 ,
2023-03-15 15:04:30 +01:00
' encoder_repetition_penalty ' : 1 ,
2023-02-08 02:08:21 +01:00
' top_k ' : 50 ,
2023-02-08 03:11:04 +01:00
' num_beams ' : 1 ,
2023-02-11 18:48:12 +01:00
' penalty_alpha ' : 0 ,
2023-02-08 03:11:04 +01:00
' min_length ' : 0 ,
' length_penalty ' : 1 ,
' no_repeat_ngram_size ' : 0 ,
' early_stopping ' : False ,
2023-02-08 02:08:21 +01:00
}
with open ( Path ( f ' presets/ { preset_menu } .txt ' ) , ' r ' ) as infile :
preset = infile . read ( )
2023-02-11 18:54:29 +01:00
for i in preset . splitlines ( ) :
i = i . rstrip ( ' , ' ) . strip ( ) . split ( ' = ' )
2023-02-08 02:08:21 +01:00
if len ( i ) == 2 and i [ 0 ] . strip ( ) != ' tokens ' :
2023-02-08 04:19:20 +01:00
generate_params [ i [ 0 ] . strip ( ) ] = eval ( i [ 1 ] . strip ( ) )
generate_params [ ' temperature ' ] = min ( 1.99 , generate_params [ ' temperature ' ] )
2023-02-08 02:08:21 +01:00
if return_dict :
2023-02-08 04:19:20 +01:00
return generate_params
2023-02-08 02:08:21 +01:00
else :
2023-04-06 06:22:15 +02:00
state . update ( generate_params )
return state , * [ generate_params [ k ] for k in [ ' do_sample ' , ' temperature ' , ' top_p ' , ' typical_p ' , ' repetition_penalty ' , ' encoder_repetition_penalty ' , ' top_k ' , ' min_length ' , ' no_repeat_ngram_size ' , ' num_beams ' , ' penalty_alpha ' , ' length_penalty ' , ' early_stopping ' ] ]
2023-02-08 02:08:21 +01:00
2023-02-23 17:28:30 +01:00
def upload_soft_prompt ( file ) :
with zipfile . ZipFile ( io . BytesIO ( file ) ) as zf :
zf . extract ( ' meta.json ' )
j = json . loads ( open ( ' meta.json ' , ' r ' ) . read ( ) )
name = j [ ' name ' ]
Path ( ' meta.json ' ) . unlink ( )
with open ( Path ( f ' softprompts/ { name } .zip ' ) , ' wb ' ) as f :
f . write ( file )
return name
2023-03-27 17:16:37 +02:00
def save_prompt ( text ) :
2023-04-05 19:08:54 +02:00
fname = f " { datetime . now ( ) . strftime ( ' % Y- % m- %d - % H % M % S ' ) } .txt "
2023-03-27 17:16:37 +02:00
with open ( Path ( f ' prompts/ { fname } ' ) , ' w ' , encoding = ' utf-8 ' ) as f :
f . write ( text )
2023-03-27 17:43:37 +02:00
return f " Saved to prompts/ { fname } "
2023-03-27 17:16:37 +02:00
def load_prompt ( fname ) :
if fname in [ ' None ' , ' ' ] :
return ' '
else :
with open ( Path ( f ' prompts/ { fname } .txt ' ) , ' r ' , encoding = ' utf-8 ' ) as f :
2023-03-30 03:40:04 +02:00
text = f . read ( )
if text [ - 1 ] == ' \n ' :
text = text [ : - 1 ]
return text
2023-04-05 16:49:59 +02:00
2023-03-27 17:16:37 +02:00
def create_prompt_menus ( ) :
with gr . Row ( ) :
with gr . Column ( ) :
with gr . Row ( ) :
shared . gradio [ ' prompt_menu ' ] = gr . Dropdown ( choices = get_available_prompts ( ) , value = ' None ' , label = ' Prompt ' )
ui . create_refresh_button ( shared . gradio [ ' prompt_menu ' ] , lambda : None , lambda : { ' choices ' : get_available_prompts ( ) } , ' refresh-button ' )
with gr . Column ( ) :
with gr . Column ( ) :
shared . gradio [ ' save_prompt ' ] = gr . Button ( ' Save prompt ' )
shared . gradio [ ' status ' ] = gr . Markdown ( ' Ready ' )
2023-03-27 19:48:43 +02:00
shared . gradio [ ' prompt_menu ' ] . change ( load_prompt , [ shared . gradio [ ' prompt_menu ' ] ] , [ shared . gradio [ ' textbox ' ] ] , show_progress = False )
2023-03-27 17:16:37 +02:00
shared . gradio [ ' save_prompt ' ] . click ( save_prompt , [ shared . gradio [ ' textbox ' ] ] , [ shared . gradio [ ' status ' ] ] , show_progress = False )
2023-04-06 06:54:05 +02:00
def create_model_menus ( ) :
with gr . Row ( ) :
with gr . Column ( ) :
with gr . Row ( ) :
shared . gradio [ ' model_menu ' ] = gr . Dropdown ( choices = available_models , value = shared . model_name , label = ' Model ' )
ui . create_refresh_button ( shared . gradio [ ' model_menu ' ] , lambda : None , lambda : { ' choices ' : get_available_models ( ) } , ' refresh-button ' )
with gr . Column ( ) :
with gr . Row ( ) :
shared . gradio [ ' lora_menu ' ] = gr . Dropdown ( choices = available_loras , value = shared . lora_name , label = ' LoRA ' )
ui . create_refresh_button ( shared . gradio [ ' lora_menu ' ] , lambda : None , lambda : { ' choices ' : get_available_loras ( ) } , ' refresh-button ' )
shared . gradio [ ' model_menu ' ] . change ( load_model_wrapper , shared . gradio [ ' model_menu ' ] , shared . gradio [ ' model_menu ' ] , show_progress = True )
shared . gradio [ ' lora_menu ' ] . change ( load_lora_wrapper , shared . gradio [ ' lora_menu ' ] , shared . gradio [ ' lora_menu ' ] , show_progress = True )
2023-03-15 16:01:32 +01:00
def create_settings_menus ( default_preset ) :
2023-04-06 06:22:15 +02:00
generate_params = load_preset_values ( default_preset if not shared . args . flexgen else ' Naive ' , { } , return_dict = True )
for k in [ ' max_new_tokens ' , ' seed ' , ' stop_at_newline ' , ' chat_prompt_size ' , ' chat_generation_attempts ' ] :
generate_params [ k ] = shared . settings [ k ]
shared . gradio [ ' generate_state ' ] = gr . State ( generate_params )
2023-03-15 16:01:32 +01:00
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 ( ) :
shared . gradio [ ' preset_menu ' ] = gr . Dropdown ( choices = available_presets , value = default_preset if not shared . args . flexgen else ' Naive ' , label = ' Generation parameters preset ' )
ui . create_refresh_button ( shared . gradio [ ' preset_menu ' ] , lambda : None , lambda : { ' choices ' : get_available_presets ( ) } , ' refresh-button ' )
2023-03-27 17:16:37 +02:00
with gr . Column ( ) :
2023-03-31 17:22:07 +02:00
shared . gradio [ ' seed ' ] = gr . Number ( value = shared . settings [ ' seed ' ] , label = ' Seed (-1 for random) ' )
2023-03-27 17:16:37 +02:00
2023-03-15 17:24:54 +01:00
with gr . Row ( ) :
with gr . Column ( ) :
with gr . Box ( ) :
2023-03-18 00:19:04 +01:00
gr . Markdown ( ' Custom generation parameters ([reference](https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.GenerationConfig)) ' )
2023-03-15 17:24:54 +01:00
with gr . Row ( ) :
with gr . Column ( ) :
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 ' )
with gr . Column ( ) :
shared . gradio [ ' repetition_penalty ' ] = gr . Slider ( 1.0 , 1.5 , value = generate_params [ ' repetition_penalty ' ] , step = 0.01 , label = ' repetition_penalty ' )
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 ' ] if shared . args . no_stream else 0 , label = ' min_length ' , interactive = shared . args . no_stream )
shared . gradio [ ' do_sample ' ] = gr . Checkbox ( value = generate_params [ ' do_sample ' ] , label = ' do_sample ' )
with gr . Column ( ) :
with gr . Box ( ) :
gr . Markdown ( ' Contrastive search ' )
shared . gradio [ ' penalty_alpha ' ] = gr . Slider ( 0 , 5 , value = generate_params [ ' penalty_alpha ' ] , label = ' penalty_alpha ' )
with gr . Box ( ) :
gr . Markdown ( ' Beam search (uses a lot of VRAM) ' )
with gr . Row ( ) :
with gr . Column ( ) :
shared . gradio [ ' num_beams ' ] = gr . Slider ( 1 , 20 , step = 1 , value = generate_params [ ' num_beams ' ] , label = ' num_beams ' )
with gr . Column ( ) :
shared . gradio [ ' length_penalty ' ] = gr . Slider ( - 5 , 5 , value = generate_params [ ' length_penalty ' ] , label = ' length_penalty ' )
shared . gradio [ ' early_stopping ' ] = gr . Checkbox ( value = generate_params [ ' early_stopping ' ] , label = ' early_stopping ' )
2023-03-17 15:39:48 +01:00
2023-03-15 17:24:54 +01:00
with gr . Accordion ( ' Soft prompt ' , open = False ) :
2023-02-13 19:25:16 +01:00
with gr . Row ( ) :
2023-02-24 21:31:23 +01:00
shared . gradio [ ' softprompts_menu ' ] = gr . Dropdown ( choices = available_softprompts , value = ' None ' , label = ' Soft prompt ' )
ui . create_refresh_button ( shared . gradio [ ' softprompts_menu ' ] , lambda : None , lambda : { ' choices ' : get_available_softprompts ( ) } , ' refresh-button ' )
2023-02-13 19:25:16 +01:00
2023-02-14 03:34:04 +01:00
gr . Markdown ( ' Upload a soft prompt (.zip format): ' )
2023-02-13 19:25:16 +01:00
with gr . Row ( ) :
2023-02-24 21:31:23 +01:00
shared . gradio [ ' upload_softprompt ' ] = gr . File ( type = ' binary ' , file_types = [ ' .zip ' ] )
2023-02-13 19:25:16 +01:00
2023-04-06 06:22:15 +02:00
shared . gradio [ ' preset_menu ' ] . change ( load_preset_values , [ shared . gradio [ k ] for k in [ ' preset_menu ' , ' generate_state ' ] ] , [ shared . gradio [ k ] for k in [ ' generate_state ' , ' do_sample ' , ' temperature ' , ' top_p ' , ' typical_p ' , ' repetition_penalty ' , ' encoder_repetition_penalty ' , ' top_k ' , ' min_length ' , ' no_repeat_ngram_size ' , ' num_beams ' , ' penalty_alpha ' , ' length_penalty ' , ' early_stopping ' ] ] )
shared . gradio [ ' softprompts_menu ' ] . change ( load_soft_prompt , shared . gradio [ ' softprompts_menu ' ] , shared . gradio [ ' softprompts_menu ' ] , show_progress = True )
shared . gradio [ ' upload_softprompt ' ] . upload ( upload_soft_prompt , shared . gradio [ ' upload_softprompt ' ] , shared . gradio [ ' softprompts_menu ' ] )
2023-02-08 02:08:21 +01: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 : ] :
exec ( f " shared.args. { k } = False " )
if interface_mode != " default " :
exec ( f " shared.args. { interface_mode } = True " )
2023-04-01 04:41:10 +02:00
for k in bool_list :
2023-03-16 16:44:03 +01:00
exec ( f " shared.args. { k } = False " )
2023-04-01 04:41:10 +02:00
for k in bool_active :
2023-03-16 16:44:03 +01:00
exec ( f " shared.args. { k } = True " )
shared . need_restart = True
2023-01-22 04:49:59 +01:00
available_models = get_available_models ( )
available_presets = get_available_presets ( )
available_characters = get_available_characters ( )
2023-02-13 19:25:16 +01:00
available_softprompts = get_available_softprompts ( )
2023-03-17 01:31:39 +01:00
available_loras = get_available_loras ( )
2023-02-23 17:03:52 +01:00
2023-03-02 15:25:04 +01:00
# Default extensions
2023-02-23 17:03:52 +01:00
extensions_module . available_extensions = get_available_extensions ( )
2023-04-02 01:14:43 +02:00
if shared . is_chat ( ) :
2023-02-28 06:20:11 +01:00
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-01-22 04:49:59 +01:00
2023-03-02 15:25:04 +01:00
# Default model
2023-02-23 16:05:25 +01:00
if shared . args . model is not None :
shared . model_name = shared . args . model
2023-01-06 23:56:44 +01:00
else :
2023-01-07 02:05:37 +01:00
if len ( available_models ) == 0 :
2023-02-24 21:31:23 +01:00
print ( ' No models are available! Please download at least one. ' )
2023-01-30 18:17:12 +01:00
sys . exit ( 0 )
2023-01-06 23:56:44 +01:00
elif len ( available_models ) == 1 :
i = 0
else :
2023-02-24 21:31:23 +01:00
print ( ' The following models are available: \n ' )
2023-02-23 18:31:28 +01:00
for i , model in enumerate ( available_models ) :
2023-02-24 21:31:23 +01:00
print ( f ' { i + 1 } . { model } ' )
print ( f ' \n Which one do you want to load? 1- { len ( available_models ) } \n ' )
2023-01-06 23:56:44 +01:00
i = int ( input ( ) ) - 1
2023-01-09 16:56:54 +01:00
print ( )
2023-02-23 16:05:25 +01:00
shared . model_name = available_models [ i ]
shared . model , shared . tokenizer = load_model ( shared . model_name )
2023-03-17 15:24:52 +01:00
if shared . args . lora :
2023-03-24 20:30:32 +01:00
add_lora_to_model ( shared . args . lora )
2023-01-06 23:56:44 +01:00
2023-03-02 15:25:04 +01:00
# Default UI settings
default_preset = shared . settings [ ' presets ' ] [ next ( ( k for k in shared . settings [ ' presets ' ] if re . match ( k . lower ( ) , shared . model_name . lower ( ) ) ) , ' default ' ) ]
2023-03-26 05:11:33 +02:00
if shared . lora_name != " None " :
2023-03-30 03:40:04 +02:00
default_text = load_prompt ( shared . settings [ ' lora_prompts ' ] [ next ( ( k for k in shared . settings [ ' lora_prompts ' ] if re . match ( k . lower ( ) , shared . lora_name . lower ( ) ) ) , ' default ' ) ] )
2023-03-26 05:11:33 +02:00
else :
2023-03-30 03:40:04 +02:00
default_text = load_prompt ( shared . settings [ ' prompts ' ] [ next ( ( k for k in shared . settings [ ' prompts ' ] if re . match ( k . lower ( ) , shared . model_name . lower ( ) ) ) , ' default ' ) ] )
2023-03-09 13:44:08 +01:00
title = ' Text generation web UI '
2023-01-19 18:03:47 +01:00
2023-03-16 03:29:56 +01:00
def create_interface ( ) :
2023-03-15 16:01:32 +01:00
2023-03-16 03:29:56 +01:00
gen_events = [ ]
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-04-02 01:14:43 +02:00
with gr . Blocks ( css = ui . css if not shared . is_chat ( ) else ui . css + ui . chat_css , analytics_enabled = False , title = title ) as shared . gradio [ ' interface ' ] :
if shared . is_chat ( ) :
2023-04-02 23:05:21 +02:00
shared . gradio [ ' Chat input ' ] = gr . State ( )
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Text generation " , elem_id = " main " ) :
2023-04-05 19:22:32 +02:00
shared . gradio [ ' display ' ] = gr . HTML ( value = chat_html_wrapper ( shared . history [ ' visible ' ] , shared . settings [ ' name1 ' ] , shared . settings [ ' name2 ' ] , ' cai-chat ' ) )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' textbox ' ] = gr . Textbox ( label = ' Input ' )
with gr . Row ( ) :
shared . gradio [ ' Generate ' ] = gr . Button ( ' Generate ' )
2023-03-27 17:16:37 +02:00
shared . gradio [ ' Stop ' ] = gr . Button ( ' Stop ' , elem_id = " stop " )
2023-03-16 03:29:56 +01:00
with gr . Row ( ) :
shared . gradio [ ' Impersonate ' ] = gr . Button ( ' Impersonate ' )
shared . gradio [ ' Regenerate ' ] = gr . Button ( ' Regenerate ' )
with gr . Row ( ) :
shared . gradio [ ' Copy last reply ' ] = gr . Button ( ' Copy last reply ' )
shared . gradio [ ' Replace last reply ' ] = gr . Button ( ' Replace last reply ' )
shared . gradio [ ' Remove last ' ] = gr . Button ( ' Remove last ' )
2023-03-15 16:01:32 +01:00
2023-03-16 03:29:56 +01:00
shared . gradio [ ' Clear history ' ] = gr . Button ( ' Clear history ' )
shared . gradio [ ' Clear history-confirm ' ] = gr . Button ( ' Confirm ' , variant = " stop " , visible = False )
shared . gradio [ ' Clear history-cancel ' ] = gr . Button ( ' Cancel ' , visible = False )
2023-01-15 22:16:46 +01:00
2023-04-05 16:49:59 +02:00
shared . gradio [ " Chat mode " ] = gr . Radio ( choices = [ " cai-chat " , " chat " , " instruct " ] , value = " cai-chat " , label = " Mode " )
shared . gradio [ " Instruction templates " ] = gr . Dropdown ( choices = get_available_instruction_templates ( ) , label = " Instruction template " , value = " None " , visible = False )
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Character " , elem_id = " chat-settings " ) :
2023-04-05 03:28:49 +02:00
with gr . Row ( ) :
with gr . Column ( scale = 8 ) :
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 ' )
2023-04-05 16:49:59 +02:00
shared . gradio [ ' greeting ' ] = gr . Textbox ( value = shared . settings [ ' greeting ' ] , lines = 4 , label = ' Greeting ' )
shared . gradio [ ' context ' ] = gr . Textbox ( value = shared . settings [ ' context ' ] , lines = 4 , label = ' Context ' )
shared . gradio [ ' end_of_turn ' ] = gr . Textbox ( value = shared . settings [ " end_of_turn " ] , lines = 1 , label = ' End of turn string ' )
2023-04-05 03:28:49 +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
with gr . Row ( ) :
shared . gradio [ ' character_menu ' ] = gr . Dropdown ( choices = available_characters , value = ' None ' , label = ' Character ' , elem_id = ' character-menu ' )
ui . create_refresh_button ( shared . gradio [ ' character_menu ' ] , lambda : None , lambda : { ' choices ' : get_available_characters ( ) } , ' refresh-button ' )
with gr . Row ( ) :
with gr . Tab ( ' Chat history ' ) :
with gr . Row ( ) :
with gr . Column ( ) :
gr . Markdown ( ' Upload ' )
shared . gradio [ ' upload_chat_history ' ] = gr . File ( type = ' binary ' , file_types = [ ' .json ' , ' .txt ' ] )
with gr . Column ( ) :
gr . Markdown ( ' Download ' )
shared . gradio [ ' download ' ] = gr . File ( )
shared . gradio [ ' download_button ' ] = gr . Button ( value = ' Click me ' )
with gr . Tab ( ' Upload character ' ) :
2023-04-03 23:01:45 +02:00
gr . Markdown ( " # JSON format " )
2023-03-16 03:29:56 +01:00
with gr . Row ( ) :
with gr . Column ( ) :
gr . Markdown ( ' 1. Select the JSON file ' )
shared . gradio [ ' upload_json ' ] = gr . File ( type = ' binary ' , file_types = [ ' .json ' ] )
with gr . Column ( ) :
gr . Markdown ( ' 2. Select your character \' s profile picture (optional) ' )
shared . gradio [ ' upload_img_bot ' ] = gr . File ( type = ' binary ' , file_types = [ ' image ' ] )
shared . gradio [ ' Upload character ' ] = gr . Button ( value = ' Submit ' )
2023-04-03 23:01:45 +02:00
gr . Markdown ( " # TavernAI PNG format " )
shared . gradio [ ' upload_img_tavern ' ] = gr . File ( type = ' binary ' , file_types = [ ' image ' ] )
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Parameters " , elem_id = " parameters " ) :
with gr . Box ( ) :
gr . Markdown ( " Chat parameters " )
2023-02-16 00:55:32 +01:00
with gr . Row ( ) :
with gr . Column ( ) :
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 ' ] )
shared . gradio [ ' chat_prompt_size_slider ' ] = gr . Slider ( minimum = shared . settings [ ' chat_prompt_size_min ' ] , maximum = shared . settings [ ' chat_prompt_size_max ' ] , step = 1 , label = ' Maximum prompt size in tokens ' , value = shared . settings [ ' chat_prompt_size ' ] )
2023-02-16 00:55:32 +01:00
with gr . Column ( ) :
2023-03-16 03:29:56 +01: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) ' )
2023-04-06 06:22:15 +02:00
shared . gradio [ ' stop_at_newline ' ] = gr . Checkbox ( value = shared . settings [ ' stop_at_newline ' ] , label = ' Stop generating at new line character? ' )
2023-03-16 03:29:56 +01:00
create_settings_menus ( default_preset )
2023-04-06 06:22:15 +02:00
shared . input_params = [ shared . gradio [ k ] for k in [ ' Chat input ' , ' generate_state ' , ' name1 ' , ' name2 ' , ' context ' , ' Chat mode ' , ' end_of_turn ' ] ]
2023-03-16 03:29:56 +01:00
2023-04-02 23:05:21 +02:00
def set_chat_input ( textbox ) :
return textbox , " "
gen_events . append ( shared . gradio [ ' Generate ' ] . click ( set_chat_input , shared . gradio [ ' textbox ' ] , [ shared . gradio [ ' Chat input ' ] , shared . gradio [ ' textbox ' ] ] , show_progress = False ) )
2023-04-05 16:49:59 +02:00
gen_events . append ( shared . gradio [ ' Generate ' ] . click ( chat . cai_chatbot_wrapper , shared . input_params , shared . gradio [ ' display ' ] , show_progress = shared . args . no_stream ) )
2023-04-02 23:05:21 +02:00
gen_events . append ( shared . gradio [ ' textbox ' ] . submit ( set_chat_input , shared . gradio [ ' textbox ' ] , [ shared . gradio [ ' Chat input ' ] , shared . gradio [ ' textbox ' ] ] , show_progress = False ) )
2023-04-05 16:49:59 +02:00
gen_events . append ( shared . gradio [ ' textbox ' ] . submit ( chat . cai_chatbot_wrapper , shared . input_params , shared . gradio [ ' display ' ] , show_progress = shared . args . no_stream ) )
2023-03-16 03:29:56 +01:00
gen_events . append ( shared . gradio [ ' Regenerate ' ] . click ( chat . regenerate_wrapper , shared . input_params , shared . gradio [ ' display ' ] , show_progress = shared . args . no_stream ) )
gen_events . append ( shared . gradio [ ' Impersonate ' ] . click ( chat . impersonate_wrapper , shared . input_params , shared . gradio [ ' textbox ' ] , show_progress = shared . args . no_stream ) )
2023-03-27 18:23:59 +02:00
shared . gradio [ ' Stop ' ] . click ( stop_everything_event , [ ] , [ ] , queue = False , cancels = gen_events if shared . args . no_stream else None )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' Copy last reply ' ] . click ( chat . send_last_reply_to_input , [ ] , shared . gradio [ ' textbox ' ] , show_progress = shared . args . no_stream )
2023-04-05 16:49:59 +02:00
shared . gradio [ ' Replace last reply ' ] . click ( chat . replace_last_reply , [ shared . gradio [ k ] for k in [ ' textbox ' , ' name1 ' , ' name2 ' , ' Chat mode ' ] ] , shared . gradio [ ' display ' ] , show_progress = shared . args . no_stream )
2023-03-16 03:29:56 +01:00
# Clear history with confirmation
clear_arr = [ shared . gradio [ k ] for k in [ ' Clear history-confirm ' , ' Clear history ' , ' Clear history-cancel ' ] ]
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-confirm ' ] . click ( lambda : [ gr . update ( visible = False ) , gr . update ( visible = True ) , gr . update ( visible = False ) ] , None , clear_arr )
2023-04-05 16:49:59 +02:00
shared . gradio [ ' Clear history-confirm ' ] . click ( chat . clear_chat_log , [ shared . gradio [ k ] for k in [ ' name1 ' , ' name2 ' , ' greeting ' , ' Chat mode ' ] ] , shared . gradio [ ' display ' ] )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' Clear history-cancel ' ] . click ( lambda : [ gr . update ( visible = False ) , gr . update ( visible = True ) , gr . update ( visible = False ) ] , None , clear_arr )
2023-04-05 16:49:59 +02:00
shared . gradio [ ' Chat mode ' ] . change ( lambda x : gr . update ( visible = x == ' instruct ' ) , shared . gradio [ ' Chat mode ' ] , shared . gradio [ ' Instruction templates ' ] )
2023-03-16 03:29:56 +01:00
2023-04-05 16:49:59 +02:00
shared . gradio [ ' Remove last ' ] . click ( chat . remove_last_message , [ shared . gradio [ k ] for k in [ ' name1 ' , ' name2 ' , ' Chat mode ' ] ] , [ shared . gradio [ ' display ' ] , shared . gradio [ ' textbox ' ] ] , show_progress = False )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' download_button ' ] . click ( chat . save_history , inputs = [ ] , outputs = [ shared . gradio [ ' download ' ] ] )
shared . gradio [ ' Upload character ' ] . click ( chat . upload_character , [ shared . gradio [ ' upload_json ' ] , shared . gradio [ ' upload_img_bot ' ] ] , [ shared . gradio [ ' character_menu ' ] ] )
# Clearing stuff and saving the history
for i in [ ' Generate ' , ' Regenerate ' , ' Replace last reply ' ] :
shared . gradio [ i ] . click ( lambda x : ' ' , shared . gradio [ ' textbox ' ] , shared . gradio [ ' textbox ' ] , show_progress = False )
shared . gradio [ i ] . click ( lambda : chat . save_history ( timestamp = False ) , [ ] , [ ] , show_progress = False )
shared . gradio [ ' Clear history-confirm ' ] . click ( lambda : chat . save_history ( timestamp = False ) , [ ] , [ ] , show_progress = False )
shared . gradio [ ' textbox ' ] . submit ( lambda x : ' ' , shared . gradio [ ' textbox ' ] , shared . gradio [ ' textbox ' ] , show_progress = False )
shared . gradio [ ' textbox ' ] . submit ( lambda : chat . save_history ( timestamp = False ) , [ ] , [ ] , show_progress = False )
2023-04-05 19:22:32 +02:00
shared . gradio [ ' character_menu ' ] . change ( chat . load_character , [ shared . gradio [ k ] for k in [ ' character_menu ' , ' name1 ' , ' name2 ' , ' Chat mode ' ] ] , [ shared . gradio [ k ] for k in [ ' name1 ' , ' name2 ' , ' character_picture ' , ' greeting ' , ' context ' , ' end_of_turn ' , ' display ' ] ] )
shared . gradio [ ' Instruction templates ' ] . change ( lambda character , name1 , name2 , mode : chat . load_character ( character , name1 , name2 , mode ) , [ shared . gradio [ k ] for k in [ ' Instruction templates ' , ' name1 ' , ' name2 ' , ' Chat mode ' ] ] , [ shared . gradio [ k ] for k in [ ' name1 ' , ' name2 ' , ' character_picture ' , ' greeting ' , ' context ' , ' end_of_turn ' , ' display ' ] ] )
2023-04-05 16:49:59 +02:00
shared . gradio [ ' upload_chat_history ' ] . upload ( chat . load_history , [ shared . gradio [ k ] for k in [ ' upload_chat_history ' , ' name1 ' , ' name2 ' ] ] , [ ] )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' upload_img_tavern ' ] . upload ( chat . upload_tavern_character , [ shared . gradio [ ' upload_img_tavern ' ] , shared . gradio [ ' name1 ' ] , shared . gradio [ ' name2 ' ] ] , [ shared . gradio [ ' character_menu ' ] ] )
2023-04-05 16:49:59 +02:00
shared . gradio [ ' your_picture ' ] . change ( chat . upload_your_profile_picture , [ shared . gradio [ k ] for k in [ ' your_picture ' , ' name1 ' , ' name2 ' , ' Chat mode ' ] ] , shared . gradio [ ' display ' ] )
2023-03-16 03:29:56 +01:00
2023-04-05 16:49:59 +02:00
reload_inputs = [ shared . gradio [ k ] for k in [ ' name1 ' , ' name2 ' , ' Chat mode ' ] ]
shared . gradio [ ' upload_chat_history ' ] . upload ( chat . redraw_html , reload_inputs , [ shared . gradio [ ' display ' ] ] )
shared . gradio [ ' Stop ' ] . click ( chat . redraw_html , reload_inputs , [ shared . gradio [ ' display ' ] ] )
shared . gradio [ ' Instruction templates ' ] . change ( chat . redraw_html , reload_inputs , [ shared . gradio [ ' display ' ] ] )
shared . gradio [ ' Chat mode ' ] . change ( chat . redraw_html , reload_inputs , [ shared . gradio [ ' display ' ] ] )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' interface ' ] . load ( None , None , None , _js = f " () => {{ { ui . main_js + ui . chat_js } }} " )
2023-03-30 02:47:36 +02:00
shared . gradio [ ' interface ' ] . load ( lambda : chat . load_default_history ( shared . settings [ ' name1 ' ] , shared . settings [ ' name2 ' ] ) , None , None )
2023-04-05 16:49:59 +02:00
shared . gradio [ ' interface ' ] . load ( chat . redraw_html , reload_inputs , [ shared . gradio [ ' display ' ] ] , show_progress = True )
2023-03-16 03:29:56 +01:00
elif shared . args . notebook :
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-03-31 05:43:00 +02:00
shared . gradio [ ' textbox ' ] = gr . Textbox ( value = default_text , elem_id = " textbox " , lines = 27 )
2023-03-27 03:20:30 +02:00
with gr . Tab ( ' Markdown ' ) :
shared . gradio [ ' markdown ' ] = gr . Markdown ( )
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-03-27 17:30:57 +02:00
with gr . Column ( ) :
with gr . Row ( ) :
shared . gradio [ ' Generate ' ] = gr . Button ( ' Generate ' )
shared . gradio [ ' Stop ' ] = gr . Button ( ' Stop ' )
with gr . Column ( ) :
pass
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-03-27 17:16:37 +02:00
create_prompt_menus ( )
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-06 06:22:15 +02:00
shared . input_params = [ shared . gradio [ k ] for k in [ ' textbox ' , ' generate_state ' ] ]
2023-03-16 03:29:56 +01:00
output_params = [ shared . gradio [ k ] for k in [ ' textbox ' , ' markdown ' , ' html ' ] ]
2023-04-06 06:22:15 +02:00
gen_events . append ( shared . gradio [ ' Generate ' ] . click ( generate_reply , shared . input_params , output_params , show_progress = shared . args . no_stream ) )
2023-03-16 03:29:56 +01:00
gen_events . append ( shared . gradio [ ' textbox ' ] . submit ( generate_reply , shared . input_params , output_params , show_progress = shared . args . no_stream ) )
2023-03-27 18:23:59 +02:00
shared . gradio [ ' Stop ' ] . click ( stop_everything_event , [ ] , [ ] , queue = False , cancels = gen_events if shared . args . no_stream else None )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' interface ' ] . load ( None , None , None , _js = f " () => {{ { ui . main_js } }} " )
else :
with gr . Tab ( " Text generation " , elem_id = " main " ) :
with gr . Row ( ) :
with gr . Column ( ) :
2023-03-31 05:43:00 +02:00
shared . gradio [ ' textbox ' ] = gr . Textbox ( value = default_text , lines = 21 , 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 ' ] )
shared . gradio [ ' Generate ' ] = gr . Button ( ' Generate ' )
with gr . Row ( ) :
with gr . Column ( ) :
shared . gradio [ ' Continue ' ] = gr . Button ( ' Continue ' )
with gr . Column ( ) :
shared . gradio [ ' Stop ' ] = gr . Button ( ' Stop ' )
2023-01-29 13:48:18 +01:00
2023-03-27 17:16:37 +02:00
create_prompt_menus ( )
2023-01-19 02:44:47 +01:00
2023-03-16 03:29:56 +01:00
with gr . Column ( ) :
with gr . Tab ( ' Raw ' ) :
2023-03-31 05:43:00 +02:00
shared . gradio [ ' output_textbox ' ] = gr . Textbox ( lines = 27 , label = ' Output ' )
2023-03-16 03:29:56 +01:00
with gr . Tab ( ' Markdown ' ) :
shared . gradio [ ' markdown ' ] = gr . Markdown ( )
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-06 06:22:15 +02:00
shared . input_params = [ shared . gradio [ k ] for k in [ ' textbox ' , ' generate_state ' ] ]
2023-03-16 03:29:56 +01:00
output_params = [ shared . gradio [ k ] for k in [ ' output_textbox ' , ' markdown ' , ' html ' ] ]
2023-04-06 06:22:15 +02:00
gen_events . append ( shared . gradio [ ' Generate ' ] . click ( generate_reply , shared . input_params , output_params , show_progress = shared . args . no_stream ) )
2023-03-16 03:29:56 +01:00
gen_events . append ( shared . gradio [ ' textbox ' ] . submit ( generate_reply , shared . input_params , output_params , show_progress = shared . args . no_stream ) )
gen_events . append ( shared . gradio [ ' Continue ' ] . click ( generate_reply , [ shared . gradio [ ' output_textbox ' ] ] + shared . input_params [ 1 : ] , output_params , show_progress = shared . args . no_stream ) )
2023-03-27 18:23:59 +02:00
shared . gradio [ ' Stop ' ] . click ( stop_everything_event , [ ] , [ ] , queue = False , cancels = gen_events if shared . args . no_stream else None )
2023-03-16 03:29:56 +01:00
shared . gradio [ ' interface ' ] . load ( None , None , None , _js = f " () => {{ { ui . main_js } }} " )
2023-04-06 06:54:05 +02:00
with gr . Tab ( " Model " , elem_id = " model-tab " ) :
create_model_menus ( )
2023-03-27 17:32:32 +02:00
with gr . Tab ( " Training " , elem_id = " training-tab " ) :
training . create_train_interface ( )
2023-03-16 03:29:56 +01:00
with gr . Tab ( " Interface mode " , elem_id = " interface-mode " ) :
modes = [ " default " , " notebook " , " chat " , " cai_chat " ]
current_mode = " default "
2023-03-16 16:44:03 +01:00
for mode in modes [ 1 : ] :
if eval ( f " shared.args. { mode } " ) :
2023-03-16 03:29:56 +01:00
current_mode = mode
2023-03-16 16:44:03 +01:00
break
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 ]
bool_active = [ k for k in bool_list if vars ( shared . args ) [ k ] ]
2023-03-16 03:29:56 +01:00
2023-03-16 03:43:35 +01:00
gr . Markdown ( " *Experimental* " )
2023-03-16 16:44:03 +01:00
shared . gradio [ ' interface_modes_menu ' ] = gr . Dropdown ( choices = modes , value = current_mode , label = " Mode " )
shared . gradio [ ' extensions_menu ' ] = gr . CheckboxGroup ( choices = get_available_extensions ( ) , value = shared . args . extensions , label = " Available extensions " )
2023-04-01 04:41:10 +02:00
shared . gradio [ ' bool_menu ' ] = gr . CheckboxGroup ( choices = bool_list , value = bool_active , label = " Boolean command-line flags " )
2023-03-16 16:44:03 +01:00
shared . gradio [ ' reset_interface ' ] = gr . Button ( " Apply and restart the interface " , type = " primary " )
2023-03-16 14:19:00 +01:00
2023-04-01 04:41:10 +02:00
shared . gradio [ ' reset_interface ' ] . click ( set_interface_arguments , [ shared . gradio [ k ] for k in [ ' interface_modes_menu ' , ' extensions_menu ' , ' bool_menu ' ] ] , None )
2023-03-28 18:25:58 +02:00
shared . gradio [ ' reset_interface ' ] . click ( lambda : None , None , None , _js = ' () => { document.body.innerHTML= \' <h1 style= " font-family:monospace;margin-top:20 % ;color:lightgray;text-align:center; " >Reloading...</h1> \' ; setTimeout(function() { location.reload()},2500); return []} ' )
2023-03-27 18:41:06 +02:00
2023-03-16 03:29:56 +01:00
if shared . args . extensions is not None :
extensions_module . create_extensions_block ( )
2023-04-06 06:22:15 +02:00
def change_dict_value ( d , key , value ) :
d [ key ] = value
return d
for k in [ ' do_sample ' , ' temperature ' , ' top_p ' , ' typical_p ' , ' repetition_penalty ' , ' encoder_repetition_penalty ' , ' top_k ' , ' min_length ' , ' no_repeat_ngram_size ' , ' num_beams ' , ' penalty_alpha ' , ' length_penalty ' , ' early_stopping ' , ' max_new_tokens ' , ' seed ' , ' stop_at_newline ' , ' chat_prompt_size_slider ' , ' chat_generation_attempts ' ] :
if k not in shared . gradio :
continue
if type ( shared . gradio [ k ] ) in [ gr . Checkbox , gr . Number ] :
shared . gradio [ k ] . change ( lambda state , value , copy = k : change_dict_value ( state , copy , value ) , inputs = [ shared . gradio [ ' generate_state ' ] , shared . gradio [ k ] ] , outputs = shared . gradio [ ' generate_state ' ] )
else :
shared . gradio [ k ] . release ( lambda state , value , copy = k : change_dict_value ( state , copy , value ) , inputs = [ shared . gradio [ ' generate_state ' ] , shared . gradio [ k ] ] , outputs = shared . gradio [ ' generate_state ' ] )
if not shared . is_chat ( ) :
api . create_apis ( )
2023-03-28 04:29:52 +02:00
# Authentication
auth = None
if shared . args . gradio_auth_path is not None :
gradio_auth_creds = [ ]
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 ( ) ]
auth = [ tuple ( cred . split ( ' : ' ) ) for cred in gradio_auth_creds ]
2023-03-16 03:29:56 +01:00
# Launch the interface
shared . gradio [ ' interface ' ] . queue ( )
if shared . args . listen :
2023-03-28 04:29:52 +02:00
shared . gradio [ ' interface ' ] . launch ( prevent_thread_lock = True , share = shared . args . share , server_name = ' 0.0.0.0 ' , server_port = shared . args . listen_port , inbrowser = shared . args . auto_launch , auth = auth )
2023-03-16 00:44:34 +01:00
else :
2023-03-28 04:29:52 +02:00
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
create_interface ( )
2023-02-08 02:08:21 +01:00
while True :
time . sleep ( 0.5 )
2023-03-16 03:29:56 +01:00
if shared . need_restart :
shared . need_restart = False
shared . gradio [ ' interface ' ] . close ( )
create_interface ( )