cryogenic22 commited on
Commit
e32a092
·
verified ·
1 Parent(s): 0c4880f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -30
app.py CHANGED
@@ -19,7 +19,6 @@ 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"""
23
  if 'chat_history' not in st.session_state:
24
  st.session_state.chat_history = []
25
  if 'current_image' not in st.session_state:
@@ -28,12 +27,12 @@ def init_session_state():
28
  st.session_state.current_analysis = None
29
  if 'current_images' not in st.session_state:
30
  st.session_state.current_images = []
31
- if 'conversation_context' not in st.session_state:
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 reset_session_state():
39
  """Reset session state for a new chat"""
@@ -60,7 +59,7 @@ def show_chart_analysis_tab(claude_service, analysis_service, storage_manager):
60
  st.session_state.chat_history,
61
  st.session_state.current_images,
62
  st.session_state.get('last_saved_chat'),
63
- claude_service # Pass claude_service for smart naming
64
  )
65
  st.success("Previous chat saved! Starting new chat...")
66
 
@@ -113,14 +112,18 @@ def show_chart_analysis_tab(claude_service, analysis_service, storage_manager):
113
  results = st.session_state.analysis_results
114
 
115
  # First show comparative analysis if it exists
116
- for result in results:
117
  if result.get('analysis_type') != 'Individual':
118
  with st.container():
119
  st.subheader(f"{result['analysis_type']} Results")
120
  st.markdown(result['analysis'])
121
 
122
- # Add follow-up section immediately after analysis
123
- follow_up = show_follow_up_section(f"comparative_followup")
 
 
 
 
124
  if follow_up:
125
  with st.spinner("Processing follow-up..."):
126
  response = analysis_service.handle_follow_up_question(
@@ -130,7 +133,10 @@ def show_chart_analysis_tab(claude_service, analysis_service, storage_manager):
130
  )
131
  if response:
132
  st.session_state.chat_history.append(response)
 
 
133
  st.markdown(response['analysis'])
 
134
  # Save to storage with smart naming
135
  storage_manager.save_chat(
136
  st.session_state.chat_history,
@@ -146,9 +152,12 @@ def show_chart_analysis_tab(claude_service, analysis_service, storage_manager):
146
  st.subheader(f"Analysis Results - Chart {idx + 1}")
147
  st.markdown(result['analysis'])
148
 
149
- # Add follow-up section for each analysis
150
- container_key = f"individual_followup_{idx}"
151
- follow_up = show_follow_up_section(container_key)
 
 
 
152
  if follow_up:
153
  with st.spinner("Processing follow-up..."):
154
  response = analysis_service.handle_follow_up_question(
@@ -158,7 +167,10 @@ def show_chart_analysis_tab(claude_service, analysis_service, storage_manager):
158
  )
159
  if response:
160
  st.session_state.chat_history.append(response)
 
 
161
  st.markdown(response['analysis'])
 
162
  # Save to storage with smart naming
163
  storage_manager.save_chat(
164
  st.session_state.chat_history,
@@ -172,25 +184,40 @@ def show_chart_analysis_tab(claude_service, analysis_service, storage_manager):
172
  "⚠️ This analysis is AI-generated and for informational purposes only. "
173
  "Do not make trading decisions solely based on this information."
174
  )
175
- #Kapil : Have commented it out as it would be good to have more space for analysis
176
- #with col2:
177
- # Chat history
178
- # show_chat_history(st.session_state.chat_history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
- # Save options
181
- # save_name = show_save_options()
182
- # if save_name and st.session_state.chat_history:
183
- # filename = storage_manager.save_chat(
184
- # st.session_state.chat_history,
185
- # st.session_state.current_images[0] if st.session_state.current_images else None,
186
- # f"{save_name}.json" if save_name else None,
187
- # claude_service
188
- #)
189
- #if filename:
190
- # st.success(f"Chat saved as {filename}")
191
- #else:
192
- # st.info("Chat saved to session state")
193
-
194
  def show_learning_tab(learning_module):
195
  """Display learning center functionality"""
196
  st.title("📚 Trading Learning Center")
 
19
  from pages.previous_chats import show_previous_chats_tab
20
 
21
  def init_session_state():
 
22
  if 'chat_history' not in st.session_state:
23
  st.session_state.chat_history = []
24
  if 'current_image' not in st.session_state:
 
27
  st.session_state.current_analysis = None
28
  if 'current_images' not in st.session_state:
29
  st.session_state.current_images = []
 
 
 
 
30
  if 'analysis_results' not in st.session_state:
31
  st.session_state.analysis_results = []
32
+ if 'followups' not in st.session_state:
33
+ st.session_state.followups = []
34
+ if 'conversation_context' not in st.session_state:
35
+ st.session_state.conversation_context = []
36
 
37
  def reset_session_state():
38
  """Reset session state for a new chat"""
 
59
  st.session_state.chat_history,
60
  st.session_state.current_images,
61
  st.session_state.get('last_saved_chat'),
62
+ claude_service
63
  )
64
  st.success("Previous chat saved! Starting new chat...")
65
 
 
112
  results = st.session_state.analysis_results
113
 
114
  # First show comparative analysis if it exists
115
+ for idx, result in enumerate(results):
116
  if result.get('analysis_type') != 'Individual':
117
  with st.container():
118
  st.subheader(f"{result['analysis_type']} Results")
119
  st.markdown(result['analysis'])
120
 
121
+ # Add enhanced follow-up section with context
122
+ follow_up = show_follow_up_section(
123
+ key_suffix=f"comparative_{idx}",
124
+ previous_response=result['analysis']
125
+ )
126
+
127
  if follow_up:
128
  with st.spinner("Processing follow-up..."):
129
  response = analysis_service.handle_follow_up_question(
 
133
  )
134
  if response:
135
  st.session_state.chat_history.append(response)
136
+
137
+ # Show response in the chat interface
138
  st.markdown(response['analysis'])
139
+
140
  # Save to storage with smart naming
141
  storage_manager.save_chat(
142
  st.session_state.chat_history,
 
152
  st.subheader(f"Analysis Results - Chart {idx + 1}")
153
  st.markdown(result['analysis'])
154
 
155
+ # Add enhanced follow-up section for each analysis
156
+ follow_up = show_follow_up_section(
157
+ key_suffix=f"individual_{idx}",
158
+ previous_response=result['analysis']
159
+ )
160
+
161
  if follow_up:
162
  with st.spinner("Processing follow-up..."):
163
  response = analysis_service.handle_follow_up_question(
 
167
  )
168
  if response:
169
  st.session_state.chat_history.append(response)
170
+
171
+ # Show response in the chat interface
172
  st.markdown(response['analysis'])
173
+
174
  # Save to storage with smart naming
175
  storage_manager.save_chat(
176
  st.session_state.chat_history,
 
184
  "⚠️ This analysis is AI-generated and for informational purposes only. "
185
  "Do not make trading decisions solely based on this information."
186
  )
187
+
188
+ with col2:
189
+ # Add a collapsible history section
190
+ with st.expander("💬 View Chat History", expanded=False):
191
+ if st.session_state.chat_history:
192
+ # Show only last 3 interactions by default
193
+ show_full = st.checkbox("Show full history", value=False)
194
+
195
+ if show_full:
196
+ history_to_show = st.session_state.chat_history
197
+ else:
198
+ history_to_show = st.session_state.chat_history[-3:]
199
+ if len(st.session_state.chat_history) > 3:
200
+ st.info(f"Showing last 3 of {len(st.session_state.chat_history)} interactions")
201
+
202
+ # Display the selected history
203
+ show_chat_history(history_to_show)
204
+ else:
205
+ st.info("No chat history yet")
206
 
207
+ # Add save options in a separate expander
208
+ with st.expander("💾 Save Analysis", expanded=False):
209
+ save_name = show_save_options()
210
+ if save_name and st.session_state.chat_history:
211
+ filename = storage_manager.save_chat(
212
+ st.session_state.chat_history,
213
+ st.session_state.current_images[0] if st.session_state.current_images else None,
214
+ f"{save_name}.json" if save_name else None,
215
+ claude_service
216
+ )
217
+ if filename:
218
+ st.success(f"Chat saved as {filename}")
219
+ else:
220
+ st.info("Chat saved to session state")
221
  def show_learning_tab(learning_module):
222
  """Display learning center functionality"""
223
  st.title("📚 Trading Learning Center")