2023-08-06 20:49:27 -04:00
import json
from functools import partial
from pathlib import Path
import gradio as gr
from PIL import Image
2023-08-13 21:14:09 -04:00
from modules import chat , prompts , shared , ui , utils
2023-08-06 20:49:27 -04:00
from modules . html_generator import chat_html_wrapper
from modules . text_generation import stop_everything_event
from modules . utils import gradio
2023-08-14 10:46:07 -04:00
inputs = ( ' Chat input ' , ' interface_state ' )
2023-08-13 00:12:15 -04:00
reload_arr = ( ' history ' , ' name1 ' , ' name2 ' , ' mode ' , ' chat_style ' )
2023-09-21 16:19:32 -04:00
clear_arr = ( ' delete_chat-confirm ' , ' delete_chat ' , ' delete_chat-cancel ' )
2023-08-13 00:12:15 -04:00
2023-08-06 20:49:27 -04:00
def create_ui ( ) :
2023-08-11 19:33:15 -04:00
shared . gradio [ ' Chat input ' ] = gr . State ( )
shared . gradio [ ' dummy ' ] = gr . State ( )
shared . gradio [ ' history ' ] = gr . State ( { ' internal ' : [ ] , ' visible ' : [ ] } )
2023-08-06 20:49:27 -04:00
2023-09-17 21:33:00 -04:00
with gr . Tab ( ' Chat ' , elem_id = ' chat-tab ' , elem_classes = ( " old-ui " if shared . args . chat_buttons else None ) ) :
2023-08-06 20:49:27 -04:00
with gr . Row ( ) :
2023-09-17 21:33:00 -04:00
with gr . Column ( elem_id = ' chat-col ' ) :
2023-09-21 16:19:32 -04:00
shared . gradio [ ' display ' ] = gr . HTML ( value = chat_html_wrapper ( { ' internal ' : [ ] , ' visible ' : [ ] } , ' ' , ' ' , ' chat ' , ' cai-chat ' ) )
2023-09-13 01:36:12 -04:00
2023-09-17 21:33:00 -04:00
with gr . Row ( elem_id = " chat-input-row " ) :
with gr . Column ( scale = 1 , elem_id = ' gr-hover-container ' ) :
2023-09-14 06:06:42 -04:00
gr . HTML ( value = ' <div class= " hover-element " onclick= " void(0) " ><span style= " width: 100px; display: block " id= " hover-element-button " >☰</span><div class= " hover-menu " id= " hover-menu " ></div> ' , elem_id = ' gr-hover ' )
2023-09-17 21:33:00 -04:00
with gr . Column ( scale = 10 , elem_id = ' chat-input-container ' ) :
2023-09-14 06:06:42 -04:00
shared . gradio [ ' textbox ' ] = gr . Textbox ( label = ' ' , placeholder = ' Send a message ' , elem_id = ' chat-input ' , elem_classes = [ ' add_scrollbar ' ] )
shared . gradio [ ' show_controls ' ] = gr . Checkbox ( value = shared . settings [ ' show_controls ' ] , label = ' Show controls (Ctrl+S) ' , elem_id = ' show-controls ' )
shared . gradio [ ' typing-dots ' ] = gr . HTML ( value = ' <div class= " typing " ><span></span><span class= " dot1 " ></span><span class= " dot2 " ></span></div> ' , label = ' typing ' , elem_id = ' typing-container ' )
2023-09-17 21:33:00 -04:00
with gr . Column ( scale = 1 , elem_id = ' generate-stop-container ' ) :
2023-09-14 06:06:42 -04:00
with gr . Row ( ) :
2023-09-14 08:16:44 -04:00
shared . gradio [ ' Stop ' ] = gr . Button ( ' Stop ' , elem_id = ' stop ' , visible = False )
shared . gradio [ ' Generate ' ] = gr . Button ( ' Generate ' , elem_id = ' Generate ' , variant = ' primary ' )
2023-09-13 01:36:12 -04:00
# Hover menu buttons
2023-09-17 21:33:00 -04:00
with gr . Column ( elem_id = ' chat-buttons ' ) :
2023-09-15 23:39:37 -04:00
with gr . Row ( ) :
shared . gradio [ ' Regenerate ' ] = gr . Button ( ' Regenerate (Ctrl + Enter) ' , elem_id = ' Regenerate ' )
shared . gradio [ ' Continue ' ] = gr . Button ( ' Continue (Alt + Enter) ' , elem_id = ' Continue ' )
shared . gradio [ ' Remove last ' ] = gr . Button ( ' Remove last reply (Ctrl + Shift + Backspace) ' , elem_id = ' Remove-last ' )
2023-09-21 16:19:32 -04:00
2023-09-15 23:39:37 -04:00
with gr . Row ( ) :
shared . gradio [ ' Replace last reply ' ] = gr . Button ( ' Replace last reply (Ctrl + Shift + L) ' , elem_id = ' Replace-last ' )
shared . gradio [ ' Copy last reply ' ] = gr . Button ( ' Copy last reply (Ctrl + Shift + K) ' , elem_id = ' Copy-last ' )
shared . gradio [ ' Impersonate ' ] = gr . Button ( ' Impersonate (Ctrl + Shift + M) ' , elem_id = ' Impersonate ' )
2023-09-21 16:19:32 -04:00
2023-09-15 23:39:37 -04:00
with gr . Row ( ) :
shared . gradio [ ' Send dummy message ' ] = gr . Button ( ' Send dummy message ' )
shared . gradio [ ' Send dummy reply ' ] = gr . Button ( ' Send dummy reply ' )
2023-09-21 16:19:32 -04:00
2023-09-15 23:39:37 -04:00
with gr . Row ( ) :
2023-09-21 16:19:32 -04:00
shared . gradio [ ' Start new chat ' ] = gr . Button ( ' Start new chat ' )
2023-09-15 23:39:37 -04:00
with gr . Row ( ) :
shared . gradio [ ' send-chat-to-default ' ] = gr . Button ( ' Send to default ' )
shared . gradio [ ' send-chat-to-notebook ' ] = gr . Button ( ' Send to notebook ' )
2023-08-20 22:54:59 -04:00
2023-09-21 16:19:32 -04:00
with gr . Row ( ) :
shared . gradio [ ' unique_id ' ] = gr . Dropdown ( label = ' Past chats ' , elem_classes = [ ' slim-dropdown ' ] )
shared . gradio [ ' delete_chat ' ] = gr . Button ( ' 🗑️ ' , elem_classes = ' refresh-button ' )
shared . gradio [ ' delete_chat-cancel ' ] = gr . Button ( ' Cancel ' , visible = False , elem_classes = ' refresh-button ' )
shared . gradio [ ' delete_chat-confirm ' ] = gr . Button ( ' Confirm ' , variant = ' stop ' , visible = False , elem_classes = ' refresh-button ' )
2023-08-06 20:49:27 -04: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 ( ) :
2023-09-21 16:19:32 -04:00
shared . gradio [ ' mode ' ] = gr . Radio ( choices = [ ' chat ' , ' chat-instruct ' , ' instruct ' ] , value = ' chat ' , label = ' Mode ' , info = ' Defines how the chat prompt is generated. In instruct and chat-instruct modes, the instruction template selected under Parameters > Instruction template must match the current model. ' , elem_id = ' chat-mode ' )
2023-08-06 20:49:27 -04:00
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-08-13 00:12:15 -04:00
def create_chat_settings_ui ( ) :
with gr . Tab ( ' Character ' ) :
with gr . Row ( ) :
with gr . Column ( scale = 8 ) :
2023-08-06 20:49:27 -04:00
with gr . Row ( ) :
2023-09-21 16:19:32 -04:00
shared . gradio [ ' character_menu ' ] = gr . Dropdown ( value = ' ' , choices = utils . get_available_characters ( ) , label = ' Character ' , elem_id = ' character-menu ' , info = ' Used in chat and chat-instruct modes. ' , elem_classes = ' slim-dropdown ' )
2023-08-13 00:12:15 -04: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-08-06 20:49:27 -04:00
2023-08-13 00:12:15 -04:00
shared . gradio [ ' name1 ' ] = gr . Textbox ( value = shared . settings [ ' name1 ' ] , lines = 1 , label = ' Your name ' )
2023-09-21 16:19:32 -04:00
shared . gradio [ ' name2 ' ] = gr . Textbox ( value = ' ' , lines = 1 , label = ' Character \' s name ' )
shared . gradio [ ' context ' ] = gr . Textbox ( value = ' ' , lines = 10 , label = ' Context ' , elem_classes = [ ' add_scrollbar ' ] )
shared . gradio [ ' greeting ' ] = gr . Textbox ( value = ' ' , lines = 5 , label = ' Greeting ' , elem_classes = [ ' add_scrollbar ' ] )
2023-08-13 00:12:15 -04: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 )
with gr . Tab ( ' Instruction template ' ) :
with gr . Row ( ) :
2023-08-06 20:49:27 -04:00
with gr . Row ( ) :
2023-08-13 00:12:15 -04: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 ' )
2023-09-15 12:17:37 -04:00
shared . gradio [ ' context_instruct ' ] = gr . Textbox ( value = ' ' , lines = 4 , label = ' Context ' , elem_classes = [ ' add_scrollbar ' ] )
shared . gradio [ ' turn_template ' ] = gr . Textbox ( value = ' ' , lines = 1 , label = ' Turn template ' , info = ' Used to precisely define the placement of spaces and new line characters in instruction prompts. ' , elem_classes = [ ' add_scrollbar ' ] )
2023-08-13 21:14:09 -04:00
with gr . Row ( ) :
shared . gradio [ ' send_instruction_to_default ' ] = gr . Button ( ' Send to default ' , elem_classes = [ ' small-button ' ] )
shared . gradio [ ' send_instruction_to_notebook ' ] = gr . Button ( ' Send to notebook ' , elem_classes = [ ' small-button ' ] )
shared . gradio [ ' send_instruction_to_negative_prompt ' ] = gr . Button ( ' Send to negative prompt ' , elem_classes = [ ' small-button ' ] )
2023-08-13 00:12:15 -04:00
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. ' , elem_classes = [ ' add_scrollbar ' ] )
2023-08-06 20:49:27 -04:00
2023-08-13 00:12:15 -04:00
with gr . Tab ( ' Chat history ' ) :
with gr . Row ( ) :
with gr . Column ( ) :
shared . gradio [ ' save_chat_history ' ] = gr . Button ( value = ' Save history ' )
2023-08-06 20:49:27 -04:00
2023-08-13 00:12:15 -04:00
with gr . Column ( ) :
shared . gradio [ ' load_chat_history ' ] = gr . File ( type = ' binary ' , file_types = [ ' .json ' , ' .txt ' ] , label = ' Upload History JSON ' )
2023-08-06 20:49:27 -04:00
2023-08-13 00:12:15 -04:00
with gr . Tab ( ' Upload character ' ) :
with gr . Tab ( ' YAML or JSON ' ) :
with gr . Row ( ) :
shared . gradio [ ' upload_json ' ] = gr . File ( type = ' binary ' , file_types = [ ' .json ' , ' .yaml ' ] , label = ' JSON or YAML File ' )
shared . gradio [ ' upload_img_bot ' ] = gr . Image ( type = ' pil ' , label = ' Profile Picture (optional) ' )
2023-08-06 20:49:27 -04:00
2023-08-13 00:12:15 -04:00
shared . gradio [ ' Submit character ' ] = gr . Button ( value = ' Submit ' , interactive = False )
2023-08-06 20:49:27 -04:00
2023-08-13 00:12:15 -04:00
with gr . Tab ( ' TavernAI PNG ' ) :
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-08-06 20:49:27 -04:00
def create_event_handlers ( ) :
2023-08-14 12:39:06 -04:00
# Obsolete variables, kept for compatibility with old extensions
shared . input_params = gradio ( inputs )
shared . reload_inputs = gradio ( reload_arr )
2023-08-06 20:49:27 -04:00
2023-08-14 13:10:39 -04:00
shared . gradio [ ' Generate ' ] . click (
2023-08-06 20:49:27 -04: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 (
2023-08-13 00:12:15 -04:00
chat . generate_chat_reply_wrapper , gradio ( inputs ) , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
2023-08-06 20:49:27 -04:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-09-21 16:19:32 -04:00
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None ) . then (
2023-08-11 19:33:15 -04:00
lambda : None , None , None , _js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-06 20:49:27 -04:00
2023-08-14 13:10:39 -04:00
shared . gradio [ ' textbox ' ] . submit (
2023-08-06 20:49:27 -04: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 (
2023-08-13 00:12:15 -04:00
chat . generate_chat_reply_wrapper , gradio ( inputs ) , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
2023-08-06 20:49:27 -04:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-09-21 16:19:32 -04:00
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None ) . then (
2023-08-11 19:33:15 -04:00
lambda : None , None , None , _js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-06 20:49:27 -04:00
2023-08-14 13:10:39 -04:00
shared . gradio [ ' Regenerate ' ] . click (
2023-08-06 20:49:27 -04:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-08-13 00:12:15 -04:00
partial ( chat . generate_chat_reply_wrapper , regenerate = True ) , gradio ( inputs ) , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
2023-08-06 20:49:27 -04:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-09-21 16:19:32 -04:00
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None ) . then (
2023-08-11 19:33:15 -04:00
lambda : None , None , None , _js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-06 20:49:27 -04:00
2023-08-14 13:10:39 -04:00
shared . gradio [ ' Continue ' ] . click (
2023-08-06 20:49:27 -04:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-08-13 00:12:15 -04:00
partial ( chat . generate_chat_reply_wrapper , _continue = True ) , gradio ( inputs ) , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
2023-08-06 20:49:27 -04:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-09-21 16:19:32 -04:00
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None ) . then (
2023-08-11 19:33:15 -04:00
lambda : None , None , None , _js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-06 20:49:27 -04:00
2023-08-14 13:10:39 -04:00
shared . gradio [ ' Impersonate ' ] . click (
2023-08-06 20:49:27 -04: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 (
2023-09-13 14:22:53 -04:00
chat . impersonate_wrapper , gradio ( inputs ) , gradio ( ' textbox ' , ' display ' ) , show_progress = False ) . then (
2023-08-06 20:49:27 -04:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-08-11 19:33:15 -04:00
lambda : None , None , None , _js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' Replace last reply ' ] . click (
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 (
2023-08-13 00:12:15 -04:00
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
2023-09-21 16:19:32 -04:00
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' Send dummy message ' ] . click (
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 (
2023-08-13 00:12:15 -04:00
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
2023-09-21 16:19:32 -04:00
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' Send dummy reply ' ] . click (
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 (
2023-08-13 00:12:15 -04:00
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
2023-09-21 16:19:32 -04:00
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None )
2023-08-06 20:49:27 -04:00
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 (
2023-08-13 00:12:15 -04:00
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
2023-09-21 16:19:32 -04:00
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' Stop ' ] . click (
2023-08-14 13:10:39 -04:00
stop_everything_event , None , None , queue = False ) . then (
2023-08-13 00:12:15 -04:00
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) )
2023-08-06 20:49:27 -04:00
2023-09-21 16:19:32 -04:00
if not shared . args . multi_user :
shared . gradio [ ' unique_id ' ] . select (
chat . load_history , gradio ( ' unique_id ' , ' character_menu ' , ' mode ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) )
shared . gradio [ ' Start new chat ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . start_new_chat , gradio ( ' interface_state ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
lambda x : gr . update ( choices = ( histories := chat . find_all_histories ( x ) ) , value = histories [ 0 ] ) , gradio ( ' interface_state ' ) , gradio ( ' unique_id ' ) )
shared . gradio [ ' delete_chat ' ] . click ( lambda : [ gr . update ( visible = True ) , gr . update ( visible = False ) , gr . update ( visible = True ) ] , None , gradio ( clear_arr ) )
shared . gradio [ ' delete_chat-cancel ' ] . click ( lambda : [ gr . update ( visible = False ) , gr . update ( visible = True ) , gr . update ( visible = False ) ] , None , gradio ( clear_arr ) )
shared . gradio [ ' delete_chat-confirm ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . delete_history , gradio ( ' unique_id ' , ' character_menu ' , ' mode ' ) , None ) . then (
chat . load_latest_history , gradio ( ' interface_state ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
lambda x : gr . update ( choices = ( histories := chat . find_all_histories ( x ) ) , value = histories [ 0 ] ) , gradio ( ' interface_state ' ) , gradio ( ' unique_id ' ) ) . then (
lambda : [ gr . update ( visible = False ) , gr . update ( visible = True ) , gr . update ( visible = False ) ] , None , gradio ( clear_arr ) )
shared . gradio [ ' load_chat_history ' ] . upload (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . start_new_chat , gradio ( ' interface_state ' ) , gradio ( ' history ' ) ) . then (
chat . load_history_json , gradio ( ' load_chat_history ' , ' history ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
lambda x : gr . update ( choices = ( histories := chat . find_all_histories ( x ) ) , value = histories [ 0 ] ) , gradio ( ' interface_state ' ) , gradio ( ' unique_id ' ) ) . then (
chat . save_history , gradio ( ' history ' , ' unique_id ' , ' character_menu ' , ' mode ' ) , None ) . then (
lambda : None , None , None , _js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_chat() }} ' )
shared . gradio [ ' character_menu ' ] . change (
partial ( chat . load_character , instruct = False ) , gradio ( ' character_menu ' , ' name1 ' , ' name2 ' ) , gradio ( ' name1 ' , ' name2 ' , ' character_picture ' , ' greeting ' , ' context ' , ' dummy ' ) ) . success (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . load_latest_history , gradio ( ' interface_state ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
lambda x : gr . update ( choices = ( histories := chat . find_all_histories ( x ) ) , value = histories [ 0 ] ) , gradio ( ' interface_state ' ) , gradio ( ' unique_id ' ) )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' mode ' ] . change (
lambda x : gr . update ( visible = x != ' instruct ' ) , gradio ( ' mode ' ) , gradio ( ' chat_style ' ) , show_progress = False ) . then (
2023-09-21 16:19:32 -04:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
partial ( chat . character_is_loaded , raise_exception = True ) , gradio ( ' interface_state ' ) , None ) . success (
chat . load_latest_history , gradio ( ' interface_state ' ) , gradio ( ' history ' ) ) . then (
chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) ) . then (
lambda x : gr . update ( choices = ( histories := chat . find_all_histories ( x ) ) , value = histories [ 0 ] ) , gradio ( ' interface_state ' ) , gradio ( ' unique_id ' ) )
2023-08-06 20:49:27 -04:00
2023-08-13 00:12:15 -04:00
shared . gradio [ ' chat_style ' ] . change ( chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' instruction_template ' ] . change (
partial ( chat . load_character , instruct = True ) , gradio ( ' instruction_template ' , ' name1_instruct ' , ' name2_instruct ' ) , gradio ( ' name1_instruct ' , ' name2_instruct ' , ' dummy ' , ' dummy ' , ' context_instruct ' , ' turn_template ' ) )
shared . gradio [ ' Copy last reply ' ] . click ( chat . send_last_reply_to_input , gradio ( ' history ' ) , gradio ( ' textbox ' ) , show_progress = False )
# Save/delete a character
shared . gradio [ ' save_character ' ] . click (
lambda x : x , gradio ( ' name2 ' ) , gradio ( ' save_character_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' character_saver ' ) )
shared . gradio [ ' delete_character ' ] . click ( lambda : gr . update ( visible = True ) , None , gradio ( ' character_deleter ' ) )
shared . gradio [ ' save_template ' ] . click (
lambda : ' My Template.yaml ' , None , gradio ( ' save_filename ' ) ) . then (
2023-08-06 20:50:07 -04:00
lambda : ' instruction-templates/ ' , None , gradio ( ' save_root ' ) ) . then (
2023-08-06 20:49:27 -04:00
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 ' ) )
shared . gradio [ ' delete_template ' ] . click (
lambda x : f ' { x } .yaml ' , gradio ( ' instruction_template ' ) , gradio ( ' delete_filename ' ) ) . then (
2023-08-06 20:50:07 -04:00
lambda : ' instruction-templates/ ' , None , gradio ( ' delete_root ' ) ) . then (
2023-08-06 20:49:27 -04:00
lambda : gr . update ( visible = True ) , None , gradio ( ' file_deleter ' ) )
shared . gradio [ ' save_chat_history ' ] . click (
lambda x : json . dumps ( x , indent = 4 ) , gradio ( ' history ' ) , gradio ( ' temporary_text ' ) ) . then (
2023-08-11 19:33:15 -04:00
None , gradio ( ' temporary_text ' , ' character_menu ' , ' mode ' ) , None , _js = f ' (hist, char, mode) => {{ { ui . save_files_js } ; saveHistory(hist, char, mode) }} ' )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' Submit character ' ] . click (
chat . upload_character , gradio ( ' upload_json ' , ' upload_img_bot ' ) , gradio ( ' character_menu ' ) ) . then (
2023-08-13 21:14:09 -04:00
lambda : None , None , None , _js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_character() }} ' )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' Submit tavern character ' ] . click (
chat . upload_tavern_character , gradio ( ' upload_img_tavern ' , ' tavern_json ' ) , gradio ( ' character_menu ' ) ) . then (
2023-08-13 21:14:09 -04:00
lambda : None , None , None , _js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_character() }} ' )
2023-08-06 20:49:27 -04:00
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 [ ' 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 )
shared . gradio [ ' your_picture ' ] . change (
chat . upload_your_profile_picture , gradio ( ' your_picture ' ) , None ) . then (
2023-08-13 00:12:15 -04:00
partial ( chat . redraw_html , reset_cache = True ) , gradio ( reload_arr ) , gradio ( ' display ' ) )
2023-08-13 21:14:09 -04:00
shared . gradio [ ' send_instruction_to_default ' ] . click (
prompts . load_instruction_prompt_simple , gradio ( ' instruction_template ' ) , gradio ( ' textbox-default ' ) ) . then (
lambda : None , None , None , _js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_default() }} ' )
shared . gradio [ ' send_instruction_to_notebook ' ] . click (
prompts . load_instruction_prompt_simple , gradio ( ' instruction_template ' ) , gradio ( ' textbox-notebook ' ) ) . then (
lambda : None , None , None , _js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_notebook() }} ' )
shared . gradio [ ' send_instruction_to_negative_prompt ' ] . click (
prompts . load_instruction_prompt_simple , gradio ( ' instruction_template ' ) , gradio ( ' negative_prompt ' ) ) . then (
lambda : None , None , None , _js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_generation_parameters() }} ' )
2023-08-16 10:03:53 -04:00
2023-08-20 22:54:59 -04:00
shared . gradio [ ' send-chat-to-default ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
partial ( chat . generate_chat_prompt , ' ' , _continue = True ) , gradio ( ' interface_state ' ) , gradio ( ' textbox-default ' ) ) . then (
lambda : None , None , None , _js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_default() }} ' )
shared . gradio [ ' send-chat-to-notebook ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
partial ( chat . generate_chat_prompt , ' ' , _continue = True ) , gradio ( ' interface_state ' ) , gradio ( ' textbox-notebook ' ) ) . then (
lambda : None , None , None , _js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_notebook() }} ' )
2023-08-16 10:03:53 -04:00
shared . gradio [ ' show_controls ' ] . change ( None , gradio ( ' show_controls ' ) , None , _js = f ' (x) => {{ { ui . show_controls_js } ; toggle_controls(x) }} ' )