File size: 4,434 Bytes
a39eb4f
b5e04eb
a39eb4f
 
e309aa5
bcba5f1
 
b0c1c4a
68c1a82
5c179ed
 
 
 
 
 
 
 
 
 
 
 
 
bcba5f1
35cdd31
bcba5f1
25cbdc8
 
3d3b467
 
 
 
c294519
3d3b467
 
c294519
25cbdc8
 
 
9d9a715
a39eb4f
 
6beb7ed
a1e28bd
a39eb4f
 
 
68c1a82
a39eb4f
1ee60c8
 
 
 
 
9d9a715
1ee60c8
b0c1c4a
 
 
 
35cdd31
a39eb4f
d212822
a39eb4f
e6f2271
a39eb4f
f0503a0
a39eb4f
 
8e58c12
 
d212822
8e58c12
a39eb4f
 
8e58c12
a39eb4f
 
d212822
 
 
 
 
 
6beb7ed
 
a1e28bd
a39eb4f
6beb7ed
d212822
6beb7ed
 
a1e28bd
6beb7ed
 
a39eb4f
 
 
 
6beb7ed
 
 
 
 
d212822
a39eb4f
 
 
68c1a82
a39eb4f
68c1a82
5eebb95
55a8ca0
68c1a82
5eebb95
55a8ca0
a39eb4f
 
 
 
 
 
 
 
 
 
 
 
 
e6f2271
a39eb4f
 
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
# app.py
import os, logging, sys, stat 
import streamlit as st

st.set_page_config(page_title="Ecomm Chatbot", layout="wide", page_icon="πŸ›οΈ")

from src.brand import inject_brand_css, page_header, card_start, card_end
from src.translate_init import ensure_models_local

def inject_small_mis_css():
    st.markdown("""
    <style>
      .block-container { padding-top: 1rem; padding-bottom: 1rem; }
      .kpi-card { border:1px solid #e5e7eb; border-radius:12px; padding:8px 10px; background:#fff; }
      /* tighter metric */
      div[data-testid="stMetric"] { padding: 0.1rem 0.2rem; }
      div[data-testid="stMetric"] label { font-size: 0.75rem; color:#64748b; }
      div[data-testid="stMetric"] div { font-size: 1.05rem; font-weight: 700; }
      .mini-chart { border:1px solid #e5e7eb; border-radius:12px; padding:8px; background:#fff; }
    </style>
    """, unsafe_allow_html=True)

inject_brand_css() 
inject_small_mis_css()

st.markdown("""
<style>
/* Make small action buttons look like links */
[data-testid="stButton"] > button[kind="secondary"] {
  background: none !important; border: none !important; box-shadow: none !important;
  color: #266EF1 !important; padding: 0 !important; text-decoration: underline;
}
[data-testid="stButton"] > button[kind="secondary"]:hover {
  text-decoration: none;
}
</style>
""", unsafe_allow_html=True)

from src.utils import get_connection
from src.auth import (
    init_auth_schema, bootstrap_admin_from_env, render_login_signup_gate,
    render_force_change_password, session_active, session_countdown_widget,
    render_change_password_page, render_whoami_sidebar
)
from src.chat import render_chat_tab, ensure_chat_schema
from src.mis import render_mis_admin, render_mis_user
from src.admin import render_admin_tabs

LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(stream=sys.stdout,
                    level=getattr(logging, LOG_LEVEL, logging.INFO),
                    force=True)
applog = logging.getLogger("app")
#applog.info("APP: Using DB_PATH=%s", DB_PATH)

try:
    ensure_models_local(cache_dir="/data/hf-cache")
except Exception as e:
    print(f"⚠️ Warning: could not pre-download IndicTrans2 models: {e}")

def main():
    # Initialize schemas and bootstrap admin
    init_auth_schema()
    bootstrap_admin_from_env()
    ensure_chat_schema()
    
    # Gate by login/session
    if not session_active():
        # If login just succeeded, rerun so the login form disappears
        if render_login_signup_gate():
            st.rerun()
        else:
            st.stop()

    # If a forced password change is required, gate here
    if st.session_state.get("_force_change_pw"):
        if not render_force_change_password():
            st.stop()

    # Safe access to user
    user = st.session_state.get("user")
    if not user:
        st.stop()

    # Sidebar
    render_whoami_sidebar()
    session_countdown_widget()

    # Change password + logout
    if "show_change_pw" not in st.session_state:
        st.session_state["show_change_pw"] = False
    st.sidebar.markdown("---")
    if st.sidebar.button("Change my password"):
        st.session_state["show_change_pw"] = True
    if st.sidebar.button("Logout"):
        st.session_state.pop("user", None)
        st.rerun()

    # Render change password page if requested
    if st.session_state.get("show_change_pw"):
        render_change_password_page()
        st.stop()

    # Role-based tabs (Admin has no Chat)
    role = user["role"]

    if role == "admin":
        tab_mis, tab_admin = st.tabs(["πŸ“Š MIS (Admin)", "πŸ›  Admin Tools"])
        with tab_mis:
            from src.mis import render_mis_admin
            with get_connection() as conn:
                render_mis_admin(conn)
        with tab_admin:
            with get_connection() as conn:
                render_admin_tabs(conn)
    else:
        tab_chat, tab_mis_user, tab_export = st.tabs(["πŸ’¬ Chat", "πŸ“Š MIS", "πŸ“₯ Export"])
        with tab_chat:
            with get_connection() as conn:
                render_chat_tab(conn)
        with tab_mis_user:
            with get_connection() as conn:
                render_mis_user(conn, user_id=user["id"])
        with tab_export:
            from src.chat import render_user_export_tab
            with get_connection() as conn:
                render_user_export_tab(conn, user_id=user["id"])


if __name__ == "__main__":
    main()