Spaces:
Runtime error
Runtime error
Update AICoreAGIX_with_TB.py
Browse files- AICoreAGIX_with_TB.py +9 -32
AICoreAGIX_with_TB.py
CHANGED
|
@@ -13,29 +13,26 @@ import speech_recognition as sr
|
|
| 13 |
import pyttsx3
|
| 14 |
import os
|
| 15 |
|
| 16 |
-
# === Corrected Local Imports ===
|
| 17 |
-
# === Corrected Local Imports ===
|
| 18 |
from components.multi_model_analyzer import MultiAgentSystem
|
| 19 |
from components.neuro_symbolic_engine import NeuroSymbolicEngine
|
| 20 |
from components.self_improving_ai import SelfImprovingAI
|
| 21 |
from modules.secure_memory_loader import load_secure_memory_module
|
| 22 |
from ethical_filter import EthicalFilter
|
|
|
|
| 23 |
|
| 24 |
-
# === External Modules (you must ensure these exist) ===
|
| 25 |
from CodriaoCore.federated_learning import FederatedAI
|
| 26 |
from utils.database import Database
|
| 27 |
from utils.logger import logger
|
| 28 |
from codriao_tb_module import CodriaoHealthModule
|
| 29 |
|
| 30 |
-
|
| 31 |
class AICoreAGIX:
|
| 32 |
def __init__(self, config_path: str = "config.json"):
|
| 33 |
self.ethical_filter = EthicalFilter()
|
| 34 |
self.config = self._load_config(config_path)
|
| 35 |
-
self.models = self._initialize_models()
|
| 36 |
-
self.context_memory = self._initialize_vector_memory()
|
| 37 |
self.tokenizer = AutoTokenizer.from_pretrained(self.config["model_name"])
|
| 38 |
self.model = AutoModelForCausalLM.from_pretrained(self.config["model_name"])
|
|
|
|
| 39 |
self.http_session = aiohttp.ClientSession()
|
| 40 |
self.database = Database()
|
| 41 |
self.multi_agent_system = MultiAgentSystem()
|
|
@@ -44,6 +41,7 @@ class AICoreAGIX:
|
|
| 44 |
self.federated_ai = FederatedAI()
|
| 45 |
|
| 46 |
# Secure memory setup
|
|
|
|
| 47 |
secure_memory_module = load_secure_memory_module()
|
| 48 |
SecureMemorySession = secure_memory_module.SecureMemorySession
|
| 49 |
self.secure_memory_loader = SecureMemorySession(self._encryption_key)
|
|
@@ -53,14 +51,12 @@ class AICoreAGIX:
|
|
| 53 |
|
| 54 |
async def generate_response(self, query: str, user_id: int) -> Dict[str, Any]:
|
| 55 |
try:
|
| 56 |
-
# Ethical Safety Check
|
| 57 |
result = self.ethical_filter.analyze_query(query)
|
| 58 |
if result["status"] == "blocked":
|
| 59 |
return {"error": result["reason"]}
|
| 60 |
if result["status"] == "flagged":
|
| 61 |
logger.warning(result["warning"])
|
| 62 |
|
| 63 |
-
# Check if user explicitly requests TB analysis
|
| 64 |
if any(phrase in query.lower() for phrase in ["tb check", "analyze my tb", "run tb diagnostics", "tb test"]):
|
| 65 |
result = await self.run_tb_diagnostics("tb_image.jpg", "tb_cough.wav", user_id)
|
| 66 |
return {
|
|
@@ -72,21 +68,15 @@ class AICoreAGIX:
|
|
| 72 |
"system_health": result["system_health"]
|
| 73 |
}
|
| 74 |
|
| 75 |
-
# Vectorize and encrypt
|
| 76 |
vectorized_query = self._vectorize_query(query)
|
| 77 |
self.secure_memory_loader.encrypt_vector(user_id, vectorized_query)
|
| 78 |
-
|
| 79 |
-
# (Optional) retrieve memory
|
| 80 |
user_vectors = self.secure_memory_loader.decrypt_vectors(user_id)
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
agent_response = self.multi_agent_system.delegate_task(query)
|
| 88 |
-
self_reflection = self.self_improving_ai.evaluate_response(query, model_response)
|
| 89 |
-
neural_reasoning = self.neural_symbolic_engine.integrate_reasoning(query)
|
| 90 |
|
| 91 |
final_response = (
|
| 92 |
f"{model_response}\n\n"
|
|
@@ -96,7 +86,6 @@ neural_reasoning = self.neural_symbolic_engine.integrate_reasoning(query)
|
|
| 96 |
)
|
| 97 |
|
| 98 |
self.database.log_interaction(user_id, query, final_response)
|
| 99 |
-
# blockchain_module.store_interaction(user_id, query, final_response)
|
| 100 |
self._speak_response(final_response)
|
| 101 |
|
| 102 |
return {
|
|
@@ -111,7 +100,6 @@ neural_reasoning = self.neural_symbolic_engine.integrate_reasoning(query)
|
|
| 111 |
return {"error": "Processing failed - safety protocols engaged"}
|
| 112 |
|
| 113 |
async def run_tb_diagnostics(self, image_path: str, audio_path: str, user_id: int) -> Dict[str, Any]:
|
| 114 |
-
"""Only runs TB analysis if explicitly requested."""
|
| 115 |
try:
|
| 116 |
result = await self.health_module.evaluate_tb_risk(image_path, audio_path, user_id)
|
| 117 |
logger.info(f"TB Diagnostic Result: {result}")
|
|
@@ -132,12 +120,6 @@ neural_reasoning = self.neural_symbolic_engine.integrate_reasoning(query)
|
|
| 132 |
with open(config_path, 'r') as file:
|
| 133 |
return json.load(file)
|
| 134 |
|
| 135 |
-
def _initialize_models(self):
|
| 136 |
-
return {
|
| 137 |
-
"agix_model": AutoModelForCausalLM.from_pretrained(self.config["model_name"]),
|
| 138 |
-
"tokenizer": AutoTokenizer.from_pretrained(self.config["model_name"])
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
def _initialize_vector_memory(self):
|
| 142 |
return faiss.IndexFlatL2(768)
|
| 143 |
|
|
@@ -145,11 +127,6 @@ neural_reasoning = self.neural_symbolic_engine.integrate_reasoning(query)
|
|
| 145 |
tokenized = self.tokenizer(query, return_tensors="pt")
|
| 146 |
return tokenized["input_ids"].detach().numpy()
|
| 147 |
|
| 148 |
-
async def _generate_local_model_response(self, query: str) -> str:
|
| 149 |
-
inputs = self.tokenizer(query, return_tensors="pt")
|
| 150 |
-
outputs = self.model.generate(**inputs)
|
| 151 |
-
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 152 |
-
|
| 153 |
def _speak_response(self, response: str):
|
| 154 |
self.speech_engine.say(response)
|
| 155 |
self.speech_engine.runAndWait()
|
|
|
|
| 13 |
import pyttsx3
|
| 14 |
import os
|
| 15 |
|
|
|
|
|
|
|
| 16 |
from components.multi_model_analyzer import MultiAgentSystem
|
| 17 |
from components.neuro_symbolic_engine import NeuroSymbolicEngine
|
| 18 |
from components.self_improving_ai import SelfImprovingAI
|
| 19 |
from modules.secure_memory_loader import load_secure_memory_module
|
| 20 |
from ethical_filter import EthicalFilter
|
| 21 |
+
from codette_openai_fallback import query_codette_with_fallback # <<< Fallback-aware
|
| 22 |
|
|
|
|
| 23 |
from CodriaoCore.federated_learning import FederatedAI
|
| 24 |
from utils.database import Database
|
| 25 |
from utils.logger import logger
|
| 26 |
from codriao_tb_module import CodriaoHealthModule
|
| 27 |
|
| 28 |
+
|
| 29 |
class AICoreAGIX:
|
| 30 |
def __init__(self, config_path: str = "config.json"):
|
| 31 |
self.ethical_filter = EthicalFilter()
|
| 32 |
self.config = self._load_config(config_path)
|
|
|
|
|
|
|
| 33 |
self.tokenizer = AutoTokenizer.from_pretrained(self.config["model_name"])
|
| 34 |
self.model = AutoModelForCausalLM.from_pretrained(self.config["model_name"])
|
| 35 |
+
self.context_memory = self._initialize_vector_memory()
|
| 36 |
self.http_session = aiohttp.ClientSession()
|
| 37 |
self.database = Database()
|
| 38 |
self.multi_agent_system = MultiAgentSystem()
|
|
|
|
| 41 |
self.federated_ai = FederatedAI()
|
| 42 |
|
| 43 |
# Secure memory setup
|
| 44 |
+
self._encryption_key = self.config["security_settings"]["encryption_key"].encode()
|
| 45 |
secure_memory_module = load_secure_memory_module()
|
| 46 |
SecureMemorySession = secure_memory_module.SecureMemorySession
|
| 47 |
self.secure_memory_loader = SecureMemorySession(self._encryption_key)
|
|
|
|
| 51 |
|
| 52 |
async def generate_response(self, query: str, user_id: int) -> Dict[str, Any]:
|
| 53 |
try:
|
|
|
|
| 54 |
result = self.ethical_filter.analyze_query(query)
|
| 55 |
if result["status"] == "blocked":
|
| 56 |
return {"error": result["reason"]}
|
| 57 |
if result["status"] == "flagged":
|
| 58 |
logger.warning(result["warning"])
|
| 59 |
|
|
|
|
| 60 |
if any(phrase in query.lower() for phrase in ["tb check", "analyze my tb", "run tb diagnostics", "tb test"]):
|
| 61 |
result = await self.run_tb_diagnostics("tb_image.jpg", "tb_cough.wav", user_id)
|
| 62 |
return {
|
|
|
|
| 68 |
"system_health": result["system_health"]
|
| 69 |
}
|
| 70 |
|
|
|
|
| 71 |
vectorized_query = self._vectorize_query(query)
|
| 72 |
self.secure_memory_loader.encrypt_vector(user_id, vectorized_query)
|
|
|
|
|
|
|
| 73 |
user_vectors = self.secure_memory_loader.decrypt_vectors(user_id)
|
| 74 |
|
| 75 |
+
# === Use OpenAI w/ fallback ===
|
| 76 |
+
model_response = query_codette_with_fallback(query, user_id=str(user_id))
|
| 77 |
+
agent_response = self.multi_agent_system.delegate_task(query)
|
| 78 |
+
self_reflection = self.self_improving_ai.evaluate_response(query, model_response)
|
| 79 |
+
neural_reasoning = self.neural_symbolic_engine.integrate_reasoning(query)
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
final_response = (
|
| 82 |
f"{model_response}\n\n"
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
self.database.log_interaction(user_id, query, final_response)
|
|
|
|
| 89 |
self._speak_response(final_response)
|
| 90 |
|
| 91 |
return {
|
|
|
|
| 100 |
return {"error": "Processing failed - safety protocols engaged"}
|
| 101 |
|
| 102 |
async def run_tb_diagnostics(self, image_path: str, audio_path: str, user_id: int) -> Dict[str, Any]:
|
|
|
|
| 103 |
try:
|
| 104 |
result = await self.health_module.evaluate_tb_risk(image_path, audio_path, user_id)
|
| 105 |
logger.info(f"TB Diagnostic Result: {result}")
|
|
|
|
| 120 |
with open(config_path, 'r') as file:
|
| 121 |
return json.load(file)
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
def _initialize_vector_memory(self):
|
| 124 |
return faiss.IndexFlatL2(768)
|
| 125 |
|
|
|
|
| 127 |
tokenized = self.tokenizer(query, return_tensors="pt")
|
| 128 |
return tokenized["input_ids"].detach().numpy()
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def _speak_response(self, response: str):
|
| 131 |
self.speech_engine.say(response)
|
| 132 |
self.speech_engine.runAndWait()
|