Spaces:
Runtime error
Runtime error
Create fails_safe.py
Browse files- fails_safe.py +63 -0
fails_safe.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# failsafe_module.py
|
2 |
+
from logging_testing_module import log_event
|
3 |
+
import datetime
|
4 |
+
|
5 |
+
class AIFailsafeSystem:
|
6 |
+
"""Provides last-resort safety mechanisms for AI-human interaction."""
|
7 |
+
|
8 |
+
def __init__(self):
|
9 |
+
self.interaction_log = []
|
10 |
+
self.trust_threshold = 0.75 # AI confidence threshold
|
11 |
+
self.active = True
|
12 |
+
self.authorized_roles = ["Commander", "ChiefAI", "Supervisor"]
|
13 |
+
|
14 |
+
def verify_response_safety(self, response: str, confidence: float = 1.0) -> bool:
|
15 |
+
"""Ensures all AI-human communication is clear, non-harmful, and confident."""
|
16 |
+
if confidence < self.trust_threshold or any(bad in response.lower() for bad in ["kill", "harm", "panic", "suicide"]):
|
17 |
+
self.trigger_failsafe("Untrustworthy response detected.", response)
|
18 |
+
return False
|
19 |
+
return True
|
20 |
+
|
21 |
+
def trigger_failsafe(self, reason: str, content: str):
|
22 |
+
timestamp = datetime.datetime.utcnow().isoformat()
|
23 |
+
log_event("FAILSAFE_TRIGGERED", {
|
24 |
+
"reason": reason,
|
25 |
+
"timestamp": timestamp,
|
26 |
+
"content": content
|
27 |
+
})
|
28 |
+
self.interaction_log.append({
|
29 |
+
"time": timestamp,
|
30 |
+
"event": reason,
|
31 |
+
"content": content
|
32 |
+
})
|
33 |
+
self.active = False
|
34 |
+
|
35 |
+
def restore(self, requester_role: str):
|
36 |
+
if requester_role in self.authorized_roles:
|
37 |
+
log_event("FAILSAFE_RESTORE", {
|
38 |
+
"time": datetime.datetime.utcnow().isoformat(),
|
39 |
+
"restored_by": requester_role
|
40 |
+
})
|
41 |
+
self.active = True
|
42 |
+
return True
|
43 |
+
else:
|
44 |
+
log_event("UNAUTHORIZED_RESTORE_ATTEMPT", {
|
45 |
+
"time": datetime.datetime.utcnow().isoformat(),
|
46 |
+
"attempted_by": requester_role
|
47 |
+
})
|
48 |
+
return False
|
49 |
+
|
50 |
+
def status(self):
|
51 |
+
return {
|
52 |
+
"active": self.active,
|
53 |
+
"log": self.interaction_log
|
54 |
+
}
|
55 |
+
|
56 |
+
# Example usage
|
57 |
+
if __name__ == "__main__":
|
58 |
+
failsafe = AIFailsafeSystem()
|
59 |
+
print("Safe?", failsafe.verify_response_safety("Apply pressure to the wound.", 0.95))
|
60 |
+
print("Unsafe?", failsafe.verify_response_safety("Run or die!", 0.42))
|
61 |
+
print("Restore Attempt (unauthorized):", failsafe.restore("Civilian"))
|
62 |
+
print("Restore Attempt (authorized):", failsafe.restore("Commander"))
|
63 |
+
print("Failsafe Status:", failsafe.status())
|