2023-08-07 02:49:27 +02:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
from modules import shared, ui, utils
|
|
|
|
from modules.prompts import count_tokens, load_prompt
|
|
|
|
from modules.text_generation import (
|
|
|
|
generate_reply_wrapper,
|
|
|
|
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():
|
|
|
|
default_text = load_prompt(shared.settings['prompt'])
|
|
|
|
|
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'):
|
2023-08-13 06:12:15 +02:00
|
|
|
shared.gradio['textbox-notebook'] = gr.Textbox(value=default_text, elem_classes=['textbox', 'add_scrollbar'], lines=27)
|
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
|
|
|
|
|
|
|
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')
|
|
|
|
ui.create_refresh_button(shared.gradio['prompt_menu-notebook'], lambda: None, lambda: {'choices': utils.get_available_prompts()}, ['refresh-button', 'refresh-button-small'])
|
|
|
|
shared.gradio['save_prompt-notebook'] = gr.Button('💾', elem_classes=['refresh-button', 'refresh-button-small'])
|
|
|
|
shared.gradio['delete_prompt-notebook'] = gr.Button('🗑️', elem_classes=['refresh-button', 'refresh-button-small'])
|
2023-08-07 02:49:27 +02:00
|
|
|
|
2023-08-13 06:12:15 +02:00
|
|
|
shared.gradio['count_tokens-notebook'] = gr.Button('Count tokens')
|
|
|
|
shared.gradio['status-notebook'] = gr.Markdown('')
|
2023-08-07 02:49:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_event_handlers():
|
|
|
|
gen_events = []
|
|
|
|
|
2023-08-13 06:12:15 +02:00
|
|
|
gen_events.append(shared.gradio['Generate-notebook'].click(
|
|
|
|
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(
|
2023-08-12 01:33:15 +02:00
|
|
|
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
|
|
|
gen_events.append(shared.gradio['textbox-notebook'].submit(
|
|
|
|
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(
|
2023-08-12 01:33:15 +02:00
|
|
|
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)
|
|
|
|
gen_events.append(shared.gradio['Regenerate-notebook'].click(
|
|
|
|
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(
|
2023-08-12 01:33:15 +02:00
|
|
|
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['Stop-notebook'].click(stop_everything_event, None, None, queue=False, cancels=gen_events if shared.args.no_stream else None)
|
|
|
|
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'))
|
|
|
|
|
2023-08-13 06:12:15 +02:00
|
|
|
shared.gradio['count_tokens-notebook'].click(count_tokens, gradio('textbox-notebook'), gradio('status-notebook'), show_progress=False)
|