Skip to content

Commit

Permalink
Merge pull request #455 from rmusser01/dev
Browse files Browse the repository at this point in the history
Added Docs for current/planned features
  • Loading branch information
rmusser01 authored Dec 3, 2024
2 parents 8deaf12 + 2eff367 commit 8712523
Show file tree
Hide file tree
Showing 38 changed files with 2,370 additions and 187 deletions.
4 changes: 2 additions & 2 deletions App_Function_Libraries/DB/DB_Manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ def list_prompts(*args, **kwargs):
# Implement Elasticsearch version
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")

def search_prompts(query):
def search_prompts(*args, **kwargs):
if db_type == 'sqlite':
return sqlite_search_prompts(query)
return sqlite_search_prompts(*args, **kwargs)
elif db_type == 'elasticsearch':
# Implement Elasticsearch version
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
Expand Down
97 changes: 63 additions & 34 deletions App_Function_Libraries/DB/Prompts_DB.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,54 +141,83 @@ def get_prompt_db_connection():
return sqlite3.connect(prompt_db_path)


def search_prompts(query):
logging.debug(f"search_prompts: Searching prompts with query: {query}")
# def search_prompts(query):
# logging.debug(f"search_prompts: Searching prompts with query: {query}")
# try:
# with get_prompt_db_connection() as conn:
# cursor = conn.cursor()
# cursor.execute("""
# SELECT p.name, p.details, p.system, p.user, GROUP_CONCAT(k.keyword, ', ') as keywords
# FROM Prompts p
# LEFT JOIN PromptKeywords pk ON p.id = pk.prompt_id
# LEFT JOIN Keywords k ON pk.keyword_id = k.id
# WHERE p.name LIKE ? OR p.details LIKE ? OR p.system LIKE ? OR p.user LIKE ? OR k.keyword LIKE ?
# GROUP BY p.id
# ORDER BY p.name
# """, (f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%'))
# return cursor.fetchall()
# except sqlite3.Error as e:
# logging.error(f"Error searching prompts: {e}")
# return []


def search_prompts(query, search_fields):
logging.debug(f"search_prompts: Searching prompts with query: {query}, fields: {search_fields}")
try:
with get_prompt_db_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT p.name, p.details, p.system, p.user, GROUP_CONCAT(k.keyword, ', ') as keywords

where_clauses = []
params = []

if 'title' in search_fields:
where_clauses.append("p.name LIKE ?")
params.append(f'%{query}%')
if 'content' in search_fields:
where_clauses.append("(p.details LIKE ? OR p.system LIKE ? OR p.user LIKE ?)")
params.extend([f'%{query}%'] * 3)
if 'keywords' in search_fields:
where_clauses.append("k.keyword LIKE ?")
params.append(f'%{query}%')

where_clause = " OR ".join(where_clauses)

cursor.execute(f"""
SELECT DISTINCT p.name, p.details, p.system, p.user, GROUP_CONCAT(k.keyword, ', ') as keywords
FROM Prompts p
LEFT JOIN PromptKeywords pk ON p.id = pk.prompt_id
LEFT JOIN Keywords k ON pk.keyword_id = k.id
WHERE p.name LIKE ? OR p.details LIKE ? OR p.system LIKE ? OR p.user LIKE ? OR k.keyword LIKE ?
WHERE {where_clause}
GROUP BY p.id
ORDER BY p.name
""", (f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%'))
""", params)
return cursor.fetchall()
except sqlite3.Error as e:
logging.error(f"Error searching prompts: {e}")
return []


def search_prompts_by_keyword(keyword, page=1, per_page=10):
logging.debug(f"search_prompts_by_keyword: Searching prompts by keyword: {keyword}")
normalized_keyword = normalize_keyword(keyword)
offset = (page - 1) * per_page
with sqlite3.connect(get_database_path('prompts.db')) as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT DISTINCT p.name
FROM Prompts p
JOIN PromptKeywords pk ON p.id = pk.prompt_id
JOIN Keywords k ON pk.keyword_id = k.id
WHERE k.keyword LIKE ?
LIMIT ? OFFSET ?
''', ('%' + normalized_keyword + '%', per_page, offset))
prompts = [row[0] for row in cursor.fetchall()]

# Get total count of matching prompts
cursor.execute('''
SELECT COUNT(DISTINCT p.id)
FROM Prompts p
JOIN PromptKeywords pk ON p.id = pk.prompt_id
JOIN Keywords k ON pk.keyword_id = k.id
WHERE k.keyword LIKE ?
''', ('%' + normalized_keyword + '%',))
total_count = cursor.fetchone()[0]

total_pages = (total_count + per_page - 1) // per_page
return prompts, total_pages, page
def fetch_item_details_with_keywords(media_id):
logging.debug(f"fetch_item_details_with_keywords: Fetching details for media item with ID: {media_id}")
try:
with sqlite3.connect(get_database_path('prompts.db')) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT m.content, mm.prompt, mm.summary, GROUP_CONCAT(k.keyword, ', ') as keywords
FROM Media m
LEFT JOIN MediaModifications mm ON m.id = mm.media_id
LEFT JOIN MediaKeywords mk ON m.id = mk.media_id
LEFT JOIN Keywords k ON mk.keyword_id = k.id
WHERE m.id = ?
GROUP BY m.id
""", (media_id,))
result = cursor.fetchone()
if result:
content, prompt, summary, keywords = result
return content, prompt, summary, keywords
return None, None, None, None
except sqlite3.Error as e:
return f"Database error: {e}"


def update_prompt_keywords(prompt_name, new_keywords):
Expand Down
95 changes: 62 additions & 33 deletions App_Function_Libraries/Gradio_UI/Media_edit.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# Media_edit.py
# Functions for Gradio Media_Edit UI

#
# Imports
import logging
import uuid

#
# External Imports
import gradio as gr
#
# Local Imports
from App_Function_Libraries.DB.DB_Manager import add_prompt, update_media_content, db, add_or_update_prompt, \
load_prompt_details, fetch_keywords_for_media, update_keywords_for_media, fetch_prompt_details, list_prompts
fetch_keywords_for_media, update_keywords_for_media, fetch_prompt_details, list_prompts
from App_Function_Libraries.DB.Prompts_DB import fetch_item_details_with_keywords
from App_Function_Libraries.Gradio_UI.Gradio_Shared import update_dropdown
from App_Function_Libraries.DB.SQLite_DB import fetch_item_details

#
#######################################################################################################################
#
# Functions:

def create_media_edit_tab():
with gr.TabItem("Edit Existing Items in the Media DB", visible=True):
Expand Down Expand Up @@ -96,7 +100,7 @@ def create_media_edit_and_clone_tab():
with gr.Column():
search_query_input = gr.Textbox(label="Search Query", placeholder="Enter your search query here...")
search_type_input = gr.Radio(choices=["Title", "URL", "Keyword", "Content"], value="Title",
label="Search By")
label="Search By")
with gr.Column():
search_button = gr.Button("Search")
clone_button = gr.Button("Clone Item")
Expand All @@ -108,6 +112,7 @@ def create_media_edit_and_clone_tab():
content_input = gr.Textbox(label="Edit Content", lines=10)
prompt_input = gr.Textbox(label="Edit Prompt", lines=3)
summary_input = gr.Textbox(label="Edit Summary", lines=5)
keywords_input = gr.Textbox(label="Edit Keywords (comma-separated)", lines=2)
new_title_input = gr.Textbox(label="New Title (for cloning)", visible=False)
status_message = gr.Textbox(label="Status", interactive=False)

Expand All @@ -120,14 +125,14 @@ def create_media_edit_and_clone_tab():
def load_selected_media_content(selected_item, item_mapping):
if selected_item and item_mapping and selected_item in item_mapping:
media_id = item_mapping[selected_item]
content, prompt, summary = fetch_item_details(media_id)
return content, prompt, summary, gr.update(visible=True), gr.update(visible=False)
return "No item selected or invalid selection", "", "", gr.update(visible=False), gr.update(visible=False)
content, prompt, summary, keywords = fetch_item_details_with_keywords(media_id)
return content, prompt, summary, keywords, gr.update(visible=True), gr.update(visible=False)
return "No item selected or invalid selection", "", "", "", gr.update(visible=False), gr.update(visible=False)

items_output.change(
fn=load_selected_media_content,
inputs=[items_output, item_mapping],
outputs=[content_input, prompt_input, summary_input, clone_button, save_clone_button]
outputs=[content_input, prompt_input, summary_input, keywords_input, clone_button, save_clone_button]
)

def prepare_for_cloning(selected_item):
Expand All @@ -139,7 +144,7 @@ def prepare_for_cloning(selected_item):
outputs=[new_title_input, save_clone_button]
)

def save_cloned_item(selected_item, item_mapping, content, prompt, summary, new_title):
def save_cloned_item(selected_item, item_mapping, content, prompt, summary, keywords, new_title):
if selected_item and item_mapping and selected_item in item_mapping:
original_media_id = item_mapping[selected_item]
try:
Expand Down Expand Up @@ -167,13 +172,16 @@ def save_cloned_item(selected_item, item_mapping, content, prompt, summary, new_
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
""", (new_media_id, prompt, summary))

# Copy keywords from the original item
cursor.execute("""
INSERT INTO MediaKeywords (media_id, keyword_id)
SELECT ?, keyword_id
FROM MediaKeywords
WHERE media_id = ?
""", (new_media_id, original_media_id))
# Handle keywords
keyword_list = [k.strip() for k in keywords.split(',') if k.strip()]
for keyword in keyword_list:
# Insert keyword if it doesn't exist
cursor.execute("INSERT OR IGNORE INTO Keywords (keyword) VALUES (?)", (keyword,))
cursor.execute("SELECT id FROM Keywords WHERE keyword = ?", (keyword,))
keyword_id = cursor.fetchone()[0]

# Associate keyword with the new media item
cursor.execute("INSERT INTO MediaKeywords (media_id, keyword_id) VALUES (?, ?)", (new_media_id, keyword_id))

# Update full-text search index
cursor.execute("""
Expand All @@ -193,7 +201,7 @@ def save_cloned_item(selected_item, item_mapping, content, prompt, summary, new_

save_clone_button.click(
fn=save_cloned_item,
inputs=[items_output, item_mapping, content_input, prompt_input, summary_input, new_title_input],
inputs=[items_output, item_mapping, content_input, prompt_input, summary_input, keywords_input, new_title_input],
outputs=[status_message, new_title_input, save_clone_button]
)

Expand Down Expand Up @@ -223,6 +231,7 @@ def create_prompt_edit_tab():
description_input = gr.Textbox(label="Description", placeholder="Enter the prompt description", lines=3)
system_prompt_input = gr.Textbox(label="System Prompt", placeholder="Enter the system prompt", lines=3)
user_prompt_input = gr.Textbox(label="User Prompt", placeholder="Enter the user prompt", lines=3)
keywords_input = gr.Textbox(label="Keywords", placeholder="Enter keywords separated by commas", lines=2)
add_prompt_button = gr.Button("Add/Update Prompt")
add_prompt_output = gr.HTML()

Expand Down Expand Up @@ -291,10 +300,16 @@ def on_next_page_click(current_page, total_pages):
]
)

# Modified function to add or update a prompt with keywords
def add_or_update_prompt_with_keywords(title, author, description, system_prompt, user_prompt, keywords):
keyword_list = [k.strip() for k in keywords.split(',') if k.strip()]
result = add_or_update_prompt(title, author, description, system_prompt, user_prompt, keyword_list)
return gr.HTML(result)

# Event handler for adding or updating a prompt
add_prompt_button.click(
fn=add_or_update_prompt,
inputs=[title_input, author_input, description_input, system_prompt_input, user_prompt_input],
fn=add_or_update_prompt_with_keywords,
inputs=[title_input, author_input, description_input, system_prompt_input, user_prompt_input, keywords_input],
outputs=[add_prompt_output]
).then(
fn=update_prompt_dropdown,
Expand All @@ -319,14 +334,16 @@ def load_prompt_details(selected_prompt):
gr.update(value=author or ""),
gr.update(value=description or ""),
gr.update(value=system_prompt or ""),
gr.update(value=user_prompt or "")
gr.update(value=user_prompt or ""),
gr.update(value=keywords or "")
)
else:
return (
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value="")
)

Expand All @@ -339,12 +356,12 @@ def load_prompt_details(selected_prompt):
author_input,
description_input,
system_prompt_input,
user_prompt_input
user_prompt_input,
keywords_input
]
)



def create_prompt_clone_tab():
# Initialize state variables for pagination
current_page_state = gr.State(value=1)
Expand All @@ -367,10 +384,11 @@ def create_prompt_clone_tab():

with gr.Column():
title_input = gr.Textbox(label="Title", placeholder="Enter the prompt title")
author_input = gr.Textbox(label="Author", placeholder="Enter the prompt's author", lines=3)
author_input = gr.Textbox(label="Author", placeholder="Enter the prompt's author", lines=1)
description_input = gr.Textbox(label="Description", placeholder="Enter the prompt description", lines=3)
system_prompt_input = gr.Textbox(label="System Prompt", placeholder="Enter the system prompt", lines=3)
user_prompt_input = gr.Textbox(label="User Prompt", placeholder="Enter the user prompt", lines=3)
keywords_input = gr.Textbox(label="Keywords", placeholder="Enter keywords separated by commas", lines=2)
clone_prompt_button = gr.Button("Clone Selected Prompt")
save_cloned_prompt_button = gr.Button("Save Cloned Prompt", visible=False)
add_prompt_output = gr.HTML()
Expand Down Expand Up @@ -451,20 +469,22 @@ def load_prompt_details(selected_prompt):
gr.update(value=author or ""),
gr.update(value=description or ""),
gr.update(value=system_prompt or ""),
gr.update(value=user_prompt or "")
gr.update(value=user_prompt or ""),
gr.update(value=keywords or "")
)
return (
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value="")
)

prompt_dropdown.change(
fn=load_prompt_details,
inputs=[prompt_dropdown],
outputs=[title_input, author_input, description_input, system_prompt_input, user_prompt_input]
outputs=[title_input, author_input, description_input, system_prompt_input, user_prompt_input, keywords_input]
)

# Prepare for cloning
Expand All @@ -480,20 +500,29 @@ def prepare_for_cloning(selected_prompt):
)

# Function to save cloned prompt
def save_cloned_prompt(title, author, description, system_prompt, user_prompt, current_page):
def save_cloned_prompt(title, author, description, system_prompt, user_prompt, keywords, current_page):
try:
result = add_prompt(title, author, description, system_prompt, user_prompt)
keyword_list = [k.strip() for k in keywords.split(',') if k.strip()]
result = add_prompt(title, author, description, system_prompt, user_prompt, keyword_list)
if result == "Prompt added successfully.":
# After adding, refresh the prompt dropdown
prompt_dropdown_update = update_prompt_dropdown(page=current_page)
return (result, *prompt_dropdown_update)
return result, *prompt_dropdown_update
else:
return (result, gr.update(), gr.update(), gr.update(), gr.update(), current_page, total_pages_state.value)
return result, gr.update(), gr.update(), gr.update(), gr.update(), current_page, \
total_pages_state.value
except Exception as e:
return (f"Error saving cloned prompt: {str(e)}", gr.update(), gr.update(), gr.update(), gr.update(), current_page, total_pages_state.value)
return f"Error saving cloned prompt: {str(e)}", gr.update(), gr.update(), gr.update(), gr.update(), \
current_page, total_pages_state.value

save_cloned_prompt_button.click(
fn=save_cloned_prompt,
inputs=[title_input, author_input, description_input, system_prompt_input, user_prompt_input, current_page_state],
outputs=[add_prompt_output, prompt_dropdown, page_display, prev_page_button, next_page_button, current_page_state, total_pages_state]
inputs=[title_input, author_input, description_input, system_prompt_input, user_prompt_input,
keywords_input, current_page_state],
outputs=[add_prompt_output, prompt_dropdown, page_display, prev_page_button, next_page_button,
current_page_state, total_pages_state]
)

#
# End of Media_edit.py
#######################################################################################################################
Loading

0 comments on commit 8712523

Please sign in to comment.