mirror of
https://github.com/oobabooga/text-generation-webui.git
synced 2026-04-05 22:55:22 +00:00
Autosave generated text in the Notebook tab (#7079)
This commit is contained in:
parent
d0befe0729
commit
faae4dc1b0
9 changed files with 325 additions and 52 deletions
|
|
@ -1,3 +1,6 @@
|
|||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import gradio as gr
|
||||
|
||||
from modules import logits, shared, ui, utils
|
||||
|
|
@ -7,7 +10,7 @@ from modules.text_generation import (
|
|||
get_token_ids,
|
||||
stop_everything_event
|
||||
)
|
||||
from modules.ui_default import handle_delete_prompt, handle_save_prompt
|
||||
from modules.ui_default import autosave_prompt
|
||||
from modules.utils import gradio
|
||||
|
||||
inputs = ('textbox-notebook', 'interface_state')
|
||||
|
|
@ -22,7 +25,8 @@ def create_ui():
|
|||
with gr.Column(scale=4):
|
||||
with gr.Tab('Raw'):
|
||||
with gr.Row():
|
||||
shared.gradio['textbox-notebook'] = gr.Textbox(value=load_prompt(shared.settings['prompt-notebook']), lines=27, elem_id='textbox-notebook', elem_classes=['textbox', 'add_scrollbar'])
|
||||
initial_text = load_prompt(shared.settings['prompt-notebook'])
|
||||
shared.gradio['textbox-notebook'] = gr.Textbox(value=initial_text, lines=27, elem_id='textbox-notebook', elem_classes=['textbox', 'add_scrollbar'])
|
||||
shared.gradio['token-counter-notebook'] = gr.HTML(value="<span>0</span>", elem_id="notebook-token-counter")
|
||||
|
||||
with gr.Tab('Markdown'):
|
||||
|
|
@ -57,9 +61,19 @@ def create_ui():
|
|||
gr.HTML('<div style="padding-bottom: 13px"></div>')
|
||||
with gr.Row():
|
||||
shared.gradio['prompt_menu-notebook'] = gr.Dropdown(choices=utils.get_available_prompts(), value=shared.settings['prompt-notebook'], 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'], 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)
|
||||
|
||||
with gr.Row():
|
||||
ui.create_refresh_button(shared.gradio['prompt_menu-notebook'], lambda: None, lambda: {'choices': utils.get_available_prompts()}, ['refresh-button'], interactive=not mu)
|
||||
shared.gradio['new_prompt-notebook'] = gr.Button('New', elem_classes=['refresh-button'], interactive=not mu)
|
||||
shared.gradio['rename_prompt-notebook'] = gr.Button('Rename', elem_classes=['refresh-button'], interactive=not mu)
|
||||
shared.gradio['delete_prompt-notebook'] = gr.Button('🗑️', elem_classes=['refresh-button'], interactive=not mu)
|
||||
shared.gradio['delete_prompt-confirm-notebook'] = gr.Button('Confirm', variant='stop', elem_classes=['refresh-button'], visible=False)
|
||||
shared.gradio['delete_prompt-cancel-notebook'] = gr.Button('Cancel', elem_classes=['refresh-button'], visible=False)
|
||||
|
||||
with gr.Row(visible=False) as shared.gradio['rename-row-notebook']:
|
||||
shared.gradio['rename_prompt_to-notebook'] = gr.Textbox(label="New name", elem_classes=['no-background'])
|
||||
shared.gradio['rename_prompt-cancel-notebook'] = gr.Button('Cancel', elem_classes=['refresh-button'])
|
||||
shared.gradio['rename_prompt-confirm-notebook'] = gr.Button('Confirm', elem_classes=['refresh-button'], variant='primary')
|
||||
|
||||
|
||||
def create_event_handlers():
|
||||
|
|
@ -67,7 +81,7 @@ def create_event_handlers():
|
|||
lambda x: x, gradio('textbox-notebook'), gradio('last_input-notebook')).then(
|
||||
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
|
||||
lambda: [gr.update(visible=True), gr.update(visible=False)], None, gradio('Stop-notebook', 'Generate-notebook')).then(
|
||||
generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then(
|
||||
generate_and_save_wrapper_notebook, gradio('textbox-notebook', 'interface_state', 'prompt_menu-notebook'), gradio(outputs), show_progress=False).then(
|
||||
lambda state, text: state.update({'textbox-notebook': text}), gradio('interface_state', 'textbox-notebook'), None).then(
|
||||
lambda: [gr.update(visible=False), gr.update(visible=True)], None, gradio('Stop-notebook', 'Generate-notebook')).then(
|
||||
None, None, None, js=f'() => {{{ui.audio_notification_js}}}')
|
||||
|
|
@ -76,7 +90,7 @@ def create_event_handlers():
|
|||
lambda x: x, gradio('textbox-notebook'), gradio('last_input-notebook')).then(
|
||||
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
|
||||
lambda: [gr.update(visible=True), gr.update(visible=False)], None, gradio('Stop-notebook', 'Generate-notebook')).then(
|
||||
generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then(
|
||||
generate_and_save_wrapper_notebook, gradio('textbox-notebook', 'interface_state', 'prompt_menu-notebook'), gradio(outputs), show_progress=False).then(
|
||||
lambda state, text: state.update({'textbox-notebook': text}), gradio('interface_state', 'textbox-notebook'), None).then(
|
||||
lambda: [gr.update(visible=False), gr.update(visible=True)], None, gradio('Stop-notebook', 'Generate-notebook')).then(
|
||||
None, None, None, js=f'() => {{{ui.audio_notification_js}}}')
|
||||
|
|
@ -85,7 +99,7 @@ def create_event_handlers():
|
|||
lambda x: x, gradio('last_input-notebook'), gradio('textbox-notebook'), show_progress=False).then(
|
||||
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
|
||||
lambda: [gr.update(visible=True), gr.update(visible=False)], None, gradio('Stop-notebook', 'Generate-notebook')).then(
|
||||
generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then(
|
||||
generate_and_save_wrapper_notebook, gradio('textbox-notebook', 'interface_state', 'prompt_menu-notebook'), gradio(outputs), show_progress=False).then(
|
||||
lambda state, text: state.update({'textbox-notebook': text}), gradio('interface_state', 'textbox-notebook'), None).then(
|
||||
lambda: [gr.update(visible=False), gr.update(visible=True)], None, gradio('Stop-notebook', 'Generate-notebook')).then(
|
||||
None, None, None, js=f'() => {{{ui.audio_notification_js}}}')
|
||||
|
|
@ -97,11 +111,126 @@ def create_event_handlers():
|
|||
shared.gradio['markdown_render-notebook'].click(lambda x: x, gradio('textbox-notebook'), gradio('markdown-notebook'), queue=False)
|
||||
shared.gradio['Stop-notebook'].click(stop_everything_event, None, None, queue=False)
|
||||
shared.gradio['prompt_menu-notebook'].change(load_prompt, gradio('prompt_menu-notebook'), gradio('textbox-notebook'), show_progress=False)
|
||||
shared.gradio['save_prompt-notebook'].click(handle_save_prompt, gradio('textbox-notebook'), gradio('save_contents', 'save_filename', 'save_root', 'file_saver'), show_progress=False)
|
||||
shared.gradio['delete_prompt-notebook'].click(handle_delete_prompt, gradio('prompt_menu-notebook'), gradio('delete_filename', 'delete_root', 'file_deleter'), show_progress=False)
|
||||
shared.gradio['new_prompt-notebook'].click(handle_new_prompt, None, gradio('prompt_menu-notebook'), show_progress=False)
|
||||
|
||||
shared.gradio['delete_prompt-notebook'].click(
|
||||
lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)],
|
||||
None,
|
||||
gradio('delete_prompt-notebook', 'delete_prompt-cancel-notebook', 'delete_prompt-confirm-notebook'),
|
||||
show_progress=False)
|
||||
|
||||
shared.gradio['delete_prompt-cancel-notebook'].click(
|
||||
lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)],
|
||||
None,
|
||||
gradio('delete_prompt-notebook', 'delete_prompt-cancel-notebook', 'delete_prompt-confirm-notebook'),
|
||||
show_progress=False)
|
||||
|
||||
shared.gradio['delete_prompt-confirm-notebook'].click(
|
||||
handle_delete_prompt_confirm_notebook,
|
||||
gradio('prompt_menu-notebook'),
|
||||
gradio('prompt_menu-notebook', 'delete_prompt-notebook', 'delete_prompt-cancel-notebook', 'delete_prompt-confirm-notebook'),
|
||||
show_progress=False)
|
||||
|
||||
shared.gradio['rename_prompt-notebook'].click(
|
||||
handle_rename_prompt_click_notebook,
|
||||
gradio('prompt_menu-notebook'),
|
||||
gradio('rename_prompt_to-notebook', 'rename_prompt-notebook', 'rename-row-notebook'),
|
||||
show_progress=False)
|
||||
|
||||
shared.gradio['rename_prompt-cancel-notebook'].click(
|
||||
lambda: [gr.update(visible=True), gr.update(visible=False)],
|
||||
None,
|
||||
gradio('rename_prompt-notebook', 'rename-row-notebook'),
|
||||
show_progress=False)
|
||||
|
||||
shared.gradio['rename_prompt-confirm-notebook'].click(
|
||||
handle_rename_prompt_confirm_notebook,
|
||||
gradio('rename_prompt_to-notebook', 'prompt_menu-notebook'),
|
||||
gradio('prompt_menu-notebook', 'rename_prompt-notebook', 'rename-row-notebook'),
|
||||
show_progress=False)
|
||||
|
||||
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)
|
||||
|
||||
shared.gradio['get_tokens-notebook'].click(get_token_ids, gradio('textbox-notebook'), gradio('tokens-notebook'), show_progress=False)
|
||||
|
||||
|
||||
def generate_and_save_wrapper_notebook(textbox_content, interface_state, prompt_name):
|
||||
"""Generate reply and automatically save the result for notebook mode with periodic saves"""
|
||||
last_save_time = time.monotonic()
|
||||
save_interval = 8
|
||||
output = textbox_content
|
||||
|
||||
# Initial autosave
|
||||
autosave_prompt(output, prompt_name)
|
||||
|
||||
for i, (output, html_output) in enumerate(generate_reply_wrapper(textbox_content, interface_state)):
|
||||
yield output, html_output
|
||||
|
||||
current_time = time.monotonic()
|
||||
# Save on first iteration or if save_interval seconds have passed
|
||||
if i == 0 or (current_time - last_save_time) >= save_interval:
|
||||
autosave_prompt(output, prompt_name)
|
||||
last_save_time = current_time
|
||||
|
||||
# Final autosave
|
||||
autosave_prompt(output, prompt_name)
|
||||
|
||||
|
||||
def handle_new_prompt():
|
||||
new_name = utils.current_time()
|
||||
|
||||
# Create the new prompt file
|
||||
prompt_path = Path("user_data/logs/notebook") / f"{new_name}.txt"
|
||||
prompt_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
prompt_path.write_text("In this story,", encoding='utf-8')
|
||||
|
||||
return gr.update(choices=utils.get_available_prompts(), value=new_name)
|
||||
|
||||
|
||||
def handle_delete_prompt_confirm_notebook(prompt_name):
|
||||
available_prompts = utils.get_available_prompts()
|
||||
current_index = available_prompts.index(prompt_name) if prompt_name in available_prompts else 0
|
||||
|
||||
(Path("user_data/logs/notebook") / f"{prompt_name}.txt").unlink(missing_ok=True)
|
||||
available_prompts = utils.get_available_prompts()
|
||||
|
||||
if available_prompts:
|
||||
new_value = available_prompts[min(current_index, len(available_prompts) - 1)]
|
||||
else:
|
||||
new_value = utils.current_time()
|
||||
Path("user_data/logs/notebook").mkdir(parents=True, exist_ok=True)
|
||||
(Path("user_data/logs/notebook") / f"{new_value}.txt").write_text("In this story,")
|
||||
available_prompts = [new_value]
|
||||
|
||||
return [
|
||||
gr.update(choices=available_prompts, value=new_value),
|
||||
gr.update(visible=True),
|
||||
gr.update(visible=False),
|
||||
gr.update(visible=False)
|
||||
]
|
||||
|
||||
|
||||
def handle_rename_prompt_click_notebook(current_name):
|
||||
return [
|
||||
gr.update(value=current_name),
|
||||
gr.update(visible=False),
|
||||
gr.update(visible=True)
|
||||
]
|
||||
|
||||
|
||||
def handle_rename_prompt_confirm_notebook(new_name, current_name):
|
||||
old_path = Path("user_data/logs/notebook") / f"{current_name}.txt"
|
||||
new_path = Path("user_data/logs/notebook") / f"{new_name}.txt"
|
||||
|
||||
if old_path.exists() and not new_path.exists():
|
||||
old_path.rename(new_path)
|
||||
|
||||
available_prompts = utils.get_available_prompts()
|
||||
return [
|
||||
gr.update(choices=available_prompts, value=new_name),
|
||||
gr.update(visible=True),
|
||||
gr.update(visible=False)
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue