cryogenic22 commited on
Commit
e1a9d58
·
verified ·
1 Parent(s): 5460c1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -135
app.py CHANGED
@@ -1,3 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def show_chart_analysis_tab(claude_service, analysis_service):
2
  """Display chart analysis functionality"""
3
  # Get user expertise level
@@ -115,139 +173,6 @@ def show_chart_analysis_tab(claude_service, analysis_service):
115
  else:
116
  st.info("Chat saved to session state")# app.py
117
 
118
- import streamlit as st
119
- from services.claude_service import ClaudeService
120
- from services.chart_analysis import ChartAnalysisService
121
- from ui.components import (
122
- create_sidebar,
123
- show_analysis_section,
124
- show_chat_history,
125
- show_follow_up_section,
126
- show_save_options,
127
- create_expertise_selector
128
- )
129
- from utils.file_handlers import save_chat_history
130
- from utils.learning_module import LearningModule
131
-
132
- def init_session_state():
133
- """Initialize session state variables"""
134
- if 'chat_history' not in st.session_state:
135
- st.session_state.chat_history = []
136
- if 'current_image' not in st.session_state:
137
- st.session_state.current_image = None
138
- if 'current_analysis' not in st.session_state:
139
- st.session_state.current_analysis = None
140
- if 'current_images' not in st.session_state:
141
- st.session_state.current_images = []
142
- if 'conversation_context' not in st.session_state:
143
- st.session_state.conversation_context = []
144
- if 'current_conversation_id' not in st.session_state:
145
- st.session_state.current_conversation_id = None
146
-
147
- def show_chart_analysis_tab(claude_service, analysis_service):
148
- """Display chart analysis functionality"""
149
- # Get user expertise level
150
- expertise_level = create_expertise_selector()
151
-
152
- # Create sidebar and get inputs
153
- upload_option, uploaded_files, patterns, indicators, comparison_type = create_sidebar()
154
-
155
- # Main content area
156
- col1, col2 = st.columns([2, 1])
157
-
158
- with col1:
159
- st.title("📈 Stock Chart Analysis Assistant")
160
-
161
- if upload_option == "Ask Question":
162
- question = st.text_input(
163
- "What would you like to know?",
164
- key="main_question_input"
165
- )
166
-
167
- # Main analysis section
168
- if uploaded_files:
169
- # Store all uploaded images
170
- st.session_state.current_images = [file.getvalue() for file in uploaded_files]
171
-
172
- analyze_clicked = show_analysis_section(uploaded_files)
173
-
174
- if analyze_clicked:
175
- if not uploaded_files:
176
- st.warning("Please upload at least one chart.")
177
- else:
178
- with st.spinner("Analyzing charts..."):
179
- results = analysis_service.analyze_multiple_charts(
180
- st.session_state.current_images,
181
- patterns,
182
- indicators,
183
- comparison_type,
184
- expertise_level
185
- )
186
-
187
- if results:
188
- # Store current analysis
189
- st.session_state.current_analysis = results[-1]['analysis'] # Latest analysis
190
- st.session_state.chat_history.extend(results)
191
-
192
- # Display analyses
193
- # First show comparative analysis if it exists
194
- for result in results:
195
- if result.get('analysis_type') != 'Individual':
196
- st.subheader(f"{result['analysis_type']} Results")
197
- st.write(result['analysis'])
198
- # Add follow-up section immediately
199
- follow_up = show_follow_up_section(f"comparative_followup")
200
- if follow_up:
201
- with st.spinner("Processing follow-up..."):
202
- response = analysis_service.handle_follow_up_question(
203
- follow_up,
204
- result['analysis'],
205
- st.session_state.current_images
206
- )
207
- if response:
208
- st.session_state.chat_history.append(response)
209
- st.write(response['analysis'])
210
-
211
- # Then show individual analyses
212
- individual_analyses = [r for r in results if r.get('analysis_type') == 'Individual']
213
- for idx, result in enumerate(individual_analyses):
214
- st.subheader(f"Analysis Results - Chart {idx + 1}")
215
- st.write(result['analysis'])
216
- # Add follow-up section for each individual analysis
217
- follow_up = show_follow_up_section(f"individual_followup_{idx}")
218
- if follow_up:
219
- with st.spinner("Processing follow-up..."):
220
- response = analysis_service.handle_follow_up_question(
221
- follow_up,
222
- result['analysis'],
223
- st.session_state.current_images[idx] if idx < len(st.session_state.current_images) else None
224
- )
225
- if response:
226
- st.session_state.chat_history.append(response)
227
- st.write(response['analysis'])
228
-
229
- # Risk warning
230
- st.warning(
231
- "⚠️ This analysis is AI-generated and for informational purposes only. "
232
- "Do not make trading decisions solely based on this information."
233
- )
234
-
235
- with col2:
236
- # Chat history
237
- show_chat_history(st.session_state.chat_history)
238
-
239
- # Save options
240
- save_name = show_save_options()
241
- if save_name and st.session_state.chat_history:
242
- filename = save_chat_history(
243
- st.session_state.chat_history,
244
- st.session_state.current_images[0] if st.session_state.current_images else None,
245
- f"{save_name}.json" if save_name else None
246
- )
247
- if filename:
248
- st.success(f"Chat saved as {filename}")
249
- else:
250
- st.info("Chat saved to session state")
251
 
252
  def show_learning_tab(learning_module):
253
  """Display learning center functionality"""
@@ -271,6 +196,18 @@ def main():
271
  initial_sidebar_state="expanded"
272
  )
273
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  # Initialize services
275
  try:
276
  claude_service = ClaudeService()
@@ -283,14 +220,33 @@ def main():
283
  # Initialize session state
284
  init_session_state()
285
 
 
 
 
 
 
 
 
 
 
 
286
  # Main application tabs
287
- tab1, tab2 = st.tabs(["Chart Analysis", "Learning Center"])
288
 
289
  with tab1:
290
- show_chart_analysis_tab(claude_service, analysis_service)
291
 
292
  with tab2:
293
  show_learning_tab(learning_module)
 
 
 
 
 
 
 
 
 
294
 
295
  if __name__ == "__main__":
296
  main()
 
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 (
32
+ create_sidebar,
33
+ show_analysis_section,
34
+ show_chat_history,
35
+ show_follow_up_section,
36
+ show_save_options,
37
+ create_expertise_selector
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"""
44
+ if 'chat_history' not in st.session_state:
45
+ st.session_state.chat_history = []
46
+ if 'current_image' not in st.session_state:
47
+ st.session_state.current_image = None
48
+ if 'current_analysis' not in st.session_state:
49
+ st.session_state.current_analysis = None
50
+ if 'current_images' not in st.session_state:
51
+ st.session_state.current_images = []
52
+ if 'conversation_context' not in st.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
 
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
  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()
 
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()