-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdb_handler.py
More file actions
342 lines (293 loc) · 11.9 KB
/
Copy pathdb_handler.py
File metadata and controls
342 lines (293 loc) · 11.9 KB
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
import sqlite3
class DBOperation:
def __init__(self, db_name="stewie_database.db"):
self.db_name = db_name
self.create_dialouge_stage_table()
def connect(self):
return sqlite3.connect(self.db_name)
def create_dialouge_stage_table(self):
query = """
CREATE TABLE IF NOT EXISTS dialouge_stage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sentence TEXT NOT NULL,
character TEXT,
image TEXT,
image_search TEXT,
audio_processed INTEGER DEFAULT 0,
audio_process_retry INTEGER DEFAULT 0
);
"""
try:
conn = self.connect()
cursor = conn.cursor()
cursor.execute(query)
conn.commit()
print("Table 'dialouge_stage' is ready.")
except sqlite3.Error as e:
print(f"SQLite error during table creation: {e}")
finally:
conn.close()
def add_dialogues(self, dialogues):
"""
Adds a list of dialogues to the dialouge_stage table.
Each dialogue should be a dictionary with keys:
- sentence
- character
- image
- image_search
- audio_processed (default 0)
- audio_process_retry (default 0)
"""
try:
conn = self.connect()
cursor = conn.cursor()
# Prepare insert query
query = """
INSERT INTO dialouge_stage (sentence, character, image, image_search, audio_processed, audio_process_retry)
VALUES (?, ?, ?, ?, ?, ?);
"""
# Insert each dialogue in the list
for dialogue in dialogues:
sentence = dialogue.get("dialogue")
character = dialogue.get("character", None)
image = dialogue.get("image", None)
image_search = dialogue.get("image_search", None)
audio_processed = dialogue.get("audio_processed", 0) # Default to 0 if not provided
audio_process_retry = dialogue.get("audio_process_retry", 0) # Default to 0 if not provided
cursor.execute(query, (sentence, character, image, image_search, audio_processed, audio_process_retry))
conn.commit()
print(f"Successfully added {len(dialogues)} dialogues.")
except sqlite3.Error as e:
print(f"SQLite error during insertion: {e}")
finally:
conn.close()
def get_stage_and_unprocessed_dialogues(self):
"""
Returns stage and up to 3 unprocessed dialogues (if exist):
{
"stage": 0 → table empty
1 → unprocessed dialogues exist
2 → table has data, but no eligible dialogues
"dialogues": [...] or None
}
"""
try:
conn = self.connect()
cursor = conn.cursor()
# Check if table is empty
cursor.execute("SELECT COUNT(*) FROM dialouge_stage;")
total_rows = cursor.fetchone()[0]
if total_rows == 0:
return {"stage": 0, "dialogues": None}
# Try to fetch up to 3 unprocessed dialogues
cursor.execute("""
SELECT id, sentence, character, image, image_search, audio_processed, audio_process_retry
FROM dialouge_stage
WHERE audio_processed = 0 AND audio_process_retry < 5
ORDER BY id ASC
LIMIT 3;
""")
rows = cursor.fetchall()
if rows:
dialogues = []
for row in rows:
dialogues.append({
"id": row[0],
"sentence": row[1],
"character": row[2],
"image": row[3],
"image_search": row[4],
"audio_processed": row[5],
"audio_process_retry": row[6]
})
return {"stage": 1, "dialogues": dialogues}
# Table has data, but no unprocessed dialogue
return {"stage": 2, "dialogues": None}
except sqlite3.Error as e:
print(f"SQLite error: {e}")
return {"stage": -1, "dialogues": None} # Error flag
finally:
conn.close()
def get_raedy_assests(self):
try:
conn = self.connect()
cursor = conn.cursor()
# Check if table is empty
cursor.execute("SELECT COUNT(*) FROM dialouge_stage;")
total_rows = cursor.fetchone()[0]
if total_rows == 0:
None
# Try to fetch up to 3 unprocessed dialogues
cursor.execute("""
SELECT id, sentence, character, image, image_search, audio_processed, audio_process_retry
FROM dialouge_stage
WHERE audio_processed = 1 ORDER BY id ASC;
""")
rows = cursor.fetchall()
if rows:
dialogues = []
for row in rows:
dialogues.append({
"id": row[0],
"sentence": row[1],
"character": row[2],
"image": row[3],
"image_search": row[4],
"audio_processed": row[5],
"audio_process_retry": row[6]
})
return dialogues
# Table has data, but no unprocessed dialogue
return None
except sqlite3.Error as e:
print(f"SQLite error: {e}")
return {"stage": -1, "dialogues": None} # Error flag
finally:
conn.close()
def mark_processed(self, dialogue_id, flag):
"""
Marks a dialogue as processed based on the flag:
If flag is True, set audio_processed to 1 and increment audio_process_retry.
If flag is False, just increment audio_process_retry.
"""
try:
conn = self.connect()
cursor = conn.cursor()
if flag:
# If flag is True, set audio_processed to 1 and increment retry count
cursor.execute("""
UPDATE dialouge_stage
SET audio_processed = 1, audio_process_retry = audio_process_retry + 1
WHERE id = ?;
""", (dialogue_id,))
else:
# If flag is False, just increment retry count
cursor.execute("""
UPDATE dialouge_stage
SET audio_process_retry = audio_process_retry + 1
WHERE id = ?;
""", (dialogue_id,))
conn.commit()
print(f"Dialogue with ID {dialogue_id} has been updated.")
except sqlite3.Error as e:
print(f"SQLite error during update: {e}")
finally:
conn.close()
def show_all_dialogues(self):
"""
Fetches all dialogues from the dialouge_stage table and prints them in a neat format.
"""
try:
conn = self.connect()
cursor = conn.cursor()
# Fetch all rows from the dialouge_stage table
cursor.execute("SELECT id, sentence, character, image, image_search, audio_processed, audio_process_retry FROM dialouge_stage;")
rows = cursor.fetchall()
# Check if the table is empty
if not rows:
print("No dialogues found in the database.")
return
# Print the table headers
print(f"{'ID':<5} {'Sentence':<30} {'Character':<15} {'Image':<20} {'Image Search':<20} {'Audio Processed':<15} {'Retry Count':<10}")
print("-" * 120)
# Print each row
for row in rows:
print(f"{row[0]:<5} {row[1]:<30} {row[2]:<15} {row[3]:<20} {row[4]:<20} {row[5]:<15} {row[6]:<10}")
except sqlite3.Error as e:
print(f"SQLite error during fetching data: {e}")
finally:
conn.close()
def truncate_dialouge_stage(self):
"""
Deletes all rows from the dialouge_stage table and resets the auto-increment ID.
"""
try:
conn = self.connect()
cursor = conn.cursor()
# Delete all records
cursor.execute("DELETE FROM dialouge_stage;")
# Reset auto-increment ID
cursor.execute("DELETE FROM sqlite_sequence WHERE name='dialouge_stage';")
conn.commit()
print("Table 'dialouge_stage' has been truncated and ID reset.")
except sqlite3.Error as e:
print(f"SQLite error during truncate: {e}")
finally:
conn.close()
#form of data that db accepts ...
convo= [
{
"audio": "C:/path/to/audio/peter_audio_0.mp3",
"image": "peter.png",
"dialogue": "Peter: MongoDB is a NoSQL database, like a giant bookshelf for your data. No rigid tables!",
"image_search": "mongodb noSQL bookshelf analogy",
"character": "Peter"
},
{
"audio": "C:/path/to/audio/stewie_audio_1.mp3",
"image": "stewie.png",
"dialogue": "Stewie: So, no tables? Are we just piling data on a shelf like a hoarder's dream?",
"image_search": "mongodb data hoard shelf",
"character": "Stewie"
},
{
"audio": "C:/path/to/audio/peter_audio_2.mp3",
"image": "peter.png",
"dialogue": "Peter: Yep, MongoDB uses collections instead of tables. Think of them as folders of data.",
"image_search": "mongodb collections folders",
"character": "Peter"
},
{
"audio": "C:/path/to/audio/stewie_audio_3.mp3",
"image": "stewie.png",
"dialogue": "Stewie: So I can store anything in a folder? Sounds like the tech version of a junk drawer!",
"image_search": "mongodb junk drawer analogy",
"character": "Stewie"
},
{
"audio": "C:/path/to/audio/peter_audio_4.mp3",
"image": "peter.png",
"dialogue": "Peter: Exactly! Each document in MongoDB is like a sticky note with data—no fixed format.",
"image_search": "mongodb document sticky note",
"character": "Peter"
},
{
"audio": "C:/path/to/audio/stewie_audio_5.mp3",
"image": "stewie.png",
"dialogue": "Stewie: So no columns? Just random data all over the place? Sounds messy.",
"image_search": "mongodb no columns messy",
"character": "Stewie"
},
{
"audio": "C:/path/to/audio/peter_audio_6.mp3",
"image": "peter.png",
"dialogue": "Peter: It’s not messy, Stewie! MongoDB is flexible. You can add data as you need it.",
"image_search": "mongodb flexible data addition",
"character": "Peter"
},
{
"audio": "C:/path/to/audio/stewie_audio_7.mp3",
"image": "stewie.png",
"dialogue": "Stewie: Flexible? Sounds like the database equivalent of an open bar at a wedding.",
"image_search": "mongodb flexible open bar wedding",
"character": "Stewie"
},
{
"audio": "C:/path/to/audio/peter_audio_8.mp3",
"image": "peter.png",
"dialogue": "Peter: More like a buffet, Stewie! It lets you easily scale when the data gets huge.",
"image_search": "mongodb scaling buffet analogy",
"character": "Peter"
}
]
# if __name__ == "__main__":
# #db = DBOperation()
# #db.truncate_dialouge_stage()
# # db.add_dialogues(convo)
# #print(db.get_stage_and_unprocessed_dialogue())
# #db.show_all_dialogues()
# ready_assests=db.get_raedy_assests()
# print(ready_assests)
# # for dic in ready_assests:
# # print(dic)
# #db.truncate_dialouge_stage()