File size: 5,171 Bytes
559af1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
"""

Codette Web Interface

Flask-based web server for the Codette AI framework

"""

from flask import Flask, render_template, request, jsonify, session
from flask_cors import CORS
import asyncio
import json
import logging
from datetime import datetime
from typing import Dict, Any

# Import main application
from main import get_app, CodetteWebApplication

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Create Flask app
web_app = Flask(__name__)
web_app.secret_key = "codette_secret_key_2025"
CORS(web_app)

# Global Codette application instance
codette_app: CodetteWebApplication = None

@web_app.before_first_request
def initialize_codette():
    """Initialize Codette systems before first request"""
    global codette_app
    try:
        codette_app = get_app()
        logger.info("Codette systems initialized for web interface")
    except Exception as e:
        logger.error(f"Failed to initialize Codette: {e}")

@web_app.route('/')
def index():
    """Main dashboard page"""
    return render_template('index.html')

@web_app.route('/api/query', methods=['POST'])
def api_query():
    """API endpoint for processing queries"""
    try:
        data = request.get_json()
        query = data.get('query', '')
        user_id = data.get('user_id', 'web_user')
        
        if not query:
            return jsonify({"error": "Query is required"}), 400
        
        # Process query asynchronously
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        
        try:
            response = loop.run_until_complete(
                codette_app.process_query(query, user_id)
            )
            return jsonify(response)
        finally:
            loop.close()
            
    except Exception as e:
        logger.error(f"Query processing error: {e}")
        return jsonify({"error": str(e)}), 500

@web_app.route('/api/quantum-simulation', methods=['POST'])
def api_quantum_simulation():
    """API endpoint for running quantum simulations"""
    try:
        data = request.get_json()
        cores = data.get('cores', 4)
        
        if cores > 16:  # Safety limit
            cores = 16
        
        results = codette_app.run_quantum_simulation(cores)
        
        return jsonify({
            "status": "success",
            "cores_used": cores,
            "results": results,
            "timestamp": datetime.now().isoformat()
        })
        
    except Exception as e:
        logger.error(f"Quantum simulation error: {e}")
        return jsonify({"error": str(e)}), 500

@web_app.route('/api/health', methods=['GET'])
def api_health():
    """Health check endpoint"""
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        
        try:
            health = loop.run_until_complete(
                codette_app.health_monitor.check_status()
            )
            return jsonify({
                "status": "healthy",
                "metrics": health,
                "timestamp": datetime.now().isoformat()
            })
        finally:
            loop.close()
            
    except Exception as e:
        logger.error(f"Health check error: {e}")
        return jsonify({"error": str(e)}), 500

@web_app.route('/api/analyze-identity', methods=['POST'])
def api_analyze_identity():
    """API endpoint for fractal identity analysis"""
    try:
        data = request.get_json()
        
        # Default example data if not provided
        micro_generations = data.get('micro_generations', [
            {"update": "Initial state", "timestamp": "2025-01-01T00:00:00Z"},
            {"update": "State change 1", "timestamp": "2025-01-02T00:00:00Z"}
        ])
        
        informational_states = data.get('informational_states', [
            {"state_id": "state_1", "data": "Sample data 1"},
            {"state_id": "state_2", "data": "Sample data 2"}
        ])
        
        perspectives = data.get('perspectives', ["Quantum", "Classical", "Ethical"])
        
        results = codette_app.analyze_identity_fractal(
            micro_generations,
            informational_states,
            perspectives
        )
        
        return jsonify(results)
        
    except Exception as e:
        logger.error(f"Identity analysis error: {e}")
        return jsonify({"error": str(e)}), 500

@web_app.route('/dashboard')
def dashboard():
    """Codette dashboard page"""
    return render_template('dashboard.html')

@web_app.route('/quantum')
def quantum():
    """Quantum simulation interface"""
    return render_template('quantum.html')

@web_app.route('/cognitive')
def cognitive():
    """Cognitive processing interface"""
    return render_template('cognitive.html')

if __name__ == "__main__":
    # Initialize Codette systems
    initialize_codette()
    
    # Run the web application
    web_app.run(
        host='0.0.0.0',
        port=5000,
        debug=True
    )