cryogenic22 commited on
Commit
4f0ef41
·
verified ·
1 Parent(s): 0bd6c51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -81
app.py CHANGED
@@ -1,31 +1,6 @@
1
- import streamlit as st
2
- from services.claude_service import ClaudeService
3
- from services.chart_analysis import ChartAnalysisService
4
- from ui.components import (
5
- create_sidebar,
6
- show_analysis_section,
7
- show_chat_history,
8
- show_follow_up_section,
9
- show_save_options,
10
- create_expertise_selector
11
- )
12
- from utils.file_handlers import save_chat_history
13
- from utils.learning_module import LearningModule
14
 
15
- def init_session_state():
16
- """Initialize session state variables"""
17
- if 'chat_history' not in st.session_state:
18
- st.session_state.chat_history = []
19
- if 'current_image' not in st.session_state:
20
- st.session_state.current_image = None
21
- if 'current_analysis' not in st.session_state:
22
- st.session_state.current_analysis = None
23
- if 'current_images' not in st.session_state:
24
- st.session_state.current_images = []
25
- if 'conversation_context' not in st.session_state:
26
- st.session_state.conversation_context = []
27
- if 'current_conversation_id' not in st.session_state:
28
- st.session_state.current_conversation_id = Noneimport streamlit as st
29
  from services.claude_service import ClaudeService
30
  from services.chart_analysis import ChartAnalysisService
31
  from ui.components import (
@@ -38,6 +13,10 @@ from ui.components import (
38
  )
39
  from utils.file_handlers import save_chat_history
40
  from utils.learning_module import LearningModule
 
 
 
 
41
 
42
  def init_session_state():
43
  """Initialize session state variables"""
@@ -53,10 +32,10 @@ def init_session_state():
53
  st.session_state.conversation_context = []
54
  if 'current_conversation_id' not in st.session_state:
55
  st.session_state.current_conversation_id = None
 
 
56
 
57
-
58
-
59
- def show_chart_analysis_tab(claude_service, analysis_service):
60
  """Display chart analysis functionality"""
61
  # Get user expertise level
62
  expertise_level = create_expertise_selector()
@@ -82,10 +61,6 @@ def show_chart_analysis_tab(claude_service, analysis_service):
82
  st.session_state.current_images = [file.getvalue() for file in uploaded_files]
83
 
84
  analyze_clicked = show_analysis_section(uploaded_files)
85
-
86
- # Store analysis results in session state
87
- if 'analysis_results' not in st.session_state:
88
- st.session_state.analysis_results = []
89
 
90
  if analyze_clicked:
91
  if not uploaded_files:
@@ -128,6 +103,8 @@ def show_chart_analysis_tab(claude_service, analysis_service):
128
  if response:
129
  st.session_state.chat_history.append(response)
130
  st.markdown(response['analysis'])
 
 
131
 
132
  # Then show individual analyses
133
  individual_analyses = [r for r in results if r.get('analysis_type') == 'Individual']
@@ -149,6 +126,8 @@ def show_chart_analysis_tab(claude_service, analysis_service):
149
  if response:
150
  st.session_state.chat_history.append(response)
151
  st.markdown(response['analysis'])
 
 
152
 
153
  # Risk warning at the bottom
154
  st.warning(
@@ -163,7 +142,7 @@ def show_chart_analysis_tab(claude_service, analysis_service):
163
  # Save options
164
  save_name = show_save_options()
165
  if save_name and st.session_state.chat_history:
166
- filename = save_chat_history(
167
  st.session_state.chat_history,
168
  st.session_state.current_images[0] if st.session_state.current_images else None,
169
  f"{save_name}.json" if save_name else None
@@ -171,8 +150,7 @@ def show_chart_analysis_tab(claude_service, analysis_service):
171
  if filename:
172
  st.success(f"Chat saved as {filename}")
173
  else:
174
- st.info("Chat saved to session state")# app.py
175
-
176
 
177
  def show_learning_tab(learning_module):
178
  """Display learning center functionality"""
@@ -196,57 +174,58 @@ def main():
196
  initial_sidebar_state="expanded"
197
  )
198
 
199
- # Initialize authentication
200
- auth_manager = AuthManager()
201
-
202
- # Show login page if not authenticated
203
- if not auth_manager.is_authenticated():
204
- show_login_page(auth_manager)
205
- return
206
-
207
- # Get user's storage paths
208
- storage_paths = auth_manager.get_user_storage_paths()
209
- storage_manager = UserStorageManager(storage_paths)
210
-
211
- # Initialize services
212
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  claude_service = ClaudeService()
214
  analysis_service = ChartAnalysisService(claude_service)
215
  learning_module = LearningModule(claude_service)
216
- except ValueError as e:
217
- st.error(str(e))
218
- return
219
-
220
- # Initialize session state
221
- init_session_state()
222
-
223
- # Show logout button in sidebar
224
- show_logout_button(auth_manager)
225
-
226
- # Load previous context if available
227
- if 'chat_history' not in st.session_state:
228
- context = storage_manager.get_context()
229
- if context:
230
- st.session_state.chat_history = context.get('chat_history', [])
231
- st.session_state.current_analysis = context.get('current_analysis')
232
-
233
- # Main application tabs
234
- tab1, tab2, tab3 = st.tabs(["Chart Analysis", "Learning Center", "Previous Chats"])
235
-
236
- with tab1:
237
- show_chart_analysis_tab(claude_service, analysis_service, storage_manager)
238
 
239
- with tab2:
240
- show_learning_tab(learning_module)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
- with tab3:
243
- show_previous_chats_tab(storage_manager)
 
 
 
244
 
245
- # Save context before closing
246
- storage_manager.save_context({
247
- 'chat_history': st.session_state.chat_history,
248
- 'current_analysis': st.session_state.current_analysis
249
- })
250
 
251
  if __name__ == "__main__":
252
  main()
 
1
+ # app.py
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from services.claude_service import ClaudeService
5
  from services.chart_analysis import ChartAnalysisService
6
  from ui.components import (
 
13
  )
14
  from utils.file_handlers import save_chat_history
15
  from utils.learning_module import LearningModule
16
+ from auth.auth_manager import AuthManager
17
+ from storage.storage_manager import UserStorageManager
18
+ from pages.login import show_login_page, show_logout_button
19
+ from pages.previous_chats import show_previous_chats_tab
20
 
21
  def init_session_state():
22
  """Initialize session state variables"""
 
32
  st.session_state.conversation_context = []
33
  if 'current_conversation_id' not in st.session_state:
34
  st.session_state.current_conversation_id = None
35
+ if 'analysis_results' not in st.session_state:
36
+ st.session_state.analysis_results = []
37
 
38
+ def show_chart_analysis_tab(claude_service, analysis_service, storage_manager):
 
 
39
  """Display chart analysis functionality"""
40
  # Get user expertise level
41
  expertise_level = create_expertise_selector()
 
61
  st.session_state.current_images = [file.getvalue() for file in uploaded_files]
62
 
63
  analyze_clicked = show_analysis_section(uploaded_files)
 
 
 
 
64
 
65
  if analyze_clicked:
66
  if not uploaded_files:
 
103
  if response:
104
  st.session_state.chat_history.append(response)
105
  st.markdown(response['analysis'])
106
+ # Save to storage
107
+ storage_manager.save_chat(st.session_state.chat_history)
108
 
109
  # Then show individual analyses
110
  individual_analyses = [r for r in results if r.get('analysis_type') == 'Individual']
 
126
  if response:
127
  st.session_state.chat_history.append(response)
128
  st.markdown(response['analysis'])
129
+ # Save to storage
130
+ storage_manager.save_chat(st.session_state.chat_history)
131
 
132
  # Risk warning at the bottom
133
  st.warning(
 
142
  # Save options
143
  save_name = show_save_options()
144
  if save_name and st.session_state.chat_history:
145
+ filename = storage_manager.save_chat(
146
  st.session_state.chat_history,
147
  st.session_state.current_images[0] if st.session_state.current_images else None,
148
  f"{save_name}.json" if save_name else None
 
150
  if filename:
151
  st.success(f"Chat saved as {filename}")
152
  else:
153
+ st.info("Chat saved to session state")
 
154
 
155
  def show_learning_tab(learning_module):
156
  """Display learning center functionality"""
 
174
  initial_sidebar_state="expanded"
175
  )
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  try:
178
+ # Initialize authentication
179
+ auth_manager = AuthManager()
180
+
181
+ # Show login page if not authenticated
182
+ if not auth_manager.is_authenticated():
183
+ show_login_page(auth_manager)
184
+ return
185
+
186
+ # Get user's storage paths and initialize storage
187
+ storage_paths = auth_manager.get_user_storage_paths()
188
+ storage_manager = UserStorageManager(storage_paths)
189
+
190
+ # Initialize services
191
  claude_service = ClaudeService()
192
  analysis_service = ChartAnalysisService(claude_service)
193
  learning_module = LearningModule(claude_service)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
+ # Initialize session state
196
+ init_session_state()
197
+
198
+ # Show logout button in sidebar
199
+ show_logout_button(auth_manager)
200
+
201
+ # Load previous context if available
202
+ if 'chat_history' not in st.session_state:
203
+ context = storage_manager.get_context()
204
+ if context:
205
+ st.session_state.chat_history = context.get('chat_history', [])
206
+ st.session_state.current_analysis = context.get('current_analysis')
207
+
208
+ # Main application tabs
209
+ tab1, tab2, tab3 = st.tabs(["Chart Analysis", "Learning Center", "Previous Chats"])
210
+
211
+ with tab1:
212
+ show_chart_analysis_tab(claude_service, analysis_service, storage_manager)
213
+
214
+ with tab2:
215
+ show_learning_tab(learning_module)
216
+
217
+ with tab3:
218
+ show_previous_chats_tab(storage_manager)
219
 
220
+ # Save context before closing
221
+ storage_manager.save_context({
222
+ 'chat_history': st.session_state.chat_history,
223
+ 'current_analysis': st.session_state.current_analysis
224
+ })
225
 
226
+ except Exception as e:
227
+ st.error(f"An error occurred: {str(e)}")
228
+ st.warning("Please try refreshing the page. If the error persists, contact support.")
 
 
229
 
230
  if __name__ == "__main__":
231
  main()