chatbot / app.py
SuhasGholkar's picture
Update app.py
b0c1c4a verified
Raw History Blame Contribute Delete
4.43 kB
# 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()