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-default', 'interface_state')
|
|
|
|
outputs = ('output_textbox', 'html-default')
|
|
|
|
|
2023-08-07 02:49:27 +02:00
|
|
|
|
|
|
|
def create_ui():
|
2023-08-13 06:12:15 +02:00
|
|
|
with gr.Tab('Default', elem_id='default-tab'):
|
|
|
|
shared.gradio['last_input-default'] = gr.State('')
|
2023-08-07 02:49:27 +02:00
|
|
|
with gr.Row():
|
|
|
|
with gr.Column():
|
2023-08-14 16:46:07 +02:00
|
|
|
shared.gradio['textbox-default'] = gr.Textbox(value='', elem_classes=['textbox_default', 'add_scrollbar'], lines=27, label='Input')
|
2023-08-07 02:49:27 +02:00
|
|
|
with gr.Row():
|
2023-08-13 06:12:15 +02:00
|
|
|
shared.gradio['Generate-default'] = gr.Button('Generate', variant='primary')
|
|
|
|
shared.gradio['Stop-default'] = gr.Button('Stop', elem_id='stop')
|
|
|
|
shared.gradio['Continue-default'] = gr.Button('Continue')
|
|
|
|
shared.gradio['count_tokens-default'] = gr.Button('Count tokens')
|
2023-08-07 02:49:27 +02:00
|
|
|
|
|
|
|
with gr.Row():
|
2023-08-13 06:12:15 +02:00
|
|
|
shared.gradio['prompt_menu-default'] = gr.Dropdown(choices=utils.get_available_prompts(), value='None', label='Prompt', elem_classes='slim-dropdown')
|
|
|
|
ui.create_refresh_button(shared.gradio['prompt_menu-default'], lambda: None, lambda: {'choices': utils.get_available_prompts()}, 'refresh-button')
|
|
|
|
shared.gradio['save_prompt-default'] = gr.Button('💾', elem_classes='refresh-button')
|
|
|
|
shared.gradio['delete_prompt-default'] = gr.Button('🗑️', elem_classes='refresh-button')
|
2023-08-07 02:49:27 +02:00
|
|
|
|
2023-08-13 06:12:15 +02:00
|
|
|
shared.gradio['status-default'] = gr.Markdown('')
|
2023-08-07 02:49:27 +02:00
|
|
|
|
|
|
|
with gr.Column():
|
|
|
|
with gr.Tab('Raw'):
|
|
|
|
shared.gradio['output_textbox'] = gr.Textbox(lines=27, label='Output', elem_classes=['textbox_default_output', 'add_scrollbar'])
|
|
|
|
|
|
|
|
with gr.Tab('Markdown'):
|
2023-08-13 06:12:15 +02:00
|
|
|
shared.gradio['markdown_render-default'] = gr.Button('Render')
|
|
|
|
shared.gradio['markdown-default'] = 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-default'] = gr.HTML()
|
2023-08-07 02:49:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_event_handlers():
|
2023-08-14 19:10:39 +02:00
|
|
|
shared.gradio['Generate-default'].click(
|
2023-08-13 06:12:15 +02:00
|
|
|
lambda x: x, gradio('textbox-default'), gradio('last_input-default')).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-14 19:10:39 +02:00
|
|
|
shared.gradio['textbox-default'].submit(
|
2023-08-13 06:12:15 +02:00
|
|
|
lambda x: x, gradio('textbox-default'), gradio('last_input-default')).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['markdown_render-default'].click(lambda x: x, gradio('output_textbox'), gradio('markdown-default'), queue=False)
|
2023-08-14 19:10:39 +02:00
|
|
|
shared.gradio['Continue-default'].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
|
|
|
generate_reply_wrapper, [shared.gradio['output_textbox']] + gradio(inputs)[1:], 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-14 19:10:39 +02:00
|
|
|
shared.gradio['Stop-default'].click(stop_everything_event, None, None, queue=False)
|
2023-08-13 06:12:15 +02:00
|
|
|
shared.gradio['prompt_menu-default'].change(load_prompt, gradio('prompt_menu-default'), gradio('textbox-default'), show_progress=False)
|
|
|
|
shared.gradio['save_prompt-default'].click(
|
|
|
|
lambda x: x, gradio('textbox-default'), 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-default'].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-default'), 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-default'].click(count_tokens, gradio('textbox-default'), gradio('status-default'), show_progress=False)
|