Spaces:
Runtime error
Runtime error
| # ui/components.py | |
| import streamlit as st | |
| from config.settings import CHART_PATTERNS, TECHNICAL_INDICATORS | |
| # ui/components.py | |
| import streamlit as st | |
| from datetime import datetime | |
| from config.settings import CHART_PATTERNS, TECHNICAL_INDICATORS | |
| def create_sidebar(): | |
| """Create the sidebar with analysis options""" | |
| with st.sidebar: | |
| st.title("🚀 Chart Analysis AI") | |
| # Add New Chat button at the top | |
| if st.button("📝 New Chat", key="new_chat_button", type="primary"): | |
| # Save current chat if it exists | |
| if st.session_state.chat_history: | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| st.session_state['last_saved_chat'] = f"chat_{timestamp}" | |
| return "new_chat", None, [], [], "Individual Analysis" | |
| upload_option = st.radio( | |
| "Choose input method:", | |
| ("Upload Images", "Take Screenshot", "Ask Question"), | |
| key="sidebar_upload_method" | |
| ) | |
| uploaded_files = None | |
| if upload_option == "Upload Images": | |
| uploaded_files = st.file_uploader( | |
| "Upload your charts", | |
| type=["png", "jpg", "jpeg"], | |
| accept_multiple_files=True, | |
| key="sidebar_file_uploader" | |
| ) | |
| if uploaded_files: | |
| st.info(f"Uploaded {len(uploaded_files)} charts") | |
| elif upload_option == "Take Screenshot": | |
| if st.button("Take Screenshot", key="sidebar_screenshot_btn"): | |
| st.info("Feature coming soon! For now, please use the Upload Images option.") | |
| st.subheader("Analysis Options") | |
| patterns = st.multiselect( | |
| "Patterns to Look For", | |
| CHART_PATTERNS, | |
| key="sidebar_patterns" | |
| ) | |
| indicators = st.multiselect( | |
| "Technical Indicators", | |
| TECHNICAL_INDICATORS, | |
| key="sidebar_indicators" | |
| ) | |
| # Add comparison options | |
| if uploaded_files and len(uploaded_files) > 1: | |
| st.subheader("Comparison Options") | |
| comparison_type = st.selectbox( | |
| "Comparison Type", | |
| ["Individual Analysis", "Correlated Analysis", "Market Trend Analysis"], | |
| key="comparison_type" | |
| ) | |
| st.info(""" | |
| - Individual Analysis: Analyze each chart separately | |
| - Correlated Analysis: Find relationships between charts | |
| - Market Trend Analysis: Identify broader market patterns | |
| """) | |
| else: | |
| comparison_type = "Individual Analysis" | |
| return upload_option, uploaded_files, patterns, indicators, comparison_type | |
| def show_analysis_section(uploaded_files): | |
| """Show the main analysis section""" | |
| if uploaded_files: | |
| # Create a grid layout for multiple images | |
| cols = st.columns(min(len(uploaded_files), 2)) # Max 2 columns | |
| for idx, uploaded_file in enumerate(uploaded_files): | |
| with cols[idx % 2]: | |
| st.image(uploaded_file, caption=f"Chart {idx + 1}", use_container_width=True) | |
| analyze_btn = st.button("Analyze Charts", key="main_analyze_btn") | |
| return analyze_btn | |
| def show_chat_history(chat_history): | |
| """Display chat history""" | |
| st.subheader("Current Chat History") | |
| for idx, chat in enumerate(chat_history): | |
| timestamp = chat.get('timestamp', datetime.now().isoformat()) | |
| analysis_type = chat.get('analysis_type', 'Analysis') | |
| with st.container(): | |
| if analysis_type in ['Individual', 'Correlated Analysis']: | |
| st.markdown("**Initial Analysis:**") | |
| elif 'question' in chat: | |
| st.markdown(f"**Follow-up Question:** {chat['question']}") | |
| st.markdown(chat.get('analysis', '')) | |
| st.markdown("---") | |
| def show_follow_up_section(key_suffix="", previous_response=None): | |
| """Show the follow-up question section with enhanced chat UI""" | |
| # Show previous response if provided | |
| if previous_response: | |
| with st.container(): | |
| st.markdown("**Previous Response:**") | |
| st.markdown(previous_response) | |
| st.markdown("---") | |
| # Input section | |
| col1, col2 = st.columns([4, 1]) | |
| with col1: | |
| follow_up = st.text_input( | |
| "Ask a follow-up question:", | |
| key=f"followup_input_{key_suffix}", | |
| placeholder="Type your question here..." | |
| ) | |
| with col2: | |
| send_btn = st.button("Send", key=f"followup_send_btn_{key_suffix}") | |
| return follow_up if send_btn else None | |
| def show_save_options(): | |
| """Show save chat options""" | |
| st.subheader("Save Analysis") | |
| save_name = st.text_input( | |
| "Save chat as (optional):", | |
| key="save_chat_name" | |
| ) | |
| save_btn = st.button("Save", key="save_chat_btn") | |
| return save_name if save_btn else None | |
| def display_conversation_group(conversation): | |
| """Display a group of related chat messages""" | |
| if not conversation: | |
| return | |
| # Use the first message timestamp for the expander label | |
| timestamp = conversation[0].get('timestamp', 'No date') | |
| analysis_type = conversation[0].get('analysis_type', 'Analysis') | |
| with st.expander(f"{analysis_type} from {timestamp[:16]}", expanded=True): | |
| for message in conversation: | |
| if message.get('analysis_type') in ['Individual', 'Correlated Analysis']: | |
| st.markdown("**Initial Analysis:**") | |
| elif message.get('question'): | |
| st.markdown(f"**Follow-up Question:** {message['question']}") | |
| st.markdown(message.get('analysis', '')) | |
| def create_expertise_selector(): | |
| """Create expertise level selector""" | |
| if 'expertise_level' not in st.session_state: | |
| st.session_state.expertise_level = "Novice" | |
| expertise_descriptions = { | |
| "Novice": "New to trading, explain concepts in simple terms", | |
| "Intermediate": "Familiar with basic concepts, can handle some technical terms", | |
| "Expert": "Experienced trader, use full technical analysis terminology" | |
| } | |
| with st.sidebar: | |
| st.subheader("Expertise Level") | |
| expertise_level = st.selectbox( | |
| "Select your trading knowledge level:", | |
| list(expertise_descriptions.keys()), | |
| key="expertise_selector" | |
| ) | |
| st.info(expertise_descriptions[expertise_level]) | |
| st.session_state.expertise_level = expertise_level | |
| return expertise_level |