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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def show_chart_analysis_tab(claude_service, analysis_service):
|
| 2 |
+
"""Display chart analysis functionality"""
|
| 3 |
+
# Get user expertise level
|
| 4 |
+
expertise_level = create_expertise_selector()
|
| 5 |
+
|
| 6 |
+
# Create sidebar and get inputs
|
| 7 |
+
upload_option, uploaded_files, patterns, indicators, comparison_type = create_sidebar()
|
| 8 |
+
|
| 9 |
+
# Main content area
|
| 10 |
+
col1, col2 = st.columns([2, 1])
|
| 11 |
+
|
| 12 |
+
with col1:
|
| 13 |
+
st.title("📈 Stock Chart Analysis Assistant")
|
| 14 |
+
|
| 15 |
+
if upload_option == "Ask Question":
|
| 16 |
+
question = st.text_input(
|
| 17 |
+
"What would you like to know?",
|
| 18 |
+
key="main_question_input"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Main analysis section
|
| 22 |
+
if uploaded_files:
|
| 23 |
+
# Store all uploaded images
|
| 24 |
+
st.session_state.current_images = [file.getvalue() for file in uploaded_files]
|
| 25 |
+
|
| 26 |
+
analyze_clicked = show_analysis_section(uploaded_files)
|
| 27 |
+
|
| 28 |
+
# Store analysis results in session state
|
| 29 |
+
if 'analysis_results' not in st.session_state:
|
| 30 |
+
st.session_state.analysis_results = []
|
| 31 |
+
|
| 32 |
+
if analyze_clicked:
|
| 33 |
+
if not uploaded_files:
|
| 34 |
+
st.warning("Please upload at least one chart.")
|
| 35 |
+
else:
|
| 36 |
+
with st.spinner("Analyzing charts..."):
|
| 37 |
+
results = analysis_service.analyze_multiple_charts(
|
| 38 |
+
st.session_state.current_images,
|
| 39 |
+
patterns,
|
| 40 |
+
indicators,
|
| 41 |
+
comparison_type,
|
| 42 |
+
expertise_level
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if results:
|
| 46 |
+
st.session_state.current_analysis = results[-1]['analysis']
|
| 47 |
+
st.session_state.chat_history.extend(results)
|
| 48 |
+
st.session_state.analysis_results = results
|
| 49 |
+
|
| 50 |
+
# Display analysis results from session state
|
| 51 |
+
if st.session_state.analysis_results:
|
| 52 |
+
results = st.session_state.analysis_results
|
| 53 |
+
|
| 54 |
+
# First show comparative analysis if it exists
|
| 55 |
+
for result in results:
|
| 56 |
+
if result.get('analysis_type') != 'Individual':
|
| 57 |
+
with st.container():
|
| 58 |
+
st.subheader(f"{result['analysis_type']} Results")
|
| 59 |
+
st.markdown(result['analysis'])
|
| 60 |
+
|
| 61 |
+
# Add follow-up section immediately after analysis
|
| 62 |
+
follow_up = show_follow_up_section(f"comparative_followup")
|
| 63 |
+
if follow_up:
|
| 64 |
+
with st.spinner("Processing follow-up..."):
|
| 65 |
+
response = analysis_service.handle_follow_up_question(
|
| 66 |
+
follow_up,
|
| 67 |
+
result['analysis'],
|
| 68 |
+
st.session_state.current_images
|
| 69 |
+
)
|
| 70 |
+
if response:
|
| 71 |
+
st.session_state.chat_history.append(response)
|
| 72 |
+
st.markdown(response['analysis'])
|
| 73 |
+
|
| 74 |
+
# Then show individual analyses
|
| 75 |
+
individual_analyses = [r for r in results if r.get('analysis_type') == 'Individual']
|
| 76 |
+
for idx, result in enumerate(individual_analyses):
|
| 77 |
+
with st.container():
|
| 78 |
+
st.subheader(f"Analysis Results - Chart {idx + 1}")
|
| 79 |
+
st.markdown(result['analysis'])
|
| 80 |
+
|
| 81 |
+
# Add follow-up section for each analysis
|
| 82 |
+
container_key = f"individual_followup_{idx}"
|
| 83 |
+
follow_up = show_follow_up_section(container_key)
|
| 84 |
+
if follow_up:
|
| 85 |
+
with st.spinner("Processing follow-up..."):
|
| 86 |
+
response = analysis_service.handle_follow_up_question(
|
| 87 |
+
follow_up,
|
| 88 |
+
result['analysis'],
|
| 89 |
+
st.session_state.current_images[idx] if idx < len(st.session_state.current_images) else None
|
| 90 |
+
)
|
| 91 |
+
if response:
|
| 92 |
+
st.session_state.chat_history.append(response)
|
| 93 |
+
st.markdown(response['analysis'])
|
| 94 |
+
|
| 95 |
+
# Risk warning at the bottom
|
| 96 |
+
st.warning(
|
| 97 |
+
"⚠️ This analysis is AI-generated and for informational purposes only. "
|
| 98 |
+
"Do not make trading decisions solely based on this information."
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with col2:
|
| 102 |
+
# Chat history
|
| 103 |
+
show_chat_history(st.session_state.chat_history)
|
| 104 |
+
|
| 105 |
+
# Save options
|
| 106 |
+
save_name = show_save_options()
|
| 107 |
+
if save_name and st.session_state.chat_history:
|
| 108 |
+
filename = save_chat_history(
|
| 109 |
+
st.session_state.chat_history,
|
| 110 |
+
st.session_state.current_images[0] if st.session_state.current_images else None,
|
| 111 |
+
f"{save_name}.json" if save_name else None
|
| 112 |
+
)
|
| 113 |
+
if filename:
|
| 114 |
+
st.success(f"Chat saved as {filename}")
|
| 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"""
|
| 254 |
+
st.title("📚 Trading Learning Center")
|
| 255 |
+
|
| 256 |
+
# Create tabs for different learning options
|
| 257 |
+
learn_tab1, learn_tab2 = st.tabs(["Structured Courses", "Custom Learning"])
|
| 258 |
+
|
| 259 |
+
with learn_tab1:
|
| 260 |
+
st.header("Learn Trading Step by Step")
|
| 261 |
+
learning_module.display_course_selection()
|
| 262 |
+
|
| 263 |
+
with learn_tab2:
|
| 264 |
+
st.header("Ask Any Trading Question")
|
| 265 |
+
learning_module.display_custom_learning()
|
| 266 |
+
|
| 267 |
+
def main():
|
| 268 |
+
st.set_page_config(
|
| 269 |
+
page_title="Stock Chart Assistant",
|
| 270 |
+
layout="wide",
|
| 271 |
+
initial_sidebar_state="expanded"
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
# Initialize services
|
| 275 |
+
try:
|
| 276 |
+
claude_service = ClaudeService()
|
| 277 |
+
analysis_service = ChartAnalysisService(claude_service)
|
| 278 |
+
learning_module = LearningModule(claude_service)
|
| 279 |
+
except ValueError as e:
|
| 280 |
+
st.error(str(e))
|
| 281 |
+
return
|
| 282 |
+
|
| 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()
|