Spaces:
Runtime error
Runtime error
Create codette_reasoning.py
Browse files- codette_reasoning.py +179 -0
codette_reasoning.py
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import asyncio
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
import logging
|
6 |
+
from typing import List
|
7 |
+
|
8 |
+
# Ensure vaderSentiment is installed
|
9 |
+
try:
|
10 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
11 |
+
except ModuleNotFoundError:
|
12 |
+
import subprocess
|
13 |
+
import sys
|
14 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "vaderSentiment"])
|
15 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
16 |
+
|
17 |
+
# Ensure nltk is installed and download required data
|
18 |
+
try:
|
19 |
+
import nltk
|
20 |
+
from nltk.tokenize import word_tokenize
|
21 |
+
nltk.download('punkt', quiet=True)
|
22 |
+
except ImportError:
|
23 |
+
import subprocess
|
24 |
+
import sys
|
25 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "nltk"])
|
26 |
+
import nltk
|
27 |
+
from nltk.tokenize import word_tokenize
|
28 |
+
nltk.download('punkt', quiet=True)
|
29 |
+
|
30 |
+
# Import perspectives
|
31 |
+
from perspectives import (
|
32 |
+
NewtonPerspective, DaVinciPerspective, HumanIntuitionPerspective,
|
33 |
+
NeuralNetworkPerspective, QuantumComputingPerspective, ResilientKindnessPerspective,
|
34 |
+
MathematicalPerspective, PhilosophicalPerspective, CopilotPerspective, BiasMitigationPerspective
|
35 |
+
)
|
36 |
+
|
37 |
+
def setup_logging(config):
|
38 |
+
if config.get('logging_enabled', True):
|
39 |
+
log_level = config.get('log_level', 'DEBUG').upper()
|
40 |
+
numeric_level = getattr(logging, log_level, logging.DEBUG)
|
41 |
+
logging.basicConfig(
|
42 |
+
filename='universal_reasoning.log',
|
43 |
+
level=numeric_level,
|
44 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
45 |
+
)
|
46 |
+
else:
|
47 |
+
logging.disable(logging.CRITICAL)
|
48 |
+
|
49 |
+
def load_json_config(file_path):
|
50 |
+
if not os.path.exists(file_path):
|
51 |
+
logging.error(f"Configuration file '{file_path}' not found.")
|
52 |
+
return {}
|
53 |
+
try:
|
54 |
+
with open(file_path, 'r') as file:
|
55 |
+
config = json.load(file)
|
56 |
+
logging.info(f"Configuration loaded from '{file_path}'.")
|
57 |
+
config['allow_network_calls'] = False
|
58 |
+
return config
|
59 |
+
except json.JSONDecodeError as e:
|
60 |
+
logging.error(f"Error decoding JSON from the configuration file '{file_path}': {e}")
|
61 |
+
return {}
|
62 |
+
|
63 |
+
def analyze_question(question):
|
64 |
+
tokens = word_tokenize(question)
|
65 |
+
logging.debug(f"Question tokens: {tokens}")
|
66 |
+
return tokens
|
67 |
+
|
68 |
+
class Element:
|
69 |
+
def __init__(self, name, symbol, representation, properties, interactions, defense_ability):
|
70 |
+
self.name = name
|
71 |
+
self.symbol = symbol
|
72 |
+
self.representation = representation
|
73 |
+
self.properties = properties
|
74 |
+
self.interactions = interactions
|
75 |
+
self.defense_ability = defense_ability
|
76 |
+
|
77 |
+
def execute_defense_function(self):
|
78 |
+
message = f"{self.name} ({self.symbol}) executes its defense ability: {self.defense_ability}"
|
79 |
+
logging.info(message)
|
80 |
+
return message
|
81 |
+
|
82 |
+
class CustomRecognizer:
|
83 |
+
def recognize(self, question):
|
84 |
+
if any(element_name.lower() in question.lower() for element_name in ["hydrogen", "diamond"]):
|
85 |
+
return RecognizerResult(question)
|
86 |
+
return RecognizerResult(None)
|
87 |
+
|
88 |
+
def get_top_intent(self, recognizer_result):
|
89 |
+
return "ElementDefense" if recognizer_result.text else "None"
|
90 |
+
|
91 |
+
class RecognizerResult:
|
92 |
+
def __init__(self, text):
|
93 |
+
self.text = text
|
94 |
+
|
95 |
+
class UniversalReasoning:
|
96 |
+
def __init__(self, config):
|
97 |
+
self.config = config
|
98 |
+
self.perspectives = self.initialize_perspectives()
|
99 |
+
self.elements = self.initialize_elements()
|
100 |
+
self.recognizer = CustomRecognizer()
|
101 |
+
self.sentiment_analyzer = SentimentIntensityAnalyzer()
|
102 |
+
|
103 |
+
def initialize_perspectives(self):
|
104 |
+
perspective_names = self.config.get('enabled_perspectives', [
|
105 |
+
"newton", "davinci", "human_intuition", "neural_network", "quantum_computing",
|
106 |
+
"resilient_kindness", "mathematical", "philosophical", "copilot", "bias_mitigation"
|
107 |
+
])
|
108 |
+
perspective_classes = {
|
109 |
+
"newton": NewtonPerspective,
|
110 |
+
"davinci": DaVinciPerspective,
|
111 |
+
"human_intuition": HumanIntuitionPerspective,
|
112 |
+
"neural_network": NeuralNetworkPerspective,
|
113 |
+
"quantum_computing": QuantumComputingPerspective,
|
114 |
+
"resilient_kindness": ResilientKindnessPerspective,
|
115 |
+
"mathematical": MathematicalPerspective,
|
116 |
+
"philosophical": PhilosophicalPerspective,
|
117 |
+
"copilot": CopilotPerspective,
|
118 |
+
"bias_mitigation": BiasMitigationPerspective
|
119 |
+
}
|
120 |
+
perspectives = []
|
121 |
+
for name in perspective_names:
|
122 |
+
cls = perspective_classes.get(name.lower())
|
123 |
+
if cls:
|
124 |
+
perspectives.append(cls(self.config))
|
125 |
+
logging.debug(f"Perspective '{name}' initialized.")
|
126 |
+
return perspectives
|
127 |
+
|
128 |
+
def initialize_elements(self):
|
129 |
+
return [
|
130 |
+
Element("Hydrogen", "H", "Lua", ["Simple", "Lightweight", "Versatile"],
|
131 |
+
["Integrates with other languages"], "Evasion"),
|
132 |
+
Element("Diamond", "D", "Kotlin", ["Modern", "Concise", "Safe"],
|
133 |
+
["Used for Android development"], "Adaptability")
|
134 |
+
]
|
135 |
+
|
136 |
+
async def generate_response(self, question):
|
137 |
+
responses = []
|
138 |
+
tasks = []
|
139 |
+
|
140 |
+
for perspective in self.perspectives:
|
141 |
+
if asyncio.iscoroutinefunction(perspective.generate_response):
|
142 |
+
tasks.append(perspective.generate_response(question))
|
143 |
+
else:
|
144 |
+
async def sync_wrapper(perspective, question):
|
145 |
+
return perspective.generate_response(question)
|
146 |
+
tasks.append(sync_wrapper(perspective, question))
|
147 |
+
|
148 |
+
perspective_results = await asyncio.gather(*tasks, return_exceptions=True)
|
149 |
+
|
150 |
+
for perspective, result in zip(self.perspectives, perspective_results):
|
151 |
+
if isinstance(result, Exception):
|
152 |
+
logging.error(f"Error from {perspective.__class__.__name__}: {result}")
|
153 |
+
else:
|
154 |
+
responses.append(result)
|
155 |
+
|
156 |
+
recognizer_result = self.recognizer.recognize(question)
|
157 |
+
top_intent = self.recognizer.get_top_intent(recognizer_result)
|
158 |
+
if top_intent == "ElementDefense":
|
159 |
+
element_name = recognizer_result.text.strip()
|
160 |
+
element = next((el for el in self.elements if el.name.lower() in element_name.lower()), None)
|
161 |
+
if element:
|
162 |
+
responses.append(element.execute_defense_function())
|
163 |
+
|
164 |
+
ethical = self.config.get("ethical_considerations", "Act transparently and respectfully.")
|
165 |
+
responses.append(f"**Ethical Considerations:**\n{ethical}")
|
166 |
+
|
167 |
+
return "\n\n".join(responses)
|
168 |
+
|
169 |
+
def save_response(self, response):
|
170 |
+
if self.config.get('enable_response_saving', False):
|
171 |
+
path = self.config.get('response_save_path', 'responses.txt')
|
172 |
+
with open(path, 'a', encoding='utf-8') as file:
|
173 |
+
file.write(response + '\n')
|
174 |
+
|
175 |
+
def backup_response(self, response):
|
176 |
+
if self.config.get('backup_responses', {}).get('enabled', False):
|
177 |
+
backup_path = self.config['backup_responses'].get('backup_path', 'backup_responses.txt')
|
178 |
+
with open(backup_path, 'a', encoding='utf-8') as file:
|
179 |
+
file.write(response + '\n')
|