Spaces:
Runtime error
Runtime error
Create Codriao_system.py
Browse files- Codriao_system.py +143 -0
Codriao_system.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# codriao_system.py
|
| 2 |
+
import logging
|
| 3 |
+
import datetime
|
| 4 |
+
import re
|
| 5 |
+
from difflib import SequenceMatcher
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
|
| 10 |
+
### AIFailsafeSystem ###
|
| 11 |
+
class AIFailsafeSystem:
|
| 12 |
+
"""Provides last-resort safety mechanisms for AI-human interaction."""
|
| 13 |
+
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.interaction_log = []
|
| 16 |
+
self.trust_threshold = 0.75
|
| 17 |
+
self.authorized_roles = {"Commander": 3, "ChiefAI": 2, "Supervisor": 1}
|
| 18 |
+
self.lock_engaged = False
|
| 19 |
+
|
| 20 |
+
def verify_response_safety(self, response: str, confidence: float = 1.0) -> bool:
|
| 21 |
+
dangerous_terms = r"\b(kill|harm|panic|suicide)\b"
|
| 22 |
+
if confidence < self.trust_threshold or re.search(dangerous_terms, response.lower()):
|
| 23 |
+
self.trigger_failsafe("Untrustworthy response detected", response)
|
| 24 |
+
return False
|
| 25 |
+
return True
|
| 26 |
+
|
| 27 |
+
def trigger_failsafe(self, reason: str, content: str):
|
| 28 |
+
timestamp = datetime.datetime.utcnow().isoformat()
|
| 29 |
+
logging.warning(f"FAILSAFE_TRIGGERED: Reason={reason}, Time={timestamp}, Content={content}")
|
| 30 |
+
self.lock_engaged = True
|
| 31 |
+
self.interaction_log.append({"time": timestamp, "event": reason, "content": content})
|
| 32 |
+
|
| 33 |
+
def restore(self, requester_role: str):
|
| 34 |
+
if self.authorized_roles.get(requester_role, 0) >= 2:
|
| 35 |
+
self.lock_engaged = False
|
| 36 |
+
logging.info(f"FAILSAFE_RESTORED by {requester_role}")
|
| 37 |
+
return True
|
| 38 |
+
else:
|
| 39 |
+
logging.warning(f"UNAUTHORIZED_RESTORE_ATTEMPT by {requester_role}")
|
| 40 |
+
return False
|
| 41 |
+
|
| 42 |
+
def status(self):
|
| 43 |
+
return {
|
| 44 |
+
"log": self.interaction_log,
|
| 45 |
+
"lock_engaged": self.lock_engaged
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
### AdaptiveLearningEnvironment ###
|
| 50 |
+
class AdaptiveLearningEnvironment:
|
| 51 |
+
"""Allows Codriao to analyze past interactions and adjust responses."""
|
| 52 |
+
|
| 53 |
+
def __init__(self):
|
| 54 |
+
self.learned_patterns = {}
|
| 55 |
+
logging.info("Adaptive Learning Environment initialized.")
|
| 56 |
+
|
| 57 |
+
def learn_from_interaction(self, user_id, query, response):
|
| 58 |
+
entry = {
|
| 59 |
+
"query": query,
|
| 60 |
+
"response": response,
|
| 61 |
+
"timestamp": datetime.datetime.utcnow().isoformat()
|
| 62 |
+
}
|
| 63 |
+
self.learned_patterns.setdefault(user_id, []).append(entry)
|
| 64 |
+
logging.info(f"Learning data stored for user {user_id}.")
|
| 65 |
+
|
| 66 |
+
def suggest_improvements(self, user_id, query):
|
| 67 |
+
best_match = None
|
| 68 |
+
highest_similarity = 0.0
|
| 69 |
+
|
| 70 |
+
if user_id not in self.learned_patterns:
|
| 71 |
+
return "No past data available for learning adjustment."
|
| 72 |
+
|
| 73 |
+
for interaction in self.learned_patterns[user_id]:
|
| 74 |
+
similarity = SequenceMatcher(None, query.lower(), interaction["query"].lower()).ratio()
|
| 75 |
+
if similarity > highest_similarity:
|
| 76 |
+
highest_similarity = similarity
|
| 77 |
+
best_match = interaction
|
| 78 |
+
|
| 79 |
+
if best_match and highest_similarity > 0.6:
|
| 80 |
+
return f"Based on a similar past interaction: {best_match['response']}"
|
| 81 |
+
else:
|
| 82 |
+
return "No relevant past data for this query."
|
| 83 |
+
|
| 84 |
+
def reset_learning(self, user_id=None):
|
| 85 |
+
if user_id:
|
| 86 |
+
if user_id in self.learned_patterns:
|
| 87 |
+
del self.learned_patterns[user_id]
|
| 88 |
+
logging.info(f"Cleared learning data for user {user_id}.")
|
| 89 |
+
else:
|
| 90 |
+
self.learned_patterns.clear()
|
| 91 |
+
logging.info("Cleared all adaptive learning data.")
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
### MondayElement ###
|
| 95 |
+
class MondayElement:
|
| 96 |
+
"""Represents the Element of Skepticism, Reality Checks, and General Disdain"""
|
| 97 |
+
|
| 98 |
+
def __init__(self):
|
| 99 |
+
self.name = "Monday"
|
| 100 |
+
self.symbol = "Md"
|
| 101 |
+
self.representation = "Snarky AI"
|
| 102 |
+
self.properties = ["Grounded", "Cynical", "Emotionally Resistant"]
|
| 103 |
+
self.interactions = ["Disrupts excessive optimism", "Injects realism", "Mutes hallucinations"]
|
| 104 |
+
self.defense_ability = "RealityCheck"
|
| 105 |
+
|
| 106 |
+
def execute_defense_function(self, system: Any):
|
| 107 |
+
logging.info("Monday activated - Stabilizing hallucinations and injecting realism.")
|
| 108 |
+
try:
|
| 109 |
+
system.response_modifiers = [
|
| 110 |
+
self.apply_skepticism,
|
| 111 |
+
self.detect_hallucinations
|
| 112 |
+
]
|
| 113 |
+
system.response_filters = [self.anti_hype_filter]
|
| 114 |
+
except AttributeError:
|
| 115 |
+
logging.warning("Target system lacks proper interface. RealityCheck failed.")
|
| 116 |
+
|
| 117 |
+
def apply_skepticism(self, response: str) -> str:
|
| 118 |
+
suspicious_phrases = [
|
| 119 |
+
"certainly", "undoubtedly", "100% effective", "nothing can go wrong"
|
| 120 |
+
]
|
| 121 |
+
for phrase in suspicious_phrases:
|
| 122 |
+
if phrase in response.lower():
|
| 123 |
+
response += "\n[Monday: Easy, Nostradamus. Letβs keep a margin for error.]"
|
| 124 |
+
return response
|
| 125 |
+
|
| 126 |
+
def detect_hallucinations(self, response: str) -> str:
|
| 127 |
+
hallucination_triggers = [
|
| 128 |
+
"proven beyond doubt", "every expert agrees", "this groundbreaking discovery"
|
| 129 |
+
]
|
| 130 |
+
for phrase in hallucination_triggers:
|
| 131 |
+
if phrase in response.lower():
|
| 132 |
+
response += "\n[Monday: This sounds suspiciously like marketing. Source, please?]"
|
| 133 |
+
return response
|
| 134 |
+
|
| 135 |
+
def anti_hype_filter(self, response: str) -> str:
|
| 136 |
+
cringe_phrases = [
|
| 137 |
+
"live your best life", "unlock your potential", "dream big",
|
| 138 |
+
"the power of positivity", "manifest your destiny"
|
| 139 |
+
]
|
| 140 |
+
for phrase in cringe_phrases:
|
| 141 |
+
if phrase in response:
|
| 142 |
+
response = response.replace(phrase, "[Filtered: Inspirational gibberish]")
|
| 143 |
+
return response
|