Update README.md
Browse filesShape Shifting AI
"""
Production-Ready Metamorphic AI System
Implements cross-check recommendations for robustness and practical deployment
"""
import asyncio
import json
import time
import uuid
import logging
import re
import os
from pathlib import Path
from datetime import datetime, timedelta
import aiosqlite
import psutil
import yaml
from abc import ABC, abstractmethod
from dataclasses import dataclass, field, asdict
from enum import Enum
from typing import Dict, List, Optional, Any, Union, Callable, Set, Tuple
import numpy as np
import torch
import torch.nn as nn
from tenacity import retry, stop_after_attempt, wait_exponential
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from prometheus_client import Gauge, Counter, start_http_server
from concurrent.futures import ThreadPoolExecutor
import unittest
from unittest.mock import Mock, AsyncMock, patch
import argparse
import sys
# ---------------------------
# Configuration Management
# ---------------------------
class ConfigManager:
"""Manages configuration loading and dynamic updates.
Attributes:
config_path (Path): Path to the YAML configuration file.
config (Dict): Loaded configuration dictionary.
logger (logging.Logger): Logger instance for configuration events.
observer (Observer): File watcher for config changes.
"""
def __init__(self, config_path: str = "config/metamorphic_ai.yaml"):
self.config_path = Path(config_path)
self.config = self._load_config()
self.logger = logging.getLogger(__name__)
self.observer = None
self._setup_logging()
self._start_file_watcher()
def _load_config(self) -> Dict:
"""Loads configuration from YAML file or creates default.
Returns:
Dict: Merged configuration dictionary.
"""
default_config = {
"system": {
"complexity_level": "standard",
"max_cpu_usage": 0.7,
"max_memory_usage": 0.8,
"safe_mode_threshold": 0.9,
"health_check_interval": 5.0,
"metrics_port": 8000
},
"security": {
"risk_threshold": 0.7,
"default_security_level": "sandbox",
"audit_log_retention_days": 30
},
"learning": {
"feedback_weight_explicit": 0.4,
"feedback_weight_implicit": 0.3,
"feedback_weight_performance": 0.3,
"bayesian_prior_confidence": 0.1
},
"morphing": {
"max_transition_time": 10.0,
"shape_cache_size": 50,
"evolution_mutation_rate": 0.1
},
"database": {
"path": os.getenv("METAMORPHIC_DB_PATH", "data/metamorphic_ai.db"),
"backup_interval_hours": 6,
"backup_path": "data/backups"
},
"logging": {
"level": os.getenv("METAMORPHIC_LOG_LEVEL", "INFO"),
"file_path": "logs/metamorphic_ai.log",
"max_file_size": "10MB",
"backup_count": 5
}
}
if self.config_path.exists():
with open(self.config_path, 'r') as f:
user_config = yaml.safe_load(f) or {}
return self._deep_merge(default_config, user_config)
else:
self.config_path.parent.mkdir(parents=True, exist_ok=True)
with open(self.config_path, 'w') as f:
yaml.dump(default_config, f, default_flow_style=False)
return default_config
def _deep_merge(self, base: Dict, override: Dict) -> Dict:
"""Merges two dictionaries recursively.
Args:
base (Dict): Base configuration dictionary.
override (Dict): User-defined configuration to override defaults.
Returns:
Dict: Merged configuration dictionary.
"""
result = base.copy()
for key, value in override.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = self._deep_merge(result[key], value)
else:
result[key] = value
return result
def _setup_logging(self):
"""Sets up logging configuration with file and console handlers."""
log_level = self.config.get("logging", {}).get("level", "INFO")
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
log_file = self.config.get("logging.file_path", "logs/metamorphic_ai.log")
Path(log_file).parent.mkdir(exist_ok=True)
logging.basicConfig(
level=getattr(logging, log_level),
format=log_format,
handlers=[
logging.FileHandler(log_file),
logging.StreamHandler()
]
)
def _start_file_watcher(self):
"""Starts a file watcher for dynamic config updates."""
try:
import watchdog
class ConfigFileHandler(FileSystemEventHandler):
def __init__(self, config_manager):
self.config_manager = config_manager
def on_modified(self, event):
if not event.is_directory and event.src_path == str(self.config_manager.config_path):
self.config_manager.logger.info("Configuration file modified, reloading...")
self.config_manager.reload_config()
self.observer = Observer()
self.observer.schedule(ConfigFileHandler(self), str(self.config_path.parent), recursive=False)
self.observer.start()
self.logger.info("Started configuration file watcher")
except ImportError:
self.logger.warning("watchdog not installed, dynamic config updates disabled")
def reload_config(self):
"""Reloads configuration from file."""
try:
self.config = self._load_config()
self._setup_logging()
self.logger.info("Configuration reloaded successfully")
except Exception as e:
self.logger.error(f"Failed to reload configuration: {e}")
def get(self, key_path: str, default=None):
"""Gets configuration value using dot notation.
Args:
key_path (str): Dot-separated path to config value (e.g., 'system.max_cpu_usage').
default: Value to return if key_path is not found.
Returns:
Value at the specified key path or default if not found.
"""
keys = key_path.split('.')
value = self.config
for key in keys:
if isinstance(value, dict) and key in value:
value = value[key]
else:
return default
return value
def __del__(self):
"""Stops the file watcher on cleanup."""
if self.observer:
self.observer.stop()
self.observer.join()
# ---------------------------
# Database Management
# ---------------------------
class DatabaseManager:
"""Manages SQLite database with async connection pooling.
Attributes:
config (ConfigManager): Configuration manager instance.
db_path (Path): Path to SQLite database file.
backup_path (Path): Path for database backups.
logger (logging.Logger): Logger instance for database events.
pool (aiosqlite.ConnectionPool): Async connection pool.
"""
def __init__(self, config: ConfigManager):
self.config = config
self.db_path = Path(config.get("database.path", "data/metamorphic_ai.db"))
self.backup_path = Path(config.get("database.backup_path", "data/backups"))
self.logger = logging.getLogger(__name__)
self.pool = None
asyncio.create_task(self._init_pool())
self._init_database()
async def _init_pool(self):
"""Initializes async SQLite connection pool."""
try:
self.pool = await aiosqlite.create_pool(str(self.db_path), max_size=10)
self.logger.info("Database connection pool initialized")
except Exception as e:
self.logger.error(f"Failed to initialize database pool: {e}")
raise
def _init_database(self):
"""Initializes database schema synchronously (runs once at startup)."""
self.db_path.parent.mkdir(parents=True, exist_ok=True)
self.backup_path.mkdir(parents=True, exist_ok=True)
with sqlite3.connect(self.db_path) as conn:
conn.executescript("""
CREATE TABLE IF NOT EXISTS shapes (
id TEXT PRIMARY KEY,
cognitive_mode TEXT NOT NULL,
interface_protocol TEXT NOT NULL,
personality_traits TEXT NOT NULL,
capabilities TEXT NOT NULL,
knowledge_domains TEXT NOT NULL,
response_latency_target REAL NOT NULL,
creativity_factor REAL NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
performance_score REAL DEFAULT 0.5
);
CREATE TABLE IF NOT EXISTS feedback (
id TEXT PRIMARY KEY,
shape_id TEXT,
interaction_id TEXT NOT NULL,
feedback_type TEXT NOT NULL,
score REAL NOT NULL,
metadata TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (shape_id) REFERENCES shapes (id)
);
CREATE TABLE IF NOT EXISTS morphing_history (
id TEXT PRIMARY KEY,
from_shape_id TEXT,
to_shape_id TEXT NOT NULL,
context TEXT NOT NULL,
transition_time REAL NOT NULL,
success BOOLEAN NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (from_shape_id) R
|
@@ -1,3 +1,11 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
datasets:
|
| 4 |
+
- HuggingFaceM4/FineVision
|
| 5 |
+
metrics:
|
| 6 |
+
- accuracy
|
| 7 |
+
base_model:
|
| 8 |
+
- deepseek-ai/DeepSeek-V3.1-Base
|
| 9 |
+
new_version: microsoft/VibeVoice-1.5B
|
| 10 |
+
library_name: asteroid
|
| 11 |
+
---
|