File size: 21,638 Bytes
5720799 86b116d 6015c25 86b116d 0e216c6 ac83e06 89a4312 86b116d 2665582 857c4c0 ac83e06 73ed159 bed2d0a e0ec429 b40bdef b5d5e39 758943e 1949ac7 e2ee43d f446f02 86b116d 857c4c0 e9b4a9e fde6c6f d891499 bed2d0a 0757010 1949ac7 0757010 22e5f83 bed2d0a d891499 0757010 22e5f83 0757010 b40bdef 0757010 22e5f83 0757010 22e5f83 0757010 bed2d0a 0757010 9144b02 0757010 9144b02 e0ec429 bed2d0a e0ec429 bed2d0a 9144b02 e0ec429 9144b02 0757010 9144b02 e0ec429 9144b02 0757010 9144b02 22e5f83 e0ec429 22e5f83 e0ec429 3c74ffa 0757010 1949ac7 0757010 86b116d bed2d0a aba1e9b bed2d0a d891499 809cf6d d891499 bed2d0a d891499 809cf6d 0757010 809cf6d 22e5f83 809cf6d 86b116d 809cf6d 9144b02 809cf6d 9144b02 809cf6d 737aa03 0757010 e2ee43d 0757010 809cf6d 0b2b7e6 809cf6d 9144b02 2262f42 809cf6d 9144b02 809cf6d 9144b02 809cf6d 0b2b7e6 9144b02 0b2b7e6 809cf6d 0b2b7e6 809cf6d 0b2b7e6 d891499 0b2b7e6 d891499 0b2b7e6 22e5f83 0b2b7e6 22e5f83 0b2b7e6 d891499 0b2b7e6 d891499 0b2b7e6 d891499 0b2b7e6 22e5f83 0b2b7e6 9144b02 0b2b7e6 22e5f83 0b2b7e6 bf25842 052fcc8 0757010 052fcc8 0757010 1949ac7 0757010 1949ac7 0757010 bed2d0a d891499 22e5f83 d891499 0757010 bed2d0a 0757010 bed2d0a 2262f42 1f63383 22e5f83 1f63383 0b2b7e6 |
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 |
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.session import session_manager
from core.memory import check_redis_health
from core.coordinator import coordinator
from core.errors import translate_error
from core.personality import personality
from services.hf_endpoint_monitor import hf_monitor
from services.weather import weather_service
from core.llm import LLMClient
from core.providers.ollama import OllamaProvider
from core.providers.huggingface import HuggingFaceProvider
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 "cosmic_mode" not in st.session_state:
st.session_state.cosmic_mode = True # Default to cosmic mode
if "show_welcome" not in st.session_state:
st.session_state.show_welcome = True
# 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
st.session_state.cosmic_mode = st.checkbox("Enable Cosmic Mode", 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:
# Use OllamaProvider to test connection
ollama_provider = OllamaProvider(st.session_state.selected_model)
# Test model validation
is_valid = ollama_provider.validate_model()
if is_valid:
st.success("β
Connection successful!")
else:
st.error("β Model validation failed")
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()
# SYSTEM STATUS
with st.expander("π System Status", 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:
hf_status = hf_monitor.check_endpoint_status()
# Enhanced HF status display with cat-themed messages
if hf_status.get('available'):
if hf_status.get('initialized', False):
st.success(f"π€ HF Endpoint: Available ({hf_status.get('status_code')} OK)")
if hf_status.get('model'):
st.info(f" Model: {hf_status.get('model')}")
if hf_status.get('region'):
st.info(f" Region: {hf_status.get('region')}")
if hf_status.get('warmup_count'):
st.info(f" Warmup Count: {hf_status.get('warmup_count')}")
else:
st.warning("β³ Kittens Waking Up...")
elif hf_status.get('status_code') == 200:
st.info("π‘ Calling Space Friends...")
else:
st.error("π΄ Nap Cat")
except Exception as e:
st.info("β³ Kittens Stretching...")
if check_redis_health():
st.success("πΎ Redis: Connected")
else:
st.error("πΎ Redis: Disconnected")
st.divider()
st.subheader("π Debug Info")
# Show enhanced debug information
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"**Fallback:** {'Enabled' if config.use_fallback else 'Disabled'}")
# Show active features
features = []
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'}")
# Show recent activity
try:
user_session = session_manager.get_session("default_user")
coord_stats = user_session.get('ai_coordination', {})
if coord_stats and coord_stats.get('last_coordination'):
st.markdown(f"**Last Request:** {coord_stats.get('last_coordination')}")
else:
st.markdown("**Last Request:** N/A")
except:
st.markdown("**Last Request:** N/A")
# Show Ollama ping status
try:
import requests
import time
start_time = time.time()
headers = {
"ngrok-skip-browser-warning": "true",
"User-Agent": "CosmicCat-Debug"
}
response = requests.get(
f"{st.session_state.ngrok_url_temp}/api/tags",
headers=headers,
timeout=15
)
ping_time = round((time.time() - start_time) * 1000)
if response.status_code == 200:
st.markdown(f"**Ollama Ping:** {response.status_code} OK ({ping_time}ms)")
else:
st.markdown(f"**Ollama Ping:** {response.status_code} Error")
except Exception as e:
st.markdown("**Ollama Ping:** Unreachable")
# Redis status
if check_redis_health():
st.markdown("**Redis:** Healthy")
else:
st.markdown("**Redis:** Unhealthy")
# Main interface
st.title("π± CosmicCat AI Assistant")
st.markdown("Ask me anything about personal development, goal setting, or life advice!")
# Show welcome message only once
if st.session_state.show_welcome:
with st.chat_message("assistant"):
greeting = personality.get_greeting(cosmic_mode=st.session_state.cosmic_mode)
st.markdown(greeting)
st.session_state.show_welcome = False
# 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 == "space_story":
st.markdown(f"### π± Cosmic Kitten Story:")
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")
)
# 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
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
st.experimental_rerun() # Fixed: use experimental_rerun
else:
st.session_state.is_processing = True
# Display user message immediately
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
response_container = st.empty()
status_placeholder = st.empty()
response_placeholder = st.empty()
try:
# Get conversation history from session
user_session = session_manager.get_session("default_user")
conversation_history = user_session.get("conversation", []).copy()
# Add the current user message to history for context
conversation_history.append({"role": "user", "content": validated_input})
# Check if cosmic mode is enabled
if st.session_state.cosmic_mode:
# Process cosmic cascade response
status_placeholder.info("π± Cosmic Kitten Responding...")
try:
# Get conversation history
user_session = session_manager.get_session("default_user")
conversation_history = user_session.get("conversation", []).copy()
conversation_history.append({"role": "user", "content": validated_input})
# Stage 1: Local Ollama Response
ollama_provider = OllamaProvider(st.session_state.selected_model)
local_response = ollama_provider.generate(validated_input, conversation_history)
if local_response:
# Display response (no nested st.chat_message)
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:
# Check HF status first
hf_status = hf_monitor.check_endpoint_status()
if not hf_status['available']:
status_placeholder.info(personality.get_initializing_message())
hf_provider = HuggingFaceProvider("meta-llama/Llama-2-7b-chat-hf")
hf_response = hf_provider.generate(validated_input, conversation_history)
if hf_response:
# Display response (no nested st.chat_message)
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 = ollama_provider.generate(
f"Synthesize these two perspectives:\n1. Local: {local_response}\n2. Cloud: {hf_response}",
synthesis_history
)
if synthesis:
# Display response (no nested st.chat_message)
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 first
status_placeholder.info("π¦ Contacting Ollama...")
ai_response = None
try:
# Use the OllamaProvider directly with proper configuration
ollama_provider = OllamaProvider(st.session_state.selected_model)
ai_response = ollama_provider.generate(validated_input, conversation_history)
if ai_response:
st.markdown(ai_response) # Use st.markdown instead of response_placeholder
status_placeholder.success("β
Response received!")
else:
status_placeholder.warning("β οΈ Empty response from Ollama")
except Exception as ollama_error:
error_message = str(ollama_error)
status_placeholder.error(f"β Ollama error: {error_message[:100]}...")
logger.error(f"Ollama error: {error_message}")
# Fallback to HF if available
if config.hf_token and not ai_response:
status_placeholder.info("β‘ Initializing HF Endpoint (2β4 minutes)...")
try:
# Check HF status first
hf_status = hf_monitor.check_endpoint_status()
if not hf_status['available']:
status_placeholder.info(personality.get_initializing_message())
# Use the HuggingFaceProvider directly
hf_provider = HuggingFaceProvider("meta-llama/Llama-2-7b-chat-hf")
ai_response = hf_provider.generate(validated_input, conversation_history)
if ai_response:
st.markdown(ai_response) # Use st.markdown instead of response_placeholder
status_placeholder.success("β
HF response received!")
else:
status_placeholder.error("β No response from HF")
except Exception as hf_error:
error_message = str(hf_error)
status_placeholder.error(f"β HF also failed: {error_message[:100]}...")
logger.error(f"HF error: {error_message}")
# Save response if successful
if ai_response:
# Update conversation history in session
conversation = user_session.get("conversation", []).copy()
conversation.append({"role": "user", "content": validated_input})
conversation.append({"role": "assistant", "content": ai_response})
session_manager.update_session("default_user", {"conversation": conversation})
# Add to message history
st.session_state.messages.append({
"role": "assistant",
"content": ai_response,
"timestamp": datetime.now().strftime("%H:%M:%S")
})
else:
error_msg = "Sorry, I couldn't process your request. Please try again."
st.session_state.messages.append({
"role": "assistant",
"content": error_msg,
"timestamp": datetime.now().strftime("%H:%M:%S")
})
st.markdown(error_msg)
except Exception as e:
error_msg = f"System error: {str(e)}"
logger.error(f"Chat processing error: {error_msg}")
st.error(error_msg)
st.session_state.messages.append({
"role": "assistant",
"content": error_msg,
"timestamp": datetime.now().strftime("%H:%M:%S")
})
finally:
st.session_state.is_processing = False
st.experimental_rerun() # Fixed: use experimental_rerun
# Add evaluation dashboard tab (separate from chat interface) - ONLY ABOUT TAB NOW
st.divider()
# Only one tab now - About
tab1, = st.tabs(["βΉοΈ About"])
with tab1:
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 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, weather data, and space information
- **Memory system**: Redis-based session management
### π Evaluation Tools
- Behavior testing with sample prompts
- Performance metrics and analytics
""")
# Add special command handling for stories
if user_input and user_input.lower().strip() in ["tell me a story", "tell me a cosmic cat story", "story", "cosmic story", "tell me a space story"]:
story = personality.get_space_story()
st.markdown(f"### π± Cosmic Kitten Story:\n\n{story}")
st.session_state.messages.append({
"role": "assistant",
"content": story,
"source": "space_story",
"timestamp": datetime.now().strftime("%H:%M:%S")
})
st.session_state.is_processing = False
st.experimental_rerun()
|