oceansweep commited on
Commit
639f0d0
1 Parent(s): 2634a4c

Upload 2 files

Browse files
App_Function_Libraries/DB/DB_Manager.py CHANGED
@@ -1,535 +1,866 @@
1
- import configparser
2
- import logging
3
- import os
4
- from contextlib import contextmanager
5
- from time import sleep
6
- from typing import Tuple
7
- import sqlite3
8
- # 3rd-Party Libraries
9
- from elasticsearch import Elasticsearch
10
-
11
- ############################################################################################################
12
- #
13
- # This file contains the DatabaseManager class, which is responsible for managing the database connection, i.e. either SQLite or Elasticsearch.
14
-
15
- ####
16
- # The DatabaseManager class provides the following methods:
17
- # - add_media: Add a new media item to the database
18
- # - fetch_items_by_keyword: Fetch media items from the database based on a keyword
19
- # - fetch_item_details: Fetch details of a specific media item from the database
20
- # - update_media_content: Update the content of a specific media item in the database
21
- # - search_and_display_items: Search for media items in the database and display the results
22
- # - close_connection: Close the database connection
23
- ####
24
-
25
- # Import your existing SQLite functions
26
- from App_Function_Libraries.DB.SQLite_DB import (
27
- update_media_content as sqlite_update_media_content,
28
- list_prompts as sqlite_list_prompts,
29
- search_and_display as sqlite_search_and_display,
30
- fetch_prompt_details as sqlite_fetch_prompt_details,
31
- keywords_browser_interface as sqlite_keywords_browser_interface,
32
- add_keyword as sqlite_add_keyword,
33
- delete_keyword as sqlite_delete_keyword,
34
- export_keywords_to_csv as sqlite_export_keywords_to_csv,
35
- ingest_article_to_db as sqlite_ingest_article_to_db,
36
- add_media_to_database as sqlite_add_media_to_database,
37
- import_obsidian_note_to_db as sqlite_import_obsidian_note_to_db,
38
- add_prompt as sqlite_add_prompt,
39
- delete_chat_message as sqlite_delete_chat_message,
40
- update_chat_message as sqlite_update_chat_message,
41
- add_chat_message as sqlite_add_chat_message,
42
- get_chat_messages as sqlite_get_chat_messages,
43
- search_chat_conversations as sqlite_search_chat_conversations,
44
- create_chat_conversation as sqlite_create_chat_conversation,
45
- save_chat_history_to_database as sqlite_save_chat_history_to_database,
46
- view_database as sqlite_view_database,
47
- get_transcripts as sqlite_get_transcripts,
48
- get_trashed_items as sqlite_get_trashed_items,
49
- user_delete_item as sqlite_user_delete_item,
50
- empty_trash as sqlite_empty_trash,
51
- create_automated_backup as sqlite_create_automated_backup,
52
- add_or_update_prompt as sqlite_add_or_update_prompt,
53
- load_prompt_details as sqlite_load_prompt_details,
54
- load_preset_prompts as sqlite_load_preset_prompts,
55
- insert_prompt_to_db as sqlite_insert_prompt_to_db,
56
- delete_prompt as sqlite_delete_prompt,
57
- search_and_display_items as sqlite_search_and_display_items,
58
- get_conversation_name as sqlite_get_conversation_name,
59
- add_media_with_keywords as sqlite_add_media_with_keywords,
60
- check_media_and_whisper_model as sqlite_check_media_and_whisper_model,
61
- DatabaseError, create_document_version as sqlite_create_document_version,
62
- get_document_version as sqlite_get_document_version
63
- )
64
-
65
- class Database:
66
- def __init__(self, db_path=None):
67
- self.db_path = db_path or os.getenv('DB_NAME', 'media_summary.db')
68
- self.pool = []
69
- self.pool_size = 10
70
-
71
- @contextmanager
72
- def get_connection(self):
73
- retry_count = 5
74
- retry_delay = 1
75
- conn = None
76
- while retry_count > 0:
77
- try:
78
- conn = self.pool.pop() if self.pool else sqlite3.connect(self.db_path, check_same_thread=False)
79
- yield conn
80
- self.pool.append(conn)
81
- return
82
- except sqlite3.OperationalError as e:
83
- if 'database is locked' in str(e):
84
- logging.warning(f"Database is locked, retrying in {retry_delay} seconds...")
85
- retry_count -= 1
86
- sleep(retry_delay)
87
- else:
88
- raise DatabaseError(f"Database error: {e}")
89
- except Exception as e:
90
- raise DatabaseError(f"Unexpected error: {e}")
91
- finally:
92
- # Ensure the connection is returned to the pool even on failure
93
- if conn and conn not in self.pool:
94
- self.pool.append(conn)
95
- raise DatabaseError("Database is locked and retries have been exhausted")
96
-
97
- def execute_query(self, query: str, params: Tuple = ()) -> None:
98
- with self.get_connection() as conn:
99
- try:
100
- cursor = conn.cursor()
101
- cursor.execute(query, params)
102
- conn.commit()
103
- except sqlite3.Error as e:
104
- raise DatabaseError(f"Database error: {e}, Query: {query}")
105
-
106
- def close_all_connections(self):
107
- for conn in self.pool:
108
- conn.close()
109
- self.pool.clear()
110
-
111
- def get_db_config():
112
- config = configparser.ConfigParser()
113
- config.read('config.txt')
114
- return {
115
- 'type': config['Database']['type'],
116
- 'sqlite_path': config.get('Database', 'sqlite_path', fallback='media_summary.db'),
117
- 'elasticsearch_host': config.get('Database', 'elasticsearch_host', fallback='localhost'),
118
- 'elasticsearch_port': config.getint('Database', 'elasticsearch_port', fallback=9200)
119
- }
120
-
121
- db_config = get_db_config()
122
- db_type = db_config['type']
123
-
124
- if db_type == 'sqlite':
125
- # Use the config path if provided, otherwise fall back to default
126
- db = Database(db_config.get('sqlite_path'))
127
- elif db_type == 'elasticsearch':
128
- es = Elasticsearch([{
129
- 'host': db_config['elasticsearch_host'],
130
- 'port': db_config['elasticsearch_port']
131
- }])
132
- else:
133
- raise ValueError(f"Unsupported database type: {db_type}")
134
-
135
- db_path = db_config['sqlite_path']
136
-
137
- # Update this path to the directory where you want to store the database backups
138
- backup_dir = os.environ.get('DB_BACKUP_DIR', 'path/to/backup/directory')
139
-
140
-
141
-
142
-
143
- if db_type == 'sqlite':
144
- conn = sqlite3.connect(db_config['sqlite_path'])
145
- cursor = conn.cursor()
146
- elif db_type == 'elasticsearch':
147
- es = Elasticsearch([{
148
- 'host': db_config['elasticsearch_host'],
149
- 'port': db_config['elasticsearch_port']
150
- }])
151
- else:
152
- raise ValueError(f"Unsupported database type: {db_type}")
153
-
154
- ############################################################################################################
155
- #
156
- # DB-Searching functions
157
-
158
- def view_database(*args, **kwargs):
159
- if db_type == 'sqlite':
160
- return sqlite_view_database(*args, **kwargs)
161
- elif db_type == 'elasticsearch':
162
- # Implement Elasticsearch version
163
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
164
-
165
- def search_and_display_items(*args, **kwargs):
166
- if db_type == 'sqlite':
167
- return sqlite_search_and_display_items(*args, **kwargs)
168
- elif db_type == 'elasticsearch':
169
- # Implement Elasticsearch version
170
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
171
-
172
- def search_and_display(*args, **kwargs):
173
- if db_type == 'sqlite':
174
- return sqlite_search_and_display(*args, **kwargs)
175
- elif db_type == 'elasticsearch':
176
- # Implement Elasticsearch version
177
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
178
-
179
- #
180
- # End of DB-Searching functions
181
- ############################################################################################################
182
-
183
- ############################################################################################################
184
- #
185
- # Transcript-related Functions
186
-
187
- def get_transcripts(*args, **kwargs):
188
- if db_type == 'sqlite':
189
- return sqlite_get_transcripts(*args, **kwargs)
190
- elif db_type == 'elasticsearch':
191
- # Implement Elasticsearch version
192
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
193
-
194
- #
195
- # End of Transcript-related Functions
196
- ############################################################################################################
197
-
198
- ############################################################################################################
199
- #
200
- # DB-Ingestion functions
201
-
202
- def add_media_to_database(*args, **kwargs):
203
- if db_type == 'sqlite':
204
- result = sqlite_add_media_to_database(*args, **kwargs)
205
-
206
- # Extract content
207
- segments = args[2]
208
- if isinstance(segments, list):
209
- content = ' '.join([segment.get('Text', '') for segment in segments if 'Text' in segment])
210
- elif isinstance(segments, dict):
211
- content = segments.get('text', '') or segments.get('content', '')
212
- else:
213
- content = str(segments)
214
-
215
- # Extract media_id from the result
216
- # Assuming the result is in the format "Media 'Title' added/updated successfully with ID: {media_id}"
217
- import re
218
- match = re.search(r"with ID: (\d+)", result)
219
- if match:
220
- media_id = int(match.group(1))
221
-
222
- # Create initial document version
223
- sqlite_create_document_version(media_id, content)
224
-
225
- return result
226
- elif db_type == 'elasticsearch':
227
- # Implement Elasticsearch version
228
- raise NotImplementedError("Elasticsearch version of add_media_to_database not yet implemented")
229
-
230
-
231
- def import_obsidian_note_to_db(*args, **kwargs):
232
- if db_type == 'sqlite':
233
- return sqlite_import_obsidian_note_to_db(*args, **kwargs)
234
- elif db_type == 'elasticsearch':
235
- # Implement Elasticsearch version
236
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
237
-
238
-
239
- def update_media_content(*args, **kwargs):
240
- if db_type == 'sqlite':
241
- result = sqlite_update_media_content(*args, **kwargs)
242
-
243
- # Extract media_id and content
244
- selected_item = args[0]
245
- item_mapping = args[1]
246
- content_input = args[2]
247
-
248
- if selected_item and item_mapping and selected_item in item_mapping:
249
- media_id = item_mapping[selected_item]
250
-
251
- # Create new document version
252
- sqlite_create_document_version(media_id, content_input)
253
-
254
- return result
255
- elif db_type == 'elasticsearch':
256
- # Implement Elasticsearch version
257
- raise NotImplementedError("Elasticsearch version of update_media_content not yet implemented")
258
-
259
-
260
- def add_media_with_keywords(*args, **kwargs):
261
- if db_type == 'sqlite':
262
- return sqlite_add_media_with_keywords(*args, **kwargs)
263
- elif db_type == 'elasticsearch':
264
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
265
-
266
- def check_media_and_whisper_model(*args, **kwargs):
267
- if db_type == 'sqlite':
268
- return sqlite_check_media_and_whisper_model(*args, **kwargs)
269
- elif db_type == 'elasticsearch':
270
- raise NotImplementedError("Elasticsearch version of check_media_and_whisper_model not yet implemented")
271
-
272
- def ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt):
273
- if db_type == 'sqlite':
274
- return sqlite_ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt)
275
- elif db_type == 'elasticsearch':
276
- # Implement Elasticsearch version
277
- raise NotImplementedError("Elasticsearch version of ingest_article_to_db not yet implemented")
278
- else:
279
- raise ValueError(f"Unsupported database type: {db_type}")
280
-
281
- #
282
- # End of DB-Ingestion functions
283
- ############################################################################################################
284
-
285
-
286
- ############################################################################################################
287
- #
288
- # Prompt-related functions
289
-
290
- def list_prompts(*args, **kwargs):
291
- if db_type == 'sqlite':
292
- return sqlite_list_prompts(*args, **kwargs)
293
- elif db_type == 'elasticsearch':
294
- # Implement Elasticsearch version
295
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
296
-
297
-
298
- def fetch_prompt_details(*args, **kwargs):
299
- if db_type == 'sqlite':
300
- return sqlite_fetch_prompt_details(*args, **kwargs)
301
- elif db_type == 'elasticsearch':
302
- # Implement Elasticsearch version
303
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
304
-
305
- def add_prompt(*args, **kwargs):
306
- if db_type == 'sqlite':
307
- return sqlite_add_prompt(*args, **kwargs)
308
- elif db_type == 'elasticsearch':
309
- # Implement Elasticsearch version
310
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
311
-
312
-
313
- def add_or_update_prompt(*args, **kwargs):
314
- if db_type == 'sqlite':
315
- return sqlite_add_or_update_prompt(*args, **kwargs)
316
- elif db_type == 'elasticsearch':
317
- # Implement Elasticsearch version
318
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
319
-
320
- def load_prompt_details(*args, **kwargs):
321
- if db_type == 'sqlite':
322
- return sqlite_load_prompt_details(*args, **kwargs)
323
- elif db_type == 'elasticsearch':
324
- # Implement Elasticsearch version
325
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
326
-
327
- def load_preset_prompts(*args, **kwargs):
328
- if db_type == 'sqlite':
329
- return sqlite_load_preset_prompts(*args, **kwargs)
330
- elif db_type == 'elasticsearch':
331
- # Implement Elasticsearch version
332
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
333
-
334
- def insert_prompt_to_db(*args, **kwargs):
335
- if db_type == 'sqlite':
336
- return sqlite_insert_prompt_to_db(*args, **kwargs)
337
- elif db_type == 'elasticsearch':
338
- # Implement Elasticsearch version
339
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
340
-
341
- def delete_prompt(*args, **kwargs):
342
- if db_type == 'sqlite':
343
- return sqlite_delete_prompt(*args, **kwargs)
344
- elif db_type == 'elasticsearch':
345
- # Implement Elasticsearch version
346
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
347
-
348
- #
349
- # End of Prompt-related functions
350
- ############################################################################################################
351
-
352
- ############################################################################################################
353
- #
354
- # Keywords-related Functions
355
-
356
- def keywords_browser_interface(*args, **kwargs):
357
- if db_type == 'sqlite':
358
- return sqlite_keywords_browser_interface(*args, **kwargs)
359
- elif db_type == 'elasticsearch':
360
- # Implement Elasticsearch version
361
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
362
-
363
- def add_keyword(*args, **kwargs):
364
- if db_type == 'sqlite':
365
- with db.get_connection() as conn:
366
- cursor = conn.cursor()
367
- return sqlite_add_keyword(*args, **kwargs)
368
- elif db_type == 'elasticsearch':
369
- # Implement Elasticsearch version
370
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
371
-
372
- def delete_keyword(*args, **kwargs):
373
- if db_type == 'sqlite':
374
- return sqlite_delete_keyword(*args, **kwargs)
375
- elif db_type == 'elasticsearch':
376
- # Implement Elasticsearch version
377
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
378
-
379
- def export_keywords_to_csv(*args, **kwargs):
380
- if db_type == 'sqlite':
381
- return sqlite_export_keywords_to_csv(*args, **kwargs)
382
- elif db_type == 'elasticsearch':
383
- # Implement Elasticsearch version
384
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
385
-
386
- #
387
- # End of Keywords-related Functions
388
- ############################################################################################################
389
-
390
- ############################################################################################################
391
- #
392
- # Chat-related Functions
393
-
394
- def delete_chat_message(*args, **kwargs):
395
- if db_type == 'sqlite':
396
- return sqlite_delete_chat_message(*args, **kwargs)
397
- elif db_type == 'elasticsearch':
398
- # Implement Elasticsearch version
399
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
400
-
401
- def update_chat_message(*args, **kwargs):
402
- if db_type == 'sqlite':
403
- return sqlite_update_chat_message(*args, **kwargs)
404
- elif db_type == 'elasticsearch':
405
- # Implement Elasticsearch version
406
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
407
-
408
- def add_chat_message(*args, **kwargs):
409
- if db_type == 'sqlite':
410
- return sqlite_add_chat_message(*args, **kwargs)
411
- elif db_type == 'elasticsearch':
412
- # Implement Elasticsearch version
413
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
414
-
415
- def get_chat_messages(*args, **kwargs):
416
- if db_type == 'sqlite':
417
- return sqlite_get_chat_messages(*args, **kwargs)
418
- elif db_type == 'elasticsearch':
419
- # Implement Elasticsearch version
420
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
421
-
422
- def search_chat_conversations(*args, **kwargs):
423
- if db_type == 'sqlite':
424
- return sqlite_search_chat_conversations(*args, **kwargs)
425
- elif db_type == 'elasticsearch':
426
- # Implement Elasticsearch version
427
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
428
-
429
- def create_chat_conversation(*args, **kwargs):
430
- if db_type == 'sqlite':
431
- return sqlite_create_chat_conversation(*args, **kwargs)
432
- elif db_type == 'elasticsearch':
433
- # Implement Elasticsearch version
434
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
435
-
436
- def save_chat_history_to_database(*args, **kwargs):
437
- if db_type == 'sqlite':
438
- return sqlite_save_chat_history_to_database(*args, **kwargs)
439
- elif db_type == 'elasticsearch':
440
- # Implement Elasticsearch version
441
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
442
-
443
- def get_conversation_name(*args, **kwargs):
444
- if db_type == 'sqlite':
445
- return sqlite_get_conversation_name(*args, **kwargs)
446
- elif db_type == 'elasticsearch':
447
- # Implement Elasticsearch version
448
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
449
-
450
- #
451
- # End of Chat-related Functions
452
- ############################################################################################################
453
-
454
- ############################################################################################################
455
- #
456
- # Trash-related Functions
457
-
458
- def get_trashed_items(*args, **kwargs):
459
- if db_type == 'sqlite':
460
- return sqlite_get_trashed_items(*args, **kwargs)
461
- elif db_type == 'elasticsearch':
462
- # Implement Elasticsearch version
463
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
464
-
465
- def user_delete_item(*args, **kwargs):
466
- if db_type == 'sqlite':
467
- return sqlite_user_delete_item(*args, **kwargs)
468
- elif db_type == 'elasticsearch':
469
- # Implement Elasticsearch version
470
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
471
-
472
- def empty_trash(*args, **kwargs):
473
- if db_type == 'sqlite':
474
- return sqlite_empty_trash(*args, **kwargs)
475
- elif db_type == 'elasticsearch':
476
- # Implement Elasticsearch version
477
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
478
-
479
- #
480
- # End of Trash-related Functions
481
- ############################################################################################################
482
-
483
-
484
- ############################################################################################################
485
- #
486
- # DB-Backup Functions
487
-
488
- def create_automated_backup(*args, **kwargs):
489
- if db_type == 'sqlite':
490
- return sqlite_create_automated_backup(*args, **kwargs)
491
- elif db_type == 'elasticsearch':
492
- # Implement Elasticsearch version
493
- raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
494
-
495
- #
496
- # End of DB-Backup Functions
497
- ############################################################################################################
498
-
499
-
500
- ############################################################################################################
501
- #
502
- # Document Versioning Functions
503
-
504
- def create_document_version(*args, **kwargs):
505
- if db_type == 'sqlite':
506
- return sqlite_create_document_version(*args, **kwargs)
507
- elif db_type == 'elasticsearch':
508
- # Implement Elasticsearch version
509
- raise NotImplementedError("Elasticsearch version of create_document_version not yet implemented")
510
-
511
- def get_document_version(*args, **kwargs):
512
- if db_type == 'sqlite':
513
- return sqlite_get_document_version(*args, **kwargs)
514
- elif db_type == 'elasticsearch':
515
- # Implement Elasticsearch version
516
- raise NotImplementedError("Elasticsearch version of get_document_version not yet implemented")
517
-
518
- #
519
- # End of Document Versioning Functions
520
- ############################################################################################################
521
-
522
-
523
-
524
- ############################################################################################################
525
- #
526
- # Function to close the database connection for SQLite
527
-
528
- def close_connection():
529
- if db_type == 'sqlite':
530
- db.close_all_connections()
531
- # Elasticsearch doesn't need explicit closing
532
-
533
- #
534
- # End of file
535
- ############################################################################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # DB_Manager.py
2
+ # Description: This file contains the DatabaseManager class, which is responsible for managing the database connection, i.e. either SQLite or Elasticsearch.
3
+ #
4
+ # Imports
5
+ import configparser
6
+ import os
7
+ import logging
8
+ import threading
9
+ from contextlib import contextmanager
10
+ from typing import Tuple, List, Union, Dict
11
+ import sqlite3
12
+ import time
13
+ #
14
+ # 3rd-Party Libraries
15
+ from elasticsearch import Elasticsearch
16
+ #
17
+ # Import your existing SQLite functions
18
+ from App_Function_Libraries.DB.SQLite_DB import (
19
+ update_media_content as sqlite_update_media_content,
20
+ list_prompts as sqlite_list_prompts,
21
+ search_and_display as sqlite_search_and_display,
22
+ fetch_prompt_details as sqlite_fetch_prompt_details,
23
+ keywords_browser_interface as sqlite_keywords_browser_interface,
24
+ add_keyword as sqlite_add_keyword,
25
+ delete_keyword as sqlite_delete_keyword,
26
+ export_keywords_to_csv as sqlite_export_keywords_to_csv,
27
+ ingest_article_to_db as sqlite_ingest_article_to_db,
28
+ add_media_to_database as sqlite_add_media_to_database,
29
+ import_obsidian_note_to_db as sqlite_import_obsidian_note_to_db,
30
+ add_prompt as sqlite_add_prompt,
31
+ delete_chat_message as sqlite_delete_chat_message,
32
+ update_chat_message as sqlite_update_chat_message,
33
+ add_chat_message as sqlite_add_chat_message,
34
+ get_chat_messages as sqlite_get_chat_messages,
35
+ search_chat_conversations as sqlite_search_chat_conversations,
36
+ create_chat_conversation as sqlite_create_chat_conversation,
37
+ save_chat_history_to_database as sqlite_save_chat_history_to_database,
38
+ view_database as sqlite_view_database,
39
+ get_transcripts as sqlite_get_transcripts,
40
+ get_trashed_items as sqlite_get_trashed_items,
41
+ user_delete_item as sqlite_user_delete_item,
42
+ empty_trash as sqlite_empty_trash,
43
+ create_automated_backup as sqlite_create_automated_backup,
44
+ add_or_update_prompt as sqlite_add_or_update_prompt,
45
+ load_prompt_details as sqlite_load_prompt_details,
46
+ load_preset_prompts as sqlite_load_preset_prompts,
47
+ insert_prompt_to_db as sqlite_insert_prompt_to_db,
48
+ delete_prompt as sqlite_delete_prompt,
49
+ search_and_display_items as sqlite_search_and_display_items,
50
+ get_conversation_name as sqlite_get_conversation_name,
51
+ add_media_with_keywords as sqlite_add_media_with_keywords,
52
+ check_media_and_whisper_model as sqlite_check_media_and_whisper_model,
53
+ DatabaseError, create_document_version as sqlite_create_document_version,
54
+ get_document_version as sqlite_get_document_version, sqlite_search_db, sqlite_add_media_chunk,
55
+ sqlite_update_fts_for_media, sqlite_get_unprocessed_media, fetch_item_details as sqlite_fetch_item_details, \
56
+ search_media_database as sqlite_search_media_database, mark_as_trash as sqlite_mark_as_trash, \
57
+ get_media_transcripts as sqlite_get_media_transcripts, get_specific_transcript as sqlite_get_specific_transcript, \
58
+ get_media_summaries as sqlite_get_media_summaries, get_specific_summary as sqlite_get_specific_summary, \
59
+ get_media_prompts as sqlite_get_media_prompts, get_specific_prompt as sqlite_get_specific_prompt, \
60
+ delete_specific_transcript as sqlite_delete_specific_transcript, delete_specific_summary as sqlite_delete_specific_summary, \
61
+ delete_specific_prompt as sqlite_delete_specific_prompt, fetch_keywords_for_media as sqlite_fetch_keywords_for_media, \
62
+ update_keywords_for_media as sqlite_update_keywords_for_media, check_media_exists as sqlite_check_media_exists, \
63
+ search_prompts as sqlite_search_prompts, get_media_content as sqlite_get_media_content, \
64
+ get_paginated_files as sqlite_get_paginated_files, get_media_title as sqlite_get_media_title, \
65
+ get_all_content_from_database as sqlite_get_all_content_from_database,
66
+ )
67
+ #
68
+ # Local Imports
69
+ from App_Function_Libraries.Utils.Utils import load_comprehensive_config, get_database_path, get_project_relative_path
70
+ #
71
+ # End of imports
72
+ ############################################################################################################
73
+ #
74
+ # Globals
75
+
76
+ # Load configuration from config file
77
+ config_path = get_project_relative_path('Config_Files/config.txt')
78
+ config = configparser.ConfigParser()
79
+ config.read(config_path)
80
+
81
+ db_path: str = config.get('Database', 'sqlite_path', fallback='./Databases/media_summary.db')
82
+
83
+ backup_path: str = config.get('Database', 'backup_path', fallback='database_backups')
84
+
85
+ backup_dir: Union[str, bytes] = os.environ.get('DB_BACKUP_DIR', backup_path)
86
+
87
+ #
88
+ # End of Globals
89
+ ############################################################################################################
90
+ #
91
+ # Database Manager Class
92
+
93
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
94
+ logger = logging.getLogger(__name__)
95
+
96
+
97
+ class Database:
98
+ def __init__(self, db_name='media_summary.db'):
99
+ self.db_path = get_database_path(db_name)
100
+ self.pool = []
101
+ self.pool_size = 10
102
+ self.lock = threading.Lock()
103
+ self.timeout = 60.0 # 60 seconds timeout
104
+
105
+ @contextmanager
106
+ def get_connection(self):
107
+ retry_count = 5
108
+ retry_delay = 1
109
+ while retry_count > 0:
110
+ try:
111
+ if self.pool:
112
+ conn = self.pool.pop()
113
+ else:
114
+ conn = sqlite3.connect(self.db_path, timeout=self.timeout, check_same_thread=False)
115
+ conn.execute("PRAGMA journal_mode=WAL;") # Enable WAL mode
116
+ yield conn
117
+ self.pool.append(conn)
118
+ return
119
+ except sqlite3.OperationalError as e:
120
+ if 'database is locked' in str(e):
121
+ logger.warning(f"Database is locked, retrying in {retry_delay} seconds...")
122
+ retry_count -= 1
123
+ time.sleep(retry_delay)
124
+ retry_delay *= 2 # Exponential backoff
125
+ else:
126
+ raise DatabaseError(f"Database error: {e}")
127
+ except Exception as e:
128
+ raise DatabaseError(f"Unexpected error: {e}")
129
+ raise DatabaseError("Database is locked and retries have been exhausted")
130
+
131
+ def execute_query(self, query: str, params: Tuple = ()) -> None:
132
+ with self.lock: # Use a global lock for write operations
133
+ with self.get_connection() as conn:
134
+ try:
135
+ cursor = conn.cursor()
136
+ cursor.execute(query, params)
137
+ conn.commit()
138
+ except sqlite3.Error as e:
139
+ logger.error(f"Database error: {e}, Query: {query}")
140
+ raise DatabaseError(f"Database error: {e}, Query: {query}")
141
+
142
+ def execute_many(self, query: str, params_list: List[Tuple]) -> None:
143
+ with self.lock: # Use a global lock for write operations
144
+ with self.get_connection() as conn:
145
+ try:
146
+ cursor = conn.cursor()
147
+ cursor.executemany(query, params_list)
148
+ conn.commit()
149
+ except sqlite3.Error as e:
150
+ logger.error(f"Database error: {e}, Query: {query}")
151
+ raise DatabaseError(f"Database error: {e}, Query: {query}")
152
+
153
+ def close_all_connections(self):
154
+ for conn in self.pool:
155
+ conn.close()
156
+ self.pool.clear()
157
+
158
+
159
+ #
160
+ # class Database:
161
+ # def __init__(self, db_name='media_summary.db'):
162
+ # self.db_path = get_database_path(db_name)
163
+ # self.pool = []
164
+ # self.pool_size = 10
165
+ #
166
+ # @contextmanager
167
+ # def get_connection(self):
168
+ # retry_count = 5
169
+ # retry_delay = 1
170
+ # while retry_count > 0:
171
+ # try:
172
+ # if self.pool:
173
+ # conn = self.pool.pop()
174
+ # else:
175
+ # conn = sqlite3.connect(self.db_path, check_same_thread=False)
176
+ # yield conn
177
+ # self.pool.append(conn)
178
+ # return
179
+ # except sqlite3.OperationalError as e:
180
+ # if 'database is locked' in str(e):
181
+ # logger.warning(f"Database is locked, retrying in {retry_delay} seconds...")
182
+ # retry_count -= 1
183
+ # time.sleep(retry_delay)
184
+ # retry_delay *= 2 # Exponential backoff
185
+ # else:
186
+ # raise DatabaseError(f"Database error: {e}")
187
+ # except Exception as e:
188
+ # raise DatabaseError(f"Unexpected error: {e}")
189
+ # raise DatabaseError("Database is locked and retries have been exhausted")
190
+ #
191
+ # def execute_query(self, query: str, params: Tuple = ()) -> None:
192
+ # with self.get_connection() as conn:
193
+ # try:
194
+ # cursor = conn.cursor()
195
+ # cursor.execute(query, params)
196
+ # conn.commit()
197
+ # except sqlite3.Error as e:
198
+ # logger.error(f"Database error: {e}, Query: {query}")
199
+ # raise DatabaseError(f"Database error: {e}, Query: {query}")
200
+ #
201
+ # def close_all_connections(self):
202
+ # for conn in self.pool:
203
+ # conn.close()
204
+ # self.pool.clear()
205
+ #
206
+
207
+ #
208
+ # End of Database Manager Class
209
+ ############################################################################################################
210
+ #
211
+ # Database Config loading
212
+
213
+ def get_db_config():
214
+ try:
215
+ config = load_comprehensive_config()
216
+
217
+ if 'Database' not in config:
218
+ print("Warning: 'Database' section not found in config. Using default values.")
219
+ return default_db_config()
220
+
221
+ return {
222
+ 'type': config.get('Database', 'type', fallback='sqlite'),
223
+ 'sqlite_path': config.get('Database', 'sqlite_path', fallback='Databases/media_summary.db'),
224
+ 'elasticsearch_host': config.get('Database', 'elasticsearch_host', fallback='localhost'),
225
+ 'elasticsearch_port': config.getint('Database', 'elasticsearch_port', fallback=9200)
226
+ }
227
+ except FileNotFoundError:
228
+ print("Warning: Config file not found. Using default database configuration.")
229
+ return default_db_config()
230
+ except Exception as e:
231
+ print(f"Error reading config: {str(e)}. Using default database configuration.")
232
+ return default_db_config()
233
+
234
+
235
+ def default_db_config():
236
+ """Return the default database configuration with project-relative paths."""
237
+ return {
238
+ 'type': 'sqlite',
239
+ 'sqlite_path': get_database_path('media_summary.db'),
240
+ 'elasticsearch_host': 'localhost',
241
+ 'elasticsearch_port': 9200
242
+ }
243
+
244
+
245
+ def ensure_directory_exists(file_path):
246
+ directory = os.path.dirname(file_path)
247
+ if not os.path.exists(directory):
248
+ os.makedirs(directory)
249
+ print(f"Created directory: {directory}")
250
+
251
+ # Use the config to set up the database
252
+ db_config = get_db_config()
253
+ db_type = db_config['type']
254
+
255
+ if db_type == 'sqlite':
256
+ db = Database(os.path.basename(db_config['sqlite_path']))
257
+ elif db_type == 'elasticsearch':
258
+ # Implement Elasticsearch setup here if needed
259
+ raise NotImplementedError("Elasticsearch support not yet implemented")
260
+ else:
261
+ raise ValueError(f"Unsupported database type: {db_type}")
262
+
263
+ # Print database path for debugging
264
+ print(f"Database path: {db.db_path}")
265
+
266
+ # Sanity Check for SQLite DB
267
+ # FIXME - Remove this after testing / Writing Unit tests
268
+ # try:
269
+ # db.execute_query("CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY)")
270
+ # logger.info("Successfully created test table")
271
+ # except DatabaseError as e:
272
+ # logger.error(f"Failed to create test table: {e}")
273
+
274
+ #
275
+ # End of Database Config loading
276
+ ############################################################################################################
277
+ #
278
+ # DB Search functions
279
+
280
+ def search_db(search_query: str, search_fields: List[str], keywords: str, page: int = 1, results_per_page: int = 10):
281
+ if db_type == 'sqlite':
282
+ return sqlite_search_db(search_query, search_fields, keywords, page, results_per_page)
283
+ elif db_type == 'elasticsearch':
284
+ # Implement Elasticsearch version when available
285
+ raise NotImplementedError("Elasticsearch version of search_db not yet implemented")
286
+ else:
287
+ raise ValueError(f"Unsupported database type: {db_type}")
288
+
289
+ def view_database(*args, **kwargs):
290
+ if db_type == 'sqlite':
291
+ return sqlite_view_database(*args, **kwargs)
292
+ elif db_type == 'elasticsearch':
293
+ # Implement Elasticsearch version
294
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
295
+
296
+ def search_and_display_items(*args, **kwargs):
297
+ if db_type == 'sqlite':
298
+ return sqlite_search_and_display_items(*args, **kwargs)
299
+ elif db_type == 'elasticsearch':
300
+ # Implement Elasticsearch version
301
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
302
+
303
+ def get_all_content_from_database():
304
+ if db_type == 'sqlite':
305
+ return sqlite_get_all_content_from_database()
306
+ elif db_type == 'elasticsearch':
307
+ # Implement Elasticsearch version
308
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
309
+
310
+ def search_and_display(*args, **kwargs):
311
+ if db_type == 'sqlite':
312
+ return sqlite_search_and_display(*args, **kwargs)
313
+ elif db_type == 'elasticsearch':
314
+ # Implement Elasticsearch version
315
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
316
+
317
+ def check_media_exists(*args, **kwargs):
318
+ if db_type == 'sqlite':
319
+ return sqlite_check_media_exists(*args, **kwargs)
320
+ elif db_type == 'elasticsearch':
321
+ # Implement Elasticsearch version
322
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
323
+
324
+ def get_paginated_files(*args, **kwargs):
325
+ if db_type == 'sqlite':
326
+ return sqlite_get_paginated_files(*args, **kwargs)
327
+ elif db_type == 'elasticsearch':
328
+ # Implement Elasticsearch version
329
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
330
+
331
+ def get_media_title(*args, **kwargs):
332
+ if db_type == 'sqlite':
333
+ return sqlite_get_media_title(*args, **kwargs)
334
+ elif db_type == 'elasticsearch':
335
+ # Implement Elasticsearch version
336
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
337
+
338
+
339
+ #
340
+ # End of DB-Searching functions
341
+ ############################################################################################################
342
+
343
+
344
+ ############################################################################################################
345
+ #
346
+ # Transcript-related Functions
347
+
348
+ def get_transcripts(*args, **kwargs):
349
+ if db_type == 'sqlite':
350
+ return sqlite_get_transcripts(*args, **kwargs)
351
+ elif db_type == 'elasticsearch':
352
+ # Implement Elasticsearch version
353
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
354
+
355
+ #
356
+ # End of Transcript-related Functions
357
+ ############################################################################################################
358
+
359
+
360
+ ############################################################################################################
361
+ #
362
+ # DB-Ingestion functions
363
+
364
+ def add_media_to_database(*args, **kwargs):
365
+ if db_type == 'sqlite':
366
+ result = sqlite_add_media_to_database(*args, **kwargs)
367
+
368
+ # Extract content
369
+ segments = args[2]
370
+ if isinstance(segments, list):
371
+ content = ' '.join([segment.get('Text', '') for segment in segments if 'Text' in segment])
372
+ elif isinstance(segments, dict):
373
+ content = segments.get('text', '') or segments.get('content', '')
374
+ else:
375
+ content = str(segments)
376
+
377
+ # Extract media_id from the result
378
+ # Assuming the result is in the format "Media 'Title' added/updated successfully with ID: {media_id}"
379
+ import re
380
+ match = re.search(r"with ID: (\d+)", result)
381
+ if match:
382
+ media_id = int(match.group(1))
383
+
384
+ # Create initial document version
385
+ sqlite_create_document_version(media_id, content)
386
+
387
+ return result
388
+ elif db_type == 'elasticsearch':
389
+ # Implement Elasticsearch version
390
+ raise NotImplementedError("Elasticsearch version of add_media_to_database not yet implemented")
391
+
392
+
393
+ def import_obsidian_note_to_db(*args, **kwargs):
394
+ if db_type == 'sqlite':
395
+ return sqlite_import_obsidian_note_to_db(*args, **kwargs)
396
+ elif db_type == 'elasticsearch':
397
+ # Implement Elasticsearch version
398
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
399
+
400
+
401
+ def update_media_content(*args, **kwargs):
402
+ if db_type == 'sqlite':
403
+ result = sqlite_update_media_content(*args, **kwargs)
404
+
405
+ # Extract media_id and content
406
+ selected_item = args[0]
407
+ item_mapping = args[1]
408
+ content_input = args[2]
409
+
410
+ if selected_item and item_mapping and selected_item in item_mapping:
411
+ media_id = item_mapping[selected_item]
412
+
413
+ # Create new document version
414
+ sqlite_create_document_version(media_id, content_input)
415
+
416
+ return result
417
+ elif db_type == 'elasticsearch':
418
+ # Implement Elasticsearch version
419
+ raise NotImplementedError("Elasticsearch version of update_media_content not yet implemented")
420
+
421
+
422
+ def add_media_with_keywords(*args, **kwargs):
423
+ if db_type == 'sqlite':
424
+ return sqlite_add_media_with_keywords(*args, **kwargs)
425
+ elif db_type == 'elasticsearch':
426
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
427
+
428
+ def check_media_and_whisper_model(*args, **kwargs):
429
+ if db_type == 'sqlite':
430
+ return sqlite_check_media_and_whisper_model(*args, **kwargs)
431
+ elif db_type == 'elasticsearch':
432
+ raise NotImplementedError("Elasticsearch version of check_media_and_whisper_model not yet implemented")
433
+
434
+ def ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt):
435
+ if db_type == 'sqlite':
436
+ return sqlite_ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt)
437
+ elif db_type == 'elasticsearch':
438
+ # Implement Elasticsearch version
439
+ raise NotImplementedError("Elasticsearch version of ingest_article_to_db not yet implemented")
440
+ else:
441
+ raise ValueError(f"Unsupported database type: {db_type}")
442
+
443
+
444
+ def add_media_chunk(media_id: int, chunk_text: str, start_index: int, end_index: int, chunk_id: str):
445
+ if db_type == 'sqlite':
446
+ sqlite_add_media_chunk(db, media_id, chunk_text, start_index, end_index, chunk_id)
447
+ elif db_type == 'elasticsearch':
448
+ # Implement Elasticsearch version
449
+ raise NotImplementedError("Elasticsearch version not yet implemented")
450
+ else:
451
+ raise ValueError(f"Unsupported database type: {db_type}")
452
+
453
+ def update_fts_for_media(media_id: int):
454
+ if db_type == 'sqlite':
455
+ sqlite_update_fts_for_media(db, media_id)
456
+ elif db_type == 'elasticsearch':
457
+ # Implement Elasticsearch version
458
+ raise NotImplementedError("Elasticsearch version not yet implemented")
459
+ else:
460
+ raise ValueError(f"Unsupported database type: {db_type}")
461
+
462
+
463
+ def get_unprocessed_media():
464
+ if db_type == 'sqlite':
465
+ return sqlite_get_unprocessed_media(db)
466
+ elif db_type == 'elasticsearch':
467
+ # Implement Elasticsearch version
468
+ raise NotImplementedError("Elasticsearch version of get_unprocessed_media not yet implemented")
469
+ else:
470
+ raise ValueError(f"Unsupported database type: {db_type}")
471
+
472
+
473
+ #
474
+ # End of DB-Ingestion functions
475
+ ############################################################################################################
476
+
477
+
478
+ ############################################################################################################
479
+ #
480
+ # Prompt-related functions #FIXME rename /resort
481
+
482
+ def list_prompts(*args, **kwargs):
483
+ if db_type == 'sqlite':
484
+ return sqlite_list_prompts(*args, **kwargs)
485
+ elif db_type == 'elasticsearch':
486
+ # Implement Elasticsearch version
487
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
488
+
489
+ def search_prompts(query):
490
+ if db_type == 'sqlite':
491
+ return sqlite_search_prompts(query)
492
+ elif db_type == 'elasticsearch':
493
+ # Implement Elasticsearch version
494
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
495
+
496
+ def fetch_prompt_details(*args, **kwargs):
497
+ if db_type == 'sqlite':
498
+ return sqlite_fetch_prompt_details(*args, **kwargs)
499
+ elif db_type == 'elasticsearch':
500
+ # Implement Elasticsearch version
501
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
502
+
503
+ def add_prompt(*args, **kwargs):
504
+ if db_type == 'sqlite':
505
+ return sqlite_add_prompt(*args, **kwargs)
506
+ elif db_type == 'elasticsearch':
507
+ # Implement Elasticsearch version
508
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
509
+
510
+
511
+ def add_or_update_prompt(*args, **kwargs):
512
+ if db_type == 'sqlite':
513
+ return sqlite_add_or_update_prompt(*args, **kwargs)
514
+ elif db_type == 'elasticsearch':
515
+ # Implement Elasticsearch version
516
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
517
+
518
+ def load_prompt_details(*args, **kwargs):
519
+ if db_type == 'sqlite':
520
+ return sqlite_load_prompt_details(*args, **kwargs)
521
+ elif db_type == 'elasticsearch':
522
+ # Implement Elasticsearch version
523
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
524
+
525
+ def load_preset_prompts(*args, **kwargs):
526
+ if db_type == 'sqlite':
527
+ return sqlite_load_preset_prompts()
528
+ elif db_type == 'elasticsearch':
529
+ # Implement Elasticsearch version
530
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
531
+
532
+ def insert_prompt_to_db(*args, **kwargs):
533
+ if db_type == 'sqlite':
534
+ return sqlite_insert_prompt_to_db(*args, **kwargs)
535
+ elif db_type == 'elasticsearch':
536
+ # Implement Elasticsearch version
537
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
538
+
539
+ def delete_prompt(*args, **kwargs):
540
+ if db_type == 'sqlite':
541
+ return sqlite_delete_prompt(*args, **kwargs)
542
+ elif db_type == 'elasticsearch':
543
+ # Implement Elasticsearch version
544
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
545
+
546
+ def search_media_database(query: str) -> List[Tuple[int, str, str]]:
547
+ if db_type == 'sqlite':
548
+ return sqlite_search_media_database(query)
549
+ elif db_type == 'elasticsearch':
550
+ # Implement Elasticsearch version when available
551
+ raise NotImplementedError("Elasticsearch version of search_media_database not yet implemented")
552
+ else:
553
+ raise ValueError(f"Unsupported database type: {db_type}")
554
+
555
+ def mark_as_trash(media_id: int) -> None:
556
+ if db_type == 'sqlite':
557
+ return sqlite_mark_as_trash(media_id)
558
+ elif db_type == 'elasticsearch':
559
+ # Implement Elasticsearch version when available
560
+ raise NotImplementedError("Elasticsearch version of mark_as_trash not yet implemented")
561
+ else:
562
+ raise ValueError(f"Unsupported database type: {db_type}")
563
+
564
+ def get_media_content(media_id: int) -> str:
565
+ if db_type == 'sqlite':
566
+ return sqlite_get_media_content(media_id)
567
+ elif db_type == 'elasticsearch':
568
+ raise NotImplementedError("Elasticsearch version of get_media_content not yet implemented")
569
+ else:
570
+ raise ValueError(f"Unsupported database type: {db_type}")
571
+
572
+ def get_media_transcripts(media_id: int) -> List[Dict]:
573
+ if db_type == 'sqlite':
574
+ return sqlite_get_media_transcripts(media_id)
575
+ elif db_type == 'elasticsearch':
576
+ raise NotImplementedError("Elasticsearch version of get_media_transcripts not yet implemented")
577
+ else:
578
+ raise ValueError(f"Unsupported database type: {db_type}")
579
+
580
+ def get_specific_transcript(transcript_id: int) -> Dict:
581
+ if db_type == 'sqlite':
582
+ return sqlite_get_specific_transcript(transcript_id)
583
+ elif db_type == 'elasticsearch':
584
+ raise NotImplementedError("Elasticsearch version of get_specific_transcript not yet implemented")
585
+ else:
586
+ raise ValueError(f"Unsupported database type: {db_type}")
587
+
588
+ def get_media_summaries(media_id: int) -> List[Dict]:
589
+ if db_type == 'sqlite':
590
+ return sqlite_get_media_summaries(media_id)
591
+ elif db_type == 'elasticsearch':
592
+ raise NotImplementedError("Elasticsearch version of get_media_summaries not yet implemented")
593
+ else:
594
+ raise ValueError(f"Unsupported database type: {db_type}")
595
+
596
+ def get_specific_summary(summary_id: int) -> Dict:
597
+ if db_type == 'sqlite':
598
+ return sqlite_get_specific_summary(summary_id)
599
+ elif db_type == 'elasticsearch':
600
+ raise NotImplementedError("Elasticsearch version of get_specific_summary not yet implemented")
601
+ else:
602
+ raise ValueError(f"Unsupported database type: {db_type}")
603
+
604
+ def get_media_prompts(media_id: int) -> List[Dict]:
605
+ if db_type == 'sqlite':
606
+ return sqlite_get_media_prompts(media_id)
607
+ elif db_type == 'elasticsearch':
608
+ raise NotImplementedError("Elasticsearch version of get_media_prompts not yet implemented")
609
+ else:
610
+ raise ValueError(f"Unsupported database type: {db_type}")
611
+
612
+ def get_specific_prompt(prompt_id: int) -> Dict:
613
+ if db_type == 'sqlite':
614
+ return sqlite_get_specific_prompt(prompt_id)
615
+ elif db_type == 'elasticsearch':
616
+ raise NotImplementedError("Elasticsearch version of get_specific_prompt not yet implemented")
617
+ else:
618
+ return {'error': f"Unsupported database type: {db_type}"}
619
+
620
+ def delete_specific_transcript(transcript_id: int) -> str:
621
+ if db_type == 'sqlite':
622
+ return sqlite_delete_specific_transcript(transcript_id)
623
+ elif db_type == 'elasticsearch':
624
+ raise NotImplementedError("Elasticsearch version of delete_specific_transcript not yet implemented")
625
+ else:
626
+ raise ValueError(f"Unsupported database type: {db_type}")
627
+
628
+ def delete_specific_summary(summary_id: int) -> str:
629
+ if db_type == 'sqlite':
630
+ return sqlite_delete_specific_summary(summary_id)
631
+ elif db_type == 'elasticsearch':
632
+ raise NotImplementedError("Elasticsearch version of delete_specific_summary not yet implemented")
633
+ else:
634
+ raise ValueError(f"Unsupported database type: {db_type}")
635
+
636
+ def delete_specific_prompt(prompt_id: int) -> str:
637
+ if db_type == 'sqlite':
638
+ return sqlite_delete_specific_prompt(prompt_id)
639
+ elif db_type == 'elasticsearch':
640
+ raise NotImplementedError("Elasticsearch version of delete_specific_prompt not yet implemented")
641
+ else:
642
+ raise ValueError(f"Unsupported database type: {db_type}")
643
+
644
+
645
+ #
646
+ # End of Prompt-related functions
647
+ ############################################################################################################
648
+
649
+ ############################################################################################################
650
+ #
651
+ # Keywords-related Functions
652
+
653
+ def keywords_browser_interface(*args, **kwargs):
654
+ if db_type == 'sqlite':
655
+ return sqlite_keywords_browser_interface()
656
+ elif db_type == 'elasticsearch':
657
+ # Implement Elasticsearch version
658
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
659
+
660
+ def add_keyword(*args, **kwargs):
661
+ if db_type == 'sqlite':
662
+ with db.get_connection() as conn:
663
+ cursor = conn.cursor()
664
+ return sqlite_add_keyword(*args, **kwargs)
665
+ elif db_type == 'elasticsearch':
666
+ # Implement Elasticsearch version
667
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
668
+
669
+ def delete_keyword(*args, **kwargs):
670
+ if db_type == 'sqlite':
671
+ return sqlite_delete_keyword(*args, **kwargs)
672
+ elif db_type == 'elasticsearch':
673
+ # Implement Elasticsearch version
674
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
675
+
676
+ def export_keywords_to_csv(*args, **kwargs):
677
+ if db_type == 'sqlite':
678
+ return sqlite_export_keywords_to_csv()
679
+ elif db_type == 'elasticsearch':
680
+ # Implement Elasticsearch version
681
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
682
+
683
+ def update_keywords_for_media(*args, **kwargs):
684
+ if db_type == 'sqlite':
685
+ return sqlite_update_keywords_for_media(*args, **kwargs)
686
+ elif db_type == 'elasticsearch':
687
+ # Implement Elasticsearch version
688
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
689
+
690
+ def fetch_keywords_for_media(*args, **kwargs):
691
+ if db_type == 'sqlite':
692
+ return sqlite_fetch_keywords_for_media(*args, **kwargs)
693
+ elif db_type == 'elasticsearch':
694
+ # Implement Elasticsearch version
695
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
696
+
697
+ #
698
+ # End of Keywords-related Functions
699
+ ############################################################################################################
700
+
701
+ ############################################################################################################
702
+ #
703
+ # Chat-related Functions
704
+
705
+ def delete_chat_message(*args, **kwargs):
706
+ if db_type == 'sqlite':
707
+ return sqlite_delete_chat_message(*args, **kwargs)
708
+ elif db_type == 'elasticsearch':
709
+ # Implement Elasticsearch version
710
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
711
+
712
+ def update_chat_message(*args, **kwargs):
713
+ if db_type == 'sqlite':
714
+ return sqlite_update_chat_message(*args, **kwargs)
715
+ elif db_type == 'elasticsearch':
716
+ # Implement Elasticsearch version
717
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
718
+
719
+ def add_chat_message(*args, **kwargs):
720
+ if db_type == 'sqlite':
721
+ return sqlite_add_chat_message(*args, **kwargs)
722
+ elif db_type == 'elasticsearch':
723
+ # Implement Elasticsearch version
724
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
725
+
726
+ def get_chat_messages(*args, **kwargs):
727
+ if db_type == 'sqlite':
728
+ return sqlite_get_chat_messages(*args, **kwargs)
729
+ elif db_type == 'elasticsearch':
730
+ # Implement Elasticsearch version
731
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
732
+
733
+ def search_chat_conversations(*args, **kwargs):
734
+ if db_type == 'sqlite':
735
+ return sqlite_search_chat_conversations(*args, **kwargs)
736
+ elif db_type == 'elasticsearch':
737
+ # Implement Elasticsearch version
738
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
739
+
740
+ def create_chat_conversation(*args, **kwargs):
741
+ if db_type == 'sqlite':
742
+ return sqlite_create_chat_conversation(*args, **kwargs)
743
+ elif db_type == 'elasticsearch':
744
+ # Implement Elasticsearch version
745
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
746
+
747
+ def save_chat_history_to_database(*args, **kwargs):
748
+ if db_type == 'sqlite':
749
+ return sqlite_save_chat_history_to_database(*args, **kwargs)
750
+ elif db_type == 'elasticsearch':
751
+ # Implement Elasticsearch version
752
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
753
+
754
+ def get_conversation_name(*args, **kwargs):
755
+ if db_type == 'sqlite':
756
+ return sqlite_get_conversation_name(*args, **kwargs)
757
+ elif db_type == 'elasticsearch':
758
+ # Implement Elasticsearch version
759
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
760
+
761
+ #
762
+ # End of Chat-related Functions
763
+ ############################################################################################################
764
+
765
+ ############################################################################################################
766
+ #
767
+ # Trash-related Functions
768
+
769
+ def get_trashed_items(*args, **kwargs):
770
+ if db_type == 'sqlite':
771
+ return sqlite_get_trashed_items()
772
+ elif db_type == 'elasticsearch':
773
+ # Implement Elasticsearch version
774
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
775
+
776
+ def user_delete_item(*args, **kwargs):
777
+ if db_type == 'sqlite':
778
+ return sqlite_user_delete_item(*args, **kwargs)
779
+ elif db_type == 'elasticsearch':
780
+ # Implement Elasticsearch version
781
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
782
+
783
+ def empty_trash(*args, **kwargs):
784
+ if db_type == 'sqlite':
785
+ return sqlite_empty_trash(*args, **kwargs)
786
+ elif db_type == 'elasticsearch':
787
+ # Implement Elasticsearch version
788
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
789
+
790
+
791
+ def fetch_item_details(media_id: int) -> Tuple[str, str, str]:
792
+ """
793
+ Fetch the details of a media item including content, prompt, and summary.
794
+
795
+ Args:
796
+ media_id (int): The ID of the media item.
797
+
798
+ Returns:
799
+ Tuple[str, str, str]: A tuple containing (content, prompt, summary).
800
+ If an error occurs, it returns empty strings for each field.
801
+ """
802
+ if db_type == 'sqlite':
803
+ return sqlite_fetch_item_details(media_id)
804
+ elif db_type == 'elasticsearch':
805
+ # Implement Elasticsearch version when available
806
+ raise NotImplementedError("Elasticsearch version of fetch_item_details not yet implemented")
807
+ else:
808
+ raise ValueError(f"Unsupported database type: {db_type}")
809
+
810
+ #
811
+ # End of Trash-related Functions
812
+ ############################################################################################################
813
+
814
+
815
+ ############################################################################################################
816
+ #
817
+ # DB-Backup Functions
818
+
819
+ def create_automated_backup(*args, **kwargs):
820
+ if db_type == 'sqlite':
821
+ return sqlite_create_automated_backup(*args, **kwargs)
822
+ elif db_type == 'elasticsearch':
823
+ # Implement Elasticsearch version
824
+ raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
825
+
826
+ #
827
+ # End of DB-Backup Functions
828
+ ############################################################################################################
829
+
830
+
831
+ ############################################################################################################
832
+ #
833
+ # Document Versioning Functions
834
+
835
+ def create_document_version(*args, **kwargs):
836
+ if db_type == 'sqlite':
837
+ return sqlite_create_document_version(*args, **kwargs)
838
+ elif db_type == 'elasticsearch':
839
+ # Implement Elasticsearch version
840
+ raise NotImplementedError("Elasticsearch version of create_document_version not yet implemented")
841
+
842
+ def get_document_version(*args, **kwargs):
843
+ if db_type == 'sqlite':
844
+ return sqlite_get_document_version(*args, **kwargs)
845
+ elif db_type == 'elasticsearch':
846
+ # Implement Elasticsearch version
847
+ raise NotImplementedError("Elasticsearch version of get_document_version not yet implemented")
848
+
849
+ #
850
+ # End of Document Versioning Functions
851
+ ############################################################################################################
852
+
853
+
854
+
855
+ ############################################################################################################
856
+ #
857
+ # Function to close the database connection for SQLite
858
+
859
+ def close_connection():
860
+ if db_type == 'sqlite':
861
+ db.close_all_connections()
862
+ # Elasticsearch doesn't need explicit closing
863
+
864
+ #
865
+ # End of file
866
+ ############################################################################################################
App_Function_Libraries/DB/SQLite_DB.py CHANGED
The diff for this file is too large to render. See raw diff