|
|
|
|
|
import streamlit as st |
|
|
from utils.config import config |
|
|
import requests |
|
|
import json |
|
|
from core.memory import load_user_state |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="AI Life Coach", page_icon="🧘", layout="centered") |
|
|
|
|
|
|
|
|
st.sidebar.title("🧘 AI Life Coach") |
|
|
user = st.sidebar.selectbox("Select User", ["Rob", "Sarah"]) |
|
|
st.sidebar.markdown("---") |
|
|
|
|
|
|
|
|
def get_ollama_status(): |
|
|
try: |
|
|
response = requests.get("http://localhost:8000/api/ollama-status") |
|
|
if response.status_code == 200: |
|
|
return response.json() |
|
|
except Exception: |
|
|
return {"running": False, "model_loaded": None} |
|
|
|
|
|
|
|
|
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 [] |
|
|
|
|
|
ollama_status = get_ollama_status() |
|
|
|
|
|
|
|
|
if ollama_status["running"]: |
|
|
st.sidebar.success(f"🧠 Model Running: {ollama_status['model_loaded']}") |
|
|
else: |
|
|
st.sidebar.error("🧠 Ollama is not running or no model loaded.") |
|
|
|
|
|
|
|
|
st.title("🧘 AI Life Coach") |
|
|
st.markdown("Talk to your personal development assistant.") |
|
|
|
|
|
if not ollama_status["running"]: |
|
|
st.warning("⚠️ Ollama is not running. Please start Ollama to use the AI Life Coach.") |
|
|
else: |
|
|
|
|
|
conversation = get_conversation_history(user) |
|
|
for msg in conversation: |
|
|
role = msg["role"].capitalize() |
|
|
content = msg["content"] |
|
|
st.markdown(f"**{role}:** {content}") |
|
|
|
|
|
|
|
|
user_input = st.text_input("Your message...", key="input") |
|
|
if st.button("Send"): |
|
|
if user_input.strip() == "": |
|
|
st.warning("Please enter a message.") |
|
|
else: |
|
|
|
|
|
st.markdown(f"**You:** {user_input}") |
|
|
|
|
|
|
|
|
with st.spinner("AI Coach is thinking..."): |
|
|
try: |
|
|
response = requests.post( |
|
|
"http://localhost:8000/api/chat", |
|
|
json={"user_id": user, "message": user_input} |
|
|
) |
|
|
if response.status_code == 200: |
|
|
response_data = response.json() |
|
|
ai_response = response_data.get("response", "") |
|
|
st.markdown(f"**AI Coach:** {ai_response}") |
|
|
else: |
|
|
st.error("Failed to get response from AI Coach.") |
|
|
except Exception as e: |
|
|
st.error(f"Connection error: {e}") |
|
|
|