import streamlit as st from brave import perform_web_search, load_web_content, generate_detailed_explanation import traceback import sys # Set page config with updated title st.set_page_config(page_title="AI Search Engine 🔍", page_icon="🔍", layout="wide") # Add this new CSS for the popup st.markdown(""" """, unsafe_allow_html=True) # Initialize session state if 'query_submitted' not in st.session_state: st.session_state.query_submitted = False if 'chat_history' not in st.session_state: st.session_state.chat_history = [] if 'show_welcome' not in st.session_state: st.session_state.show_welcome = True # Main content st.title("AI Search Engine 🔍") st.write("Ask any question and get detailed answers with sources.") # Welcome popover if st.session_state.show_welcome: with st.popover("Instructions"): st.markdown(""" 1. Only the first search performs a web search. 2. The follow-up chat is for curating or modifying the response based on the initial search results. 3. Follow-up questions do not perform additional web searches. 4. To perform a new web search, click "Start New Search" at the bottom. """) st.markdown("Enjoy using AI Search Engine!") if st.button("Let's Search"): st.session_state.show_welcome = False st.rerun() # Search input (only show if query hasn't been submitted) if not st.session_state.query_submitted: query = st.text_input( label="Search Query", placeholder="Enter your question and press Enter", key="search_input", label_visibility="collapsed" ) if query: st.session_state.query_submitted = True st.session_state.chat_history = [] # Reset chat history st.rerun() # Main content area if st.session_state.query_submitted: # Create two columns: one for the answer and chat, one for thumbnails col1, col2 = st.columns([3, 1]) with col1: if not st.session_state.chat_history: # Initial query processing with st.spinner("Searching and analyzing..."): try: search_results = perform_web_search(st.session_state.search_input) web_content = load_web_content([result['url'] for result in search_results]) detailed_explanation = generate_detailed_explanation(st.session_state.search_input, web_content) st.session_state.chat_history.append((st.session_state.search_input, detailed_explanation)) st.session_state.search_results = search_results except Exception as e: st.error(f"An error occurred: {str(e)}") st.error("Please try again or rephrase your question.") st.session_state.query_submitted = False st.rerun() # Display chat history for i, (q, a) in enumerate(st.session_state.chat_history): if i > 0: st.markdown('
', unsafe_allow_html=True) st.markdown(f'