rdune71's picture
Fix Ollama connection issues and improve error handling for Hugging Face Spaces
6015c25
raw
history blame
5.69 kB
# Force redeploy trigger - version 1.2
import streamlit as st
from utils.config import config
import requests
import json
import os
from core.memory import load_user_state
# Set page config
st.set_page_config(page_title="AI Life Coach", page_icon="🧘", layout="centered")
# Sidebar for user selection
st.sidebar.title("🧘 AI Life Coach")
user = st.sidebar.selectbox("Select User", ["Rob", "Sarah"])
st.sidebar.markdown("---")
# Get the base URL for API calls (works in Hugging Face Spaces)
# In HF Spaces, we need to use the same port for both frontend and backend
# or properly configure the backend service
BASE_URL = os.environ.get("SPACE_ID", "") # Will be set in HF Spaces
IS_HF_SPACE = bool(BASE_URL)
# Headers to skip ngrok browser warning
NGROK_HEADERS = {
"ngrok-skip-browser-warning": "true",
"User-Agent": "AI-Life-Coach-App"
}
# Fetch Ollama status
def get_ollama_status():
try:
# Try to connect to the remote Ollama service directly
response = requests.get(
f"{config.ollama_host}/api/tags",
headers=NGROK_HEADERS,
timeout=10
)
if response.status_code == 200:
models = response.json().get("models", [])
if models:
return {
"running": True,
"model_loaded": models[0].get("name"),
"remote_host": config.ollama_host
}
except Exception as e:
# If direct connection fails, show error
return {
"running": False,
"model_loaded": None,
"error": str(e),
"remote_host": config.ollama_host
}
# If we get here, connection worked but no models
return {
"running": False,
"model_loaded": None,
"remote_host": config.ollama_host
}
# After user selects name, load conversation history
def get_conversation_history(user_id):
user_state = load_user_state(user_id)
if user_state and "conversation" in user_state:
return json.loads(user_state["conversation"])
return []
# Check Ollama status
ollama_status = get_ollama_status()
# Display Ollama status
if ollama_status["running"]:
st.sidebar.success(f"🧠 Model Running: {ollama_status['model_loaded']}")
st.sidebar.info(f"Connected to: {ollama_status['remote_host']}")
else:
st.sidebar.error("🧠 Ollama is not accessible")
st.sidebar.info(f"Configured host: {ollama_status['remote_host']}")
if "error" in ollama_status:
st.sidebar.caption(f"Error: {ollama_status['error']}")
# Main chat interface
st.title("🧘 AI Life Coach")
st.markdown("Talk to your personal development assistant.")
# Show detailed status
with st.expander("πŸ” Connection Status"):
st.write("Ollama Status:", ollama_status)
st.write("Environment Info:")
st.write("- Is HF Space:", IS_HF_SPACE)
st.write("- Base URL:", BASE_URL or "Not in HF Space")
st.write("- Configured Ollama Host:", config.ollama_host)
if not ollama_status["running"]:
st.warning("⚠️ Ollama is not accessible. Please check your Ollama/ngrok setup.")
st.info("""
Troubleshooting tips:
1. Ensure your Ollama service is running locally
2. Verify your ngrok tunnel is active and pointing to Ollama (port 11434)
3. Check that the ngrok URL in your .env file matches your active tunnel
4. Confirm that your ngrok account allows connections from Hugging Face Spaces
""")
else:
# Display conversation history
conversation = get_conversation_history(user)
for msg in conversation:
role = msg["role"].capitalize()
content = msg["content"]
st.markdown(f"**{role}:** {content}")
# Chat input
user_input = st.text_input("Your message...", key="input")
if st.button("Send"):
if user_input.strip() == "":
st.warning("Please enter a message.")
else:
# Display user message
st.markdown(f"**You:** {user_input}")
# Send to Ollama directly (bypassing backend for simplicity)
with st.spinner("AI Coach is thinking..."):
try:
# Prepare the prompt with conversation history
conversation_history = [{"role": msg["role"], "content": msg["content"]}
for msg in conversation[-5:]] # Last 5 messages
conversation_history.append({"role": "user", "content": user_input})
payload = {
"model": config.local_model_name,
"messages": conversation_history,
"stream": False
}
response = requests.post(
f"{config.ollama_host}/api/chat",
json=payload,
headers=NGROK_HEADERS,
timeout=60
)
if response.status_code == 200:
response_data = response.json()
ai_response = response_data.get("message", {}).get("content", "")
st.markdown(f"**AI Coach:** {ai_response}")
# Note: In a production app, we'd save the conversation to Redis here
else:
st.error(f"Failed to get response from Ollama: {response.status_code}")
st.error(response.text[:200])
except Exception as e:
st.error(f"Connection error: {e}")