diff --git a/css/main.css b/css/main.css index ee50ac6a..d85fe600 100644 --- a/css/main.css +++ b/css/main.css @@ -167,11 +167,11 @@ gradio-app > :first-child { } .textbox_default textarea { - height: calc(100dvh - 201px); + height: calc(100dvh - 202px); } .textbox_default_output textarea { - height: calc(100dvh - 117px); + height: calc(100dvh - 118px); } .textbox textarea { diff --git a/modules/prompts.py b/modules/prompts.py index 8f00cac2..79d9b56e 100644 --- a/modules/prompts.py +++ b/modules/prompts.py @@ -1,22 +1,33 @@ from pathlib import Path +from modules import shared, utils from modules.text_generation import get_encoded_length def load_prompt(fname): - if fname in ['None', '']: - return '' - else: - file_path = Path(f'user_data/prompts/{fname}.txt') - if not file_path.exists(): - return '' + if not fname: + # Create new file + new_name = utils.current_time() + prompt_path = Path("user_data/logs/notebook") / f"{new_name}.txt" + prompt_path.parent.mkdir(parents=True, exist_ok=True) + initial_content = "In this story," + prompt_path.write_text(initial_content, encoding='utf-8') + # Update settings to point to new file + shared.settings['prompt-notebook'] = new_name + + return initial_content + + file_path = Path(f'user_data/logs/notebook/{fname}.txt') + if file_path.exists(): with open(file_path, 'r', encoding='utf-8') as f: text = f.read() - if text[-1] == '\n': + if len(text) > 0 and text[-1] == '\n': text = text[:-1] return text + else: + return '' def count_tokens(text): diff --git a/modules/shared.py b/modules/shared.py index 4c9e34a7..5333ec4f 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -202,7 +202,7 @@ settings = { 'chat-instruct_command': 'Continue the chat dialogue below. Write a single reply for the character "<|character|>".\n\n<|prompt|>', 'enable_web_search': False, 'web_search_pages': 3, - 'prompt-notebook': 'QA', + 'prompt-notebook': '', 'preset': 'Qwen3 - Thinking' if Path('user_data/presets/Qwen3 - Thinking.yaml').exists() else None, 'max_new_tokens': 512, 'max_new_tokens_min': 1, diff --git a/modules/ui_default.py b/modules/ui_default.py index e6d2f92d..66174e35 100644 --- a/modules/ui_default.py +++ b/modules/ui_default.py @@ -1,3 +1,6 @@ +import time +from pathlib import Path + import gradio as gr from modules import logits, shared, ui, utils @@ -19,7 +22,8 @@ def create_ui(): with gr.Row(): with gr.Column(): with gr.Row(): - shared.gradio['textbox-default'] = gr.Textbox(value=load_prompt(shared.settings['prompt-notebook']), lines=27, label='Input', elem_classes=['textbox_default', 'add_scrollbar']) + initial_text = load_prompt(shared.settings['prompt-notebook']) + shared.gradio['textbox-default'] = gr.Textbox(value=initial_text, lines=27, label='Input', elem_classes=['textbox_default', 'add_scrollbar']) shared.gradio['token-counter-default'] = gr.HTML(value="0", elem_id="default-token-counter") with gr.Row(): @@ -30,9 +34,19 @@ def create_ui(): with gr.Row(): shared.gradio['prompt_menu-default'] = 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-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['new_prompt-default'] = gr.Button('New', elem_classes='refresh-button', interactive=not mu) + shared.gradio['rename_prompt-default'] = gr.Button('Rename', elem_classes='refresh-button', interactive=not mu) shared.gradio['delete_prompt-default'] = gr.Button('🗑️', elem_classes='refresh-button', interactive=not mu) + # Rename elements (initially hidden) + shared.gradio['rename_prompt_to-default'] = gr.Textbox(label="New name", elem_classes=['no-background'], visible=False) + shared.gradio['rename_prompt-cancel-default'] = gr.Button('Cancel', elem_classes=['refresh-button'], visible=False) + shared.gradio['rename_prompt-confirm-default'] = gr.Button('Confirm', elem_classes=['refresh-button'], variant='primary', visible=False) + + # Delete confirmation elements (initially hidden) + shared.gradio['delete_prompt-cancel-default'] = gr.Button('Cancel', elem_classes=['refresh-button'], visible=False) + shared.gradio['delete_prompt-confirm-default'] = gr.Button('Confirm', variant='stop', elem_classes=['refresh-button'], visible=False) + with gr.Column(): with gr.Tab('Raw'): shared.gradio['output_textbox'] = gr.Textbox(lines=27, label='Output', elem_id='textbox-default', elem_classes=['textbox_default_output', 'add_scrollbar']) @@ -64,7 +78,7 @@ def create_event_handlers(): shared.gradio['Generate-default'].click( ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( lambda: [gr.update(visible=True), gr.update(visible=False)], None, gradio('Stop-default', 'Generate-default')).then( - generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then( + generate_and_save_wrapper, gradio('textbox-default', 'interface_state', 'prompt_menu-default'), gradio(outputs), show_progress=False).then( lambda state, left, right: state.update({'textbox-default': left, 'output_textbox': right}), gradio('interface_state', 'textbox-default', 'output_textbox'), None).then( lambda: [gr.update(visible=False), gr.update(visible=True)], None, gradio('Stop-default', 'Generate-default')).then( None, None, None, js=f'() => {{{ui.audio_notification_js}}}') @@ -72,7 +86,7 @@ def create_event_handlers(): shared.gradio['textbox-default'].submit( ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( lambda: [gr.update(visible=True), gr.update(visible=False)], None, gradio('Stop-default', 'Generate-default')).then( - generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then( + generate_and_save_wrapper, gradio('textbox-default', 'interface_state', 'prompt_menu-default'), gradio(outputs), show_progress=False).then( lambda state, left, right: state.update({'textbox-default': left, 'output_textbox': right}), gradio('interface_state', 'textbox-default', 'output_textbox'), None).then( lambda: [gr.update(visible=False), gr.update(visible=True)], None, gradio('Stop-default', 'Generate-default')).then( None, None, None, js=f'() => {{{ui.audio_notification_js}}}') @@ -80,7 +94,7 @@ def create_event_handlers(): shared.gradio['Continue-default'].click( ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( lambda: [gr.update(visible=True), gr.update(visible=False)], None, gradio('Stop-default', 'Generate-default')).then( - generate_reply_wrapper, [shared.gradio['output_textbox']] + gradio(inputs)[1:], gradio(outputs), show_progress=False).then( + continue_and_save_wrapper, gradio('output_textbox', 'textbox-default', 'interface_state', 'prompt_menu-default'), gradio(outputs), show_progress=False).then( lambda state, left, right: state.update({'textbox-default': left, 'output_textbox': right}), gradio('interface_state', 'textbox-default', 'output_textbox'), None).then( lambda: [gr.update(visible=False), gr.update(visible=True)], None, gradio('Stop-default', 'Generate-default')).then( None, None, None, js=f'() => {{{ui.audio_notification_js}}}') @@ -88,8 +102,44 @@ def create_event_handlers(): shared.gradio['Stop-default'].click(stop_everything_event, None, None, queue=False) shared.gradio['markdown_render-default'].click(lambda x: x, gradio('output_textbox'), gradio('markdown-default'), queue=False) shared.gradio['prompt_menu-default'].change(load_prompt, gradio('prompt_menu-default'), gradio('textbox-default'), show_progress=False) - 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) + shared.gradio['new_prompt-default'].click(handle_new_prompt, None, gradio('prompt_menu-default'), show_progress=False) + + shared.gradio['delete_prompt-default'].click( + lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)], + None, + gradio('delete_prompt-default', 'delete_prompt-cancel-default', 'delete_prompt-confirm-default'), + show_progress=False) + + shared.gradio['delete_prompt-cancel-default'].click( + lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)], + None, + gradio('delete_prompt-default', 'delete_prompt-cancel-default', 'delete_prompt-confirm-default'), + show_progress=False) + + shared.gradio['delete_prompt-confirm-default'].click( + handle_delete_prompt_confirm_default, + gradio('prompt_menu-default'), + gradio('prompt_menu-default', 'delete_prompt-default', 'delete_prompt-cancel-default', 'delete_prompt-confirm-default'), + show_progress=False) + + shared.gradio['rename_prompt-default'].click( + handle_rename_prompt_click_default, + gradio('prompt_menu-default'), + gradio('rename_prompt_to-default', 'rename_prompt-default', 'rename_prompt-cancel-default', 'rename_prompt-confirm-default'), + show_progress=False) + + shared.gradio['rename_prompt-cancel-default'].click( + lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)], + None, + gradio('rename_prompt_to-default', 'rename_prompt-default', 'rename_prompt-cancel-default', 'rename_prompt-confirm-default'), + show_progress=False) + + shared.gradio['rename_prompt-confirm-default'].click( + handle_rename_prompt_confirm_default, + gradio('rename_prompt_to-default', 'prompt_menu-default'), + gradio('prompt_menu-default', 'rename_prompt_to-default', 'rename_prompt-default', 'rename_prompt-cancel-default', 'rename_prompt-confirm-default'), + show_progress=False) + shared.gradio['textbox-default'].change(lambda x: f"{count_tokens(x)}", 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( @@ -98,18 +148,113 @@ def create_event_handlers(): shared.gradio['get_tokens-default'].click(get_token_ids, gradio('textbox-default'), gradio('tokens-default'), show_progress=False) -def handle_save_prompt(text): +def autosave_prompt(text, prompt_name): + """Automatically save the text to the selected prompt file""" + if prompt_name and text.strip(): + prompt_path = Path("user_data/logs/notebook") / f"{prompt_name}.txt" + prompt_path.parent.mkdir(parents=True, exist_ok=True) + prompt_path.write_text(text, encoding='utf-8') + + +def generate_and_save_wrapper(textbox_content, interface_state, prompt_name): + """Generate reply and automatically save the result 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 continue_and_save_wrapper(output_textbox, textbox_content, interface_state, prompt_name): + """Continue generation and automatically save the result with periodic saves""" + last_save_time = time.monotonic() + save_interval = 8 + output = output_textbox + + # Initial autosave + autosave_prompt(output, prompt_name) + + for i, (output, html_output) in enumerate(generate_reply_wrapper(output_textbox, 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_default(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 [ - text, - utils.current_time() + ".txt", - "user_data/prompts/", + 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_default(current_name): + return [ + gr.update(value=current_name, visible=True), + gr.update(visible=False), + gr.update(visible=True), gr.update(visible=True) ] -def handle_delete_prompt(prompt): +def handle_rename_prompt_confirm_default(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 [ - prompt + ".txt", - "user_data/prompts/", - gr.update(visible=True) + gr.update(choices=available_prompts, value=new_name), + gr.update(visible=False), + gr.update(visible=True), + gr.update(visible=False), + gr.update(visible=False) ] diff --git a/modules/ui_notebook.py b/modules/ui_notebook.py index e6b87342..e154a0ba 100644 --- a/modules/ui_notebook.py +++ b/modules/ui_notebook.py @@ -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="0", elem_id="notebook-token-counter") with gr.Tab('Markdown'): @@ -57,9 +61,19 @@ def create_ui(): gr.HTML('
') 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"{count_tokens(x)}", 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) + ] diff --git a/modules/ui_session.py b/modules/ui_session.py index c0a34228..431aeeb0 100644 --- a/modules/ui_session.py +++ b/modules/ui_session.py @@ -77,7 +77,7 @@ def handle_default_to_notebook_change(show_two_columns, default_input, default_o gr.update(), gr.update(), gr.update(), - default_input, + default_output, gr.update(value=default_prompt, choices=utils.get_available_prompts()) ] diff --git a/modules/utils.py b/modules/utils.py index 21873541..da5ac978 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -159,10 +159,12 @@ def get_available_presets(): def get_available_prompts(): - prompt_files = list(Path('user_data/prompts').glob('*.txt')) + notebook_dir = Path('user_data/logs/notebook') + notebook_dir.mkdir(parents=True, exist_ok=True) + + prompt_files = list(notebook_dir.glob('*.txt')) sorted_files = sorted(prompt_files, key=lambda x: x.stat().st_mtime, reverse=True) prompts = [file.stem for file in sorted_files] - prompts.append('None') return prompts diff --git a/user_data/prompts/Alpaca-with-Input.txt b/user_data/prompts/Alpaca-with-Input.txt deleted file mode 100644 index 56df0e28..00000000 --- a/user_data/prompts/Alpaca-with-Input.txt +++ /dev/null @@ -1,10 +0,0 @@ -Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. - -### Instruction: -Instruction - -### Input: -Input - -### Response: - diff --git a/user_data/prompts/QA.txt b/user_data/prompts/QA.txt deleted file mode 100644 index 32b0e235..00000000 --- a/user_data/prompts/QA.txt +++ /dev/null @@ -1,4 +0,0 @@ -Common sense questions and answers - -Question: -Factual answer: