2023-08-06 20:49:27 -04:00
import gradio as gr
2023-09-21 16:19:32 -04:00
from modules import chat , presets , shared , ui , utils
2023-08-06 20:49:27 -04:00
from modules . utils import gradio
def create_ui ( ) :
2023-09-26 08:44:04 -04:00
mu = shared . args . multi_user
2023-08-06 20:49:27 -04:00
# Text file saver
2023-10-10 21:20:49 -04:00
with gr . Group ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' file_saver ' ] :
2023-08-06 20:49:27 -04:00
shared . gradio [ ' save_filename ' ] = gr . Textbox ( lines = 1 , label = ' File name ' )
shared . gradio [ ' save_root ' ] = gr . Textbox ( lines = 1 , label = ' File folder ' , info = ' For reference. Unchangeable. ' , interactive = False )
shared . gradio [ ' save_contents ' ] = gr . Textbox ( lines = 10 , label = ' File contents ' )
with gr . Row ( ) :
shared . gradio [ ' save_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
2024-01-09 09:33:47 -05:00
shared . gradio [ ' save_confirm ' ] = gr . Button ( ' Save ' , elem_classes = " small-button " , variant = ' primary ' , interactive = not mu )
2023-08-06 20:49:27 -04:00
# Text file deleter
2023-10-10 21:20:49 -04:00
with gr . Group ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' file_deleter ' ] :
2023-08-06 20:49:27 -04:00
shared . gradio [ ' delete_filename ' ] = gr . Textbox ( lines = 1 , label = ' File name ' )
shared . gradio [ ' delete_root ' ] = gr . Textbox ( lines = 1 , label = ' File folder ' , info = ' For reference. Unchangeable. ' , interactive = False )
with gr . Row ( ) :
shared . gradio [ ' delete_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
2024-01-09 09:33:47 -05:00
shared . gradio [ ' delete_confirm ' ] = gr . Button ( ' Delete ' , elem_classes = " small-button " , variant = ' stop ' , interactive = not mu )
2023-08-06 20:49:27 -04:00
# Character saver/deleter
2023-10-10 21:20:49 -04:00
with gr . Group ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' character_saver ' ] :
2023-08-13 00:12:15 -04:00
shared . gradio [ ' save_character_filename ' ] = gr . Textbox ( lines = 1 , label = ' File name ' , info = ' The character will be saved to your characters/ folder with this base filename. ' )
with gr . Row ( ) :
shared . gradio [ ' save_character_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
2024-01-09 09:33:47 -05:00
shared . gradio [ ' save_character_confirm ' ] = gr . Button ( ' Save ' , elem_classes = " small-button " , variant = ' primary ' , interactive = not mu )
2023-08-06 20:49:27 -04:00
2023-10-10 21:20:49 -04:00
with gr . Group ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' character_deleter ' ] :
2023-08-13 00:12:15 -04:00
gr . Markdown ( ' Confirm the character deletion? ' )
with gr . Row ( ) :
shared . gradio [ ' delete_character_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
2024-01-09 09:24:27 -05:00
shared . gradio [ ' delete_character_confirm ' ] = gr . Button ( ' Delete ' , elem_classes = " small-button " , variant = ' stop ' , interactive = not mu )
2023-08-06 20:49:27 -04:00
2023-11-18 17:03:42 -05:00
# Preset saver
with gr . Group ( visible = False , elem_classes = ' file-saver ' ) as shared . gradio [ ' preset_saver ' ] :
shared . gradio [ ' save_preset_filename ' ] = gr . Textbox ( lines = 1 , label = ' File name ' , info = ' The preset will be saved to your presets/ folder with this base filename. ' )
shared . gradio [ ' save_preset_contents ' ] = gr . Textbox ( lines = 10 , label = ' File contents ' )
with gr . Row ( ) :
shared . gradio [ ' save_preset_cancel ' ] = gr . Button ( ' Cancel ' , elem_classes = " small-button " )
2024-01-09 09:33:47 -05:00
shared . gradio [ ' save_preset_confirm ' ] = gr . Button ( ' Save ' , elem_classes = " small-button " , variant = ' primary ' , interactive = not mu )
2023-11-18 17:03:42 -05:00
2023-08-06 20:49:27 -04:00
def create_event_handlers ( ) :
shared . gradio [ ' save_confirm ' ] . click (
lambda x , y , z : utils . save_file ( x + y , z ) , gradio ( ' save_root ' , ' save_filename ' , ' save_contents ' ) , None ) . then (
lambda : gr . update ( visible = False ) , None , gradio ( ' file_saver ' ) )
shared . gradio [ ' delete_confirm ' ] . click (
lambda x , y : utils . delete_file ( x + y ) , gradio ( ' delete_root ' , ' delete_filename ' ) , None ) . then (
lambda : gr . update ( visible = False ) , None , gradio ( ' file_deleter ' ) )
shared . gradio [ ' delete_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' file_deleter ' ) )
shared . gradio [ ' save_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' file_saver ' ) )
2023-08-13 00:12:15 -04:00
shared . gradio [ ' save_character_confirm ' ] . click (
chat . save_character , gradio ( ' name2 ' , ' greeting ' , ' context ' , ' character_picture ' , ' save_character_filename ' ) , None ) . then (
2023-08-14 11:18:21 -04:00
lambda : gr . update ( visible = False ) , None , gradio ( ' character_saver ' ) ) . then (
lambda x : gr . update ( choices = utils . get_available_characters ( ) , value = x ) , gradio ( ' save_character_filename ' ) , gradio ( ' character_menu ' ) )
2023-08-13 00:12:15 -04:00
shared . gradio [ ' delete_character_confirm ' ] . click (
2024-01-09 09:24:27 -05:00
lambda x : str ( utils . get_available_characters ( ) . index ( x ) ) , gradio ( ' character_menu ' ) , gradio ( ' temporary_text ' ) ) . then (
2023-08-13 00:12:15 -04:00
chat . delete_character , gradio ( ' character_menu ' ) , None ) . then (
2024-01-09 09:24:27 -05:00
chat . update_character_menu_after_deletion , gradio ( ' temporary_text ' ) , gradio ( ' character_menu ' ) ) . then (
lambda : gr . update ( visible = False ) , None , gradio ( ' character_deleter ' ) )
2023-08-06 20:49:27 -04:00
2023-08-13 00:12:15 -04:00
shared . gradio [ ' save_character_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' character_saver ' ) )
shared . gradio [ ' delete_character_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' character_deleter ' ) )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' save_preset ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
2023-11-18 17:03:42 -05:00
presets . generate_preset_yaml , gradio ( ' interface_state ' ) , gradio ( ' save_preset_contents ' ) ) . then (
lambda : ' My Preset ' , None , gradio ( ' save_preset_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' preset_saver ' ) )
shared . gradio [ ' save_preset_confirm ' ] . click (
lambda x , y : utils . save_file ( f ' presets/ { x } .yaml ' , y ) , gradio ( ' save_preset_filename ' , ' save_preset_contents ' ) , None ) . then (
lambda : gr . update ( visible = False ) , None , gradio ( ' preset_saver ' ) ) . then (
lambda x : gr . update ( choices = utils . get_available_presets ( ) , value = x ) , gradio ( ' save_preset_filename ' ) , gradio ( ' preset_menu ' ) )
shared . gradio [ ' save_preset_cancel ' ] . click ( lambda : gr . update ( visible = False ) , None , gradio ( ' preset_saver ' ) )
2023-08-06 20:49:27 -04:00
shared . gradio [ ' delete_preset ' ] . click (
lambda x : f ' { x } .yaml ' , gradio ( ' preset_menu ' ) , gradio ( ' delete_filename ' ) ) . then (
lambda : ' presets/ ' , None , gradio ( ' delete_root ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_deleter ' ) )
2023-09-24 17:05:24 -04:00
shared . gradio [ ' save_grammar ' ] . click (
ui . gather_interface_values , gradio ( shared . input_elements ) , gradio ( ' interface_state ' ) ) . then (
lambda x : x , gradio ( ' grammar_string ' ) , gradio ( ' save_contents ' ) ) . then (
lambda : ' grammars/ ' , None , gradio ( ' save_root ' ) ) . then (
lambda : ' My Fancy Grammar.gbnf ' , None , gradio ( ' save_filename ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_saver ' ) )
shared . gradio [ ' delete_grammar ' ] . click (
lambda x : x , gradio ( ' grammar_file ' ) , gradio ( ' delete_filename ' ) ) . then (
lambda : ' grammars/ ' , None , gradio ( ' delete_root ' ) ) . then (
lambda : gr . update ( visible = True ) , None , gradio ( ' file_deleter ' ) )