File size: 11,980 Bytes
6d6b8af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# app.py
import sys
import os
import traceback
import gradio as gr
import logging
import asyncio
import torch
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from transformers import AutoModelForCausalLM, AutoTokenizer
from ai_core import AICore
from aegis_integration import AegisBridge
from aegis_integration.config import AEGIS_CONFIG
from components.search_engine import SearchEngine

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

# Initialize language model
logger.info("Initializing language model...")
model_name = "gpt2-large"  # Using larger model for better responses

try:
    # Initialize components with proper error handling
    try:
        # Initialize tokenizer with padding
        tokenizer = AutoTokenizer.from_pretrained(model_name)
        tokenizer.pad_token = tokenizer.eos_token
        logger.info("Tokenizer initialized successfully")
    except Exception as e:
        logger.error(f"Error initializing tokenizer: {e}")
        raise
    
    try:
        # Load model with optimal settings
        model = AutoModelForCausalLM.from_pretrained(
            model_name,
            pad_token_id=tokenizer.eos_token_id,
            repetition_penalty=1.2
        )
        logger.info("Model loaded successfully")
    except Exception as e:
        logger.error(f"Error loading model: {e}")
        raise
    
    # Use GPU if available
    try:
        if torch.cuda.is_available():
            model = model.cuda()
            logger.info("Using GPU for inference")
        else:
            logger.info("Using CPU for inference")
            
        # Set to evaluation mode
        model.eval()
    except Exception as e:
        logger.error(f"Error configuring model device: {e}")
        raise
    
    try:
        # Initialize AI Core with full component setup
        ai_core = AICore()
        ai_core.model = model
        ai_core.tokenizer = tokenizer
        ai_core.model_id = model_name
        
        # Initialize cognitive processor with default modes
        from cognitive_processor import CognitiveProcessor
        cognitive_modes = ["scientific", "creative", "quantum", "philosophical"]
        ai_core.cognitive_processor = CognitiveProcessor(
            modes=cognitive_modes,
            quantum_state={"coherence": 0.5}
        )
        logger.info(
            f"AI Core initialized successfully with modes: {cognitive_modes}"
        )
    except Exception as e:
        logger.error(f"Error initializing AI Core: {e}")
        raise
    
    # Initialize AEGIS
    aegis_bridge = AegisBridge(ai_core, AEGIS_CONFIG)
    ai_core.set_aegis_bridge(aegis_bridge)
    
    # Initialize cocoon manager
    try:
        from utils.cocoon_manager import CocoonManager
        cocoon_manager = CocoonManager("./cocoons")
        cocoon_manager.load_cocoons()
        
        # Set up AI core with cocoon data
        ai_core.cocoon_manager = cocoon_manager
        ai_core.quantum_state = cocoon_manager.get_latest_quantum_state()
        logger.info(
            f"Loaded {len(cocoon_manager.cocoon_data)} existing cocoons "
            f"with quantum coherence {ai_core.quantum_state.get('coherence', 0.5)}"
        )
    except Exception as e:
        logger.error(f"Error initializing cocoon manager: {e}")
        # Initialize with defaults if cocoon loading fails
        ai_core.quantum_state = {"coherence": 0.5}
    
    logger.info("Core systems initialized successfully")
    
except Exception as e:
    logger.error(f"Error initializing model: {e}")
    sys.exit(1)

async def process_message(message: str, history: list) -> tuple:
    """Process chat messages with improved context management"""
    try:
        # Clean input
        message = message.strip()
        if not message:
            return "", history
            
        try:
            # Get response from AI core asynchronously
            if hasattr(ai_core, 'generate_text_async'):
                response = await ai_core.generate_text_async(message)
            else:
                # Fallback to sync version in ThreadPoolExecutor
                loop = asyncio.get_event_loop()
                with ThreadPoolExecutor() as pool:
                    response = await loop.run_in_executor(
                        pool, 
                        ai_core.generate_text, 
                        message
                    )
            
            # Clean and validate response
            if response is None:
                raise ValueError("Generated response is None")
                
            if len(response) > 1000:  # Increased safety check limit
                response = response[:997] + "..."
            
            # Update history
            history.append((message, response))
            return "", history
                
        except Exception as e:
            logger.error(f"Error generating response: {e}")
            raise
            
    except Exception as e:
        logger.error(f"Error in chat: {str(e)}\n{traceback.format_exc()}")
        error_msg = (
            "I apologize, but I encountered an error processing your request. "
            "Please try again with a different query."
        )
        history.append((message, error_msg))
        return "", history

def clear_history():
    """Clear the chat history and AI core memory"""
    ai_core.response_memory = []  # Clear AI memory
    ai_core.last_clean_time = datetime.now()
    return [], []

# Initialize search engine
search_engine = SearchEngine()

async def search_knowledge(query: str) -> str:
    """Perform a search and return formatted results"""
    try:
        return await search_engine.get_knowledge(query)
    except Exception as e:
        logger.error(f"Search error: {e}")
        return f"I encountered an error while searching: {str(e)}"

def sync_search(query: str) -> str:
    """Synchronous wrapper for search function"""
    return asyncio.run(search_knowledge(query))

# Create the Gradio interface with improved chat components and search
with gr.Blocks(title="Codette", theme=gr.themes.Soft()) as iface:
    gr.Markdown("""# 🤖 Codette

    Your AI programming assistant with chat and search capabilities.""")
    
    with gr.Tabs():
        with gr.Tab("Chat"):
            chatbot = gr.Chatbot(
                [],
                elem_id="chatbot",
                bubble_full_width=False,
                avatar_images=("👤", "🤖"),
                height=500,
                show_label=False,
                container=True
            )
            
            with gr.Row():
                txt = gr.Textbox(
                    show_label=False,
                    placeholder="Type your message here...",
                    container=False,
                    scale=8,
                    autofocus=True
                )
                submit_btn = gr.Button("Send", scale=1, variant="primary")
            
            with gr.Row():
                clear_btn = gr.Button("Clear Chat")
            
            # Set up chat event handlers with proper async queuing
            txt.submit(
                process_message, 
                [txt, chatbot], 
                [txt, chatbot],
                api_name="chat_submit",
                queue=True  # Enable queuing for async
            ).then(
                lambda: None,  # Cleanup callback
                None,
                None,
                api_name=None
            )
            
            submit_btn.click(
                process_message, 
                [txt, chatbot], 
                [txt, chatbot],
                api_name="chat_button",
                queue=True  # Enable queuing for async
            ).then(
                lambda: None,  # Cleanup callback
                None,
                None,
                api_name=None
            )
            
            clear_btn.click(
                clear_history, 
                None, 
                [chatbot, txt], 
                queue=False,
                api_name="clear_chat"
            )
            
        with gr.Tab("Search"):
            gr.Markdown("""### 🔍 Knowledge Search

            Search through Codette's knowledge base for information about AI, programming, and technology.""")
            
            with gr.Row():
                search_input = gr.Textbox(
                    show_label=False,
                    placeholder="Enter your search query...",
                    container=False,
                    scale=8
                )
                search_btn = gr.Button("Search", scale=1, variant="primary")
            
            search_output = gr.Markdown()
            
            # Set up search event handlers
            search_btn.click(sync_search, search_input, search_output)
            search_input.submit(sync_search, search_input, search_output)
# Run the Gradio interface with proper async handling
async def shutdown():
    """Cleanup function for graceful shutdown"""
    try:
        # Save final quantum state if available
        if hasattr(ai_core, 'cocoon_manager') and ai_core.cocoon_manager:
            try:
                ai_core.cocoon_manager.save_cocoon({
                    "type": "shutdown",
                    "quantum_state": ai_core.quantum_state
                })
                logger.info("Final quantum state saved")
            except Exception as e:
                logger.error(f"Error saving final quantum state: {e}")
        
        # Shutdown AI core
        try:
            await ai_core.shutdown()
            logger.info("AI Core shutdown complete")
        except Exception as e:
            logger.error(f"Error shutting down AI Core: {e}")
            
        # Clear CUDA cache if GPU was used
        if torch.cuda.is_available():
            try:
                torch.cuda.empty_cache()
                logger.info("CUDA cache cleared")
            except Exception as e:
                logger.error(f"Error clearing CUDA cache: {e}")
                
    except Exception as e:
        logger.error(f"Error during shutdown: {e}")
        raise

if __name__ == "__main__":
    try:
        # Set up exception handling
        def handle_exception(loop, context):
            msg = context.get("exception", context["message"])
            logger.error(f"Caught exception: {msg}")
            
        # Set up asyncio event loop with proper error handling
        loop = asyncio.new_event_loop()
        loop.set_exception_handler(handle_exception)
        asyncio.set_event_loop(loop)
        
        # Launch Gradio interface
        iface.queue().launch(
            prevent_thread_lock=True,
            share=False,
            server_name="127.0.0.1",
            show_error=True
        )
        
        try:
            # Keep the main loop running
            loop.run_forever()
        except Exception as e:
            logger.error(f"Error in main loop: {e}")
            traceback.print_exc()
    except KeyboardInterrupt:
        logger.info("Shutting down gracefully...")
        try:
            loop.run_until_complete(shutdown())
        except Exception as e:
            logger.error(f"Error during shutdown: {e}")
    finally:
        try:
            tasks = asyncio.all_tasks(loop)
            for task in tasks:
                task.cancel()
            loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
            loop.close()
        except Exception as e:
            logger.error(f"Error closing loop: {e}")
            sys.exit(1)
        sys.exit(0)