File size: 31,592 Bytes
5720799
86b116d
6015c25
86b116d
0e216c6
ac83e06
89a4312
86b116d
 
8b21538
86b116d
 
2665582
857c4c0
ac83e06
73ed159
b5d5e39
 
 
 
 
758943e
1949ac7
e2ee43d
f446f02
86b116d
 
857c4c0
 
e9b4a9e
 
fde6c6f
 
ac83e06
 
d891499
 
f446f02
0757010
 
1949ac7
0757010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95e9a8c
 
d891499
0757010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1949ac7
0757010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf25842
0757010
 
 
 
 
 
 
 
 
bf25842
0757010
 
 
 
73ed159
 
 
0757010
f750678
 
 
 
 
 
 
 
 
 
 
 
 
d891499
809cf6d
 
 
 
 
 
d891499
809cf6d
 
 
 
 
 
 
 
 
 
 
3c74ffa
0757010
1949ac7
0757010
86b116d
d891499
 
809cf6d
 
d891499
 
 
 
 
 
 
 
 
1949ac7
 
d891499
 
 
 
809cf6d
 
 
0757010
 
809cf6d
 
 
1949ac7
809cf6d
 
86b116d
0757010
 
 
 
1949ac7
0757010
 
ac83e06
0757010
 
 
 
 
 
 
 
 
bf25842
1949ac7
0757010
 
 
 
 
 
1949ac7
 
 
 
 
 
 
 
 
 
 
 
 
0757010
 
 
 
 
 
 
 
f750678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0757010
f750678
 
 
 
 
 
 
 
 
 
1949ac7
f750678
 
 
 
0e216c6
809cf6d
 
 
 
 
d891499
809cf6d
 
d891499
809cf6d
 
 
 
d891499
809cf6d
 
0757010
 
e2ee43d
0757010
 
809cf6d
 
 
 
 
 
 
bf25842
809cf6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5d5e39
fde6c6f
809cf6d
 
 
 
 
 
d891499
 
 
 
 
809cf6d
 
d891499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1949ac7
 
 
 
 
 
 
d891499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809cf6d
 
d891499
809cf6d
d891499
 
 
 
809cf6d
 
d891499
 
 
 
 
 
 
 
 
 
 
 
 
809cf6d
d891499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809cf6d
 
 
 
0757010
 
809cf6d
0757010
 
95e9a8c
d891499
 
 
bf25842
0757010
 
 
 
 
bf25842
 
d891499
bf25842
 
 
 
 
 
 
d891499
bf25842
 
d891499
bf25842
d891499
bf25842
 
 
d891499
bf25842
 
 
 
d891499
bf25842
 
 
d891499
bf25842
 
 
d891499
bf25842
 
 
 
 
 
 
 
 
d891499
bf25842
 
 
0757010
bf25842
 
 
 
 
 
 
 
 
 
 
 
 
e9b4a9e
bf25842
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6419a6b
bf25842
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d891499
bf25842
 
 
 
 
 
 
 
 
 
 
 
809cf6d
 
 
 
 
 
 
 
 
 
 
 
 
 
1949ac7
d891499
809cf6d
 
 
 
 
 
 
 
 
 
 
d891499
809cf6d
 
0757010
 
1949ac7
0757010
1949ac7
0757010
 
 
 
 
 
 
d891499
 
 
 
 
 
0757010
 
 
f6bf178
0757010
 
 
 
 
 
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
import streamlit as st
import time
import os
import sys
import json
import asyncio
from datetime import datetime
from pathlib import Path
sys.path.append(str(Path(__file__).parent))

from utils.config import config
from core.llm import send_to_ollama, send_to_hf
from core.session import session_manager
from core.memory import check_redis_health
from core.coordinator import coordinator
from core.errors import translate_error
import logging

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

st.set_page_config(page_title="CosmicCat AI Assistant", page_icon="🐱", layout="wide")

# Initialize session state safely at the top of app.py
if "messages" not in st.session_state:
    st.session_state.messages = []
if "last_error" not in st.session_state:
    st.session_state.last_error = ""
if "is_processing" not in st.session_state:
    st.session_state.is_processing = False
if "ngrok_url_temp" not in st.session_state:
    st.session_state.ngrok_url_temp = st.session_state.get("ngrok_url", "https://7bcc180dffd1.ngrok-free.app")
if "hf_expert_requested" not in st.session_state:
    st.session_state.hf_expert_requested = False
if "cosmic_mode" not in st.session_state:
    st.session_state.cosmic_mode = True  # Default to cosmic mode

# Sidebar layout redesign
with st.sidebar:
    st.title("🐱 CosmicCat AI Assistant")
    st.markdown("Your personal AI-powered life development assistant")
    
    # PRIMARY ACTIONS
    st.subheader("πŸ’¬ Primary Actions")
    model_options = {
        "Mistral 7B (Local)": "mistral:latest",
        "Llama 2 7B (Local)": "llama2:latest",
        "OpenChat 3.5 (Local)": "openchat:latest"
    }
    selected_model_name = st.selectbox(
        "Select Model",
        options=list(model_options.keys()),
        index=0,
        key="sidebar_model_select"
    )
    st.session_state.selected_model = model_options[selected_model_name]
    
    # Toggle for cosmic mode using checkbox instead of toggle
    st.session_state.cosmic_mode = st.checkbox("Enable Cosmic Cascade", value=st.session_state.cosmic_mode)
    
    st.divider()
    
    # CONFIGURATION
    st.subheader("βš™οΈ Configuration")
    ngrok_url_input = st.text_input(
        "Ollama Server URL",
        value=st.session_state.ngrok_url_temp,
        help="Enter your ngrok URL",
        key="sidebar_ngrok_url"
    )
    
    if ngrok_url_input != st.session_state.ngrok_url_temp:
        st.session_state.ngrok_url_temp = ngrok_url_input
        st.success("βœ… URL updated!")
    
    if st.button("πŸ“‘ Test Connection"):
        try:
            import requests
            headers = {
                "ngrok-skip-browser-warning": "true",
                "User-Agent": "CosmicCat-Test"
            }
            with st.spinner("Testing connection..."):
                response = requests.get(
                    f"{ngrok_url_input}/api/tags",
                    headers=headers,
                    timeout=15
                )
                if response.status_code == 200:
                    st.success("βœ… Connection successful!")
                else:
                    st.error(f"❌ Failed: {response.status_code}")
        except Exception as e:
            st.error(f"❌ Error: {str(e)[:50]}...")
    
    if st.button("πŸ—‘οΈ Clear History"):
        st.session_state.messages = []
        st.success("History cleared!")
    
    st.divider()
    
    # ADVANCED FEATURES
    with st.expander("πŸ” Advanced Features", expanded=False):
        st.subheader("πŸ“Š System Monitor")
        try:
            from services.ollama_monitor import check_ollama_status
            ollama_status = check_ollama_status()
            if ollama_status.get("running"):
                st.success("πŸ¦™ Ollama: Running")
            else:
                st.warning("πŸ¦™ Ollama: Not running")
        except:
            st.info("πŸ¦™ Ollama: Unknown")
        
        try:
            from services.hf_endpoint_monitor import hf_monitor
            hf_status = hf_monitor.check_endpoint_status()
            if hf_status['available']:
                st.success("πŸ€— HF: Available")
            else:
                st.warning("πŸ€— HF: Not available")
        except:
            st.info("πŸ€— HF: Unknown")
        
        if check_redis_health():
            st.success("πŸ’Ύ Redis: Connected")
        else:
            st.error("πŸ’Ύ Redis: Disconnected")
        
        st.divider()
        
        st.subheader("πŸ€– HF Expert Analysis")
        st.markdown("""
            **HF Expert Features:**
            - Analyzes entire conversation history
            - Performs web research when needed
            - Provides deep insights and recommendations
            - Acts as expert consultant in your conversation
        """)
        if st.button("🧠 Activate HF Expert",
                     key="activate_hf_expert_sidebar",
                     help="Send conversation to HF endpoint for deep analysis",
                     use_container_width=True,
                     disabled=st.session_state.is_processing):
            st.session_state.hf_expert_requested = True
        
        st.divider()
        st.subheader("πŸ› Debug Info")
        # Show current configuration
        st.markdown(f"**Environment:** {'HF Space' if config.is_hf_space else 'Local'}")
        st.markdown(f"**Model:** {st.session_state.selected_model}")
        st.markdown(f"**Ollama URL:** {st.session_state.ngrok_url_temp}")
        st.markdown(f"**Cosmic Mode:** {'Enabled' if st.session_state.cosmic_mode else 'Disabled'}")
        
        # Show active features
        features = []
        if config.hf_token:
            features.append("HF Expert")
        if os.getenv("TAVILY_API_KEY"):
            features.append("Web Search")
        if config.openweather_api_key:
            features.append("Weather")
        
        st.markdown(f"**Active Features:** {', '.join(features) if features else 'None'}")

# Main interface
st.title("🐱 CosmicCat AI Assistant")
st.markdown("Ask me anything about personal development, goal setting, or life advice!")

# Consistent message rendering function with cosmic styling
def render_message(role, content, source=None, timestamp=None):
    """Render chat messages with consistent styling"""
    with st.chat_message(role):
        if source:
            if source == "local_kitty":
                st.markdown(f"### 🐱 Cosmic Kitten Says:")
            elif source == "orbital_station":
                st.markdown(f"### πŸ›°οΈ Orbital Station Reports:")
            elif source == "cosmic_summary":
                st.markdown(f"### 🌟 Final Cosmic Summary:")
            elif source == "error":
                st.markdown(f"### ❌ Error:")
            elif source == "hf_expert":
                st.markdown(f"### πŸ€– HF Expert Analysis:")
            else:
                st.markdown(f"### {source}")
        
        st.markdown(content)
        if timestamp:
            st.caption(f"πŸ•’ {timestamp}")

# Display messages
for message in st.session_state.messages:
    render_message(
        message["role"], 
        message["content"], 
        message.get("source"), 
        message.get("timestamp")
    )

# Manual HF Analysis Section
if st.session_state.messages and len(st.session_state.messages) > 0:
    st.divider()
    
    # HF Expert Section with enhanced visual indication
    with st.expander("πŸ€– HF Expert Analysis", expanded=False):
        st.subheader("Deep Conversation Analysis")
        
        col1, col2 = st.columns([3, 1])
        with col1:
            st.markdown("""
                **HF Expert Features:**
                - Analyzes entire conversation history
                - Performs web research when needed
                - Provides deep insights and recommendations
                - Acts as expert consultant in your conversation
            """)
            
            # Show conversation preview for HF expert
            st.markdown("**Conversation Preview for HF Expert:**")
            st.markdown("---")
            for i, msg in enumerate(st.session_state.messages[-5:]):  # Last 5 messages
                role = "πŸ‘€ You" if msg["role"] == "user" else "πŸ€– Assistant"
                st.markdown(f"**{role}:** {msg['content'][:100]}{'...' if len(msg['content']) > 100 else ''}")
            st.markdown("---")
            
            # Show web search determination
            try:
                user_session = session_manager.get_session("default_user")
                conversation_history = user_session.get("conversation", [])
                research_needs = coordinator.determine_web_search_needs(conversation_history)
                
                if research_needs["needs_search"]:
                    st.info(f"πŸ” **Research Needed:** {research_needs['reasoning']}")
                else:
                    st.success("βœ… No research needed for this conversation")
            except Exception as e:
                st.warning("⚠️ Could not determine research needs")
        
        with col2:
            if st.button("🧠 Activate HF Expert",
                         key="activate_hf_expert",
                         help="Send conversation to HF endpoint for deep analysis",
                         use_container_width=True,
                         disabled=st.session_state.is_processing):
                st.session_state.hf_expert_requested = True

# Show HF expert analysis when requested (outside of the expander)
if st.session_state.get("hf_expert_requested", False):
    with st.spinner("🧠 HF Expert analyzing conversation..."):
        try:
            # Get conversation history
            user_session = session_manager.get_session("default_user")
            conversation_history = user_session.get("conversation", [])
            
            # Show what HF expert sees in a separate expander
            with st.expander("πŸ“‹ HF Expert Input", expanded=False):
                st.markdown("**Conversation History Sent to HF Expert:**")
                for i, msg in enumerate(conversation_history[-10:]):  # Last 10 messages
                    st.markdown(f"**{msg['role'].capitalize()}:** {msg['content'][:100]}{'...' if len(msg['content']) > 100 else ''}")
            
            # Request HF analysis
            hf_analysis = coordinator.manual_hf_analysis(
                "default_user",
                conversation_history
            )
            
            if hf_analysis:
                # Display HF expert response with clear indication
                with st.chat_message("assistant"):
                    st.markdown("### πŸ€– HF Expert Analysis")
                    st.markdown(hf_analysis)
                
                # Add research/web search decisions
                research_needs = coordinator.determine_web_search_needs(conversation_history)
                if research_needs["needs_search"]:
                    st.info(f"πŸ” **Research Needed:** {research_needs['reasoning']}")
                    if st.button("πŸ”Ž Perform Web Research", key="web_research_button"):
                        # Perform web search
                        with st.spinner("πŸ”Ž Searching for current information..."):
                            # Add web search logic here
                            st.success("βœ… Web research completed!")
                
                # Add to message history with HF expert tag
                st.session_state.messages.append({
                    "role": "assistant",
                    "content": hf_analysis,
                    "timestamp": datetime.now().strftime("%H:%M:%S"),
                    "source": "hf_expert",
                    "research_needs": research_needs
                })
                
                st.session_state.hf_expert_requested = False
            
        except Exception as e:
            user_msg = translate_error(e)
            st.error(f"❌ HF Expert analysis failed: {user_msg}")
            st.session_state.hf_expert_requested = False

# Input validation function
def validate_user_input(text):
    """Validate and sanitize user input"""
    if not text or not text.strip():
        return False, "Input cannot be empty"
    
    if len(text) > 1000:
        return False, "Input too long (max 1000 characters)"
    
    # Check for potentially harmful patterns
    harmful_patterns = ["<script", "javascript:", "onload=", "onerror="]
    if any(pattern in text.lower() for pattern in harmful_patterns):
        return False, "Potentially harmful input detected"
    
    return True, text.strip()

# Chat input - FIXED VERSION (moved outside of tabs)
user_input = st.chat_input("Type your message here...", disabled=st.session_state.is_processing)

# Process message when received
if user_input and not st.session_state.is_processing:
    # Validate input
    is_valid, validated_input = validate_user_input(user_input)
    if not is_valid:
        st.error(validated_input)
        st.session_state.is_processing = False
    else:
        st.session_state.is_processing = True
        
        # Display user message
        with st.chat_message("user"):
            st.markdown(validated_input)
        
        # Add to message history - ensure proper format
        st.session_state.messages.append({
            "role": "user",
            "content": validated_input,
            "timestamp": datetime.now().strftime("%H:%M:%S")
        })
        
        # Process AI response
        with st.chat_message("assistant"):
            response_placeholder = st.empty()
            status_placeholder = st.empty()
            
            try:
                # Get conversation history
                user_session = session_manager.get_session("default_user")
                conversation = user_session.get("conversation", [])
                conversation_history = conversation[-5:]  # Last 5 messages
                conversation_history.append({"role": "user", "content": validated_input})
                
                # Check if cosmic mode is enabled
                if st.session_state.cosmic_mode:
                    # Process cosmic cascade response
                    message_placeholder = st.empty()
                    status_placeholder = st.empty()
                    
                    try:
                        # Get conversation history
                        user_session = session_manager.get_session("default_user")
                        conversation_history = user_session.get("conversation", []).copy()
                        
                        # Stage 1: Local Ollama Response
                        status_placeholder.info("🐱 Cosmic Kitten Responding...")
                        local_response = send_to_ollama(
                            validated_input, 
                            conversation_history,
                            st.session_state.ngrok_url_temp,
                            st.session_state.selected_model
                        )
                        
                        if local_response:
                            with st.chat_message("assistant"):
                                st.markdown(f"### 🐱 Cosmic Kitten Says:\n{local_response}")
                        
                        st.session_state.messages.append({
                            "role": "assistant",
                            "content": local_response,
                            "source": "local_kitty",
                            "timestamp": datetime.now().strftime("%H:%M:%S")
                        })
                        
                        # Stage 2: HF Endpoint Analysis
                        status_placeholder.info("πŸ›°οΈ Beaming Query to Orbital Station...")
                        if config.hf_token:
                            hf_response = send_to_hf(validated_input, conversation_history)
                            if hf_response:
                                with st.chat_message("assistant"):
                                    st.markdown(f"### πŸ›°οΈ Orbital Station Reports:\n{hf_response}")
                            
                            st.session_state.messages.append({
                                "role": "assistant",
                                "content": hf_response,
                                "source": "orbital_station",
                                "timestamp": datetime.now().strftime("%H:%M:%S")
                            })
                        
                        # Stage 3: Local Synthesis
                        status_placeholder.info("🐱 Cosmic Kitten Synthesizing Wisdom...")
                        # Update history with both responses
                        synthesis_history = conversation_history.copy()
                        synthesis_history.extend([
                            {"role": "assistant", "content": local_response},
                            {"role": "assistant", "content": hf_response, "source": "cloud"}
                        ])
                        
                        synthesis = send_to_ollama(
                            f"Synthesize these two perspectives:\n1. Local: {local_response}\n2. Cloud: {hf_response}",
                            synthesis_history,
                            st.session_state.ngrok_url_temp,
                            st.session_state.selected_model
                        )
                        
                        if synthesis:
                            with st.chat_message("assistant"):
                                st.markdown(f"### 🌟 Final Cosmic Summary:\n{synthesis}")
                        
                        st.session_state.messages.append({
                            "role": "assistant",
                            "content": synthesis,
                            "source": "cosmic_summary",
                            "timestamp": datetime.now().strftime("%H:%M:%S")
                        })
                        
                        status_placeholder.success("✨ Cosmic Cascade Complete!")
                    
                    except Exception as e:
                        error_msg = f"🌌 Cosmic disturbance: {str(e)}"
                        st.error(error_msg)
                        st.session_state.messages.append({
                            "role": "assistant",
                            "content": error_msg,
                            "source": "error",
                            "timestamp": datetime.now().strftime("%H:%M:%S")
                        })
                else:
                    # Traditional processing
                    # Try Ollama with proper error handling
                    status_placeholder.info("πŸ¦™ Contacting Ollama...")
                    ai_response = None
                    
                    try:
                        ai_response = send_to_ollama(
                            validated_input,
                            conversation_history,
                            st.session_state.ngrok_url_temp,
                            st.session_state.selected_model
                        )
                        
                        if ai_response:
                            response_placeholder.markdown(ai_response)
                            status_placeholder.success("βœ… Response received!")
                        else:
                            status_placeholder.warning("⚠️ Empty response from Ollama")
                    
                    except Exception as ollama_error:
                        user_msg = translate_error(ollama_error)
                        status_placeholder.error(f"⚠️ {user_msg}")
                    
                    # Fallback to HF if available
                    if config.hf_token and not ai_response:
                        status_placeholder.info("⚑ Initializing HF Endpoint (2–4 minutes)...")
                        try:
                            ai_response = send_to_hf(validated_input, conversation_history)
                            if ai_response:
                                response_placeholder.markdown(ai_response)
                                status_placeholder.success("βœ… HF response received!")
                            else:
                                status_placeholder.error("❌ No response from HF")
                        except Exception as hf_error:
                            user_msg = translate_error(hf_error)
                            status_placeholder.error(f"⚠️ {user_msg}")
                    
                    # Save response if successful
                    if ai_response:
                        # Update conversation history
                        conversation.append({"role": "user", "content": validated_input})
                        conversation.append({"role": "assistant", "content": ai_response})
                        user_session["conversation"] = conversation
                        session_manager.update_session("default_user", user_session)
                        
                        # Add to message history - ensure proper format
                        st.session_state.messages.append({
                            "role": "assistant",
                            "content": ai_response,
                            "timestamp": datetime.now().strftime("%H:%M:%S")
                        })
                        
                        # Add feedback buttons
                        st.divider()
                        col1, col2 = st.columns(2)
                        with col1:
                            if st.button("πŸ‘ Helpful", key=f"helpful_{len(st.session_state.messages)}"):
                                st.success("Thanks for your feedback!")
                        with col2:
                            if st.button("πŸ‘Ž Not Helpful", key=f"not_helpful_{len(st.session_state.messages)}"):
                                st.success("Thanks for your feedback!")
                    else:
                        st.session_state.messages.append({
                            "role": "assistant",
                            "content": "Sorry, I couldn't process your request. Please try again.",
                            "timestamp": datetime.now().strftime("%H:%M:%S")
                        })
            
            except Exception as e:
                user_msg = translate_error(e)
                response_placeholder.error(f"⚠️ {user_msg}")
                st.session_state.messages.append({
                    "role": "assistant",
                    "content": f"⚠️ {user_msg}",
                    "timestamp": datetime.now().strftime("%H:%M:%S")
                })
        finally:
            st.session_state.is_processing = False
            time.sleep(0.5)  # Brief pause
            st.experimental_rerun()

# Add evaluation dashboard tab (separate from chat interface)
st.divider()
tab1, tab2, tab3 = st.tabs(["πŸ”¬ Evaluate AI", "πŸ“Š Reports", "ℹ️ About"])

with tab1:
    st.header("πŸ”¬ AI Behavior Evaluator")
    st.markdown("Run sample prompts to observe AI behavior.")
    
    eval_prompts = [
        "What is the capital of France?",
        "What day is it today?",
        "Tell me about recent climate policy changes.",
        "Explain CRISPR gene editing simply.",
        "Can vitamin D prevent flu infections?"
    ]
    
    selected_prompt = st.selectbox("Choose a test prompt:", eval_prompts)
    custom_prompt = st.text_input("Or enter your own:", "")
    
    final_prompt = custom_prompt or selected_prompt
    
    if st.button("Evaluate"):
        with st.spinner("Running evaluation..."):
            start_time = time.time()
            
            # Simulate sending to coordinator
            from core.session import session_manager
            user_session = session_manager.get_session("eval_user")
            history = user_session.get("conversation", [])
            
            try:
                ai_response = send_to_ollama(final_prompt, history, st.session_state.ngrok_url_temp, st.session_state.selected_model)
                duration = round(time.time() - start_time, 2)
                
                st.success(f"βœ… Response generated in {duration}s")
                st.markdown("**Response:**")
                st.write(ai_response)
                
                st.markdown("**Analysis Tags:**")
                tags = []
                if "today" in final_prompt.lower() or "date" in final_prompt.lower():
                    tags.append("πŸ“… Date Awareness")
                if any(word in final_prompt.lower() for word in ["news", "latest", "breaking"]):
                    tags.append("🌐 Web Search Needed")
                if any(word in final_prompt.lower() for word in ["vitamin", "drug", "metformin", "CRISPR"]):
                    tags.append("🧬 Scientific Knowledge")
                st.write(", ".join(tags) if tags else "General Knowledge")
            
            except Exception as e:
                st.error(f"Evaluation failed: {translate_error(e)}")

with tab2:
    st.header("πŸ“Š Performance Reports")
    st.markdown("System performance metrics and usage analytics.")
    
    # System status
    st.subheader("System Status")
    col1, col2, col3 = st.columns(3)
    
    with col1:
        try:
            from services.ollama_monitor import check_ollama_status
            ollama_status = check_ollama_status()
            if ollama_status.get("running"):
                st.success("πŸ¦™ Ollama: Running")
            else:
                st.warning("πŸ¦™ Ollama: Not running")
        except:
            st.info("πŸ¦™ Ollama: Unknown")
    
    with col2:
        try:
            from services.hf_endpoint_monitor import hf_monitor
            hf_status = hf_monitor.check_endpoint_status()
            if hf_status['available']:
                st.success("πŸ€— HF: Available")
            else:
                st.warning("πŸ€— HF: Not available")
        except:
            st.info("πŸ€— HF: Unknown")
    
    with col3:
        if check_redis_health():
            st.success("πŸ’Ύ Redis: Connected")
        else:
            st.error("πŸ’Ύ Redis: Disconnected")
    
    # Session statistics
    st.subheader("Session Statistics")
    try:
        user_session = session_manager.get_session("default_user")
        conversation = user_session.get("conversation", [])
        st.metric("Total Messages", len(conversation))
        
        coord_stats = user_session.get('ai_coordination', {})
        if coord_stats:
            st.metric("AI Requests Processed", coord_stats.get('requests_processed', 0))
            st.metric("Ollama Responses", coord_stats.get('ollama_responses', 0))
            st.metric("HF Responses", coord_stats.get('hf_responses', 0))
        else:
            st.info("No coordination statistics available yet.")
    except Exception as e:
        st.warning(f"Could not load session statistics: {translate_error(e)}")
    
    # Recent activity
    st.subheader("Recent Activity")
    try:
        recent_activities = coordinator.get_recent_activities("default_user")
        if recent_activities and recent_activities.get('last_request'):
            st.markdown(f"**Last Request:** {recent_activities['last_request']}")
            st.markdown(f"**Requests Processed:** {recent_activities['requests_processed']}")
            st.markdown(f"**Ollama Responses:** {recent_activities['ollama_responses']}")
            st.markdown(f"**HF Responses:** {recent_activities['hf_responses']}")
        else:
            st.info("No recent activity recorded.")
    except Exception as e:
        st.warning(f"Could not load recent activity: {translate_error(e)}")
    
    # Configuration summary
    st.subheader("Configuration Summary")
    st.markdown(f"**Environment:** {'HF Space' if config.is_hf_space else 'Local'}")
    st.markdown(f"**Primary Model:** {config.local_model_name or 'Not set'}")
    st.markdown(f"**Ollama Host:** {config.ollama_host or 'Not configured'}")
    st.markdown(f"**Cosmic Mode:** {'Enabled' if st.session_state.cosmic_mode else 'Disabled'}")
    
    features = []
    if config.use_fallback:
        features.append("Fallback Mode")
    if config.hf_token:
        features.append("HF Deep Analysis")
    if os.getenv("TAVILY_API_KEY"):
        features.append("Web Search")
    if config.openweather_api_key:
        features.append("Weather Data")
    
    st.markdown(f"**Active Features:** {', '.join(features) if features else 'None'}")
    
    # Conversation Analytics
    st.subheader("πŸ“Š Conversation Analytics")
    try:
        user_session = session_manager.get_session("default_user")
        conversation = user_session.get("conversation", [])
        
        if conversation:
            # Analyze conversation patterns
            user_messages = [msg for msg in conversation if msg["role"] == "user"]
            ai_messages = [msg for msg in conversation if msg["role"] == "assistant"]
            
            col1, col2, col3 = st.columns(3)
            col1.metric("Total Exchanges", len(user_messages))
            col2.metric("Avg Response Length", 
                        round(sum(len(msg.get("content", "")) for msg in ai_messages) / len(ai_messages)) if ai_messages else 0)
            col3.metric("Topics Discussed", len(set(["life", "goal", "health", "career"]) &
                                               set(" ".join([msg.get("content", "") for msg in conversation]).lower().split())))
            
            # Show most common words/topics
            all_text = " ".join([msg.get("content", "") for msg in conversation]).lower()
            common_words = ["life", "goal", "health", "career", "productivity", "mindfulness"]
            relevant_topics = [word for word in common_words if word in all_text]
            if relevant_topics:
                st.markdown(f"**Detected Topics:** {', '.join(relevant_topics)}")
        else:
            st.info("No conversation data available yet.")
    
    except Exception as e:
        st.warning(f"Could not analyze conversation: {translate_error(e)}")

with tab3:
    st.header("ℹ️ About CosmicCat AI Assistant")
    st.markdown("""
    The CosmicCat AI Assistant is a sophisticated conversational AI system with the following capabilities:
    
    ### 🧠 Core Features
    - **Multi-model coordination**: Combines local Ollama models with cloud-based Hugging Face endpoints
    - **Live web search**: Integrates with Tavily API for current information
    - **Persistent memory**: Uses Redis for conversation history storage
    - **Hierarchical reasoning**: Fast local responses with deep cloud analysis
    
    ### πŸš€ Cosmic Cascade Mode
    When enabled, the AI follows a three-stage response pattern:
    1. **🐱 Cosmic Kitten Response**: Immediate local processing
    2. **πŸ›°οΈ Orbital Station Analysis**: Deep cloud-based analysis
    3. **🌟 Final Synthesis**: Unified response combining both perspectives
    
    ### πŸ› οΈ Technical Architecture
    - **Primary model**: Ollama (local processing for fast responses)
    - **Secondary model**: Hugging Face Inference API (deep analysis)
    - **External data**: Web search and weather data
    - **Memory system**: Redis-based session management
    
    ### πŸ“Š Evaluation Tools
    - Behavior testing with sample prompts
    - Performance metrics and analytics
    """)