Spaces:
Sleeping
Sleeping
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 |
-
|
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 |
-
def
|
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 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
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 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|