Spaces:
Sleeping
Sleeping
Create memory_utils.py
Browse files- memory_utils.py +34 -0
memory_utils.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from cryptography.fernet import Fernet
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Generiraj ili učitaj enkripcijski ključ
|
| 7 |
+
def load_key():
|
| 8 |
+
key_path = "secrets/encryption_key.bin"
|
| 9 |
+
if not os.path.exists(key_path):
|
| 10 |
+
os.makedirs("secrets", exist_ok=True)
|
| 11 |
+
key = Fernet.generate_key()
|
| 12 |
+
with open(key_path, "wb") as key_file:
|
| 13 |
+
key_file.write(key)
|
| 14 |
+
return open(key_path, "rb").read()
|
| 15 |
+
|
| 16 |
+
key = load_key()
|
| 17 |
+
cipher = Fernet(key)
|
| 18 |
+
|
| 19 |
+
def encrypt_data(data):
|
| 20 |
+
return cipher.encrypt(data.encode()).decode()
|
| 21 |
+
|
| 22 |
+
def decrypt_data(encrypted_data):
|
| 23 |
+
return cipher.decrypt(encrypted_data.encode()).decode()
|
| 24 |
+
|
| 25 |
+
# Modificirane DB funkcije
|
| 26 |
+
def save_to_db(user_input, bot_response, mode):
|
| 27 |
+
conn = sqlite3.connect('memory.db')
|
| 28 |
+
cursor = conn.cursor()
|
| 29 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 30 |
+
cursor.execute(
|
| 31 |
+
"INSERT INTO chat_history VALUES (?, ?, ?, ?)",
|
| 32 |
+
(timestamp, encrypt_data(user_input), encrypt_data(bot_response), mode)
|
| 33 |
+
conn.commit()
|
| 34 |
+
conn.close()
|