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-default', 'interface_state')
|
|
|
|
outputs = ('output_textbox', 'html-default')
|
|
|
|
|
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('Default', elem_id='default-tab'):
|
2023-08-07 02:49:27 +02:00
|
|
|
with gr.Row():
|
|
|
|
with gr.Column():
|
2023-08-21 04:13:01 +02:00
|
|
|
with gr.Row():
|
2023-08-29 03:22:03 +02:00
|
|
|
shared.gradio['textbox-default'] = gr.Textbox(value='', lines=27, label='Input', elem_classes=['textbox_default', 'add_scrollbar'])
|
2023-08-21 04:13:01 +02:00
|
|
|
shared.gradio['token-counter-default'] = gr.HTML(value="<span>0</span>", elem_classes=["token-counter", "default-token-counter"])
|
|
|
|
|
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')
|
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')
|
2023-09-26 14:44:04 +02:00
|
|
|
ui.create_refresh_button(shared.gradio['prompt_menu-default'], lambda: None, lambda: {'choices': utils.get_available_prompts()}, 'refresh-button', interactive=not mu)
|
|
|
|
shared.gradio['save_prompt-default'] = gr.Button('💾', elem_classes='refresh-button', interactive=not mu)
|
|
|
|
shared.gradio['delete_prompt-default'] = gr.Button('🗑️', elem_classes='refresh-button', interactive=not mu)
|
2023-08-07 02:49:27 +02:00
|
|
|
|
|
|
|
with gr.Column():
|
|
|
|
with gr.Tab('Raw'):
|
2023-08-29 03:22:03 +02:00
|
|
|
shared.gradio['output_textbox'] = gr.Textbox(lines=27, label='Output', elem_id='textbox-default', elem_classes=['textbox_default_output', 'add_scrollbar'])
|
2023-08-07 02:49:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-08-21 01:49:21 +02:00
|
|
|
with gr.Tab('Logits'):
|
2023-08-23 05:18:16 +02:00
|
|
|
with gr.Row():
|
|
|
|
with gr.Column(scale=10):
|
|
|
|
shared.gradio['get_logits-default'] = gr.Button('Get next token probabilities')
|
|
|
|
with gr.Column(scale=1):
|
|
|
|
shared.gradio['use_samplers-default'] = gr.Checkbox(label='Use samplers', value=True, elem_classes=['no-background'])
|
|
|
|
|
|
|
|
with gr.Row():
|
|
|
|
shared.gradio['logits-default'] = gr.Textbox(lines=23, label='Output', elem_classes=['textbox_logits', 'add_scrollbar'])
|
|
|
|
shared.gradio['logits-default-previous'] = gr.Textbox(lines=23, label='Previous output', elem_classes=['textbox_logits', '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-default'] = gr.Button('Get token IDs for the input')
|
2023-09-16 04:30:44 +02:00
|
|
|
shared.gradio['tokens-default'] = gr.Textbox(lines=23, label='Tokens', elem_classes=['textbox_logits', 'add_scrollbar', 'monospace'])
|
2023-09-16 03:51:55 +02:00
|
|
|
|
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-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(
|
2024-06-13 05:52:42 +02:00
|
|
|
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-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(
|
2024-06-13 05:52:42 +02:00
|
|
|
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(
|
2024-06-13 05:52:42 +02:00
|
|
|
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)
|
2024-07-21 05:01:42 +02:00
|
|
|
shared.gradio['save_prompt-default'].click(handle_save_prompt, gradio('textbox-default'), gradio('save_contents', 'save_filename', 'save_root', 'file_saver'), show_progress=False)
|
|
|
|
shared.gradio['delete_prompt-default'].click(handle_delete_prompt, gradio('prompt_menu-default'), gradio('delete_filename', 'delete_root', 'file_deleter'), show_progress=False)
|
2023-08-23 05:18:16 +02:00
|
|
|
shared.gradio['textbox-default'].change(lambda x: f"<span>{count_tokens(x)}</span>", gradio('textbox-default'), gradio('token-counter-default'), show_progress=False)
|
|
|
|
shared.gradio['get_logits-default'].click(
|
|
|
|
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
|
|
|
|
logits.get_next_logits, gradio('textbox-default', 'interface_state', 'use_samplers-default', 'logits-default'), gradio('logits-default', 'logits-default-previous'), show_progress=False)
|
2023-09-16 03:51:55 +02:00
|
|
|
|
2023-09-16 04:30:44 +02:00
|
|
|
shared.gradio['get_tokens-default'].click(get_token_ids, gradio('textbox-default'), gradio('tokens-default'), show_progress=False)
|
2024-07-21 05:01:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def handle_save_prompt(text):
|
|
|
|
return [
|
|
|
|
text,
|
|
|
|
utils.current_time() + ".txt",
|
|
|
|
"prompts/",
|
|
|
|
gr.update(visible=True)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def handle_delete_prompt(prompt):
|
|
|
|
return [
|
|
|
|
prompt + ".txt",
|
|
|
|
"prompts/",
|
|
|
|
gr.update(visible=True)
|
|
|
|
]
|