Skip to content

Commit

Permalink
Merge pull request #418 from rmusser01/dev
Browse files Browse the repository at this point in the history
More prompt fixes; but no keyword search added yet
  • Loading branch information
rmusser01 authored Nov 2, 2024
2 parents c613e94 + 7700232 commit 600de4e
Show file tree
Hide file tree
Showing 19 changed files with 1,513 additions and 410 deletions.
12 changes: 2 additions & 10 deletions App_Function_Libraries/DB/DB_Manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from App_Function_Libraries.DB.Prompts_DB import list_prompts as sqlite_list_prompts, \
fetch_prompt_details as sqlite_fetch_prompt_details, add_prompt as sqlite_add_prompt, \
search_prompts as sqlite_search_prompts, add_or_update_prompt as sqlite_add_or_update_prompt, \
load_prompt_details as sqlite_load_prompt_details, load_preset_prompts as sqlite_load_preset_prompts, \
insert_prompt_to_db as sqlite_insert_prompt_to_db, delete_prompt as sqlite_delete_prompt
load_prompt_details as sqlite_load_prompt_details, insert_prompt_to_db as sqlite_insert_prompt_to_db, \
delete_prompt as sqlite_delete_prompt
from App_Function_Libraries.DB.SQLite_DB import (
update_media_content as sqlite_update_media_content,
search_and_display as sqlite_search_and_display,
Expand Down Expand Up @@ -501,13 +501,6 @@ def load_prompt_details(*args, **kwargs):
# Implement Elasticsearch version
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")

def load_preset_prompts(*args, **kwargs):
if db_type == 'sqlite':
return sqlite_load_preset_prompts()
elif db_type == 'elasticsearch':
# Implement Elasticsearch version
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")

def insert_prompt_to_db(*args, **kwargs):
if db_type == 'sqlite':
return sqlite_insert_prompt_to_db(*args, **kwargs)
Expand Down Expand Up @@ -540,7 +533,6 @@ def mark_as_trash(media_id: int) -> None:
else:
raise ValueError(f"Unsupported database type: {db_type}")


def get_latest_transcription(*args, **kwargs):
if db_type == 'sqlite':
return sqlite_get_latest_transcription(*args, **kwargs)
Expand Down
14 changes: 0 additions & 14 deletions App_Function_Libraries/DB/Prompts_DB.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,6 @@ def list_prompts(page=1, per_page=10):
total_pages = (total_count + per_page - 1) // per_page
return prompts, total_pages, page

# This will not scale. For a large number of prompts, use a more efficient method.
# FIXME - see above statement.
def load_preset_prompts():
logging.debug("load_preset_prompts: Loading preset prompts.")
try:
with sqlite3.connect(get_database_path('prompts.db')) as conn:
cursor = conn.cursor()
cursor.execute('SELECT name FROM Prompts ORDER BY name ASC')
prompts = [row[0] for row in cursor.fetchall()]
return prompts
except sqlite3.Error as e:
print(f"Database error: {e}")
return []


def insert_prompt_to_db(title, author, description, system_prompt, user_prompt, keywords=None):
return add_prompt(title, author, description, system_prompt, user_prompt, keywords)
Expand Down
13 changes: 7 additions & 6 deletions App_Function_Libraries/Gradio_Related.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,13 @@ def launch_ui(share_public=None, server_mode=False):
create_ragdb_keyword_items_tab()

with gr.TabItem("Prompts", id='view prompts group', visible=True):
create_prompt_view_tab()
create_prompt_search_tab()
create_prompt_edit_tab()
create_prompt_clone_tab()
create_prompt_suggestion_tab()
create_prompts_export_tab()
with gr.Tabs():
create_prompt_view_tab()
create_prompt_search_tab()
create_prompt_edit_tab()
create_prompt_clone_tab()
create_prompt_suggestion_tab()
create_prompts_export_tab()

with gr.TabItem("Manage Media DB Items", id="manage group", visible=True):
create_media_edit_tab()
Expand Down
103 changes: 64 additions & 39 deletions App_Function_Libraries/Gradio_UI/Anki_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@
# Imports
import json
import logging
from typing import Optional, Tuple, List
import os
import tempfile
from typing import Optional, Tuple, List, Dict
#
# External Imports
import genanki
import gradio as gr

#
# Local Imports
from App_Function_Libraries.Chat.Chat_Functions import approximate_token_count, update_chat_content, save_chat_history, \
save_chat_history_to_db_wrapper
from App_Function_Libraries.DB.DB_Manager import load_preset_prompts
from App_Function_Libraries.DB.DB_Manager import list_prompts
from App_Function_Libraries.Gradio_UI.Chat_ui import update_dropdown_multiple, chat_wrapper, update_selected_parts, \
search_conversations, regenerate_last_message, load_conversation, debug_output
from App_Function_Libraries.Gradio_UI.Gradio_Shared import update_user_prompt
#from outlines import models, prompts
#
# Local Imports
from App_Function_Libraries.Third_Party.Anki import sanitize_html, generate_card_choices, \
export_cards, load_card_for_editing, handle_file_upload, \
validate_for_ui, update_card_with_validation, update_card_choices, enhanced_file_upload, \
handle_validation
from App_Function_Libraries.Utils.Utils import default_api_endpoint, global_api_endpoints, format_api_name


#
############################################################################################################
#
Expand Down Expand Up @@ -374,12 +372,6 @@ def process_validation_result(is_valid, message):


def create_anki_generator_tab():
import genanki
import json
from typing import List, Dict, Any
import tempfile
import os

with gr.TabItem("Anki Deck Generator", visible=True):
try:
default_value = None
Expand All @@ -402,6 +394,7 @@ def create_anki_generator_tab():
media_content = gr.State({})
selected_parts = gr.State([])
conversation_id = gr.State(None)
initial_prompts, total_pages, current_page = list_prompts(page=1, per_page=10)

with gr.Row():
with gr.Column(scale=1):
Expand Down Expand Up @@ -447,12 +440,19 @@ def create_anki_generator_tab():
custom_prompt_checkbox = gr.Checkbox(label="Use a Custom Prompt",
value=False,
visible=True)
preset_prompt_checkbox = gr.Checkbox(label="Use a pre-set Prompt",
preset_prompt_checkbox = gr.Checkbox(label="Use a Pre-set Prompt",
value=False,
visible=True)
preset_prompt = gr.Dropdown(label="Select Preset Prompt",
choices=load_preset_prompts(),
visible=False)
with gr.Row(visible=False) as preset_prompt_controls:
prev_prompt_page = gr.Button("Previous")
next_prompt_page = gr.Button("Next")
current_prompt_page_text = gr.Text(f"Page {current_page} of {total_pages}")
current_prompt_page_state = gr.State(value=1)

preset_prompt = gr.Dropdown(
label="Select Preset Prompt",
choices=initial_prompts
)
user_prompt = gr.Textbox(label="Custom Prompt",
placeholder="Enter custom prompt here",
lines=3,
Expand All @@ -475,44 +475,63 @@ def create_anki_generator_tab():
save_chat_history_as_file = gr.Button("Save Chat History as File")
download_file = gr.File(label="Download Chat History")

# Restore original functionality
search_button.click(
fn=update_dropdown_multiple,
inputs=[search_query_input, search_type_input, keyword_filter_input],
outputs=[items_output, item_mapping]
)

def update_prompts(preset_name):
prompts = update_user_prompt(preset_name)
def update_prompt_visibility(custom_prompt_checked, preset_prompt_checked):
user_prompt_visible = custom_prompt_checked
system_prompt_visible = custom_prompt_checked
preset_prompt_visible = preset_prompt_checked
preset_prompt_controls_visible = preset_prompt_checked
return (
gr.update(value=prompts["user_prompt"], visible=True),
gr.update(value=prompts["system_prompt"], visible=True)
gr.update(visible=user_prompt_visible, interactive=user_prompt_visible),
gr.update(visible=system_prompt_visible, interactive=system_prompt_visible),
gr.update(visible=preset_prompt_visible, interactive=preset_prompt_visible),
gr.update(visible=preset_prompt_controls_visible)
)

def update_prompt_page(direction, current_page_val):
new_page = current_page_val + direction
if new_page < 1:
new_page = 1
prompts, total_pages, _ = list_prompts(page=new_page, per_page=20)
if new_page > total_pages:
new_page = total_pages
prompts, total_pages, _ = list_prompts(page=new_page, per_page=20)
return (
gr.update(choices=prompts),
gr.update(value=f"Page {new_page} of {total_pages}"),
new_page
)

def clear_chat():
return [], None # Return empty list for chatbot and None for conversation_id

clear_chat_button.click(
clear_chat,
outputs=[chatbot, conversation_id]
custom_prompt_checkbox.change(
update_prompt_visibility,
inputs=[custom_prompt_checkbox, preset_prompt_checkbox],
outputs=[user_prompt, system_prompt_input, preset_prompt, preset_prompt_controls]
)

preset_prompt.change(
update_prompts,
inputs=preset_prompt,
outputs=[user_prompt, system_prompt_input]
preset_prompt_checkbox.change(
update_prompt_visibility,
inputs=[custom_prompt_checkbox, preset_prompt_checkbox],
outputs=[user_prompt, system_prompt_input, preset_prompt, preset_prompt_controls]
)

custom_prompt_checkbox.change(
fn=lambda x: (gr.update(visible=x), gr.update(visible=x)),
inputs=[custom_prompt_checkbox],
outputs=[user_prompt, system_prompt_input]
prev_prompt_page.click(
lambda x: update_prompt_page(-1, x),
inputs=[current_prompt_page_state],
outputs=[preset_prompt, current_prompt_page_text, current_prompt_page_state]
)

preset_prompt_checkbox.change(
fn=lambda x: gr.update(visible=x),
inputs=[preset_prompt_checkbox],
outputs=[preset_prompt]
next_prompt_page.click(
lambda x: update_prompt_page(1, x),
inputs=[current_prompt_page_state],
outputs=[preset_prompt, current_prompt_page_text, current_prompt_page_state]
)

submit.click(
Expand All @@ -534,6 +553,12 @@ def clear_chat():
outputs=[token_count_display]
)


clear_chat_button.click(
clear_chat,
outputs=[chatbot, conversation_id]
)

items_output.change(
update_chat_content,
inputs=[items_output, use_content, use_summary, use_prompt, item_mapping],
Expand Down
Loading

0 comments on commit 600de4e

Please sign in to comment.