File size: 15,369 Bytes
43cd37c
 
 
 
 
 
 
 
 
c5b0bb7
 
 
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
# Keywords.py
# Purpose: This file contains the functions to create the Keywords tab in the Gradio UI.
#
# The Keywords tab allows the user to add, delete, view, and export keywords from the database.
#
# Imports:
#
# External Imports
import gradio as gr

from App_Function_Libraries.DB.Character_Chat_DB import view_char_keywords, add_char_keywords, delete_char_keyword, \
    export_char_keywords_to_csv
#
# Internal Imports
from App_Function_Libraries.DB.DB_Manager import add_keyword, delete_keyword, keywords_browser_interface, export_keywords_to_csv
from App_Function_Libraries.DB.Prompts_DB import view_prompt_keywords, delete_prompt_keyword, \
    export_prompt_keywords_to_csv
from App_Function_Libraries.DB.RAG_QA_Chat_DB import view_rag_keywords, get_all_collections, \
    get_keywords_for_collection, create_keyword_collection, add_keyword_to_collection, delete_rag_keyword, \
    export_rag_keywords_to_csv


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

def create_export_keywords_tab():
    with gr.TabItem("Export MediaDB Keywords", visible=True):
        with gr.Row():
            with gr.Column():
                export_keywords_button = gr.Button("Export Keywords")
            with gr.Column():
                export_keywords_output = gr.File(label="Download Exported Keywords")
                export_keywords_status = gr.Textbox(label="Export Status")

            export_keywords_button.click(
                fn=export_keywords_to_csv,
                outputs=[export_keywords_status, export_keywords_output]
            )

def create_view_keywords_tab():
    with gr.TabItem("View MediaDB Keywords", visible=True):
        gr.Markdown("# Browse MediaDB Keywords")
        with gr.Column():
            browse_output = gr.Markdown()
            browse_button = gr.Button("View Existing Keywords")
            browse_button.click(fn=keywords_browser_interface, outputs=browse_output)


def create_add_keyword_tab():
    with gr.TabItem("Add MediaDB Keywords", visible=True):
        with gr.Row():
            with gr.Column():
                gr.Markdown("# Add Keywords to the Database")
                add_input = gr.Textbox(label="Add Keywords (comma-separated)", placeholder="Enter keywords here...")
                add_button = gr.Button("Add Keywords")
            with gr.Row():
                add_output = gr.Textbox(label="Result")
                add_button.click(fn=add_keyword, inputs=add_input, outputs=add_output)


def create_delete_keyword_tab():
    with gr.Tab("Delete MediaDB Keywords", visible=True):
        with gr.Row():
            with gr.Column():
                gr.Markdown("# Delete Keywords from the Database")
                delete_input = gr.Textbox(label="Delete Keyword", placeholder="Enter keyword to delete here...")
                delete_button = gr.Button("Delete Keyword")
            with gr.Row():
                delete_output = gr.Textbox(label="Result")
                delete_button.click(fn=delete_keyword, inputs=delete_input, outputs=delete_output)

#
# End of Media DB Keyword tabs
##########################################################


############################################################
#
# Character DB Keyword functions

def create_character_keywords_tab():
    """Creates the Character Keywords management tab"""
    with gr.Tab("Character Keywords"):
        gr.Markdown("# Character Keywords Management")

        with gr.Tabs():
            # View Character Keywords Tab
            with gr.TabItem("View Keywords"):
                with gr.Column():
                    refresh_char_keywords = gr.Button("Refresh Character Keywords")
                    char_keywords_output = gr.Markdown()
                    view_char_keywords()
                    refresh_char_keywords.click(
                        fn=view_char_keywords,
                        outputs=char_keywords_output
                    )

            # Add Character Keywords Tab
            with gr.TabItem("Add Keywords"):
                with gr.Column():
                    char_name = gr.Textbox(label="Character Name")
                    new_keywords = gr.Textbox(label="New Keywords (comma-separated)")
                    add_char_keyword_btn = gr.Button("Add Keywords")
                    add_char_result = gr.Markdown()

                    add_char_keyword_btn.click(
                        fn=add_char_keywords,
                        inputs=[char_name, new_keywords],
                        outputs=add_char_result
                    )

            # Delete Character Keywords Tab (New)
            with gr.TabItem("Delete Keywords"):
                with gr.Column():
                    delete_char_name = gr.Textbox(label="Character Name")
                    delete_char_keyword_input = gr.Textbox(label="Keyword to Delete")
                    delete_char_keyword_btn = gr.Button("Delete Keyword")
                    delete_char_result = gr.Markdown()

                    delete_char_keyword_btn.click(
                        fn=delete_char_keyword,
                        inputs=[delete_char_name, delete_char_keyword_input],
                        outputs=delete_char_result
                    )

            # Export Character Keywords Tab (New)
            with gr.TabItem("Export Keywords"):
                with gr.Column():
                    export_char_keywords_btn = gr.Button("Export Character Keywords")
                    export_char_file = gr.File(label="Download Exported Keywords")
                    export_char_status = gr.Textbox(label="Export Status")

                    export_char_keywords_btn.click(
                        fn=export_char_keywords_to_csv,
                        outputs=[export_char_status, export_char_file]
                    )

#
# End of Character Keywords tab
##########################################################

############################################################
#
# RAG QA Keywords functions

def create_rag_qa_keywords_tab():
    """Creates the RAG QA Keywords management tab"""
    with gr.Tab("RAG QA Keywords"):
        gr.Markdown("# RAG QA Keywords Management")

        with gr.Tabs():
            # View RAG QA Keywords Tab
            with gr.TabItem("View Keywords"):
                with gr.Column():
                    refresh_rag_keywords = gr.Button("Refresh RAG QA Keywords")
                    rag_keywords_output = gr.Markdown()

                    view_rag_keywords()

                    refresh_rag_keywords.click(
                        fn=view_rag_keywords,
                        outputs=rag_keywords_output
                    )

            # Add RAG QA Keywords Tab
            with gr.TabItem("Add Keywords"):
                with gr.Column():
                    new_rag_keywords = gr.Textbox(label="New Keywords (comma-separated)")
                    add_rag_keyword_btn = gr.Button("Add Keywords")
                    add_rag_result = gr.Markdown()

                    add_rag_keyword_btn.click(
                        fn=add_keyword,
                        inputs=new_rag_keywords,
                        outputs=add_rag_result
                    )

            # Delete RAG QA Keywords Tab (New)
            with gr.TabItem("Delete Keywords"):
                with gr.Column():
                    delete_rag_keyword_input = gr.Textbox(label="Keyword to Delete")
                    delete_rag_keyword_btn = gr.Button("Delete Keyword")
                    delete_rag_result = gr.Markdown()

                    delete_rag_keyword_btn.click(
                        fn=delete_rag_keyword,
                        inputs=delete_rag_keyword_input,
                        outputs=delete_rag_result
                    )

            # Export RAG QA Keywords Tab (New)
            with gr.TabItem("Export Keywords"):
                with gr.Column():
                    export_rag_keywords_btn = gr.Button("Export RAG QA Keywords")
                    export_rag_file = gr.File(label="Download Exported Keywords")
                    export_rag_status = gr.Textbox(label="Export Status")

                    export_rag_keywords_btn.click(
                        fn=export_rag_keywords_to_csv,
                        outputs=[export_rag_status, export_rag_file]
                    )

#
# End of RAG QA Keywords tab
##########################################################


############################################################
#
# Prompt Keywords functions

def create_prompt_keywords_tab():
    """Creates the Prompt Keywords management tab"""
    with gr.Tab("Prompt Keywords"):
        gr.Markdown("# Prompt Keywords Management")

        with gr.Tabs():
            # View Keywords Tab
            with gr.TabItem("View Keywords"):
                with gr.Column():
                    refresh_prompt_keywords = gr.Button("Refresh Prompt Keywords")
                    prompt_keywords_output = gr.Markdown()

                    refresh_prompt_keywords.click(
                        fn=view_prompt_keywords,
                        outputs=prompt_keywords_output
                    )

            # Add Keywords Tab (using existing prompt management functions)
            with gr.TabItem("Add Keywords"):
                gr.Markdown("""

                    To add keywords to prompts, please use the Prompt Management interface.

                    Keywords can be added when creating or editing a prompt.

                """)

            # Delete Keywords Tab
            with gr.TabItem("Delete Keywords"):
                with gr.Column():
                    delete_prompt_keyword_input = gr.Textbox(label="Keyword to Delete")
                    delete_prompt_keyword_btn = gr.Button("Delete Keyword")
                    delete_prompt_result = gr.Markdown()

                    delete_prompt_keyword_btn.click(
                        fn=delete_prompt_keyword,
                        inputs=delete_prompt_keyword_input,
                        outputs=delete_prompt_result
                    )

            # Export Keywords Tab
            with gr.TabItem("Export Keywords"):
                with gr.Column():
                    export_prompt_keywords_btn = gr.Button("Export Prompt Keywords")
                    export_prompt_status = gr.Textbox(label="Export Status", interactive=False)
                    export_prompt_file = gr.File(label="Download Exported Keywords", interactive=False)

                    def handle_export():
                        status, file_path = export_prompt_keywords_to_csv()
                        if file_path:
                            return status, file_path
                        return status, None

                    export_prompt_keywords_btn.click(
                        fn=handle_export,
                        outputs=[export_prompt_status, export_prompt_file]
                    )
#
# End of Prompt Keywords tab
############################################################


############################################################
#
# Meta-Keywords functions

def create_meta_keywords_tab():
    """Creates the Meta-Keywords management tab"""
    with gr.Tab("Meta-Keywords"):
        gr.Markdown("# Meta-Keywords Management")

        with gr.Tabs():
            # View Meta-Keywords Tab
            with gr.TabItem("View Collections"):
                with gr.Column():
                    refresh_collections = gr.Button("Refresh Collections")
                    collections_output = gr.Markdown()

                    def view_collections():
                        try:
                            collections, _, _ = get_all_collections()
                            if collections:
                                result = "### Keyword Collections:\n"
                                for collection in collections:
                                    keywords = get_keywords_for_collection(collection)
                                    result += f"\n**{collection}**:\n"
                                    result += "\n".join([f"- {k}" for k in keywords])
                                    result += "\n"
                                return result
                            return "No collections found."
                        except Exception as e:
                            return f"Error retrieving collections: {str(e)}"

                    refresh_collections.click(
                        fn=view_collections,
                        outputs=collections_output
                    )

            # Create Collection Tab
            with gr.TabItem("Create Collection"):
                with gr.Column():
                    collection_name = gr.Textbox(label="Collection Name")
                    create_collection_btn = gr.Button("Create Collection")
                    create_result = gr.Markdown()

                    def create_collection(name: str):
                        try:
                            create_keyword_collection(name)
                            return f"Successfully created collection: {name}"
                        except Exception as e:
                            return f"Error creating collection: {str(e)}"

                    create_collection_btn.click(
                        fn=create_collection,
                        inputs=collection_name,
                        outputs=create_result
                    )

            # Add Keywords to Collection Tab
            with gr.TabItem("Add to Collection"):
                with gr.Column():
                    collection_select = gr.Textbox(label="Collection Name")
                    keywords_to_add = gr.Textbox(label="Keywords to Add (comma-separated)")
                    add_to_collection_btn = gr.Button("Add Keywords to Collection")
                    add_to_collection_result = gr.Markdown()

                    def add_keywords_to_collection(collection: str, keywords: str):
                        try:
                            keywords_list = [k.strip() for k in keywords.split(",") if k.strip()]
                            for keyword in keywords_list:
                                add_keyword_to_collection(collection, keyword)
                            return f"Successfully added {len(keywords_list)} keywords to collection {collection}"
                        except Exception as e:
                            return f"Error adding keywords to collection: {str(e)}"

                    add_to_collection_btn.click(
                        fn=add_keywords_to_collection,
                        inputs=[collection_select, keywords_to_add],
                        outputs=add_to_collection_result
                    )

#
# End of Meta-Keywords tab
##########################################################

#
# End of Keywords.py
######################################################################################################################