Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,88 @@
|
|
| 1 |
-
app.py
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="Olist Chatbot", layout="wide", page_icon="ποΈ")
|
| 6 |
+
|
| 7 |
+
# Local imports
|
| 8 |
+
from src.utils import get_connection
|
| 9 |
+
from src.auth import (
|
| 10 |
+
init_auth_schema, bootstrap_admin_from_env, render_login_signup_gate,
|
| 11 |
+
render_force_change_password, session_active, session_countdown_widget
|
| 12 |
+
)
|
| 13 |
+
from src.chat import render_chat_tab, ensure_chat_schema
|
| 14 |
+
from src.mis import render_mis_admin, render_mis_user
|
| 15 |
+
from src.admin import render_admin_create_user, render_admin_manage_users
|
| 16 |
+
from src.video_export import render_admin_export_video_tab, ensure_video_deps
|
| 17 |
+
|
| 18 |
+
def main():
|
| 19 |
+
# Initialize DB schemas
|
| 20 |
+
init_auth_schema()
|
| 21 |
+
ensure_chat_schema()
|
| 22 |
+
ensure_video_deps() # validates optional deps, fonts etc.
|
| 23 |
+
|
| 24 |
+
# Gate by login/session
|
| 25 |
+
if not session_active():
|
| 26 |
+
if not render_login_signup_gate():
|
| 27 |
+
st.stop()
|
| 28 |
+
if st.session_state.get("_force_change_pw"):
|
| 29 |
+
if not render_force_change_password():
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
+
if st.session_state.get("_force_change_pw"):
|
| 33 |
+
if not render_force_change_password():
|
| 34 |
+
st.stop()
|
| 35 |
+
|
| 36 |
+
# Sidebar: user info + logout + session timer
|
| 37 |
+
user = st.session_state["user"]
|
| 38 |
+
st.sidebar.markdown(f"**Logged in as:** {user['username']} \n**Role:** {user['role']}")
|
| 39 |
+
session_countdown_widget()
|
| 40 |
+
if st.sidebar.button("Logout"):
|
| 41 |
+
st.session_state.pop("user", None)
|
| 42 |
+
st.rerun()
|
| 43 |
+
|
| 44 |
+
role = user["role"]
|
| 45 |
+
|
| 46 |
+
if role == "admin":
|
| 47 |
+
# Admin: NO Chat tab
|
| 48 |
+
tab_mis, tab_export_video, tab_create_user, tab_manage_users = st.tabs(
|
| 49 |
+
["π MIS (Admin)", "π₯ Export + π¬ Video", "π€ Create User", "π§ Manage Users"]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
with tab_mis:
|
| 53 |
+
with get_connection() as conn:
|
| 54 |
+
render_mis_admin(conn)
|
| 55 |
+
|
| 56 |
+
with tab_export_video:
|
| 57 |
+
with get_connection() as conn:
|
| 58 |
+
render_admin_export_video_tab(conn)
|
| 59 |
+
|
| 60 |
+
with tab_create_user:
|
| 61 |
+
render_admin_create_user()
|
| 62 |
+
|
| 63 |
+
with tab_manage_users:
|
| 64 |
+
render_admin_manage_users()
|
| 65 |
+
|
| 66 |
+
else:
|
| 67 |
+
# User: Chat, MIS (user), Export (no video)
|
| 68 |
+
tab_chat, tab_mis_user, tab_export = st.tabs(["π¬ Chat", "π MIS", "π₯ Export"])
|
| 69 |
+
|
| 70 |
+
with tab_chat:
|
| 71 |
+
with get_connection() as conn:
|
| 72 |
+
render_chat_tab(conn)
|
| 73 |
+
|
| 74 |
+
with tab_mis_user:
|
| 75 |
+
with get_connection() as conn:
|
| 76 |
+
render_mis_user(conn, user_id=user["id"])
|
| 77 |
+
|
| 78 |
+
with tab_export:
|
| 79 |
+
# simple CSV/TXT export of this user's chats (no video here)
|
| 80 |
+
from src.chat import render_user_export_tab
|
| 81 |
+
with get_connection() as conn:
|
| 82 |
+
render_user_export_tab(conn, user_id=user["id"])
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
# One-time admin bootstrap if DB is empty
|
| 87 |
+
bootstrap_admin_from_env()
|
| 88 |
+
main()
|