text-generation-webui/modules/ui_notebook.py

107 lines
6.8 KiB
Python
Raw Normal View History

2023-08-07 02:49:27 +02:00
import gradio as gr
2023-08-21 01:49:21 +02:00
from modules import logits, shared, ui, utils
2023-08-07 02:49:27 +02:00
from modules.prompts import count_tokens, load_prompt
from modules.text_generation import (
generate_reply_wrapper,
2023-09-16 04:30:44 +02:00
get_token_ids,
2023-08-07 02:49:27 +02:00
stop_everything_event
)
from modules.utils import gradio
2023-08-13 06:12:15 +02:00
inputs = ('textbox-notebook', 'interface_state')
outputs = ('textbox-notebook', 'html-notebook')
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-13 06:12:15 +02:00
with gr.Tab('Notebook', elem_id='notebook-tab'):
shared.gradio['last_input-notebook'] = gr.State('')
2023-08-07 02:49:27 +02:00
with gr.Row():
with gr.Column(scale=4):
with gr.Tab('Raw'):
with gr.Row():
shared.gradio['textbox-notebook'] = gr.Textbox(value='', lines=27, elem_id='textbox-notebook', elem_classes=['textbox', 'add_scrollbar'])
shared.gradio['token-counter-notebook'] = gr.HTML(value="<span>0</span>", elem_classes=["token-counter"])
2023-08-07 02:49:27 +02:00
with gr.Tab('Markdown'):
2023-08-13 06:12:15 +02:00
shared.gradio['markdown_render-notebook'] = gr.Button('Render')
shared.gradio['markdown-notebook'] = gr.Markdown()
2023-08-07 02:49:27 +02:00
with gr.Tab('HTML'):
2023-08-13 06:12:15 +02:00
shared.gradio['html-notebook'] = gr.HTML()
2023-08-07 02:49:27 +02:00
2023-08-21 01:49:21 +02:00
with gr.Tab('Logits'):
with gr.Row():
with gr.Column(scale=10):
shared.gradio['get_logits-notebook'] = gr.Button('Get next token probabilities')
with gr.Column(scale=1):
shared.gradio['use_samplers-notebook'] = gr.Checkbox(label='Use samplers', value=True, elem_classes=['no-background'])
with gr.Row():
2023-08-23 06:00:12 +02:00
shared.gradio['logits-notebook'] = gr.Textbox(lines=23, label='Output', elem_classes=['textbox_logits_notebook', 'add_scrollbar'])
shared.gradio['logits-notebook-previous'] = gr.Textbox(lines=23, label='Previous output', elem_classes=['textbox_logits_notebook', 'add_scrollbar'])
2023-08-21 01:49:21 +02:00
2023-09-16 03:51:55 +02:00
with gr.Tab('Tokens'):
shared.gradio['get_tokens-notebook'] = gr.Button('Get token IDs for the input')
2023-09-16 04:30:44 +02:00
shared.gradio['tokens-notebook'] = gr.Textbox(lines=23, label='Tokens', elem_classes=['textbox_logits_notebook', 'add_scrollbar', 'monospace'])
2023-09-16 03:51:55 +02:00
2023-08-07 02:49:27 +02:00
with gr.Row():
2023-08-13 06:12:15 +02:00
shared.gradio['Generate-notebook'] = gr.Button('Generate', variant='primary', elem_classes='small-button')
shared.gradio['Stop-notebook'] = gr.Button('Stop', elem_classes='small-button', elem_id='stop')
2023-08-12 01:33:15 +02:00
shared.gradio['Undo'] = gr.Button('Undo', elem_classes='small-button')
2023-08-13 06:12:15 +02:00
shared.gradio['Regenerate-notebook'] = gr.Button('Regenerate', elem_classes='small-button')
2023-08-07 02:49:27 +02:00
with gr.Column(scale=1):
gr.HTML('<div style="padding-bottom: 13px"></div>')
with gr.Row():
2023-08-13 06:12:15 +02:00
shared.gradio['prompt_menu-notebook'] = gr.Dropdown(choices=utils.get_available_prompts(), value='None', label='Prompt', elem_classes='slim-dropdown')
2023-09-26 14:44:04 +02:00
ui.create_refresh_button(shared.gradio['prompt_menu-notebook'], lambda: None, lambda: {'choices': utils.get_available_prompts()}, ['refresh-button', 'refresh-button-small'], interactive=not mu)
shared.gradio['save_prompt-notebook'] = gr.Button('💾', elem_classes=['refresh-button', 'refresh-button-small'], interactive=not mu)
shared.gradio['delete_prompt-notebook'] = gr.Button('🗑️', elem_classes=['refresh-button', 'refresh-button-small'], interactive=not mu)
2023-08-07 02:49:27 +02:00
def create_event_handlers():
2023-08-14 19:10:39 +02:00
shared.gradio['Generate-notebook'].click(
2023-08-13 06:12:15 +02:00
lambda x: x, gradio('textbox-notebook'), gradio('last_input-notebook')).then(
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
generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then(
2023-08-07 02:49:27 +02:00
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
lambda: 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-notebook'].submit(
2023-08-13 06:12:15 +02:00
lambda x: x, gradio('textbox-notebook'), gradio('last_input-notebook')).then(
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
generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then(
2023-08-07 02:49:27 +02:00
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
lambda: None, None, None, js=f'() => {{{ui.audio_notification_js}}}')
2023-08-07 02:49:27 +02:00
2023-08-13 06:12:15 +02:00
shared.gradio['Undo'].click(lambda x: x, gradio('last_input-notebook'), gradio('textbox-notebook'), show_progress=False)
shared.gradio['markdown_render-notebook'].click(lambda x: x, gradio('textbox-notebook'), gradio('markdown-notebook'), queue=False)
2023-08-14 19:10:39 +02:00
shared.gradio['Regenerate-notebook'].click(
2023-08-13 06:12:15 +02:00
lambda x: x, gradio('last_input-notebook'), gradio('textbox-notebook'), show_progress=False).then(
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
generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then(
2023-08-07 02:49:27 +02:00
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
lambda: 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['Stop-notebook'].click(stop_everything_event, None, None, queue=False)
2023-08-13 06:12:15 +02:00
shared.gradio['prompt_menu-notebook'].change(load_prompt, gradio('prompt_menu-notebook'), gradio('textbox-notebook'), show_progress=False)
shared.gradio['save_prompt-notebook'].click(
lambda x: x, gradio('textbox-notebook'), gradio('save_contents')).then(
2023-08-07 02:49:27 +02:00
lambda: 'prompts/', None, gradio('save_root')).then(
lambda: utils.current_time() + '.txt', None, gradio('save_filename')).then(
lambda: gr.update(visible=True), None, gradio('file_saver'))
2023-08-13 06:12:15 +02:00
shared.gradio['delete_prompt-notebook'].click(
2023-08-07 02:49:27 +02:00
lambda: 'prompts/', None, gradio('delete_root')).then(
2023-08-13 06:12:15 +02:00
lambda x: x + '.txt', gradio('prompt_menu-notebook'), gradio('delete_filename')).then(
2023-08-07 02:49:27 +02:00
lambda: gr.update(visible=True), None, gradio('file_deleter'))
shared.gradio['textbox-notebook'].input(lambda x: f"<span>{count_tokens(x)}</span>", gradio('textbox-notebook'), gradio('token-counter-notebook'), show_progress=False)
shared.gradio['get_logits-notebook'].click(
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
logits.get_next_logits, gradio('textbox-notebook', 'interface_state', 'use_samplers-notebook', 'logits-notebook'), gradio('logits-notebook', 'logits-notebook-previous'), show_progress=False)
2023-09-16 03:51:55 +02:00
2023-09-16 04:30:44 +02:00
shared.gradio['get_tokens-notebook'].click(get_token_ids, gradio('textbox-notebook'), gradio('tokens-notebook'), show_progress=False)