rdune71's picture
Fix critical HF endpoint spamming and response delivery issues
e9b4a9e
raw
history blame
8.75 kB
import streamlit as st
import time
import os
import sys
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
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
st.set_page_config(page_title="AI Life Coach", page_icon="🧠", layout="wide")
# Initialize session state
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")
# Sidebar
with st.sidebar:
st.title("AI Life Coach 🧠")
st.markdown("Your personal AI-powered life development assistant")
# Model selection
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
)
st.session_state.selected_model = model_options[selected_model_name]
# Ollama URL input
st.subheader("Ollama Configuration")
ngrok_url_input = st.text_input(
"Ollama Server URL",
value=st.session_state.ngrok_url_temp,
help="Enter your ngrok URL",
key="ngrok_url_input"
)
if ngrok_url_input != st.session_state.ngrok_url_temp:
st.session_state.ngrok_url_temp = ngrok_url_input
st.success("βœ… URL updated!")
# Test connection button
if st.button("πŸ“‘ Test Connection"):
try:
import requests
headers = {
"ngrok-skip-browser-warning": "true",
"User-Agent": "AI-Life-Coach-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]}...")
# Conversation history
st.subheader("Conversation History")
if st.button("πŸ—‘οΈ Clear History"):
st.session_state.messages = []
st.success("History cleared!")
if st.session_state.messages:
user_msgs = len([m for m in st.session_state.messages if m["role"] == "user"])
ai_msgs = len([m for m in st.session_state.messages if m["role"] == "assistant"])
st.caption(f"πŸ’¬ {user_msgs} user, {ai_msgs} AI messages")
# Advanced Debug Panel (now properly collapsible)
with st.expander("πŸ” System Monitor", expanded=False):
st.subheader("πŸ“Š Status")
# Ollama Status
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")
# HF Status
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")
# Redis Status
if check_redis_health():
st.success("πŸ’Ύ Redis: Connected")
else:
st.error("πŸ’Ύ Redis: Disconnected")
# Main interface
st.title("🧠 AI Life Coach")
st.markdown("Ask me anything about personal development, goal setting, or life advice!")
# Display messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if "timestamp" in message:
st.caption(f"πŸ•’ {message['timestamp']}")
# 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:
st.session_state.is_processing = True
# Display user message
with st.chat_message("user"):
st.markdown(user_input)
st.session_state.messages.append({
"role": "user",
"content": user_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": user_input})
# Try Ollama with proper error handling
status_placeholder.info("πŸ¦™ Contacting Ollama...")
ai_response = None
try:
ai_response = send_to_ollama(
user_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:
status_placeholder.error(f"❌ Ollama error: {str(ollama_error)[:50]}...")
# Fallback to HF if available
if config.hf_token:
status_placeholder.info("πŸ”„ Trying Hugging Face...")
try:
ai_response = send_to_hf(user_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:
status_placeholder.error(f"❌ HF also failed: {str(hf_error)[:50]}...")
# Save response if successful
if ai_response:
# Update conversation history
conversation.append({"role": "user", "content": user_input})
conversation.append({"role": "assistant", "content": ai_response})
user_session["conversation"] = conversation
session_manager.update_session("default_user", user_session)
# Add to message history
st.session_state.messages.append({
"role": "assistant",
"content": ai_response,
"timestamp": datetime.now().strftime("%H:%M:%S")
})
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:
error_msg = f"System error: {str(e)}"
response_placeholder.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
time.sleep(0.5) # Brief pause
st.experimental_rerun()