Spaces:
Running
Running
File size: 13,776 Bytes
fa9a583 cd5e862 83c8d2b fa9a583 cd5e862 fa9a583 83c8d2b fa9a583 83c8d2b fa9a583 83c8d2b fa9a583 83c8d2b fa9a583 83c8d2b fa9a583 83c8d2b fa9a583 83c8d2b fa9a583 cd5e862 fa9a583 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# 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
from App_Function_Libraries.Gradio_UI.Gradio_Shared import update_dropdown, update_prompt_dropdown
from App_Function_Libraries.DB.SQLite_DB import fetch_item_details
def create_media_edit_tab():
with gr.TabItem("Edit Existing Items"):
gr.Markdown("# Search and Edit Media Items")
with gr.Row():
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")
search_button = gr.Button("Search")
with gr.Row():
items_output = gr.Dropdown(label="Select Item", choices=[], interactive=True)
item_mapping = gr.State({})
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)
# Adding keyword input box for editing
keywords_input = gr.Textbox(label="Edit Keywords (comma-separated)", placeholder="Enter keywords here...")
update_button = gr.Button("Update Media Content")
status_message = gr.Textbox(label="Status", interactive=False)
# Function to update the dropdown with search results
search_button.click(
fn=update_dropdown,
inputs=[search_query_input, search_type_input],
outputs=[items_output, item_mapping]
)
# Function to load selected media content including keywords
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)
# Fetch keywords for the selected item
keywords = fetch_keywords_for_media(media_id)
keywords_str = ", ".join(keywords) if keywords else ""
return content, prompt, summary, keywords_str
return "No item selected or invalid selection", "", "", ""
# Load the selected media content and associated keywords
items_output.change(
fn=load_selected_media_content,
inputs=[items_output, item_mapping],
outputs=[content_input, prompt_input, summary_input, keywords_input]
)
# Function to update media content, prompt, summary, and keywords
def update_media_with_keywords(selected_item, item_mapping, content, prompt, summary, keywords):
if selected_item and item_mapping and selected_item in item_mapping:
media_id = item_mapping[selected_item]
# Split keywords into a list
keyword_list = [kw.strip() for kw in keywords.split(",") if kw.strip()]
# Update content, prompt, summary, and keywords in the database
status = update_media_content(media_id, content, prompt, summary)
keyword_status = update_keywords_for_media(media_id, keyword_list)
return f"{status}\nKeywords: {keyword_status}"
return "No item selected or invalid selection"
# Update button click event
update_button.click(
fn=update_media_with_keywords,
inputs=[items_output, item_mapping, content_input, prompt_input, summary_input, keywords_input],
outputs=status_message
)
def create_media_edit_and_clone_tab():
with gr.TabItem("Clone and Edit Existing Items"):
gr.Markdown("# Search, Edit, and Clone Existing Items")
with gr.Row():
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")
with gr.Column():
search_button = gr.Button("Search")
clone_button = gr.Button("Clone Item")
save_clone_button = gr.Button("Save Cloned Item", visible=False)
with gr.Row():
items_output = gr.Dropdown(label="Select Item", choices=[], interactive=True)
item_mapping = gr.State({})
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)
new_title_input = gr.Textbox(label="New Title (for cloning)", visible=False)
status_message = gr.Textbox(label="Status", interactive=False)
search_button.click(
fn=update_dropdown,
inputs=[search_query_input, search_type_input],
outputs=[items_output, item_mapping]
)
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)
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]
)
def prepare_for_cloning(selected_item):
return gr.update(value=f"Copy of {selected_item}", visible=True), gr.update(visible=True)
clone_button.click(
fn=prepare_for_cloning,
inputs=[items_output],
outputs=[new_title_input, save_clone_button]
)
def save_cloned_item(selected_item, item_mapping, content, prompt, summary, new_title):
if selected_item and item_mapping and selected_item in item_mapping:
original_media_id = item_mapping[selected_item]
try:
with db.get_connection() as conn:
cursor = conn.cursor()
# Fetch the original item's details
cursor.execute("SELECT type, url FROM Media WHERE id = ?", (original_media_id,))
original_type, original_url = cursor.fetchone()
# Generate a new unique URL
new_url = f"{original_url}_clone_{uuid.uuid4().hex[:8]}"
# Insert new item into Media table
cursor.execute("""
INSERT INTO Media (title, content, url, type)
VALUES (?, ?, ?, ?)
""", (new_title, content, new_url, original_type))
new_media_id = cursor.lastrowid
# Insert new item into MediaModifications table
cursor.execute("""
INSERT INTO MediaModifications (media_id, prompt, summary, modification_date)
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))
# Update full-text search index
cursor.execute("""
INSERT INTO media_fts (rowid, title, content)
VALUES (?, ?, ?)
""", (new_media_id, new_title, content))
conn.commit()
return f"Cloned item saved successfully with ID: {new_media_id}", gr.update(
visible=False), gr.update(visible=False)
except Exception as e:
logging.error(f"Error saving cloned item: {e}")
return f"Error saving cloned item: {str(e)}", gr.update(visible=True), gr.update(visible=True)
else:
return "No item selected or invalid selection", gr.update(visible=True), gr.update(visible=True)
save_clone_button.click(
fn=save_cloned_item,
inputs=[items_output, item_mapping, content_input, prompt_input, summary_input, new_title_input],
outputs=[status_message, new_title_input, save_clone_button]
)
def create_prompt_edit_tab():
with gr.TabItem("Add & Edit Prompts"):
with gr.Row():
with gr.Column():
prompt_dropdown = gr.Dropdown(
label="Select Prompt",
choices=[],
interactive=True
)
prompt_list_button = gr.Button("List Prompts")
with gr.Column():
title_input = gr.Textbox(label="Title", placeholder="Enter the prompt title")
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)
add_prompt_button = gr.Button("Add/Update Prompt")
add_prompt_output = gr.HTML()
# Event handlers
prompt_list_button.click(
fn=update_prompt_dropdown,
outputs=prompt_dropdown
)
add_prompt_button.click(
fn=add_or_update_prompt,
inputs=[title_input, description_input, system_prompt_input, user_prompt_input],
outputs=add_prompt_output
)
# Load prompt details when selected
prompt_dropdown.change(
fn=load_prompt_details,
inputs=[prompt_dropdown],
outputs=[title_input, description_input, system_prompt_input, user_prompt_input]
)
def create_prompt_clone_tab():
with gr.TabItem("Clone and Edit Prompts"):
with gr.Row():
with gr.Column():
gr.Markdown("# Clone and Edit Prompts")
prompt_dropdown = gr.Dropdown(
label="Select Prompt",
choices=[],
interactive=True
)
prompt_list_button = gr.Button("List Prompts")
with gr.Column():
title_input = gr.Textbox(label="Title", placeholder="Enter the prompt title")
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)
clone_prompt_button = gr.Button("Clone Selected Prompt")
save_cloned_prompt_button = gr.Button("Save Cloned Prompt", visible=False)
add_prompt_output = gr.HTML()
# Event handlers
prompt_list_button.click(
fn=update_prompt_dropdown,
outputs=prompt_dropdown
)
# Load prompt details when selected
prompt_dropdown.change(
fn=load_prompt_details,
inputs=[prompt_dropdown],
outputs=[title_input, description_input, system_prompt_input, user_prompt_input]
)
def prepare_for_cloning(selected_prompt):
if selected_prompt:
return gr.update(value=f"Copy of {selected_prompt}"), gr.update(visible=True)
return gr.update(), gr.update(visible=False)
clone_prompt_button.click(
fn=prepare_for_cloning,
inputs=[prompt_dropdown],
outputs=[title_input, save_cloned_prompt_button]
)
def save_cloned_prompt(title, description, system_prompt, user_prompt):
try:
result = add_prompt(title, description, system_prompt, user_prompt)
if result == "Prompt added successfully.":
return result, gr.update(choices=update_prompt_dropdown())
else:
return result, gr.update()
except Exception as e:
return f"Error saving cloned prompt: {str(e)}", gr.update()
save_cloned_prompt_button.click(
fn=save_cloned_prompt,
inputs=[title_input, description_input, system_prompt_input, user_prompt_input],
outputs=[add_prompt_output, prompt_dropdown]
) |