Update app.py
Browse files
app.py
CHANGED
@@ -1,280 +1,68 @@
|
|
1 |
import streamlit as st
|
2 |
-
import logging
|
3 |
import sys
|
4 |
-
import traceback
|
5 |
-
from datetime import datetime
|
6 |
import os
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
logging.basicConfig(
|
12 |
-
level=logging.INFO,
|
13 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
14 |
-
handlers=[
|
15 |
-
logging.StreamHandler(sys.stdout),
|
16 |
-
logging.FileHandler('app.log', mode='a')
|
17 |
-
]
|
18 |
-
)
|
19 |
-
return logging.getLogger(__name__)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
logger.error(f"{message}: {str(error)}")
|
26 |
-
logger.error(f"Traceback: {traceback.format_exc()}")
|
27 |
-
else:
|
28 |
-
logger.error(message)
|
29 |
-
|
30 |
-
def log_info(message):
|
31 |
-
"""Log info message"""
|
32 |
-
logger = logging.getLogger(__name__)
|
33 |
-
logger.info(message)
|
34 |
|
35 |
# Initialize logging
|
36 |
-
|
37 |
-
|
38 |
-
# Page configuration
|
39 |
-
st.set_page_config(
|
40 |
-
page_title="Mona - AI Assistant",
|
41 |
-
page_icon="🤖",
|
42 |
-
layout="wide",
|
43 |
-
initial_sidebar_state="expanded"
|
44 |
-
)
|
45 |
-
|
46 |
-
# Custom CSS
|
47 |
-
st.markdown("""
|
48 |
-
<style>
|
49 |
-
.main-header {
|
50 |
-
font-size: 3rem;
|
51 |
-
font-weight: bold;
|
52 |
-
text-align: center;
|
53 |
-
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
54 |
-
-webkit-background-clip: text;
|
55 |
-
-webkit-text-fill-color: transparent;
|
56 |
-
margin-bottom: 2rem;
|
57 |
-
}
|
58 |
-
.feature-card {
|
59 |
-
background: white;
|
60 |
-
padding: 1.5rem;
|
61 |
-
border-radius: 10px;
|
62 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
63 |
-
border-left: 4px solid #667eea;
|
64 |
-
margin: 1rem 0;
|
65 |
-
}
|
66 |
-
.sidebar-content {
|
67 |
-
background: #f8f9fa;
|
68 |
-
padding: 1rem;
|
69 |
-
border-radius: 10px;
|
70 |
-
margin: 1rem 0;
|
71 |
-
}
|
72 |
-
</style>
|
73 |
-
""", unsafe_allow_html=True)
|
74 |
-
|
75 |
-
def create_dashboard_page():
|
76 |
-
"""Create the main dashboard page"""
|
77 |
-
try:
|
78 |
-
st.markdown('<h1 class="main-header">🤖 Mona AI Assistant</h1>', unsafe_allow_html=True)
|
79 |
-
|
80 |
-
# Welcome message
|
81 |
-
st.markdown("""
|
82 |
-
<div class="feature-card">
|
83 |
-
<h3>Welcome to Mona!</h3>
|
84 |
-
<p>Your intelligent AI assistant ready to help with various tasks.</p>
|
85 |
-
</div>
|
86 |
-
""", unsafe_allow_html=True)
|
87 |
-
|
88 |
-
# Feature cards
|
89 |
-
col1, col2, col3 = st.columns(3)
|
90 |
-
|
91 |
-
with col1:
|
92 |
-
st.markdown("""
|
93 |
-
<div class="feature-card">
|
94 |
-
<h4>💬 Chat</h4>
|
95 |
-
<p>Have conversations with AI</p>
|
96 |
-
</div>
|
97 |
-
""", unsafe_allow_html=True)
|
98 |
-
|
99 |
-
with col2:
|
100 |
-
st.markdown("""
|
101 |
-
<div class="feature-card">
|
102 |
-
<h4>📊 Analytics</h4>
|
103 |
-
<p>Data analysis and insights</p>
|
104 |
-
</div>
|
105 |
-
""", unsafe_allow_html=True)
|
106 |
-
|
107 |
-
with col3:
|
108 |
-
st.markdown("""
|
109 |
-
<div class="feature-card">
|
110 |
-
<h4>🔧 Tools</h4>
|
111 |
-
<p>Various utility functions</p>
|
112 |
-
</div>
|
113 |
-
""", unsafe_allow_html=True)
|
114 |
-
|
115 |
-
# Main content area
|
116 |
-
st.subheader("Quick Actions")
|
117 |
-
|
118 |
-
col1, col2 = st.columns(2)
|
119 |
-
|
120 |
-
with col1:
|
121 |
-
if st.button("Start Chat", type="primary"):
|
122 |
-
st.switch_page("pages/chat.py")
|
123 |
-
|
124 |
-
with col2:
|
125 |
-
if st.button("View Analytics"):
|
126 |
-
st.switch_page("pages/analytics.py")
|
127 |
-
|
128 |
-
# Recent activity
|
129 |
-
st.subheader("Recent Activity")
|
130 |
-
st.info("No recent activity to display.")
|
131 |
-
|
132 |
-
log_info("Dashboard page loaded successfully")
|
133 |
-
|
134 |
-
except Exception as e:
|
135 |
-
log_error("Error in dashboard page", error=e)
|
136 |
-
st.error("An error occurred while loading the dashboard. Please try again.")
|
137 |
-
|
138 |
-
def create_chat_page():
|
139 |
-
"""Create the chat page"""
|
140 |
-
try:
|
141 |
-
st.title("💬 Chat with Mona")
|
142 |
-
|
143 |
-
# Initialize session state
|
144 |
-
if "messages" not in st.session_state:
|
145 |
-
st.session_state.messages = []
|
146 |
-
|
147 |
-
# Display chat messages
|
148 |
-
for message in st.session_state.messages:
|
149 |
-
with st.chat_message(message["role"]):
|
150 |
-
st.markdown(message["content"])
|
151 |
-
|
152 |
-
# Chat input
|
153 |
-
if prompt := st.chat_input("What would you like to know?"):
|
154 |
-
# Add user message
|
155 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
156 |
-
with st.chat_message("user"):
|
157 |
-
st.markdown(prompt)
|
158 |
-
|
159 |
-
# Add assistant response
|
160 |
-
with st.chat_message("assistant"):
|
161 |
-
response = f"I received your message: '{prompt}'. This is a demo response. In a real implementation, this would connect to an AI service."
|
162 |
-
st.markdown(response)
|
163 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
164 |
-
|
165 |
-
log_info("Chat page loaded successfully")
|
166 |
-
|
167 |
-
except Exception as e:
|
168 |
-
log_error("Error in chat page", error=e)
|
169 |
-
st.error("An error occurred in the chat. Please try again.")
|
170 |
-
|
171 |
-
def create_analytics_page():
|
172 |
-
"""Create the analytics page"""
|
173 |
-
try:
|
174 |
-
st.title("📊 Analytics Dashboard")
|
175 |
-
|
176 |
-
# Sample analytics content
|
177 |
-
col1, col2, col3 = st.columns(3)
|
178 |
-
|
179 |
-
with col1:
|
180 |
-
st.metric("Total Users", "1,234", "+5%")
|
181 |
-
|
182 |
-
with col2:
|
183 |
-
st.metric("Active Sessions", "456", "+12%")
|
184 |
-
|
185 |
-
with col3:
|
186 |
-
st.metric("Messages Sent", "7,890", "+8%")
|
187 |
-
|
188 |
-
# Sample chart
|
189 |
-
import pandas as pd
|
190 |
-
import numpy as np
|
191 |
-
|
192 |
-
chart_data = pd.DataFrame(
|
193 |
-
np.random.randn(20, 3),
|
194 |
-
columns=['Series A', 'Series B', 'Series C']
|
195 |
-
)
|
196 |
-
|
197 |
-
st.line_chart(chart_data)
|
198 |
-
|
199 |
-
log_info("Analytics page loaded successfully")
|
200 |
-
|
201 |
-
except Exception as e:
|
202 |
-
log_error("Error in analytics page", error=e)
|
203 |
-
st.error("An error occurred while loading analytics. Please try again.")
|
204 |
-
|
205 |
-
def create_sidebar():
|
206 |
-
"""Create the sidebar navigation"""
|
207 |
-
try:
|
208 |
-
with st.sidebar:
|
209 |
-
st.markdown('<div class="sidebar-content">', unsafe_allow_html=True)
|
210 |
-
st.title("Navigation")
|
211 |
-
|
212 |
-
# Navigation buttons
|
213 |
-
if st.button("🏠 Dashboard", use_container_width=True):
|
214 |
-
st.switch_page("app.py")
|
215 |
-
|
216 |
-
if st.button("💬 Chat", use_container_width=True):
|
217 |
-
st.switch_page("pages/chat.py")
|
218 |
-
|
219 |
-
if st.button("📊 Analytics", use_container_width=True):
|
220 |
-
st.switch_page("pages/analytics.py")
|
221 |
-
|
222 |
-
st.markdown("---")
|
223 |
-
|
224 |
-
# Settings section
|
225 |
-
st.subheader("Settings")
|
226 |
-
theme = st.selectbox("Theme", ["Light", "Dark"])
|
227 |
-
language = st.selectbox("Language", ["English", "Spanish", "French"])
|
228 |
-
|
229 |
-
st.markdown("---")
|
230 |
-
|
231 |
-
# Info section
|
232 |
-
st.subheader("Info")
|
233 |
-
st.markdown(f"**Version:** 1.0.0")
|
234 |
-
st.markdown(f"**Last Updated:** {datetime.now().strftime('%Y-%m-%d')}")
|
235 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
236 |
-
|
237 |
-
log_info("Sidebar created successfully")
|
238 |
-
|
239 |
-
except Exception as e:
|
240 |
-
log_error("Error creating sidebar", error=e)
|
241 |
|
242 |
def main():
|
243 |
-
"""Main application
|
244 |
try:
|
245 |
-
#
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
248 |
|
249 |
-
#
|
250 |
-
|
|
|
251 |
|
252 |
-
#
|
253 |
-
|
|
|
|
|
|
|
|
|
254 |
|
255 |
# Route to appropriate page
|
256 |
-
if
|
257 |
-
create_chat_page()
|
258 |
-
elif current_page == 'analytics':
|
259 |
-
create_analytics_page()
|
260 |
-
else:
|
261 |
create_dashboard_page()
|
262 |
-
|
263 |
-
|
264 |
-
|
|
|
|
|
265 |
except Exception as e:
|
266 |
-
|
267 |
-
st.error("
|
268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
|
270 |
if __name__ == "__main__":
|
271 |
-
|
272 |
-
main()
|
273 |
-
except Exception as e:
|
274 |
-
# Final fallback error handling
|
275 |
-
print(f"Critical application error: {str(e)}")
|
276 |
-
print(f"Traceback: {traceback.format_exc()}")
|
277 |
-
|
278 |
-
# Still try to show something in Streamlit
|
279 |
-
st.error("Application failed to start. Please check the logs.")
|
280 |
-
st.exception(e)
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import sys
|
|
|
|
|
3 |
import os
|
4 |
+
from pathlib import Path
|
5 |
|
6 |
+
# Add the app directory to Python path
|
7 |
+
app_dir = Path(__file__).parent
|
8 |
+
sys.path.insert(0, str(app_dir))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
from pages.dashboard import create_dashboard_page
|
11 |
+
from pages.settings import create_settings_page
|
12 |
+
from utils.config import load_config
|
13 |
+
from utils.logging import setup_logging, get_logger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Initialize logging
|
16 |
+
setup_logging()
|
17 |
+
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def main():
|
20 |
+
"""Main application entry point"""
|
21 |
try:
|
22 |
+
# Configure Streamlit page
|
23 |
+
st.set_page_config(
|
24 |
+
page_title="MONA - Monitoring & Analytics",
|
25 |
+
page_icon="📊",
|
26 |
+
layout="wide",
|
27 |
+
initial_sidebar_state="expanded"
|
28 |
+
)
|
29 |
|
30 |
+
# Load configuration
|
31 |
+
config = load_config()
|
32 |
+
logger.info("Application started successfully")
|
33 |
|
34 |
+
# Create sidebar navigation
|
35 |
+
st.sidebar.title("MONA Dashboard")
|
36 |
+
page = st.sidebar.selectbox(
|
37 |
+
"Navigate to:",
|
38 |
+
["Dashboard", "Settings", "About"]
|
39 |
+
)
|
40 |
|
41 |
# Route to appropriate page
|
42 |
+
if page == "Dashboard":
|
|
|
|
|
|
|
|
|
43 |
create_dashboard_page()
|
44 |
+
elif page == "Settings":
|
45 |
+
create_settings_page()
|
46 |
+
elif page == "About":
|
47 |
+
create_about_page()
|
48 |
+
|
49 |
except Exception as e:
|
50 |
+
logger.error(f"Application error: {str(e)}")
|
51 |
+
st.error(f"An error occurred: {str(e)}")
|
52 |
+
|
53 |
+
def create_about_page():
|
54 |
+
"""Create the about page"""
|
55 |
+
st.title("About MONA")
|
56 |
+
st.write("""
|
57 |
+
MONA (Monitoring & Analytics) is a comprehensive dashboard application
|
58 |
+
for data visualization and monitoring.
|
59 |
+
|
60 |
+
**Features:**
|
61 |
+
- Real-time data monitoring
|
62 |
+
- Interactive dashboards
|
63 |
+
- Data analytics and reporting
|
64 |
+
- Customizable settings
|
65 |
+
""")
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|