File size: 35,751 Bytes
43cd37c
 
 
 
 
c5b0bb7
 
43cd37c
 
 
 
 
 
c5b0bb7
 
 
 
 
 
 
43cd37c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5b0bb7
 
 
43cd37c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5b0bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43cd37c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5b0bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43cd37c
c5b0bb7
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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# View_DB_Items_tab.py
# Description: This file contains the code for the search tab in the Gradio UI
#
# Imports
import html
import logging

#
# External Imports
import gradio as gr
#
# Local Imports
from App_Function_Libraries.DB.DB_Manager import view_database, get_all_document_versions, \
    fetch_paginated_data, fetch_item_details, get_latest_transcription, list_prompts, fetch_prompt_details
from App_Function_Libraries.DB.RAG_QA_Chat_DB import get_keywords_for_note, search_conversations_by_keywords, \
    get_notes_by_keywords, get_keywords_for_conversation, get_db_connection, get_all_conversations, load_chat_history, \
    get_notes
from App_Function_Libraries.DB.SQLite_DB import get_document_version, fetch_items_by_keyword, fetch_all_keywords


#
####################################################################################################
#
# Functions

def format_as_html(content, title):
    escaped_content = html.escape(content)
    formatted_content = escaped_content.replace('\n', '<br>')
    return f"""

    <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">

        <h3>{title}</h3>

        <div style="max-height: 700px; overflow-y: auto;">

            {formatted_content}

        </div>

    </div>

    """

def extract_prompt_and_summary(content: str):
    # Implement this function based on how prompt and summary are stored in your DocumentVersions content
    # This is a placeholder implementation
    parts = content.split('\n\n', 2)
    prompt = parts[0] if len(parts) > 0 else "No prompt available."
    summary = parts[1] if len(parts) > 1 else "No summary available."
    return prompt, summary


def create_view_all_mediadb_with_versions_tab():
    with gr.TabItem("View All MediaDB Items", visible=True):
        gr.Markdown("# View All Media Database Entries with Version Selection")
        with gr.Row():
            with gr.Column(scale=1):
                entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
                page_number = gr.Number(value=1, label="Page Number", precision=0)
                view_button = gr.Button("View Page")
                next_page_button = gr.Button("Next Page")
                previous_page_button = gr.Button("Previous Page")
            with gr.Column(scale=2):
                items_output = gr.Dropdown(label="Select Item to View Details", choices=[])
                version_dropdown = gr.Dropdown(label="Select Version", choices=[], visible=False)
        with gr.Row():
            with gr.Column(scale=1):
                pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
            with gr.Column(scale=2):
                prompt_output = gr.Textbox(label="Prompt Used", visible=True)
                summary_output = gr.HTML(label="Summary", visible=True)
                transcription_output = gr.HTML(label="Transcription", visible=True)

        item_mapping = gr.State({})

        def update_page(page, entries_per_page):
            results, total_entries = fetch_paginated_data(page, entries_per_page)
            total_pages = (total_entries + entries_per_page - 1) // entries_per_page
            pagination = f"Page {page} of {total_pages} (Total items: {total_entries})"

            choices = [f"{item[1]} (ID: {item[0]})" for item in results]
            new_item_mapping = {f"{item[1]} (ID: {item[0]})": item[0] for item in results}

            next_disabled = page >= total_pages
            prev_disabled = page <= 1

            return (gr.update(choices=choices, value=None),
                    pagination,
                    page,
                    gr.update(interactive=not next_disabled),
                    gr.update(interactive=not prev_disabled),
                    gr.update(visible=False, choices=[]),
                    "", "", "",
                    new_item_mapping)

        def format_as_html(content, title):
            if content is None:
                content = "No content available."
            escaped_content = html.escape(str(content))
            formatted_content = escaped_content.replace('\n', '<br>')
            return f"""

            <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">

                <h3>{title}</h3>

                <div style="max-height: 700px; overflow-y: auto;">

                    {formatted_content}

                </div>

            </div>

            """

        def display_item_details(selected_item, item_mapping):
            if selected_item and item_mapping and selected_item in item_mapping:
                media_id = item_mapping[selected_item]
                prompt, summary, transcription = fetch_item_details(media_id)
                versions = get_all_document_versions(media_id)

                # Filter out duplicate versions and sort them
                unique_versions = list(set((v['version_number'], v['created_at']) for v in versions))
                unique_versions.sort(key=lambda x: x[0], reverse=True)
                version_choices = [f"Version {v[0]} ({v[1]})" for v in unique_versions]

                summary_html = format_as_html(summary, "Summary")
                transcription_html = format_as_html(transcription, "Transcription")

                return (
                    gr.update(visible=True, choices=version_choices,
                              value=version_choices[0] if version_choices else None),
                    prompt if prompt is not None else "",
                    summary_html,
                    transcription_html
                )
            return gr.update(visible=False, choices=[]), "", "", ""

        def update_version_content(selected_item, item_mapping, selected_version):
            if selected_item and item_mapping and selected_item in item_mapping and selected_version:
                media_id = item_mapping[selected_item]
                version_number = int(selected_version.split()[1].split('(')[0])
                version_data = get_document_version(media_id, version_number)

                if 'error' not in version_data:
                    content = version_data['content']
                    prompt, summary = extract_prompt_and_summary(content)
                    transcription = get_latest_transcription(media_id)

                    summary_html = format_as_html(summary, "Summary")
                    transcription_html = format_as_html(transcription, "Transcription")

                    return prompt if prompt is not None else "", summary_html, transcription_html
            return gr.update(value=selected_item), gr.update(), gr.update()

        view_button.click(
            fn=update_page,
            inputs=[page_number, entries_per_page],
            outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
                     version_dropdown, prompt_output, summary_output, transcription_output, item_mapping]
        )

        next_page_button.click(
            fn=lambda page, entries: update_page(page + 1, entries),
            inputs=[page_number, entries_per_page],
            outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
                     version_dropdown, prompt_output, summary_output, transcription_output, item_mapping]
        )

        previous_page_button.click(
            fn=lambda page, entries: update_page(max(1, page - 1), entries),
            inputs=[page_number, entries_per_page],
            outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
                     version_dropdown, prompt_output, summary_output, transcription_output, item_mapping]
        )

        items_output.change(
            fn=display_item_details,
            inputs=[items_output, item_mapping],
            outputs=[version_dropdown, prompt_output, summary_output, transcription_output]
        )

        version_dropdown.change(
            fn=update_version_content,
            inputs=[items_output, item_mapping, version_dropdown],
            outputs=[prompt_output, summary_output, transcription_output]
        )


def create_mediadb_keyword_search_tab():
    with gr.TabItem("Search MediaDB by Keyword", visible=True):
        gr.Markdown("# List Media Database Items by Keyword")

        with gr.Row():
            with gr.Column(scale=1):
                # Keyword selection dropdown - initialize with empty list, will be populated on load
                keyword_dropdown = gr.Dropdown(
                    label="Select Keyword",
                    choices=fetch_all_keywords(),  # Initialize with keywords on creation
                    value=None
                )
                entries_per_page = gr.Dropdown(
                    choices=[10, 20, 50, 100],
                    label="Entries per Page",
                    value=10
                )
                page_number = gr.Number(
                    value=1,
                    label="Page Number",
                    precision=0
                )

                # Navigation buttons
                refresh_keywords_button = gr.Button("Refresh Keywords")
                view_button = gr.Button("View Results")
                next_page_button = gr.Button("Next Page")
                previous_page_button = gr.Button("Previous Page")

                # Pagination information
                pagination_info = gr.Textbox(
                    label="Pagination Info",
                    interactive=False
                )

            with gr.Column(scale=2):
                # Results area
                results_table = gr.HTML(
                    label="Search Results"
                )
                item_details = gr.HTML(
                    label="Item Details",
                    visible=True
                )

        def update_keyword_choices():
            try:
                keywords = fetch_all_keywords()
                return gr.update(choices=keywords)
            except Exception as e:
                return gr.update(choices=[], value=None)

        def search_items(keyword, page, entries_per_page):
            try:
                # Calculate offset for pagination
                offset = (page - 1) * entries_per_page

                # Fetch items for the selected keyword
                items = fetch_items_by_keyword(keyword)
                total_items = len(items)
                total_pages = (total_items + entries_per_page - 1) // entries_per_page

                # Paginate results
                paginated_items = items[offset:offset + entries_per_page]

                # Generate HTML table for results
                table_html = "<table style='width:100%; border-collapse: collapse;'>"
                table_html += "<tr><th style='border: 1px solid black; padding: 8px;'>Title</th>"
                table_html += "<th style='border: 1px solid black; padding: 8px;'>URL</th></tr>"

                for item_id, title, url in paginated_items:
                    table_html += f"""

                    <tr>

                        <td style='border: 1px solid black; padding: 8px;'>{html.escape(title)}</td>

                        <td style='border: 1px solid black; padding: 8px;'>{html.escape(url)}</td>

                    </tr>

                    """
                table_html += "</table>"

                # Update pagination info
                pagination = f"Page {page} of {total_pages} (Total items: {total_items})"

                # Determine button states
                next_disabled = page >= total_pages
                prev_disabled = page <= 1

                return (
                    table_html,
                    pagination,
                    gr.update(interactive=not next_disabled),
                    gr.update(interactive=not prev_disabled)
                )
            except Exception as e:
                return (
                    f"<p>Error: {str(e)}</p>",
                    "Error in pagination",
                    gr.update(interactive=False),
                    gr.update(interactive=False)
                )

        def go_to_next_page(keyword, current_page, entries_per_page):
            next_page = current_page + 1
            return search_items(keyword, next_page, entries_per_page) + (next_page,)

        def go_to_previous_page(keyword, current_page, entries_per_page):
            previous_page = max(1, current_page - 1)
            return search_items(keyword, previous_page, entries_per_page) + (previous_page,)

        # Event handlers
        refresh_keywords_button.click(
            fn=update_keyword_choices,
            inputs=[],
            outputs=[keyword_dropdown]
        )

        view_button.click(
            fn=search_items,
            inputs=[keyword_dropdown, page_number, entries_per_page],
            outputs=[results_table, pagination_info, next_page_button, previous_page_button]
        )

        next_page_button.click(
            fn=go_to_next_page,
            inputs=[keyword_dropdown, page_number, entries_per_page],
            outputs=[results_table, pagination_info, next_page_button, previous_page_button, page_number]
        )

        previous_page_button.click(
            fn=go_to_previous_page,
            inputs=[keyword_dropdown, page_number, entries_per_page],
            outputs=[results_table, pagination_info, next_page_button, previous_page_button, page_number]
        )


def create_viewing_mediadb_tab():
    with gr.TabItem("View Media Database Entries", visible=True):
        gr.Markdown("# View Media Database Entries")
        with gr.Row():
            with gr.Column():
                entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
                page_number = gr.Number(value=1, label="Page Number", precision=0)
                view_button = gr.Button("View Page")
                next_page_button = gr.Button("Next Page")
                previous_page_button = gr.Button("Previous Page")
                pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
            with gr.Column():
                results_display = gr.HTML()


        def update_page(page, entries_per_page):
            results, pagination, total_pages = view_database(page, entries_per_page)
            next_disabled = page >= total_pages
            prev_disabled = page <= 1
            return results, pagination, page, gr.update(interactive=not next_disabled), gr.update(interactive=not prev_disabled)

        def go_to_next_page(current_page, entries_per_page):
            next_page = current_page + 1
            return update_page(next_page, entries_per_page)

        def go_to_previous_page(current_page, entries_per_page):
            previous_page = max(1, current_page - 1)
            return update_page(previous_page, entries_per_page)

        view_button.click(
            fn=update_page,
            inputs=[page_number, entries_per_page],
            outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
        )

        next_page_button.click(
            fn=go_to_next_page,
            inputs=[page_number, entries_per_page],
            outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
        )

        previous_page_button.click(
            fn=go_to_previous_page,
            inputs=[page_number, entries_per_page],
            outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
        )

#####################################################################
#
# RAG DB Viewing Functions:

def create_viewing_ragdb_tab():
    with gr.TabItem("View RAG Database Entries", visible=True):
        gr.Markdown("# View RAG Database Entries")
        with gr.Row():
            with gr.Column():
                entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
                page_number = gr.Number(value=1, label="Page Number", precision=0)
                view_button = gr.Button("View Page")
                next_page_button = gr.Button("Next Page")
                previous_page_button = gr.Button("Previous Page")
                pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
            with gr.Column():
                results_display = gr.HTML()

        def format_conversations_table(conversations):
            table_html = "<table style='width:100%; border-collapse: collapse;'>"
            table_html += """

                <tr>

                    <th style='border: 1px solid black; padding: 8px;'>Title</th>

                    <th style='border: 1px solid black; padding: 8px;'>Keywords</th>

                    <th style='border: 1px solid black; padding: 8px;'>Notes</th>

                    <th style='border: 1px solid black; padding: 8px;'>Rating</th>

                </tr>

            """

            for conversation in conversations:
                conv_id = conversation['conversation_id']
                title = conversation['title']
                rating = conversation.get('rating', '')  # Use get() to handle cases where rating might not exist

                keywords = get_keywords_for_conversation(conv_id)
                notes = get_notes(conv_id)

                table_html += f"""

                    <tr>

                        <td style='border: 1px solid black; padding: 8px;'>{html.escape(str(title))}</td>

                        <td style='border: 1px solid black; padding: 8px;'>{html.escape(', '.join(keywords))}</td>

                        <td style='border: 1px solid black; padding: 8px;'>{len(notes)} note(s)</td>

                        <td style='border: 1px solid black; padding: 8px;'>{html.escape(str(rating))}</td>

                    </tr>

                """
            table_html += "</table>"
            return table_html

        def update_page(page, entries_per_page):
            try:
                conversations, total_pages, total_count = get_all_conversations(page, entries_per_page)
                results_html = format_conversations_table(conversations)
                pagination = f"Page {page} of {total_pages} (Total conversations: {total_count})"

                next_disabled = page >= total_pages
                prev_disabled = page <= 1

                return (
                    results_html,
                    pagination,
                    page,
                    gr.update(interactive=not next_disabled),
                    gr.update(interactive=not prev_disabled)
                )
            except Exception as e:
                return (
                    f"<p>Error: {str(e)}</p>",
                    "Error in pagination",
                    page,
                    gr.update(interactive=False),
                    gr.update(interactive=False)
                )

        def go_to_next_page(current_page, entries_per_page):
            return update_page(current_page + 1, entries_per_page)

        def go_to_previous_page(current_page, entries_per_page):
            return update_page(max(1, current_page - 1), entries_per_page)

        view_button.click(
            fn=update_page,
            inputs=[page_number, entries_per_page],
            outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
        )

        next_page_button.click(
            fn=go_to_next_page,
            inputs=[page_number, entries_per_page],
            outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
        )

        previous_page_button.click(
            fn=go_to_previous_page,
            inputs=[page_number, entries_per_page],
            outputs=[results_display, pagination_info, page_number, next_page_button, previous_page_button]
        )


def create_view_all_rag_notes_tab():
    with gr.TabItem("View All RAG notes/Conversation Items", visible=True):
        gr.Markdown("# View All RAG Notes/Conversation Entries")
        with gr.Row():
            with gr.Column(scale=1):
                entries_per_page = gr.Dropdown(choices=[10, 20, 50, 100], label="Entries per Page", value=10)
                page_number = gr.Number(value=1, label="Page Number", precision=0)
                view_button = gr.Button("View Page")
                next_page_button = gr.Button("Next Page")
                previous_page_button = gr.Button("Previous Page")
            with gr.Column(scale=2):
                items_output = gr.Dropdown(label="Select Conversation to View Details", choices=[])
                conversation_title = gr.Textbox(label="Conversation Title", visible=True)
        with gr.Row():
            with gr.Column(scale=1):
                pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
            with gr.Column(scale=2):
                keywords_output = gr.Textbox(label="Keywords", visible=True)
                chat_history_output = gr.HTML(label="Chat History", visible=True)
                notes_output = gr.HTML(label="Associated Notes", visible=True)

        item_mapping = gr.State({})

        def update_page(page, entries_per_page):
            try:
                conversations, total_pages, total_count = get_all_conversations(page, entries_per_page)
                pagination = f"Page {page} of {total_pages} (Total conversations: {total_count})"

                # Handle the dictionary structure correctly
                choices = [f"{conv['title']} (ID: {conv['conversation_id']})" for conv in conversations]
                new_item_mapping = {
                    f"{conv['title']} (ID: {conv['conversation_id']})": conv['conversation_id']
                    for conv in conversations
                }

                next_disabled = page >= total_pages
                prev_disabled = page <= 1

                return (
                    gr.update(choices=choices, value=None),
                    pagination,
                    page,
                    gr.update(interactive=not next_disabled),
                    gr.update(interactive=not prev_disabled),
                    "",  # conversation_title
                    "",  # keywords_output
                    "",  # chat_history_output
                    "",  # notes_output
                    new_item_mapping
                )
            except Exception as e:
                logging.error(f"Error in update_page: {str(e)}", exc_info=True)
                return (
                    gr.update(choices=[], value=None),
                    f"Error: {str(e)}",
                    page,
                    gr.update(interactive=False),
                    gr.update(interactive=False),
                    "", "", "", "",
                    {}
                )

        def format_as_html(content, title):
            if content is None:
                content = "No content available."
            escaped_content = html.escape(str(content))
            formatted_content = escaped_content.replace('\n', '<br>')
            return f"""

            <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">

                <h3>{title}</h3>

                <div style="max-height: 700px; overflow-y: auto;">

                    {formatted_content}

                </div>

            </div>

            """

        def format_chat_history(messages):
            html_content = "<div style='max-height: 500px; overflow-y: auto;'>"
            for role, content in messages:
                role_class = "assistant" if role.lower() == "assistant" else "user"
                html_content += f"""

                <div class='{role_class}-message' style='margin: 10px 0; padding: 10px; border-radius: 5px; 

                    background-color: {"#f0f0f0" if role_class == "user" else "#e3f2fd"}'>

                    <strong>{html.escape(role)}:</strong><br>

                    {html.escape(content)}

                </div>

                """
            html_content += "</div>"
            return html_content

        def display_conversation_details(selected_item, item_mapping):
            if selected_item and item_mapping and selected_item in item_mapping:
                conv_id = item_mapping[selected_item]

                # Get keywords
                keywords = get_keywords_for_conversation(conv_id)
                keywords_text = ", ".join(keywords) if keywords else "No keywords"

                # Get chat history
                chat_messages, _, _ = load_chat_history(conv_id)
                chat_html = format_chat_history(chat_messages)

                # Get associated notes
                notes = get_notes(conv_id)
                notes_html = ""
                for note in notes:
                    notes_html += format_as_html(note, "Note")
                if not notes:
                    notes_html = "<p>No notes associated with this conversation.</p>"

                return (
                    selected_item.split(" (ID:")[0],  # Conversation title
                    keywords_text,
                    chat_html,
                    notes_html
                )
            return "", "", "", ""

        view_button.click(
            fn=update_page,
            inputs=[page_number, entries_per_page],
            outputs=[
                items_output,
                pagination_info,
                page_number,
                next_page_button,
                previous_page_button,
                conversation_title,
                keywords_output,
                chat_history_output,
                notes_output,
                item_mapping
            ]
        )

        next_page_button.click(
            fn=lambda page, entries: update_page(page + 1, entries),
            inputs=[page_number, entries_per_page],
            outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
                     conversation_title, keywords_output, chat_history_output, notes_output, item_mapping]
        )

        previous_page_button.click(
            fn=lambda page, entries: update_page(max(1, page - 1), entries),
            inputs=[page_number, entries_per_page],
            outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
                     conversation_title, keywords_output, chat_history_output, notes_output, item_mapping]
        )

        items_output.change(
            fn=display_conversation_details,
            inputs=[items_output, item_mapping],
            outputs=[conversation_title, keywords_output, chat_history_output, notes_output]
        )


def create_ragdb_keyword_items_tab():
    with gr.TabItem("View RAG Notes/Conversations by Keyword", visible=True):
        gr.Markdown("# View RAG Notes and Conversations by Keyword")

        with gr.Row():
            with gr.Column(scale=1):
                # Keyword selection
                keyword_dropdown = gr.Dropdown(
                    label="Select Keyword",
                    choices=[],
                    value=None,
                    multiselect=True
                )
                entries_per_page = gr.Dropdown(
                    choices=[10, 20, 50, 100],
                    label="Entries per Page",
                    value=10
                )
                page_number = gr.Number(
                    value=1,
                    label="Page Number",
                    precision=0
                )

                # Navigation buttons
                refresh_keywords_button = gr.Button("Refresh Keywords")
                view_button = gr.Button("View Items")
                next_page_button = gr.Button("Next Page")
                previous_page_button = gr.Button("Previous Page")
                pagination_info = gr.Textbox(
                    label="Pagination Info",
                    interactive=False
                )

            with gr.Column(scale=2):
                # Results tabs for conversations and notes
                with gr.Tabs():
                    with gr.Tab("Notes"):
                        notes_results = gr.HTML()
                    with gr.Tab("Conversations"):
                        conversation_results = gr.HTML()

        def update_keyword_choices():
            """Fetch all available keywords for the dropdown."""
            try:
                query = "SELECT keyword FROM rag_qa_keywords ORDER BY keyword"
                with get_db_connection() as conn:
                    cursor = conn.cursor()
                    cursor.execute(query)
                    keywords = [row[0] for row in cursor.fetchall()]
                return gr.update(choices=keywords)
            except Exception as e:
                return gr.update(choices=[], value=None)

        def format_conversations_html(conversations_data):
            """Format conversations data as HTML."""
            if not conversations_data:
                return "<p>No conversations found for selected keywords.</p>"

            html_content = "<div class='results-container'>"
            for conv_id, title in conversations_data:
                html_content += f"""

                <div style='border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;'>

                    <h3>{html.escape(title)}</h3>

                    <p>Conversation ID: {html.escape(conv_id)}</p>

                    <p><strong>Keywords:</strong> {', '.join(html.escape(k) for k in get_keywords_for_conversation(conv_id))}</p>

                </div>

                """
            html_content += "</div>"
            return html_content

        def format_notes_html(notes_data):
            """Format notes data as HTML."""
            if not notes_data:
                return "<p>No notes found for selected keywords.</p>"

            html_content = "<div class='results-container'>"
            for note_id, title, content, timestamp in notes_data:
                keywords = get_keywords_for_note(note_id)
                html_content += f"""

                <div style='border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;'>

                    <h3>{html.escape(title)}</h3>

                    <p><strong>Created:</strong> {timestamp}</p>

                    <p><strong>Keywords:</strong> {', '.join(html.escape(k) for k in keywords)}</p>

                    <div style='background: #f5f5f5; padding: 10px; margin-top: 10px;'>

                        {html.escape(content)}

                    </div>

                </div>

                """
            html_content += "</div>"
            return html_content

        def view_items(keywords, page, entries_per_page):
            if not keywords or (isinstance(keywords, list) and len(keywords) == 0):
                return (
                    "<p>Please select at least one keyword.</p>",
                    "<p>Please select at least one keyword.</p>",
                    "No results",
                    gr.update(interactive=False),
                    gr.update(interactive=False)
                )

            try:
                # Ensure keywords is a list
                keywords_list = keywords if isinstance(keywords, list) else [keywords]

                # Get conversations for selected keywords
                conversations, conv_total_pages, conv_count = search_conversations_by_keywords(
                    keywords_list, page, entries_per_page
                )

                # Get notes for selected keywords
                notes, notes_total_pages, notes_count = get_notes_by_keywords(
                    keywords_list, page, entries_per_page
                )

                # Format results as HTML
                conv_html = format_conversations_html(conversations)
                notes_html = format_notes_html(notes)

                # Create pagination info
                pagination = f"Page {page} of {max(conv_total_pages, notes_total_pages)} "
                pagination += f"(Conversations: {conv_count}, Notes: {notes_count})"

                # Determine button states
                max_pages = max(conv_total_pages, notes_total_pages)
                next_disabled = page >= max_pages
                prev_disabled = page <= 1

                return (
                    conv_html,
                    notes_html,
                    pagination,
                    gr.update(interactive=not next_disabled),
                    gr.update(interactive=not prev_disabled)
                )
            except Exception as e:
                logging.error(f"Error in view_items: {str(e)}")
                return (
                    f"<p>Error: {str(e)}</p>",
                    f"<p>Error: {str(e)}</p>",
                    "Error in retrieval",
                    gr.update(interactive=False),
                    gr.update(interactive=False)
                )

        def go_to_next_page(keywords, current_page, entries_per_page):
            return view_items(keywords, current_page + 1, entries_per_page)

        def go_to_previous_page(keywords, current_page, entries_per_page):
            return view_items(keywords, max(1, current_page - 1), entries_per_page)

        # Event handlers
        refresh_keywords_button.click(
            fn=update_keyword_choices,
            inputs=[],
            outputs=[keyword_dropdown]
        )

        view_button.click(
            fn=view_items,
            inputs=[keyword_dropdown, page_number, entries_per_page],
            outputs=[
                conversation_results,
                notes_results,
                pagination_info,
                next_page_button,
                previous_page_button
            ]
        )

        next_page_button.click(
            fn=go_to_next_page,
            inputs=[keyword_dropdown, page_number, entries_per_page],
            outputs=[
                conversation_results,
                notes_results,
                pagination_info,
                next_page_button,
                previous_page_button
            ]
        )

        previous_page_button.click(
            fn=go_to_previous_page,
            inputs=[keyword_dropdown, page_number, entries_per_page],
            outputs=[
                conversation_results,
                notes_results,
                pagination_info,
                next_page_button,
                previous_page_button
            ]
        )

        # Initialize keyword dropdown on page load
        keyword_dropdown.value = update_keyword_choices()

#
# End of RAG DB Viewing tabs
################################################################

#
#######################################################################################################################