2023-08-07 02:49:27 +02:00
import json
from functools import partial
from pathlib import Path
import gradio as gr
from PIL import Image
2023-11-17 03:16:42 +01:00
from modules import chat , shared , ui , utils
2023-08-07 02:49:27 +02:00
from modules . html_generator import chat_html_wrapper
from modules . utils import gradio
2023-08-14 16:46:07 +02:00
inputs = ( ' Chat input ' , ' interface_state ' )
2023-12-04 02:45:50 +01:00
reload_arr = ( ' history ' , ' name1 ' , ' name2 ' , ' mode ' , ' chat_style ' , ' character_menu ' )
2023-09-21 22:19:32 +02:00
clear_arr = ( ' delete_chat-confirm ' , ' delete_chat ' , ' delete_chat-cancel ' )
2023-08-13 06:12:15 +02:00
2023-08-07 02:49:27 +02:00
def create_ui ( ) :
2023-09-26 14:44:04 +02:00
mu = shared . args . multi_user
2023-08-12 01:33:15 +02:00
shared . gradio [ ' Chat input ' ] = gr . State ( )
2024-06-25 23:18:07 +02:00
shared . gradio [ ' history ' ] = gr . JSON ( { ' internal ' : [ ] , ' visible ' : [ ] } , visible = False )
2023-08-07 02:49:27 +02:00
2023-09-18 03:33:00 +02:00
with gr . Tab ( ' Chat ' , elem_id = ' chat-tab ' , elem_classes = ( " old-ui " if shared . args . chat_buttons else None ) ) :
2023-08-07 02:49:27 +02:00
with gr . Row ( ) :
2023-09-18 03:33:00 +02:00
with gr . Column ( elem_id = ' chat-col ' ) :
2023-12-04 02:45:50 +01:00
shared . gradio [ ' display ' ] = gr . HTML ( value = chat_html_wrapper ( { ' internal ' : [ ] , ' visible ' : [ ] } , ' ' , ' ' , ' chat ' , ' cai-chat ' , ' ' ) )
2023-09-13 07:36:12 +02:00
2023-09-18 03:33:00 +02:00
with gr . Row ( elem_id = " chat-input-row " ) :
with gr . Column ( scale = 1 , elem_id = ' gr-hover-container ' ) :
2023-09-14 12:06:42 +02: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-18 03:33:00 +02:00
with gr . Column ( scale = 10 , elem_id = ' chat-input-container ' ) :
2023-09-14 12:06:42 +02: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-18 03:33:00 +02:00
with gr . Column ( scale = 1 , elem_id = ' generate-stop-container ' ) :
2023-09-14 12:06:42 +02:00
with gr . Row ( ) :
2023-09-14 14:16:44 +02: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 07:36:12 +02:00
# Hover menu buttons
2023-09-18 03:33:00 +02:00
with gr . Column ( elem_id = ' chat-buttons ' ) :
2023-09-16 05:39:37 +02: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 22:19:32 +02:00
2023-09-16 05:39:37 +02: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 22:19:32 +02:00
2023-09-16 05:39:37 +02: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 22:19:32 +02:00
2023-09-16 05:39:37 +02: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-21 04:54:59 +02:00
2024-01-10 01:27:50 +01:00
with gr . Row ( elem_id = ' past-chats-row ' , elem_classes = [ ' pretty_scrollbar ' ] ) :
2024-01-09 05:57:29 +01:00
with gr . Column ( ) :
with gr . Row ( ) :
2024-06-26 03:28:58 +02:00
shared . gradio [ ' rename_chat ' ] = gr . Button ( ' Rename ' , elem_classes = ' refresh-button ' , interactive = not mu )
2024-01-09 05:57:29 +01:00
shared . gradio [ ' delete_chat ' ] = gr . Button ( ' 🗑️ ' , elem_classes = ' refresh-button ' , interactive = not mu )
2024-04-06 21:57:57 +02:00
shared . gradio [ ' delete_chat-confirm ' ] = gr . Button ( ' Confirm ' , variant = ' stop ' , visible = False , elem_classes = [ ' refresh-button ' , ' focus-on-chat-input ' ] )
shared . gradio [ ' delete_chat-cancel ' ] = gr . Button ( ' Cancel ' , visible = False , elem_classes = [ ' refresh-button ' , ' focus-on-chat-input ' ] )
shared . gradio [ ' Start new chat ' ] = gr . Button ( ' New chat ' , elem_classes = [ ' refresh-button ' , ' focus-on-chat-input ' ] )
2024-01-09 05:57:29 +01:00
2024-06-26 03:28:58 +02:00
with gr . Row ( elem_id = ' rename-row ' ) :
shared . gradio [ ' rename_to ' ] = gr . Textbox ( label = ' Rename to: ' , placeholder = ' New name ' , visible = False , elem_classes = [ ' no-background ' ] )
with gr . Row ( ) :
shared . gradio [ ' rename_to-confirm ' ] = gr . Button ( ' Confirm ' , visible = False , elem_classes = [ ' refresh-button ' , ' focus-on-chat-input ' ] )
shared . gradio [ ' rename_to-cancel ' ] = gr . Button ( ' Cancel ' , visible = False , elem_classes = [ ' refresh-button ' , ' focus-on-chat-input ' ] )
2024-06-25 05:07:22 +02:00
gr . Markdown ( " Past chats " )
with gr . Row ( ) :
shared . gradio [ ' unique_id ' ] = gr . Radio ( label = " " , elem_classes = [ ' slim-dropdown ' , ' pretty_scrollbar ' ] , interactive = not mu , elem_id = ' past-chats ' )
2023-09-22 03:53:03 +02:00
2024-02-18 18:32:05 +01:00
with gr . Row ( elem_id = ' chat-controls ' , elem_classes = [ ' pretty_scrollbar ' ] ) :
with gr . Column ( ) :
with gr . Row ( ) :
shared . gradio [ ' start_with ' ] = gr . Textbox ( label = ' Start reply with ' , placeholder = ' Sure thing! ' , value = shared . settings [ ' start_with ' ] , elem_classes = [ ' add_scrollbar ' ] )
2023-08-07 02:49:27 +02:00
2024-02-18 18:32:05 +01:00
with gr . Row ( ) :
2024-07-10 15:01:36 +02:00
shared . gradio [ ' mode ' ] = gr . Radio ( choices = [ ' chat ' , ' chat-instruct ' , ' instruct ' ] , label = ' Mode ' , info = ' Defines how the chat prompt is generated. In instruct and chat-instruct modes, the instruction template Parameters > Instruction template is used. ' , elem_id = ' chat-mode ' )
2024-02-18 18:32:05 +01:00
with gr . Row ( ) :
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-07 02:49:27 +02:00
2024-03-08 21:52:52 +01:00
with gr . Row ( ) :
2024-06-29 06:41:30 +02:00
shared . gradio [ ' chat-instruct_command ' ] = gr . Textbox ( value = shared . settings [ ' chat-instruct_command ' ] , lines = 12 , label = ' Command for chat-instruct mode ' , info = ' <|character|> and <|prompt|> get replaced with the bot name and the regular chat prompt respectively. ' , visible = False , elem_classes = [ ' add_scrollbar ' ] )
2024-03-08 21:52:52 +01:00
2023-08-13 06:12:15 +02:00
def create_chat_settings_ui ( ) :
2023-09-26 14:44:04 +02:00
mu = shared . args . multi_user
2024-03-12 03:41:57 +01:00
with gr . Tab ( ' Chat ' ) :
2023-08-13 06:12:15 +02:00
with gr . Row ( ) :
with gr . Column ( scale = 8 ) :
2024-03-12 03:41:57 +01:00
with gr . Tab ( " Character " ) :
2024-06-29 00:38:11 +02:00
with gr . Row ( ) :
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 ' )
ui . create_refresh_button ( shared . gradio [ ' character_menu ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_characters ( ) } , ' refresh-button ' , interactive = not mu )
2024-06-29 07:11:31 +02:00
shared . gradio [ ' save_character ' ] = gr . Button ( ' 💾 ' , elem_classes = ' refresh-button ' , elem_id = " save-character " , interactive = not mu )
2024-06-29 00:38:11 +02:00
shared . gradio [ ' delete_character ' ] = gr . Button ( ' 🗑️ ' , elem_classes = ' refresh-button ' , interactive = not mu )
2024-03-12 03:41:57 +01: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 ' ] )
with gr . Tab ( " User " ) :
shared . gradio [ ' name1 ' ] = gr . Textbox ( value = shared . settings [ ' name1 ' ] , lines = 1 , label = ' Name ' )
2024-03-12 03:49:22 +01:00
shared . gradio [ ' user_bio ' ] = gr . Textbox ( value = shared . settings [ ' user_bio ' ] , lines = 10 , label = ' Description ' , info = ' Here you can optionally write a description of yourself. ' , placeholder = ' {{ user}} \' s personality: ... ' , elem_classes = [ ' add_scrollbar ' ] )
2024-03-12 03:41:57 +01:00
with gr . Tab ( ' Chat history ' ) :
with gr . Row ( ) :
with gr . Column ( ) :
shared . gradio [ ' save_chat_history ' ] = gr . Button ( value = ' Save history ' )
with gr . Column ( ) :
shared . gradio [ ' load_chat_history ' ] = gr . File ( type = ' binary ' , file_types = [ ' .json ' , ' .txt ' ] , label = ' Upload History JSON ' )
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 ' , interactive = not mu )
shared . gradio [ ' upload_img_bot ' ] = gr . Image ( type = ' pil ' , label = ' Profile Picture (optional) ' , interactive = not mu )
shared . gradio [ ' Submit character ' ] = gr . Button ( value = ' Submit ' , interactive = False )
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 ' , interactive = not mu )
shared . gradio [ ' tavern_json ' ] = gr . State ( )
with gr . Column ( ) :
shared . gradio [ ' tavern_name ' ] = gr . Textbox ( value = ' ' , lines = 1 , label = ' Name ' , interactive = False )
2024-07-21 05:01:42 +02:00
shared . gradio [ ' tavern_desc ' ] = gr . Textbox ( value = ' ' , lines = 10 , label = ' Description ' , interactive = False , elem_classes = [ ' add_scrollbar ' ] )
2023-08-07 02:49:27 +02:00
2024-03-12 03:41:57 +01:00
shared . gradio [ ' Submit tavern character ' ] = gr . Button ( value = ' Submit ' , interactive = False )
2023-08-13 06:12:15 +02:00
with gr . Column ( scale = 1 ) :
2023-09-26 14:44:04 +02:00
shared . gradio [ ' character_picture ' ] = gr . Image ( label = ' Character picture ' , type = ' pil ' , interactive = not mu )
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 , interactive = not mu )
2023-08-13 06:12:15 +02:00
with gr . Tab ( ' Instruction template ' ) :
with gr . Row ( ) :
2023-12-12 21:23:14 +01:00
with gr . Column ( ) :
with gr . Row ( ) :
2024-02-16 18:21:17 +01:00
shared . gradio [ ' instruction_template ' ] = gr . Dropdown ( choices = utils . get_available_instruction_templates ( ) , label = ' Saved instruction templates ' , info = " After selecting the template, click on \" Load \" to load and apply it. " , value = ' None ' , elem_classes = ' slim-dropdown ' )
2023-12-12 21:23:14 +01:00
ui . create_refresh_button ( shared . gradio [ ' instruction_template ' ] , lambda : None , lambda : { ' choices ' : utils . get_available_instruction_templates ( ) } , ' refresh-button ' , interactive = not mu )
shared . gradio [ ' load_template ' ] = gr . Button ( " Load " , elem_classes = ' refresh-button ' )
shared . gradio [ ' save_template ' ] = gr . Button ( ' 💾 ' , elem_classes = ' refresh-button ' , interactive = not mu )
shared . gradio [ ' delete_template ' ] = gr . Button ( ' 🗑️ ' , elem_classes = ' refresh-button ' , interactive = not mu )
with gr . Column ( ) :
pass
2023-08-14 03:14:09 +02:00
2023-08-13 06:12:15 +02:00
with gr . Row ( ) :
2023-12-12 21:23:14 +01:00
with gr . Column ( ) :
shared . gradio [ ' custom_system_message ' ] = gr . Textbox ( value = shared . settings [ ' custom_system_message ' ] , lines = 2 , label = ' Custom system message ' , info = ' If not empty, will be used instead of the default one. ' , elem_classes = [ ' add_scrollbar ' ] )
2023-12-18 06:19:06 +01:00
shared . gradio [ ' instruction_template_str ' ] = gr . Textbox ( value = ' ' , label = ' Instruction template ' , lines = 24 , info = ' Change this according to the model/LoRA that you are using. Used in instruct and chat-instruct modes. ' , elem_classes = [ ' add_scrollbar ' , ' monospace ' ] )
2023-12-12 21:23:14 +01: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 ' ] )
with gr . Column ( ) :
shared . gradio [ ' chat_template_str ' ] = gr . Textbox ( value = shared . settings [ ' chat_template_str ' ] , label = ' Chat template ' , lines = 22 , elem_classes = [ ' add_scrollbar ' , ' monospace ' ] )
2023-08-07 02:49:27 +02:00
def create_event_handlers ( ) :
2023-08-14 18:39:06 +02:00
# Obsolete variables, kept for compatibility with old extensions
shared . input_params = gradio ( inputs )
shared . reload_inputs = gradio ( reload_arr )
2023-08-07 02:49:27 +02:00
2023-08-14 19:10:39 +02:00
shared . gradio [ ' Generate ' ] . click (
2023-08-07 02:49:27 +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 (
2023-08-13 06:12:15 +02:00
chat . generate_chat_reply_wrapper , gradio ( inputs ) , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-07 02:49:27 +02:00
2023-08-14 19:10:39 +02:00
shared . gradio [ ' textbox ' ] . submit (
2023-08-07 02:49:27 +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 (
2023-08-13 06:12:15 +02:00
chat . generate_chat_reply_wrapper , gradio ( inputs ) , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-07 02:49:27 +02:00
2023-08-14 19:10:39 +02:00
shared . gradio [ ' Regenerate ' ] . click (
2023-08-07 02:49:27 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-08-13 06:12:15 +02:00
partial ( chat . generate_chat_reply_wrapper , regenerate = True ) , gradio ( inputs ) , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-07 02:49:27 +02:00
2023-08-14 19:10:39 +02:00
shared . gradio [ ' Continue ' ] . click (
2023-08-07 02:49:27 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-08-13 06:12:15 +02:00
partial ( chat . generate_chat_reply_wrapper , _continue = True ) , gradio ( inputs ) , gradio ( ' display ' , ' history ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-07 02:49:27 +02:00
2023-08-14 19:10:39 +02:00
shared . gradio [ ' Impersonate ' ] . click (
2023-08-07 02:49:27 +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 (
2023-09-13 20:22:53 +02:00
chat . impersonate_wrapper , gradio ( inputs ) , gradio ( ' textbox ' , ' display ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . audio_notification_js } }} ' )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' Replace last reply ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_replace_last_reply_click , gradio ( ' textbox ' , ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' textbox ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' Send dummy message ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_send_dummy_message_click , gradio ( ' textbox ' , ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' textbox ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' Send dummy reply ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_send_dummy_reply_click , gradio ( ' textbox ' , ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' textbox ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' Remove last ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_remove_last_click , gradio ( ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' textbox ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' Stop ' ] . click (
2024-07-21 05:01:42 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . handle_stop_click , gradio ( ' interface_state ' ) , gradio ( ' display ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
2023-09-21 22:19:32 +02:00
if not shared . args . multi_user :
shared . gradio [ ' unique_id ' ] . select (
2024-07-21 05:01:42 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . handle_unique_id_select , gradio ( ' interface_state ' ) , gradio ( ' history ' , ' display ' ) , show_progress = False )
2023-09-21 22:19:32 +02:00
shared . gradio [ ' Start new chat ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_start_new_chat_click , gradio ( ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' unique_id ' ) , show_progress = False )
2023-09-21 22:19:32 +02:00
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 (
2024-07-21 05:01:42 +02:00
chat . handle_delete_chat_confirm_click , gradio ( ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' unique_id ' ) + gradio ( clear_arr ) , show_progress = False )
2024-06-26 03:28:58 +02:00
2024-07-21 05:01:42 +02:00
shared . gradio [ ' rename_chat ' ] . click ( chat . handle_rename_chat_click , None , gradio ( ' rename_to ' , ' rename_to-confirm ' , ' rename_to-cancel ' ) , show_progress = False )
shared . gradio [ ' rename_to-cancel ' ] . click ( lambda : [ gr . update ( visible = False ) ] * 3 , None , gradio ( ' rename_to ' , ' rename_to-confirm ' , ' rename_to-cancel ' ) , show_progress = False )
2024-06-26 03:28:58 +02:00
shared . gradio [ ' rename_to-confirm ' ] . click (
2024-07-21 05:01:42 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . handle_rename_chat_confirm , gradio ( ' rename_to ' , ' interface_state ' ) , gradio ( ' unique_id ' , ' rename_to ' , ' rename_to-confirm ' , ' rename_to-cancel ' ) , show_progress = False )
2024-06-26 03:28:58 +02:00
shared . gradio [ ' rename_to ' ] . submit (
2024-07-21 05:01:42 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . handle_rename_chat_confirm , gradio ( ' rename_to ' , ' interface_state ' ) , gradio ( ' unique_id ' , ' rename_to ' , ' rename_to-confirm ' , ' rename_to-cancel ' ) , show_progress = False )
2024-06-26 03:28:58 +02:00
2023-09-21 22:19:32 +02:00
shared . gradio [ ' load_chat_history ' ] . upload (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_upload_chat_history , gradio ( ' load_chat_history ' , ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' unique_id ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_chat() }} ' )
2023-09-21 22:19:32 +02:00
shared . gradio [ ' character_menu ' ] . change (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_character_menu_change , gradio ( ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' name1 ' , ' name2 ' , ' character_picture ' , ' greeting ' , ' context ' , ' unique_id ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . update_big_picture_js } ; updateBigPicture() }} ' )
2023-09-21 22:19:32 +02:00
2023-08-07 02:49:27 +02:00
shared . gradio [ ' mode ' ] . change (
2023-09-21 22:19:32 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_mode_change , gradio ( ' interface_state ' ) , gradio ( ' history ' , ' display ' , ' chat_style ' , ' chat-instruct_command ' , ' unique_id ' ) , show_progress = False ) . then (
None , gradio ( ' mode ' ) , None , js = " (mode) => { mode === ' instruct ' ? document.getElementById( ' character-menu ' ).parentNode.parentNode.style.display = ' none ' : document.getElementById( ' character-menu ' ).parentNode.parentNode.style.display = ' ' } " )
2023-08-07 02:49:27 +02:00
2024-07-21 05:01:42 +02:00
shared . gradio [ ' chat_style ' ] . change ( chat . redraw_html , gradio ( reload_arr ) , gradio ( ' display ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' Copy last reply ' ] . click ( chat . send_last_reply_to_input , gradio ( ' history ' ) , gradio ( ' textbox ' ) , show_progress = False )
# Save/delete a character
2024-07-21 05:01:42 +02:00
shared . gradio [ ' save_character ' ] . click ( chat . handle_save_character_click , gradio ( ' name2 ' ) , gradio ( ' save_character_filename ' , ' character_saver ' ) , show_progress = False )
shared . gradio [ ' delete_character ' ] . click ( lambda : gr . update ( visible = True ) , None , gradio ( ' character_deleter ' ) , show_progress = False )
shared . gradio [ ' load_template ' ] . click ( chat . handle_load_template_click , gradio ( ' instruction_template ' ) , gradio ( ' instruction_template_str ' , ' instruction_template ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' save_template ' ] . click (
2024-07-21 05:01:42 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . handle_save_template_click , gradio ( ' instruction_template_str ' ) , gradio ( ' save_filename ' , ' save_root ' , ' save_contents ' , ' file_saver ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
2024-07-21 05:01:42 +02:00
shared . gradio [ ' delete_template ' ] . click ( chat . handle_delete_template_click , gradio ( ' instruction_template ' ) , gradio ( ' delete_filename ' , ' delete_root ' , ' file_deleter ' ) , show_progress = False )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' save_chat_history ' ] . click (
lambda x : json . dumps ( x , indent = 4 ) , gradio ( ' history ' ) , gradio ( ' temporary_text ' ) ) . then (
2024-03-26 20:32:20 +01:00
None , gradio ( ' temporary_text ' , ' character_menu ' , ' mode ' ) , None , js = f ' (hist, char, mode) => {{ { ui . save_files_js } ; saveHistory(hist, char, mode) }} ' )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' Submit character ' ] . click (
2024-07-21 05:01:42 +02:00
chat . upload_character , gradio ( ' upload_json ' , ' upload_img_bot ' ) , gradio ( ' character_menu ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_character() }} ' )
2023-08-07 02:49:27 +02:00
shared . gradio [ ' Submit tavern character ' ] . click (
2024-07-21 05:01:42 +02:00
chat . upload_tavern_character , gradio ( ' upload_img_tavern ' , ' tavern_json ' ) , gradio ( ' character_menu ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_character() }} ' )
2023-08-07 02:49:27 +02: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 (
2024-07-21 05:01:42 +02:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
chat . handle_your_picture_change , gradio ( ' your_picture ' , ' interface_state ' ) , gradio ( ' display ' ) , show_progress = False )
2023-08-14 03:14:09 +02:00
shared . gradio [ ' send_instruction_to_default ' ] . click (
2023-11-17 03:16:42 +01:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_send_instruction_click , gradio ( ' interface_state ' ) , gradio ( ' textbox-default ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_default() }} ' )
2023-08-14 03:14:09 +02:00
shared . gradio [ ' send_instruction_to_notebook ' ] . click (
2023-11-17 03:16:42 +01:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_send_instruction_click , gradio ( ' interface_state ' ) , gradio ( ' textbox-notebook ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_notebook() }} ' )
2023-08-14 03:14:09 +02:00
shared . gradio [ ' send_instruction_to_negative_prompt ' ] . click (
2023-11-17 03:16:42 +01:00
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_send_instruction_click , gradio ( ' interface_state ' ) , gradio ( ' negative_prompt ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_generation_parameters() }} ' )
2023-08-16 16:03:53 +02:00
2023-08-21 04:54:59 +02:00
shared . gradio [ ' send-chat-to-default ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_send_chat_click , gradio ( ' interface_state ' ) , gradio ( ' textbox-default ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_default() }} ' )
2023-08-21 04:54:59 +02:00
shared . gradio [ ' send-chat-to-notebook ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2024-07-21 05:01:42 +02:00
chat . handle_send_chat_click , gradio ( ' interface_state ' ) , gradio ( ' textbox-notebook ' ) , show_progress = False ) . then (
2024-06-13 05:52:42 +02:00
None , None , None , js = f ' () => {{ { ui . switch_tabs_js } ; switch_to_notebook() }} ' )
2023-08-21 04:54:59 +02:00
2024-06-13 05:52:42 +02:00
shared . gradio [ ' show_controls ' ] . change ( None , gradio ( ' show_controls ' ) , None , js = f ' (x) => {{ { ui . show_controls_js } ; toggle_controls(x) }} ' )