text-generation-webui/modules/ui_session.py

93 lines
5.0 KiB
Python
Raw Normal View History

2023-08-07 02:49:27 +02:00
import gradio as gr
2024-08-28 17:32:07 +02:00
from modules import shared, ui, utils, localization
2023-08-07 02:49:27 +02:00
from modules.github import clone_or_pull_repository
from modules.utils import gradio
def create_ui():
2023-09-26 14:44:04 +02:00
mu = shared.args.multi_user
2023-08-07 02:49:27 +02:00
with gr.Tab("Session", elem_id="session-tab"):
with gr.Row():
with gr.Column():
2023-09-26 14:44:04 +02:00
shared.gradio['reset_interface'] = gr.Button("Apply flags/extensions and restart", interactive=not mu)
with gr.Row():
shared.gradio['toggle_dark_mode'] = gr.Button('Toggle 💡')
2023-09-26 14:44:04 +02:00
shared.gradio['save_settings'] = gr.Button('Save UI defaults to settings.yaml', interactive=not mu)
2023-08-07 02:49:27 +02:00
with gr.Row():
with gr.Column():
shared.gradio['extensions_menu'] = gr.CheckboxGroup(choices=utils.get_available_extensions(), value=shared.args.extensions, label="Available extensions", info='Note that some of these extensions may require manually installing Python requirements through the command: pip install -r extensions/extension_name/requirements.txt', elem_classes='checkboxgroup-table')
with gr.Column():
2023-08-13 06:12:15 +02:00
shared.gradio['bool_menu'] = gr.CheckboxGroup(choices=get_boolean_arguments(), value=get_boolean_arguments(active=True), label="Boolean command-line flags", elem_classes='checkboxgroup-table')
2023-08-07 02:49:27 +02:00
with gr.Column():
2023-09-26 14:44:04 +02:00
extension_name = gr.Textbox(lines=1, label='Install or update an extension', info='Enter the GitHub URL below and press Enter. For a list of extensions, see: https://github.com/oobabooga/text-generation-webui-extensions ⚠️ WARNING ⚠️ : extensions can execute arbitrary code. Make sure to inspect their source code before activating them.', interactive=not mu)
2024-08-28 17:32:07 +02:00
with gr.Row():
2024-08-29 05:46:55 +02:00
shared.gradio['localization_menu'] = gr.Dropdown(choices=utils.get_available_localizations(), value=shared.settings['localization'], label='Localization', elem_classes='slim-dropdown')
ui.create_refresh_button(shared.gradio['localization_menu'], lambda: None, lambda: {'choices': utils.get_available_localizations()}, 'refresh-button', interactive=not mu)
2024-08-28 17:32:07 +02:00
shared.gradio['download_localization'] = gr.Button(value='Download localization template', elem_id='download_localization')
2023-08-07 02:49:27 +02:00
extension_status = gr.Markdown()
shared.gradio['theme_state'] = gr.Textbox(visible=False, value='dark' if shared.settings['dark_theme'] else 'light')
2023-09-26 05:27:06 +02:00
extension_name.submit(clone_or_pull_repository, extension_name, extension_status, show_progress=False)
2023-08-07 02:49:27 +02:00
# Reset interface event
shared.gradio['reset_interface'].click(
2023-08-13 06:12:15 +02:00
set_interface_arguments, gradio('extensions_menu', 'bool_menu'), None).then(
None, None, None, js='() => {document.body.innerHTML=\'<h1 style="font-family:monospace;padding-top:20%;margin:0;height:100vh;color:lightgray;text-align:center;background:var(--body-background-fill)">Reloading...</h1>\'; setTimeout(function(){location.reload()},2500); return []}')
2023-08-07 02:49:27 +02:00
shared.gradio['toggle_dark_mode'].click(
2024-07-21 05:01:42 +02:00
lambda x: 'dark' if x == 'light' else 'light', gradio('theme_state'), gradio('theme_state')).then(
None, None, None, js=f'() => {{{ui.dark_theme_js}; toggleDarkMode()}}')
shared.gradio['save_settings'].click(
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
2024-08-29 05:46:55 +02:00
handle_save_settings, gradio('interface_state', 'preset_menu', 'extensions_menu', 'localization_menu', 'show_controls', 'theme_state'), gradio('save_contents', 'save_filename', 'save_root', 'file_saver'), show_progress=False)
2024-07-21 05:01:42 +02:00
2024-08-28 17:32:07 +02:00
shared.gradio['download_localization'].click(
None, None, None, js=f'() => {{{ui.localization_js}; download_localization()}}')
shared.gradio['localization_menu'].change(
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state'))
2024-07-21 05:01:42 +02:00
2024-08-29 05:46:55 +02:00
def handle_save_settings(state, preset, extensions, localization, show_controls, theme):
contents = ui.save_settings(state, preset, extensions, localization, show_controls, theme)
2024-07-21 05:01:42 +02:00
return [
contents,
"settings.yaml",
"./",
gr.update(visible=True)
]
2023-08-07 02:49:27 +02:00
2023-08-13 06:12:15 +02:00
def set_interface_arguments(extensions, bool_active):
2023-08-07 02:49:27 +02:00
shared.args.extensions = extensions
2023-08-13 06:12:15 +02:00
bool_list = get_boolean_arguments()
2023-08-07 02:49:27 +02:00
for k in bool_list:
setattr(shared.args, k, False)
for k in bool_active:
setattr(shared.args, k, True)
if k == 'api':
shared.add_extension('openai', last=True)
2023-08-07 02:49:27 +02:00
shared.need_restart = True
2023-08-13 06:12:15 +02:00
def get_boolean_arguments(active=False):
2023-11-22 00:15:16 +01:00
exclude = shared.deprecated_args
2023-08-13 06:12:15 +02:00
cmd_list = vars(shared.args)
bool_list = sorted([k for k in cmd_list if type(cmd_list[k]) is bool and k not in exclude + ui.list_model_elements()])
bool_active = [k for k in bool_list if vars(shared.args)[k]]
if active:
return bool_active
else:
return bool_list