-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstreamlit_app.py
155 lines (131 loc) · 4.55 KB
/
streamlit_app.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import streamlit as st
from src.utils import *
from src import home, playground, build, notification, setup
from snowflake.snowpark.context import get_active_session
from pathlib import Path
import json
from snowflake.snowpark.exceptions import SnowparkSQLException
from src.notification import *
# Load the config file
config_path = Path("src/settings_config.json")
with open(config_path, "r") as f:
config = json.load(f)
# Set the page title and layout
st.set_page_config(page_title="Snowflake AI Toolkit", layout="wide")
# Ensure session state variables are initialized
if 'page' not in st.session_state:
st.session_state.page = "Home" # Default page
if 'snowflake_session' not in st.session_state:
st.session_state.snowflake_session = None
# Establish the session if not already initialized
if st.session_state.snowflake_session is None:
if config["mode"] == "native":
try:
st.session_state.snowflake_session = get_active_session()
except Exception as e:
st.error(f"Failed to get active session in native mode: {e}")
elif config["mode"] == "debug":
try:
connection_parameters = {
"account": config["snowflake"]["account"],"user": config["snowflake"]["user"],"password": config["snowflake"]["password"],
"role": config["snowflake"]["role"],"warehouse": config["snowflake"]["warehouse"],"database": config["snowflake"]["database"],
"schema": config["snowflake"]["schema"]
}
st.session_state.snowflake_session = Session.builder.configs(connection_parameters).create()
except Exception as e:
st.error(f"Failed to create session in debug mode: {e}")
# Check if session is successfully created
if st.session_state.snowflake_session is None:
st.error("Failed to connect to Snowflake.")
else:
try:
create_database_and_stage_if_not_exists(st.session_state.snowflake_session)
except Exception as e:
st.error(f"Error while creating database and stage: {e}")
# Set up UDF at app start
setup_pdf_text_chunker(st.session_state.snowflake_session)
# Load custom CSS for sidebar styling
st.markdown("""
<style>
/* Custom CSS for Sidebar */
.css-18e3c8v {
background-color: #333;
padding: 20px;
color: white;
}
.sidebar-title {
font-size: 1.5em;
font-weight: bold;
margin-bottom: 10px;
color: white;
}
.sidebar-section {
background-color: #444;
border-radius: 8px;
margin-bottom: 10px;
padding: 15px;
color: white;
border: 1px solid #555;
}
.sidebar-section h2 {
font-size: 1.3em;
margin-bottom: 10px;
font-weight: bold;
color: #56CCF2;
}
.sidebar-section-item {
font-size: 1em;
padding: 5px 0;
margin-left: 10px;
color: white;
cursor: pointer;
}
.sidebar-section-item:hover {
color: #56CCF2;
cursor: pointer;
}
.sidebar-image {
text-align: center;
margin-bottom: 20px;
}
.sidebar-image img {
width: 150px;
height: auto;
border-radius: 8px;
margin-bottom: 20px;
}
</style>
""", unsafe_allow_html=True)
# Sidebar Navigation with logo
render_image(config["default_settings"]["logo_path"])
# Sidebar content with expanders and buttons under each category
with st.sidebar:
st.title("📋 Select Menu")
# Overview Section
with st.expander("🔍 **Overview**", expanded=False):
if st.button("📄 About"):
st.session_state.page = "Home"
if st.button("⚙️ Setup"):
st.session_state.page = "Setup"
# Components Section
with st.expander("✨ **Components**", expanded=False):
if st.button("🎮 Playground"):
st.session_state.page = "Playground"
if st.button("🔧 Build"):
st.session_state.page = "Build"
if st.button("🔔 Notification"):
st.session_state.page = "Notification"
# Pages dictionary with corresponding display functions
pages = {
"Home": home.display_home,
"Playground": playground.display_playground,
"Build": build.display_build,
"Notification": notification.display_notification,
"Setup": setup.display_setup
}
# Render the selected page from the pages dictionary
if st.session_state.page in pages:
try:
pages[st.session_state.page](st.session_state.snowflake_session)
except Exception as e:
st.error(f"Error loading {st.session_state.page} page: {e}")