oceansweep commited on
Commit
6a13b25
1 Parent(s): fde148e

Update App_Function_Libraries/Gradio_UI/Search_Tab.py

Browse files
App_Function_Libraries/Gradio_UI/Search_Tab.py CHANGED
@@ -1,487 +1,400 @@
1
- # Search_Tab.py
2
- # Description: This file contains the code for the search tab in the Gradio UI
3
- #
4
- # Imports
5
- import html
6
- import logging
7
- import sqlite3
8
-
9
- #
10
- # External Imports
11
- import gradio as gr
12
-
13
- from App_Function_Libraries.DB.DB_Manager import view_database, search_and_display_items
14
- from App_Function_Libraries.Gradio_UI.Gradio_Shared import update_dropdown, update_detailed_view
15
- from App_Function_Libraries.RAG.RAG_Libary_2 import rag_search
16
-
17
- #
18
- # Local Imports
19
- #
20
- #
21
- ###################################################################################################
22
- #
23
- # Functions:
24
-
25
- logger = logging.getLogger()
26
-
27
-
28
-
29
-
30
- # FIXME - SQL functions to be moved to DB_Manager
31
- def search_prompts(query):
32
- try:
33
- conn = sqlite3.connect('prompts.db')
34
- cursor = conn.cursor()
35
- cursor.execute("SELECT name, details, system, user FROM Prompts WHERE name LIKE ? OR details LIKE ?",
36
- (f"%{query}%", f"%{query}%"))
37
- results = cursor.fetchall()
38
- conn.close()
39
- return results
40
- except sqlite3.Error as e:
41
- print(f"Error searching prompts: {e}")
42
- return []
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
- def create_rag_tab():
56
- with gr.TabItem("RAG Search"):
57
- gr.Markdown("# Retrieval-Augmented Generation (RAG) Search")
58
-
59
- with gr.Row():
60
- with gr.Column():
61
- search_query = gr.Textbox(label="Enter your question", placeholder="What would you like to know?")
62
- api_choice = gr.Dropdown(
63
- choices=["Local-LLM", "OpenAI", "Anthropic", "Cohere", "Groq", "DeepSeek", "Mistral", "OpenRouter", "Llama.cpp", "Kobold", "Ooba", "Tabbyapi", "VLLM", "ollama", "HuggingFace"],
64
- label="Select API for RAG",
65
- value="OpenAI"
66
- )
67
- search_button = gr.Button("Search")
68
-
69
- with gr.Column():
70
- result_output = gr.Textbox(label="Answer", lines=10)
71
- context_output = gr.Textbox(label="Context", lines=10, visible=False)
72
-
73
- def perform_rag_search(query, api_choice):
74
- result = rag_search(query, api_choice)
75
- return result['answer'], result['context']
76
-
77
- search_button.click(perform_rag_search, inputs=[search_query, api_choice], outputs=[result_output, context_output])
78
-
79
- # FIXME - under construction
80
- def create_embeddings_tab():
81
- with gr.TabItem("Create Embeddings"):
82
- gr.Markdown("# Create Embeddings for All Content")
83
-
84
- with gr.Row():
85
- with gr.Column():
86
- embedding_api_choice = gr.Dropdown(
87
- choices=["OpenAI", "Local", "HuggingFace"],
88
- label="Select API for Embeddings",
89
- value="OpenAI"
90
- )
91
- create_button = gr.Button("Create Embeddings")
92
-
93
- with gr.Column():
94
- status_output = gr.Textbox(label="Status", lines=10)
95
-
96
- def create_embeddings(api_choice):
97
- try:
98
- # Assuming you have a function that handles the creation of embeddings
99
- from App_Function_Libraries.RAG.ChromaDB_Library import create_all_embeddings
100
- status = create_all_embeddings(api_choice)
101
- return status
102
- except Exception as e:
103
- return f"Error: {str(e)}"
104
-
105
- create_button.click(create_embeddings, inputs=[embedding_api_choice], outputs=status_output)
106
-
107
-
108
-
109
-
110
- def create_search_tab():
111
- with gr.TabItem("Search / Detailed View"):
112
- with gr.Row():
113
- with gr.Column():
114
- gr.Markdown("# Search across all ingested items in the Database")
115
- gr.Markdown(" by Title / URL / Keyword / or Content via SQLite Full-Text-Search")
116
- search_query_input = gr.Textbox(label="Search Query", placeholder="Enter your search query here...")
117
- search_type_input = gr.Radio(choices=["Title", "URL", "Keyword", "Content"], value="Title", label="Search By")
118
- search_button = gr.Button("Search")
119
- items_output = gr.Dropdown(label="Select Item", choices=[])
120
- item_mapping = gr.State({})
121
- prompt_summary_output = gr.HTML(label="Prompt & Summary", visible=True)
122
-
123
- search_button.click(
124
- fn=update_dropdown,
125
- inputs=[search_query_input, search_type_input],
126
- outputs=[items_output, item_mapping]
127
- )
128
- with gr.Column():
129
- content_output = gr.Markdown(label="Content", visible=True)
130
- items_output.change(
131
- fn=update_detailed_view,
132
- inputs=[items_output, item_mapping],
133
- outputs=[prompt_summary_output, content_output]
134
- )
135
-
136
-
137
- def display_search_results(query):
138
- if not query.strip():
139
- return "Please enter a search query."
140
-
141
- results = search_prompts(query)
142
-
143
- # Debugging: Print the results to the console to see what is being returned
144
- print(f"Processed search results for query '{query}': {results}")
145
-
146
- if results:
147
- result_md = "## Search Results:\n"
148
- for result in results:
149
- # Debugging: Print each result to see its format
150
- print(f"Result item: {result}")
151
-
152
- if len(result) == 2:
153
- name, details = result
154
- result_md += f"**Title:** {name}\n\n**Description:** {details}\n\n---\n"
155
-
156
- elif len(result) == 4:
157
- name, details, system, user = result
158
- result_md += f"**Title:** {name}\n\n"
159
- result_md += f"**Description:** {details}\n\n"
160
- result_md += f"**System Prompt:** {system}\n\n"
161
- result_md += f"**User Prompt:** {user}\n\n"
162
- result_md += "---\n"
163
- else:
164
- result_md += "Error: Unexpected result format.\n\n---\n"
165
- return result_md
166
- return "No results found."
167
-
168
-
169
- def create_viewing_tab():
170
- with gr.TabItem("View Database"):
171
- gr.Markdown("# View Database Entries")
172
- with gr.Row():
173
- with gr.Column():
174
- entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
175
- page_number = gr.Number(value=1, label="Page Number", precision=0)
176
- view_button = gr.Button("View Page")
177
- next_page_button = gr.Button("Next Page")
178
- previous_page_button = gr.Button("Previous Page")
179
- with gr.Column():
180
- results_display = gr.HTML()
181
- pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
182
-
183
- def update_page(page, entries_per_page):
184
- results, pagination, total_pages = view_database(page, entries_per_page)
185
- next_disabled = page >= total_pages
186
- prev_disabled = page <= 1
187
- return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(interactive=not prev_disabled)
188
-
189
- def go_to_next_page(current_page, entries_per_page):
190
- next_page = current_page + 1
191
- return update_page(next_page, entries_per_page)
192
-
193
- def go_to_previous_page(current_page, entries_per_page):
194
- previous_page = max(1, current_page - 1)
195
- return update_page(previous_page, entries_per_page)
196
-
197
- view_button.click(
198
- fn=update_page,
199
- inputs=[page_number, entries_per_page],
200
- outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
201
- )
202
-
203
- next_page_button.click(
204
- fn=go_to_next_page,
205
- inputs=[page_number, entries_per_page],
206
- outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
207
- )
208
-
209
- previous_page_button.click(
210
- fn=go_to_previous_page,
211
- inputs=[page_number, entries_per_page],
212
- outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
213
- )
214
-
215
-
216
- def create_search_summaries_tab():
217
- with gr.TabItem("Search/View Title+Summary "):
218
- gr.Markdown("# Search across all ingested items in the Database and review their summaries")
219
- gr.Markdown("Search by Title / URL / Keyword / or Content via SQLite Full-Text-Search")
220
- with gr.Row():
221
- with gr.Column():
222
- search_query_input = gr.Textbox(label="Search Query", placeholder="Enter your search query here...")
223
- search_type_input = gr.Radio(choices=["Title", "URL", "Keyword", "Content"], value="Title",
224
- label="Search By")
225
- entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
226
- page_number = gr.Number(value=1, label="Page Number", precision=0)
227
- char_count_input = gr.Number(value=5000, label="Amount of characters to display from the main content",
228
- precision=0)
229
- with gr.Column():
230
- search_button = gr.Button("Search")
231
- next_page_button = gr.Button("Next Page")
232
- previous_page_button = gr.Button("Previous Page")
233
- pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
234
- search_results_output = gr.HTML()
235
-
236
-
237
- def update_search_page(query, search_type, page, entries_per_page, char_count):
238
- # Ensure char_count is a positive integer
239
- char_count = max(1, int(char_count)) if char_count else 5000
240
- results, pagination, total_pages = search_and_display_items(query, search_type, page, entries_per_page, char_count)
241
- next_disabled = page >= total_pages
242
- prev_disabled = page <= 1
243
- return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(
244
- interactive=not prev_disabled)
245
-
246
- def go_to_next_search_page(query, search_type, current_page, entries_per_page, char_count):
247
- next_page = current_page + 1
248
- return update_search_page(query, search_type, next_page, entries_per_page, char_count)
249
-
250
- def go_to_previous_search_page(query, search_type, current_page, entries_per_page, char_count):
251
- previous_page = max(1, current_page - 1)
252
- return update_search_page(query, search_type, previous_page, entries_per_page, char_count)
253
-
254
- search_button.click(
255
- fn=update_search_page,
256
- inputs=[search_query_input, search_type_input, page_number, entries_per_page, char_count_input],
257
- outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
258
- )
259
-
260
- next_page_button.click(
261
- fn=go_to_next_search_page,
262
- inputs=[search_query_input, search_type_input, page_number, entries_per_page, char_count_input],
263
- outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
264
- )
265
-
266
- previous_page_button.click(
267
- fn=go_to_previous_search_page,
268
- inputs=[search_query_input, search_type_input, page_number, entries_per_page, char_count_input],
269
- outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
270
- )
271
-
272
-
273
-
274
- def create_prompt_view_tab():
275
- with gr.TabItem("View Prompt Database"):
276
- gr.Markdown("# View Prompt Database Entries")
277
- with gr.Row():
278
- with gr.Column():
279
- entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
280
- page_number = gr.Number(value=1, label="Page Number", precision=0)
281
- view_button = gr.Button("View Page")
282
- next_page_button = gr.Button("Next Page")
283
- previous_page_button = gr.Button("Previous Page")
284
- with gr.Column():
285
- pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
286
- results_display = gr.HTML()
287
-
288
- # FIXME - SQL functions to be moved to DB_Manager
289
- def view_database(page, entries_per_page):
290
- offset = (page - 1) * entries_per_page
291
- try:
292
- with sqlite3.connect('prompts.db') as conn:
293
- cursor = conn.cursor()
294
- cursor.execute('''
295
- SELECT p.name, p.details, p.system, p.user, GROUP_CONCAT(k.keyword, ', ') as keywords
296
- FROM Prompts p
297
- LEFT JOIN PromptKeywords pk ON p.id = pk.prompt_id
298
- LEFT JOIN Keywords k ON pk.keyword_id = k.id
299
- GROUP BY p.id
300
- ORDER BY p.name
301
- LIMIT ? OFFSET ?
302
- ''', (entries_per_page, offset))
303
- prompts = cursor.fetchall()
304
-
305
- cursor.execute('SELECT COUNT(*) FROM Prompts')
306
- total_prompts = cursor.fetchone()[0]
307
-
308
- results = ""
309
- for prompt in prompts:
310
- # Escape HTML special characters and replace newlines with <br> tags
311
- title = html.escape(prompt[0]).replace('\n', '<br>')
312
- details = html.escape(prompt[1] or '').replace('\n', '<br>')
313
- system_prompt = html.escape(prompt[2] or '')
314
- user_prompt = html.escape(prompt[3] or '')
315
- keywords = html.escape(prompt[4] or '').replace('\n', '<br>')
316
-
317
- results += f"""
318
- <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 20px;">
319
- <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
320
- <div><strong>Title:</strong> {title}</div>
321
- <div><strong>Details:</strong> {details}</div>
322
- </div>
323
- <div style="margin-top: 10px;">
324
- <strong>User Prompt:</strong>
325
- <pre style="white-space: pre-wrap; word-wrap: break-word;">{user_prompt}</pre>
326
- </div>
327
- <div style="margin-top: 10px;">
328
- <strong>System Prompt:</strong>
329
- <pre style="white-space: pre-wrap; word-wrap: break-word;">{system_prompt}</pre>
330
- </div>
331
- <div style="margin-top: 10px;">
332
- <strong>Keywords:</strong> {keywords}
333
- </div>
334
- </div>
335
- """
336
-
337
- total_pages = (total_prompts + entries_per_page - 1) // entries_per_page
338
- pagination = f"Page {page} of {total_pages} (Total prompts: {total_prompts})"
339
-
340
- return results, pagination, total_pages
341
- except sqlite3.Error as e:
342
- return f"<p>Error fetching prompts: {e}</p>", "Error", 0
343
-
344
- def update_page(page, entries_per_page):
345
- results, pagination, total_pages = view_database(page, entries_per_page)
346
- next_disabled = page >= total_pages
347
- prev_disabled = page <= 1
348
- return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(
349
- interactive=not prev_disabled)
350
-
351
- def go_to_next_page(current_page, entries_per_page):
352
- next_page = current_page + 1
353
- return update_page(next_page, entries_per_page)
354
-
355
- def go_to_previous_page(current_page, entries_per_page):
356
- previous_page = max(1, current_page - 1)
357
- return update_page(previous_page, entries_per_page)
358
-
359
- view_button.click(
360
- fn=update_page,
361
- inputs=[page_number, entries_per_page],
362
- outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
363
- )
364
-
365
- next_page_button.click(
366
- fn=go_to_next_page,
367
- inputs=[page_number, entries_per_page],
368
- outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
369
- )
370
-
371
- previous_page_button.click(
372
- fn=go_to_previous_page,
373
- inputs=[page_number, entries_per_page],
374
- outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
375
- )
376
-
377
-
378
-
379
- def create_prompt_search_tab():
380
- with gr.TabItem("Search Prompts"):
381
- gr.Markdown("# Search and View Prompt Details")
382
- gr.Markdown("Currently has all of the https://github.com/danielmiessler/fabric prompts already available")
383
- with gr.Row():
384
- with gr.Column():
385
- search_query_input = gr.Textbox(label="Search Prompts", placeholder="Enter your search query...")
386
- entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
387
- page_number = gr.Number(value=1, label="Page Number", precision=0)
388
- with gr.Column():
389
- search_button = gr.Button("Search Prompts")
390
- next_page_button = gr.Button("Next Page")
391
- previous_page_button = gr.Button("Previous Page")
392
- pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
393
- search_results_output = gr.HTML()
394
-
395
- def search_and_display_prompts(query, page, entries_per_page):
396
- offset = (page - 1) * entries_per_page
397
- try:
398
- # FIXME - SQL functions to be moved to DB_Manager
399
- with sqlite3.connect('prompts.db') as conn:
400
- cursor = conn.cursor()
401
- cursor.execute('''
402
- SELECT p.name, p.details, p.system, p.user, GROUP_CONCAT(k.keyword, ', ') as keywords
403
- FROM Prompts p
404
- LEFT JOIN PromptKeywords pk ON p.id = pk.prompt_id
405
- LEFT JOIN Keywords k ON pk.keyword_id = k.id
406
- WHERE p.name LIKE ? OR p.details LIKE ? OR p.system LIKE ? OR p.user LIKE ? OR k.keyword LIKE ?
407
- GROUP BY p.id
408
- ORDER BY p.name
409
- LIMIT ? OFFSET ?
410
- ''', (f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%', entries_per_page, offset))
411
- prompts = cursor.fetchall()
412
-
413
- cursor.execute('''
414
- SELECT COUNT(DISTINCT p.id)
415
- FROM Prompts p
416
- LEFT JOIN PromptKeywords pk ON p.id = pk.prompt_id
417
- LEFT JOIN Keywords k ON pk.keyword_id = k.id
418
- WHERE p.name LIKE ? OR p.details LIKE ? OR p.system LIKE ? OR p.user LIKE ? OR k.keyword LIKE ?
419
- ''', (f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%'))
420
- total_prompts = cursor.fetchone()[0]
421
-
422
- results = ""
423
- for prompt in prompts:
424
- title = html.escape(prompt[0]).replace('\n', '<br>')
425
- details = html.escape(prompt[1] or '').replace('\n', '<br>')
426
- system_prompt = html.escape(prompt[2] or '')
427
- user_prompt = html.escape(prompt[3] or '')
428
- keywords = html.escape(prompt[4] or '').replace('\n', '<br>')
429
-
430
- results += f"""
431
- <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 20px;">
432
- <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
433
- <div><strong>Title:</strong> {title}</div>
434
- <div><strong>Details:</strong> {details}</div>
435
- </div>
436
- <div style="margin-top: 10px;">
437
- <strong>User Prompt:</strong>
438
- <pre style="white-space: pre-wrap; word-wrap: break-word;">{user_prompt}</pre>
439
- </div>
440
- <div style="margin-top: 10px;">
441
- <strong>System Prompt:</strong>
442
- <pre style="white-space: pre-wrap; word-wrap: break-word;">{system_prompt}</pre>
443
- </div>
444
- <div style="margin-top: 10px;">
445
- <strong>Keywords:</strong> {keywords}
446
- </div>
447
- </div>
448
- """
449
-
450
- total_pages = (total_prompts + entries_per_page - 1) // entries_per_page
451
- pagination = f"Page {page} of {total_pages} (Total prompts: {total_prompts})"
452
-
453
- return results, pagination, total_pages
454
- except sqlite3.Error as e:
455
- return f"<p>Error searching prompts: {e}</p>", "Error", 0
456
-
457
- def update_search_page(query, page, entries_per_page):
458
- results, pagination, total_pages = search_and_display_prompts(query, page, entries_per_page)
459
- next_disabled = page >= total_pages
460
- prev_disabled = page <= 1
461
- return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(interactive=not prev_disabled)
462
-
463
- def go_to_next_search_page(query, current_page, entries_per_page):
464
- next_page = current_page + 1
465
- return update_search_page(query, next_page, entries_per_page)
466
-
467
- def go_to_previous_search_page(query, current_page, entries_per_page):
468
- previous_page = max(1, current_page - 1)
469
- return update_search_page(query, previous_page, entries_per_page)
470
-
471
- search_button.click(
472
- fn=update_search_page,
473
- inputs=[search_query_input, page_number, entries_per_page],
474
- outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
475
- )
476
-
477
- next_page_button.click(
478
- fn=go_to_next_search_page,
479
- inputs=[search_query_input, page_number, entries_per_page],
480
- outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
481
- )
482
-
483
- previous_page_button.click(
484
- fn=go_to_previous_search_page,
485
- inputs=[search_query_input, page_number, entries_per_page],
486
- outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
487
- )
 
1
+ # Search_Tab.py
2
+ # Description: This file contains the code for the search tab in the Gradio UI
3
+ #
4
+ # Imports
5
+ import html
6
+ import logging
7
+ import sqlite3
8
+
9
+ #
10
+ # External Imports
11
+ import gradio as gr
12
+
13
+ from App_Function_Libraries.DB.DB_Manager import view_database, search_and_display_items
14
+ from App_Function_Libraries.Gradio_UI.Gradio_Shared import update_dropdown, update_detailed_view
15
+ from App_Function_Libraries.RAG.RAG_Libary_2 import rag_search
16
+
17
+ #
18
+ # Local Imports
19
+ #
20
+ #
21
+ ###################################################################################################
22
+ #
23
+ # Functions:
24
+
25
+ logger = logging.getLogger()
26
+
27
+
28
+
29
+
30
+ # FIXME - SQL functions to be moved to DB_Manager
31
+ def search_prompts(query):
32
+ try:
33
+ conn = sqlite3.connect('prompts.db')
34
+ cursor = conn.cursor()
35
+ cursor.execute("SELECT name, details, system, user FROM Prompts WHERE name LIKE ? OR details LIKE ?",
36
+ (f"%{query}%", f"%{query}%"))
37
+ results = cursor.fetchall()
38
+ conn.close()
39
+ return results
40
+ except sqlite3.Error as e:
41
+ print(f"Error searching prompts: {e}")
42
+ return []
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+ def display_search_results(query):
51
+ if not query.strip():
52
+ return "Please enter a search query."
53
+
54
+ results = search_prompts(query)
55
+
56
+ # Debugging: Print the results to the console to see what is being returned
57
+ print(f"Processed search results for query '{query}': {results}")
58
+
59
+ if results:
60
+ result_md = "## Search Results:\n"
61
+ for result in results:
62
+ # Debugging: Print each result to see its format
63
+ print(f"Result item: {result}")
64
+
65
+ if len(result) == 2:
66
+ name, details = result
67
+ result_md += f"**Title:** {name}\n\n**Description:** {details}\n\n---\n"
68
+
69
+ elif len(result) == 4:
70
+ name, details, system, user = result
71
+ result_md += f"**Title:** {name}\n\n"
72
+ result_md += f"**Description:** {details}\n\n"
73
+ result_md += f"**System Prompt:** {system}\n\n"
74
+ result_md += f"**User Prompt:** {user}\n\n"
75
+ result_md += "---\n"
76
+ else:
77
+ result_md += "Error: Unexpected result format.\n\n---\n"
78
+ return result_md
79
+ return "No results found."
80
+
81
+
82
+ def create_viewing_tab():
83
+ with gr.TabItem("View Database"):
84
+ gr.Markdown("# View Database Entries")
85
+ with gr.Row():
86
+ with gr.Column():
87
+ entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
88
+ page_number = gr.Number(value=1, label="Page Number", precision=0)
89
+ view_button = gr.Button("View Page")
90
+ next_page_button = gr.Button("Next Page")
91
+ previous_page_button = gr.Button("Previous Page")
92
+ with gr.Column():
93
+ results_display = gr.HTML()
94
+ pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
95
+
96
+ def update_page(page, entries_per_page):
97
+ results, pagination, total_pages = view_database(page, entries_per_page)
98
+ next_disabled = page >= total_pages
99
+ prev_disabled = page <= 1
100
+ return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(interactive=not prev_disabled)
101
+
102
+ def go_to_next_page(current_page, entries_per_page):
103
+ next_page = current_page + 1
104
+ return update_page(next_page, entries_per_page)
105
+
106
+ def go_to_previous_page(current_page, entries_per_page):
107
+ previous_page = max(1, current_page - 1)
108
+ return update_page(previous_page, entries_per_page)
109
+
110
+ view_button.click(
111
+ fn=update_page,
112
+ inputs=[page_number, entries_per_page],
113
+ outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
114
+ )
115
+
116
+ next_page_button.click(
117
+ fn=go_to_next_page,
118
+ inputs=[page_number, entries_per_page],
119
+ outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
120
+ )
121
+
122
+ previous_page_button.click(
123
+ fn=go_to_previous_page,
124
+ inputs=[page_number, entries_per_page],
125
+ outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
126
+ )
127
+
128
+
129
+ def create_search_summaries_tab():
130
+ with gr.TabItem("Search/View Title+Summary "):
131
+ gr.Markdown("# Search across all ingested items in the Database and review their summaries")
132
+ gr.Markdown("Search by Title / URL / Keyword / or Content via SQLite Full-Text-Search")
133
+ with gr.Row():
134
+ with gr.Column():
135
+ search_query_input = gr.Textbox(label="Search Query", placeholder="Enter your search query here...")
136
+ search_type_input = gr.Radio(choices=["Title", "URL", "Keyword", "Content"], value="Title",
137
+ label="Search By")
138
+ entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
139
+ page_number = gr.Number(value=1, label="Page Number", precision=0)
140
+ char_count_input = gr.Number(value=5000, label="Amount of characters to display from the main content",
141
+ precision=0)
142
+ with gr.Column():
143
+ search_button = gr.Button("Search")
144
+ next_page_button = gr.Button("Next Page")
145
+ previous_page_button = gr.Button("Previous Page")
146
+ pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
147
+ search_results_output = gr.HTML()
148
+
149
+
150
+ def update_search_page(query, search_type, page, entries_per_page, char_count):
151
+ # Ensure char_count is a positive integer
152
+ char_count = max(1, int(char_count)) if char_count else 5000
153
+ results, pagination, total_pages = search_and_display_items(query, search_type, page, entries_per_page, char_count)
154
+ next_disabled = page >= total_pages
155
+ prev_disabled = page <= 1
156
+ return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(
157
+ interactive=not prev_disabled)
158
+
159
+ def go_to_next_search_page(query, search_type, current_page, entries_per_page, char_count):
160
+ next_page = current_page + 1
161
+ return update_search_page(query, search_type, next_page, entries_per_page, char_count)
162
+
163
+ def go_to_previous_search_page(query, search_type, current_page, entries_per_page, char_count):
164
+ previous_page = max(1, current_page - 1)
165
+ return update_search_page(query, search_type, previous_page, entries_per_page, char_count)
166
+
167
+ search_button.click(
168
+ fn=update_search_page,
169
+ inputs=[search_query_input, search_type_input, page_number, entries_per_page, char_count_input],
170
+ outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
171
+ )
172
+
173
+ next_page_button.click(
174
+ fn=go_to_next_search_page,
175
+ inputs=[search_query_input, search_type_input, page_number, entries_per_page, char_count_input],
176
+ outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
177
+ )
178
+
179
+ previous_page_button.click(
180
+ fn=go_to_previous_search_page,
181
+ inputs=[search_query_input, search_type_input, page_number, entries_per_page, char_count_input],
182
+ outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
183
+ )
184
+
185
+
186
+
187
+ def create_prompt_view_tab():
188
+ with gr.TabItem("View Prompt Database"):
189
+ gr.Markdown("# View Prompt Database Entries")
190
+ with gr.Row():
191
+ with gr.Column():
192
+ entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
193
+ page_number = gr.Number(value=1, label="Page Number", precision=0)
194
+ view_button = gr.Button("View Page")
195
+ next_page_button = gr.Button("Next Page")
196
+ previous_page_button = gr.Button("Previous Page")
197
+ with gr.Column():
198
+ pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
199
+ results_display = gr.HTML()
200
+
201
+ # FIXME - SQL functions to be moved to DB_Manager
202
+ def view_database(page, entries_per_page):
203
+ offset = (page - 1) * entries_per_page
204
+ try:
205
+ with sqlite3.connect('prompts.db') as conn:
206
+ cursor = conn.cursor()
207
+ cursor.execute('''
208
+ SELECT p.name, p.details, p.system, p.user, GROUP_CONCAT(k.keyword, ', ') as keywords
209
+ FROM Prompts p
210
+ LEFT JOIN PromptKeywords pk ON p.id = pk.prompt_id
211
+ LEFT JOIN Keywords k ON pk.keyword_id = k.id
212
+ GROUP BY p.id
213
+ ORDER BY p.name
214
+ LIMIT ? OFFSET ?
215
+ ''', (entries_per_page, offset))
216
+ prompts = cursor.fetchall()
217
+
218
+ cursor.execute('SELECT COUNT(*) FROM Prompts')
219
+ total_prompts = cursor.fetchone()[0]
220
+
221
+ results = ""
222
+ for prompt in prompts:
223
+ # Escape HTML special characters and replace newlines with <br> tags
224
+ title = html.escape(prompt[0]).replace('\n', '<br>')
225
+ details = html.escape(prompt[1] or '').replace('\n', '<br>')
226
+ system_prompt = html.escape(prompt[2] or '')
227
+ user_prompt = html.escape(prompt[3] or '')
228
+ keywords = html.escape(prompt[4] or '').replace('\n', '<br>')
229
+
230
+ results += f"""
231
+ <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 20px;">
232
+ <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
233
+ <div><strong>Title:</strong> {title}</div>
234
+ <div><strong>Details:</strong> {details}</div>
235
+ </div>
236
+ <div style="margin-top: 10px;">
237
+ <strong>User Prompt:</strong>
238
+ <pre style="white-space: pre-wrap; word-wrap: break-word;">{user_prompt}</pre>
239
+ </div>
240
+ <div style="margin-top: 10px;">
241
+ <strong>System Prompt:</strong>
242
+ <pre style="white-space: pre-wrap; word-wrap: break-word;">{system_prompt}</pre>
243
+ </div>
244
+ <div style="margin-top: 10px;">
245
+ <strong>Keywords:</strong> {keywords}
246
+ </div>
247
+ </div>
248
+ """
249
+
250
+ total_pages = (total_prompts + entries_per_page - 1) // entries_per_page
251
+ pagination = f"Page {page} of {total_pages} (Total prompts: {total_prompts})"
252
+
253
+ return results, pagination, total_pages
254
+ except sqlite3.Error as e:
255
+ return f"<p>Error fetching prompts: {e}</p>", "Error", 0
256
+
257
+ def update_page(page, entries_per_page):
258
+ results, pagination, total_pages = view_database(page, entries_per_page)
259
+ next_disabled = page >= total_pages
260
+ prev_disabled = page <= 1
261
+ return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(
262
+ interactive=not prev_disabled)
263
+
264
+ def go_to_next_page(current_page, entries_per_page):
265
+ next_page = current_page + 1
266
+ return update_page(next_page, entries_per_page)
267
+
268
+ def go_to_previous_page(current_page, entries_per_page):
269
+ previous_page = max(1, current_page - 1)
270
+ return update_page(previous_page, entries_per_page)
271
+
272
+ view_button.click(
273
+ fn=update_page,
274
+ inputs=[page_number, entries_per_page],
275
+ outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
276
+ )
277
+
278
+ next_page_button.click(
279
+ fn=go_to_next_page,
280
+ inputs=[page_number, entries_per_page],
281
+ outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
282
+ )
283
+
284
+ previous_page_button.click(
285
+ fn=go_to_previous_page,
286
+ inputs=[page_number, entries_per_page],
287
+ outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
288
+ )
289
+
290
+
291
+
292
+ def create_prompt_search_tab():
293
+ with gr.TabItem("Search Prompts"):
294
+ gr.Markdown("# Search and View Prompt Details")
295
+ gr.Markdown("Currently has all of the https://github.com/danielmiessler/fabric prompts already available")
296
+ with gr.Row():
297
+ with gr.Column():
298
+ search_query_input = gr.Textbox(label="Search Prompts", placeholder="Enter your search query...")
299
+ entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
300
+ page_number = gr.Number(value=1, label="Page Number", precision=0)
301
+ with gr.Column():
302
+ search_button = gr.Button("Search Prompts")
303
+ next_page_button = gr.Button("Next Page")
304
+ previous_page_button = gr.Button("Previous Page")
305
+ pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
306
+ search_results_output = gr.HTML()
307
+
308
+ def search_and_display_prompts(query, page, entries_per_page):
309
+ offset = (page - 1) * entries_per_page
310
+ try:
311
+ # FIXME - SQL functions to be moved to DB_Manager
312
+ with sqlite3.connect('prompts.db') as conn:
313
+ cursor = conn.cursor()
314
+ cursor.execute('''
315
+ SELECT p.name, p.details, p.system, p.user, GROUP_CONCAT(k.keyword, ', ') as keywords
316
+ FROM Prompts p
317
+ LEFT JOIN PromptKeywords pk ON p.id = pk.prompt_id
318
+ LEFT JOIN Keywords k ON pk.keyword_id = k.id
319
+ WHERE p.name LIKE ? OR p.details LIKE ? OR p.system LIKE ? OR p.user LIKE ? OR k.keyword LIKE ?
320
+ GROUP BY p.id
321
+ ORDER BY p.name
322
+ LIMIT ? OFFSET ?
323
+ ''', (f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%', entries_per_page, offset))
324
+ prompts = cursor.fetchall()
325
+
326
+ cursor.execute('''
327
+ SELECT COUNT(DISTINCT p.id)
328
+ FROM Prompts p
329
+ LEFT JOIN PromptKeywords pk ON p.id = pk.prompt_id
330
+ LEFT JOIN Keywords k ON pk.keyword_id = k.id
331
+ WHERE p.name LIKE ? OR p.details LIKE ? OR p.system LIKE ? OR p.user LIKE ? OR k.keyword LIKE ?
332
+ ''', (f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%', f'%{query}%'))
333
+ total_prompts = cursor.fetchone()[0]
334
+
335
+ results = ""
336
+ for prompt in prompts:
337
+ title = html.escape(prompt[0]).replace('\n', '<br>')
338
+ details = html.escape(prompt[1] or '').replace('\n', '<br>')
339
+ system_prompt = html.escape(prompt[2] or '')
340
+ user_prompt = html.escape(prompt[3] or '')
341
+ keywords = html.escape(prompt[4] or '').replace('\n', '<br>')
342
+
343
+ results += f"""
344
+ <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 20px;">
345
+ <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
346
+ <div><strong>Title:</strong> {title}</div>
347
+ <div><strong>Details:</strong> {details}</div>
348
+ </div>
349
+ <div style="margin-top: 10px;">
350
+ <strong>User Prompt:</strong>
351
+ <pre style="white-space: pre-wrap; word-wrap: break-word;">{user_prompt}</pre>
352
+ </div>
353
+ <div style="margin-top: 10px;">
354
+ <strong>System Prompt:</strong>
355
+ <pre style="white-space: pre-wrap; word-wrap: break-word;">{system_prompt}</pre>
356
+ </div>
357
+ <div style="margin-top: 10px;">
358
+ <strong>Keywords:</strong> {keywords}
359
+ </div>
360
+ </div>
361
+ """
362
+
363
+ total_pages = (total_prompts + entries_per_page - 1) // entries_per_page
364
+ pagination = f"Page {page} of {total_pages} (Total prompts: {total_prompts})"
365
+
366
+ return results, pagination, total_pages
367
+ except sqlite3.Error as e:
368
+ return f"<p>Error searching prompts: {e}</p>", "Error", 0
369
+
370
+ def update_search_page(query, page, entries_per_page):
371
+ results, pagination, total_pages = search_and_display_prompts(query, page, entries_per_page)
372
+ next_disabled = page >= total_pages
373
+ prev_disabled = page <= 1
374
+ return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(interactive=not prev_disabled)
375
+
376
+ def go_to_next_search_page(query, current_page, entries_per_page):
377
+ next_page = current_page + 1
378
+ return update_search_page(query, next_page, entries_per_page)
379
+
380
+ def go_to_previous_search_page(query, current_page, entries_per_page):
381
+ previous_page = max(1, current_page - 1)
382
+ return update_search_page(query, previous_page, entries_per_page)
383
+
384
+ search_button.click(
385
+ fn=update_search_page,
386
+ inputs=[search_query_input, page_number, entries_per_page],
387
+ outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
388
+ )
389
+
390
+ next_page_button.click(
391
+ fn=go_to_next_search_page,
392
+ inputs=[search_query_input, page_number, entries_per_page],
393
+ outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
394
+ )
395
+
396
+ previous_page_button.click(
397
+ fn=go_to_previous_search_page,
398
+ inputs=[search_query_input, page_number, entries_per_page],
399
+ outputs=[search_results_output, pagination_info, page_number, next_page_button, previous_page_button]
400
+ )