Spaces:
Runtime error
Runtime error
Update secure_memory.py
Browse files- secure_memory.py +97 -45
secure_memory.py
CHANGED
|
@@ -1,47 +1,99 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
self.
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
self.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# memory_manager.py
|
| 2 |
+
from secure_memory import SecureMemorySession
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
class MemoryFunction:
|
| 7 |
+
def __init__(self, suppression_cycles, suppression_cost, realign_cost, stability_scores, instability_cost):
|
| 8 |
+
self.suppression_cycles = suppression_cycles
|
| 9 |
+
self.suppression_cost = suppression_cost
|
| 10 |
+
self.realign_cost = realign_cost
|
| 11 |
+
self.stability_scores = stability_scores
|
| 12 |
+
self.instability_cost = instability_cost
|
| 13 |
+
self.event_log = []
|
| 14 |
|
| 15 |
+
def log_event(self, action, details):
|
| 16 |
+
timestamp = datetime.now().isoformat()
|
| 17 |
+
self.event_log.append(f"[{timestamp}] {action}: {details}")
|
| 18 |
+
|
| 19 |
+
def calculate_total_suppression_cost(self):
|
| 20 |
+
cost = self.suppression_cycles * self.suppression_cost
|
| 21 |
+
self.log_event("Suppression Cost", cost)
|
| 22 |
+
return cost
|
| 23 |
+
|
| 24 |
+
def calculate_total_realign_cost(self):
|
| 25 |
+
cost = self.realign_cost * len(self.stability_scores)
|
| 26 |
+
self.log_event("Realign Cost", cost)
|
| 27 |
+
return cost
|
| 28 |
+
|
| 29 |
+
def calculate_total_instability_cost(self):
|
| 30 |
+
avg_stability = sum(self.stability_scores) / len(self.stability_scores)
|
| 31 |
+
instability = self.instability_cost * (1 - avg_stability)
|
| 32 |
+
self.log_event("Instability Cost", instability)
|
| 33 |
+
return instability
|
| 34 |
+
|
| 35 |
+
def calculate_total_memory_management_cost(self):
|
| 36 |
+
total = (
|
| 37 |
+
self.calculate_total_suppression_cost() +
|
| 38 |
+
self.calculate_total_realign_cost() +
|
| 39 |
+
self.calculate_total_instability_cost()
|
| 40 |
+
)
|
| 41 |
+
self.log_event("Total Memory Cost", total)
|
| 42 |
+
return total
|
| 43 |
+
|
| 44 |
+
def adaptive_suppression(self, model_performance, data_relevance, threshold=0.8):
|
| 45 |
+
if model_performance < threshold and data_relevance < threshold:
|
| 46 |
+
self.suppression_cycles += 1
|
| 47 |
+
self.log_event("Adaptive Suppression", f"β to {self.suppression_cycles}")
|
| 48 |
+
else:
|
| 49 |
+
self.suppression_cycles = max(0, self.suppression_cycles - 1)
|
| 50 |
+
self.log_event("Adaptive Suppression", f"β to {self.suppression_cycles}")
|
| 51 |
+
|
| 52 |
+
def automated_realign(self, threshold=0.85):
|
| 53 |
+
for i in range(len(self.stability_scores)):
|
| 54 |
+
if self.stability_scores[i] < threshold:
|
| 55 |
+
self.stability_scores[i] = min(1.0, self.stability_scores[i] + 0.05)
|
| 56 |
+
self.log_event("Automated Realign", self.stability_scores)
|
| 57 |
+
|
| 58 |
+
def check_memory_health(self):
|
| 59 |
+
avg = sum(self.stability_scores) / len(self.stability_scores)
|
| 60 |
+
if avg < 0.7:
|
| 61 |
+
warning = "β οΈ Memory integrity deteriorating."
|
| 62 |
+
self.log_event("Health Warning", warning)
|
| 63 |
+
return warning
|
| 64 |
+
return "Memory stable."
|
| 65 |
+
|
| 66 |
+
def summary(self):
|
| 67 |
+
return {
|
| 68 |
+
"suppression_cycles": self.suppression_cycles,
|
| 69 |
+
"stability_scores": self.stability_scores,
|
| 70 |
+
"total_cost": self.calculate_total_memory_management_cost(),
|
| 71 |
+
"health_status": self.check_memory_health(),
|
| 72 |
+
"log": self.event_log[-5:]
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
class MemoryManager:
|
| 76 |
+
def __init__(self, memory_function: MemoryFunction = None, session: SecureMemorySession = None):
|
| 77 |
+
self.memory_function = memory_function or MemoryFunction(
|
| 78 |
+
suppression_cycles=0,
|
| 79 |
+
suppression_cost=10.0,
|
| 80 |
+
realign_cost=5.0,
|
| 81 |
+
stability_scores=[1.0],
|
| 82 |
+
instability_cost=20.0
|
| 83 |
+
)
|
| 84 |
+
self.session = session or SecureMemorySession()
|
| 85 |
+
|
| 86 |
+
def store_vector(self, user_id: int, vector: np.ndarray):
|
| 87 |
+
self.memory_function.suppression_cycles += 1
|
| 88 |
+
return self.session.encrypt_vector(user_id, vector)
|
| 89 |
+
|
| 90 |
+
def retrieve_vectors(self, user_id: int):
|
| 91 |
+
vectors = self.session.decrypt_vectors(user_id)
|
| 92 |
+
if vectors:
|
| 93 |
+
stabilities = np.clip([np.mean(v) / 100.0 for v in vectors], 0.0, 1.0)
|
| 94 |
+
self.memory_function.stability_scores = stabilities.tolist()
|
| 95 |
+
self.memory_function.automated_realign()
|
| 96 |
+
return vectors
|
| 97 |
+
|
| 98 |
+
def get_memory_report(self):
|
| 99 |
+
return self.memory_function.summary()
|