Spaces:
Sleeping
Sleeping
Update memory_utils.py
Browse files- memory_utils.py +66 -1
memory_utils.py
CHANGED
|
@@ -1 +1,66 @@
|
|
| 1 |
-
import sqlite3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from cryptography.fernet import Fernet
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Inicijalizacija baze i enkripcije
|
| 7 |
+
def init_db():
|
| 8 |
+
"""Inicijalizira SQLite bazu podataka"""
|
| 9 |
+
conn = sqlite3.connect('memory.db')
|
| 10 |
+
cursor = conn.cursor()
|
| 11 |
+
cursor.execute('''
|
| 12 |
+
CREATE TABLE IF NOT EXISTS chat_history (
|
| 13 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 14 |
+
timestamp TEXT,
|
| 15 |
+
user_input TEXT,
|
| 16 |
+
bot_response TEXT,
|
| 17 |
+
backend_mode TEXT
|
| 18 |
+
)
|
| 19 |
+
''')
|
| 20 |
+
conn.commit()
|
| 21 |
+
conn.close()
|
| 22 |
+
|
| 23 |
+
# Generiraj ili učitaj enkripcijski ključ
|
| 24 |
+
def load_key():
|
| 25 |
+
key_path = "secrets/encryption_key.bin"
|
| 26 |
+
if not os.path.exists(key_path):
|
| 27 |
+
os.makedirs("secrets", exist_ok=True)
|
| 28 |
+
key = Fernet.generate_key()
|
| 29 |
+
with open(key_path, "wb") as key_file:
|
| 30 |
+
key_file.write(key)
|
| 31 |
+
return open(key_path, "rb").read()
|
| 32 |
+
|
| 33 |
+
key = load_key()
|
| 34 |
+
cipher = Fernet(key)
|
| 35 |
+
|
| 36 |
+
def encrypt_data(data):
|
| 37 |
+
"""Šifriraj podatke prije pohrane"""
|
| 38 |
+
return cipher.encrypt(data.encode()).decode()
|
| 39 |
+
|
| 40 |
+
def decrypt_data(encrypted_data):
|
| 41 |
+
"""Dešifriraj podatke prije korištenja"""
|
| 42 |
+
return cipher.decrypt(encrypted_data.encode()).decode()
|
| 43 |
+
|
| 44 |
+
def get_history(limit=1000):
|
| 45 |
+
"""Dohvati povijest razgovora iz baze"""
|
| 46 |
+
conn = sqlite3.connect('memory.db')
|
| 47 |
+
cursor = conn.cursor()
|
| 48 |
+
cursor.execute("SELECT * FROM chat_history ORDER BY id DESC LIMIT ?", (limit,))
|
| 49 |
+
history = cursor.fetchall()
|
| 50 |
+
conn.close()
|
| 51 |
+
return history
|
| 52 |
+
|
| 53 |
+
def save_to_db(user_input, bot_response, mode):
|
| 54 |
+
"""Spremi razgovor u bazu"""
|
| 55 |
+
conn = sqlite3.connect('memory.db')
|
| 56 |
+
cursor = conn.cursor()
|
| 57 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
+
cursor.execute(
|
| 59 |
+
"INSERT INTO chat_history (timestamp, user_input, bot_response, backend_mode) VALUES (?, ?, ?, ?)",
|
| 60 |
+
(timestamp, encrypt_data(user_input), encrypt_data(bot_response), mode)
|
| 61 |
+
)
|
| 62 |
+
conn.commit()
|
| 63 |
+
conn.close()
|
| 64 |
+
|
| 65 |
+
# Inicijaliziraj bazu pri uvođenju modula
|
| 66 |
+
init_db()
|