codriao / AICoreAGIX_with_TB.py
Raiff1982's picture
Update AICoreAGIX_with_TB.py
418e0c1 verified
raw
history blame
5.61 kB
import aiohttp
import json
import logging
import torch
import faiss
import numpy as np
from transformers import AutoModelForCausalLM, AutoTokenizer
from typing import List, Dict, Any
from cryptography.fernet import Fernet
from jwt import encode, decode, ExpiredSignatureError
from datetime import datetime, timedelta
import speech_recognition as sr
import pyttsx3
import os
from components.multi_model_analyzer import MultiAgentSystem
from components.neuro_symbolic_engine import NeuroSymbolicEngine
from components.self_improving_ai import SelfImprovingAI
from modules.secure_memory_loader import load_secure_memory_module
from ethical_filter import EthicalFilter
from codette_openai_fallback import query_codette_with_fallback # <<< Fallback-aware
from CodriaoCore.federated_learning import FederatedAI
from utils.database import Database
from utils.logger import logger
from codriao_tb_module import CodriaoHealthModule
class AICoreAGIX:
def __init__(self, config_path: str = "config.json"):
self.ethical_filter = EthicalFilter()
self.config = self._load_config(config_path)
self.tokenizer = AutoTokenizer.from_pretrained(self.config["model_name"])
self.model = AutoModelForCausalLM.from_pretrained(self.config["model_name"])
self.context_memory = self._initialize_vector_memory()
self.http_session = aiohttp.ClientSession()
self.database = Database()
self.multi_agent_system = MultiAgentSystem()
self.self_improving_ai = SelfImprovingAI()
self.neural_symbolic_engine = NeuroSymbolicEngine()
self.federated_ai = FederatedAI()
# Secure memory setup
self._encryption_key = self.config["security_settings"]["encryption_key"].encode()
secure_memory_module = load_secure_memory_module()
SecureMemorySession = secure_memory_module.SecureMemorySession
self.secure_memory_loader = SecureMemorySession(self._encryption_key)
self.speech_engine = pyttsx3.init()
self.health_module = CodriaoHealthModule(ai_core=self)
async def generate_response(self, query: str, user_id: int) -> Dict[str, Any]:
try:
result = self.ethical_filter.analyze_query(query)
if result["status"] == "blocked":
return {"error": result["reason"]}
if result["status"] == "flagged":
logger.warning(result["warning"])
if any(phrase in query.lower() for phrase in ["tb check", "analyze my tb", "run tb diagnostics", "tb test"]):
result = await self.run_tb_diagnostics("tb_image.jpg", "tb_cough.wav", user_id)
return {
"response": result["ethical_analysis"],
"explanation": result["explanation"],
"tb_risk": result["tb_risk"],
"image_analysis": result["image_analysis"],
"audio_analysis": result["audio_analysis"],
"system_health": result["system_health"]
}
vectorized_query = self._vectorize_query(query)
self.secure_memory_loader.encrypt_vector(user_id, vectorized_query)
user_vectors = self.secure_memory_loader.decrypt_vectors(user_id)
# === Use OpenAI w/ fallback ===
model_response = query_codette_with_fallback(query, user_id=str(user_id))
agent_response = self.multi_agent_system.delegate_task(query)
self_reflection = self.self_improving_ai.evaluate_response(query, model_response)
neural_reasoning = self.neural_symbolic_engine.integrate_reasoning(query)
final_response = (
f"{model_response}\n\n"
f"{agent_response}\n\n"
f"{self_reflection}\n\n"
f"Logic: {neural_reasoning}"
)
self.database.log_interaction(user_id, query, final_response)
self._speak_response(final_response)
return {
"response": final_response,
"real_time_data": self.federated_ai.get_latest_data(),
"context_enhanced": True,
"security_status": "Fully Secure"
}
except Exception as e:
logger.error(f"Response generation failed: {e}")
return {"error": "Processing failed - safety protocols engaged"}
async def run_tb_diagnostics(self, image_path: str, audio_path: str, user_id: int) -> Dict[str, Any]:
try:
result = await self.health_module.evaluate_tb_risk(image_path, audio_path, user_id)
logger.info(f"TB Diagnostic Result: {result}")
return result
except Exception as e:
logger.error(f"TB diagnostics failed: {e}")
return {
"tb_risk": "ERROR",
"error": str(e),
"image_analysis": {},
"audio_analysis": {},
"ethical_analysis": "Unable to complete TB diagnostic.",
"explanation": None,
"system_health": None
}
def _load_config(self, config_path: str) -> dict:
with open(config_path, 'r') as file:
return json.load(file)
def _initialize_vector_memory(self):
return faiss.IndexFlatL2(768)
def _vectorize_query(self, query: str):
tokenized = self.tokenizer(query, return_tensors="pt")
return tokenized["input_ids"].detach().numpy()
def _speak_response(self, response: str):
self.speech_engine.say(response)
self.speech_engine.runAndWait()