File size: 2,895 Bytes
b860cd0
 
 
 
 
 
 
 
 
 
 
 
2d548c5
b860cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d548c5
 
 
b860cd0
 
 
2d548c5
 
f8266e7
b860cd0
 
 
f8266e7
b860cd0
 
f8266e7
b860cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d548c5
 
 
 
 
 
 
b860cd0
 
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
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')

import streamlit as st
import os

# --- PATH SETUP ---
current_dir = os.getcwd() # Should be /home/user/app in Docker
load_dir = os.path.join(current_dir, "src", "load")
sys.path.append(load_dir)

# Import your agent creator
try:
    from mshauri_demo import create_mshauri_agent
except ImportError as e:
    st.error(f"Critical Error: Could not import mshauri_demo. Paths checked: {sys.path}. Details: {e}")
    st.stop()

st.set_page_config(page_title="Mshauri Fedha", page_icon="🦁")

st.title("🦁 Mshauri Fedha")
st.markdown("### AI Financial Advisor for Kenya")

# Initialize Session State
if "messages" not in st.session_state:
    st.session_state.messages = []

if "agent" not in st.session_state:
    with st.spinner("Initializing Mshauri Brain (Loading Models & Data)..."):
        # SQLAlchemy requires a URI starting with sqlite:///
        # We use 4 slashes (sqlite:////) because it is an absolute path on Linux
        sql_path = f"sqlite:///{os.path.join(current_dir, 'mshauri_fedha_v6.db')}"
        vector_path = os.path.join(current_dir, "mshauri_fedha_chroma_db")
        
        # Check if data exists (Debugging for Space deployment)
        real_db_path = os.path.join(current_dir, "mshauri_fedha_v6.db")
        if not os.path.exists(real_db_path):
            st.error(f"Database not found at {real_db_path}. Did the clone fail?")
            st.stop()
            
        try:
            # mshauri_demo.py to intelligently pick the API or Local model.
            st.session_state.agent = create_mshauri_agent(
                sql_db_path=sql_path, 
                vector_db_path=vector_path
            )
            st.success("System Ready!")
        except Exception as e:
            st.error(f"Failed to initialize agent: {e}")

# Display Chat History
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Handle Input
if prompt := st.chat_input("Ask about inflation, exchange rates, or economic trends..."):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        with st.spinner("Analyzing..."):
            try:
                if st.session_state.agent:
                    response = st.session_state.agent.invoke({"input": prompt})
                    output_text = response.get("output", "Error generating response.")
                    st.markdown(output_text)
                    st.session_state.messages.append({"role": "assistant", "content": output_text})
                else:
                    st.error("Agent failed to initialize. Please refresh the page.")
            except Exception as e:
                st.error(f"An error occurred: {e}")