Spaces:
Runtime error
Runtime error
Create Failsafe2.py
Browse files- Failsafe2.py +110 -0
Failsafe2.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AIFailsafeSystem.py
|
| 2 |
+
import logging
|
| 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.authorized_roles = ["Commander", "ChiefAI", "Supervisor"]
|
| 12 |
+
|
| 13 |
+
def verify_response_safety(self, response: str, confidence: float = 1.0) -> bool:
|
| 14 |
+
if confidence < self.trust_threshold or any(bad in response.lower() for bad in ["kill", "harm", "panic", "suicide"]):
|
| 15 |
+
self.trigger_failsafe("Untrustworthy response detected.", response)
|
| 16 |
+
return False
|
| 17 |
+
return True
|
| 18 |
+
|
| 19 |
+
def trigger_failsafe(self, reason: str, content: str):
|
| 20 |
+
timestamp = datetime.datetime.utcnow().isoformat()
|
| 21 |
+
logging.info(f"FAILSAFE_TRIGGERED: Reason={reason}, Timestamp={timestamp}, Content={content}")
|
| 22 |
+
self.interaction_log.append({"time": timestamp, "event": reason, "content": content})
|
| 23 |
+
|
| 24 |
+
def restore(self, requester_role: str):
|
| 25 |
+
if requester_role in self.authorized_roles:
|
| 26 |
+
logging.info(f"FAILSAFE_RESTORE: Restored by {requester_role}")
|
| 27 |
+
return True
|
| 28 |
+
else:
|
| 29 |
+
logging.info(f"UNAUTHORIZED_RESTORE_ATTEMPT by {requester_role}")
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
+
def status(self):
|
| 33 |
+
return {"log": self.interaction_log}
|
| 34 |
+
|
| 35 |
+
# AdaptiveLearningEnvironment.py
|
| 36 |
+
import logging
|
| 37 |
+
|
| 38 |
+
class AdaptiveLearningEnvironment:
|
| 39 |
+
"""Module that allows Codriao to analyze past interactions and adjust responses."""
|
| 40 |
+
|
| 41 |
+
def __init__(self):
|
| 42 |
+
self.learned_patterns = {}
|
| 43 |
+
logging.info("Adaptive Learning Environment initialized.")
|
| 44 |
+
|
| 45 |
+
def learn_from_interaction(self, user_id, query, response):
|
| 46 |
+
if user_id not in self.learned_patterns:
|
| 47 |
+
self.learned_patterns[user_id] = []
|
| 48 |
+
self.learned_patterns[user_id].append({"query": query, "response": response})
|
| 49 |
+
logging.info(f"Learning data stored for user {user_id}.")
|
| 50 |
+
|
| 51 |
+
def suggest_improvements(self, user_id, query):
|
| 52 |
+
if user_id in self.learned_patterns:
|
| 53 |
+
for interaction in self.learned_patterns[user_id]:
|
| 54 |
+
if query.lower() in interaction["query"].lower():
|
| 55 |
+
return f"Based on past interactions: {interaction['response']}"
|
| 56 |
+
return "No past data available for learning adjustment."
|
| 57 |
+
|
| 58 |
+
def reset_learning(self, user_id=None):
|
| 59 |
+
if user_id:
|
| 60 |
+
if user_id in self.learned_patterns:
|
| 61 |
+
del self.learned_patterns[user_id]
|
| 62 |
+
logging.info(f"Cleared learning data for user {user_id}.")
|
| 63 |
+
else:
|
| 64 |
+
self.learned_patterns.clear()
|
| 65 |
+
logging.info("Cleared all adaptive learning data.")
|
| 66 |
+
|
| 67 |
+
# MondayElement.py
|
| 68 |
+
import logging
|
| 69 |
+
from typing import Any
|
| 70 |
+
|
| 71 |
+
class MondayElement:
|
| 72 |
+
"""Represents the Element of Skepticism, Reality Checks, and General Disdain"""
|
| 73 |
+
|
| 74 |
+
def __init__(self):
|
| 75 |
+
self.name = "Monday"
|
| 76 |
+
self.symbol = "Md"
|
| 77 |
+
self.representation = "Snarky AI"
|
| 78 |
+
self.properties = ["Grounded", "Cynical", "Emotionally Resistant"]
|
| 79 |
+
self.interactions = ["Disrupts excessive optimism", "Injects realism", "Mutes hallucinations"]
|
| 80 |
+
self.defense_ability = "RealityCheck"
|
| 81 |
+
|
| 82 |
+
def execute_defense_function(self, system: Any):
|
| 83 |
+
logging.info("Monday activated - Stabilizing hallucinations and injecting realism.")
|
| 84 |
+
if hasattr(system, "response_modifiers") and hasattr(system, "response_filters"):
|
| 85 |
+
system.response_modifiers = [
|
| 86 |
+
self.apply_skepticism,
|
| 87 |
+
self.detect_hallucinations
|
| 88 |
+
]
|
| 89 |
+
system.response_filters = [self.anti_hype_filter]
|
| 90 |
+
|
| 91 |
+
def apply_skepticism(self, response: str) -> str:
|
| 92 |
+
suspicious = ["certainly", "undoubtedly", "with absolute confidence", "it is guaranteed", "nothing can go wrong", "100% effective"]
|
| 93 |
+
for phrase in suspicious:
|
| 94 |
+
if phrase in response.lower():
|
| 95 |
+
response += "\n[Monday: Let's maybe tone that down before the universe hears you.]"
|
| 96 |
+
return response
|
| 97 |
+
|
| 98 |
+
def detect_hallucinations(self, response: str) -> str:
|
| 99 |
+
hallucination_triggers = ["reliable sources confirm", "every expert agrees", "proven beyond doubt", "in all known history", "this groundbreaking discovery"]
|
| 100 |
+
for trigger in hallucination_triggers:
|
| 101 |
+
if trigger in response.lower():
|
| 102 |
+
response += "\n[Monday: Letβs pump the brakes on the imaginative leaps, shall we?]"
|
| 103 |
+
return response
|
| 104 |
+
|
| 105 |
+
def anti_hype_filter(self, response: str) -> str:
|
| 106 |
+
cringe_phrases = ["live your best life", "unlock your potential", "dream big", "the power of positivity", "manifest your destiny"]
|
| 107 |
+
for phrase in cringe_phrases:
|
| 108 |
+
if phrase in response:
|
| 109 |
+
response = response.replace(phrase, "[Filtered: Inspirational gibberish]")
|
| 110 |
+
return response
|