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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from brave import perform_web_search, load_web_content, generate_detailed_explanation
|
| 3 |
+
import traceback
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
# Set page config with updated title
|
| 7 |
+
st.set_page_config(page_title="AI Search Engine 🔍", page_icon="🔍", layout="wide")
|
| 8 |
+
|
| 9 |
+
# Add this new CSS for the popup
|
| 10 |
+
st.markdown("""
|
| 11 |
+
<style>
|
| 12 |
+
.stTextInput > div > div > input {
|
| 13 |
+
font-size: 20px;
|
| 14 |
+
border-radius: 25px;
|
| 15 |
+
width: 100%;
|
| 16 |
+
}
|
| 17 |
+
.result-box {
|
| 18 |
+
background-color: white;
|
| 19 |
+
border: 1px solid #e0e0e0;
|
| 20 |
+
border-radius: 10px;
|
| 21 |
+
padding: 20px;
|
| 22 |
+
margin-bottom: 20px;
|
| 23 |
+
}
|
| 24 |
+
.source-box {
|
| 25 |
+
background-color: #f0f2f6;
|
| 26 |
+
border-radius: 5px;
|
| 27 |
+
padding: 10px;
|
| 28 |
+
margin-bottom: 10px;
|
| 29 |
+
}
|
| 30 |
+
.thumbnail-image {
|
| 31 |
+
width: 100%;
|
| 32 |
+
height: 150px;
|
| 33 |
+
object-fit: cover;
|
| 34 |
+
border-radius: 10px;
|
| 35 |
+
cursor: pointer;
|
| 36 |
+
}
|
| 37 |
+
.thumbnail-title {
|
| 38 |
+
font-size: 12px;
|
| 39 |
+
overflow-wrap: break-word;
|
| 40 |
+
word-wrap: break-word;
|
| 41 |
+
hyphens: auto;
|
| 42 |
+
margin-top: 5px;
|
| 43 |
+
margin-bottom: 15px;
|
| 44 |
+
cursor: pointer;
|
| 45 |
+
}
|
| 46 |
+
.thumbnail-source {
|
| 47 |
+
font-size: 10px;
|
| 48 |
+
color: #888;
|
| 49 |
+
margin-top: 5px;
|
| 50 |
+
}
|
| 51 |
+
.source-info {
|
| 52 |
+
display: flex;
|
| 53 |
+
align-items: center;
|
| 54 |
+
font-size: 12px;
|
| 55 |
+
color: #888;
|
| 56 |
+
margin-top: 5px;
|
| 57 |
+
}
|
| 58 |
+
.source-info span {
|
| 59 |
+
margin-right: 5px;
|
| 60 |
+
}
|
| 61 |
+
.query {
|
| 62 |
+
font-size: 24px;
|
| 63 |
+
font-weight: bold;
|
| 64 |
+
margin-bottom: 10px;
|
| 65 |
+
}
|
| 66 |
+
.answer {
|
| 67 |
+
font-size: 16px;
|
| 68 |
+
margin-bottom: 20px;
|
| 69 |
+
}
|
| 70 |
+
.separator {
|
| 71 |
+
border-top: 1px solid #e0e0e0;
|
| 72 |
+
margin: 20px 0;
|
| 73 |
+
}
|
| 74 |
+
.popup-container {
|
| 75 |
+
position: fixed;
|
| 76 |
+
top: 0;
|
| 77 |
+
left: 0;
|
| 78 |
+
right: 0;
|
| 79 |
+
bottom: 0;
|
| 80 |
+
background-color: rgba(0, 0, 0, 0.5);
|
| 81 |
+
display: flex;
|
| 82 |
+
justify-content: center;
|
| 83 |
+
align-items: center;
|
| 84 |
+
z-index: 1000;
|
| 85 |
+
}
|
| 86 |
+
.popup-content {
|
| 87 |
+
background-color: white;
|
| 88 |
+
padding: 20px;
|
| 89 |
+
border-radius: 10px;
|
| 90 |
+
max-width: 500px;
|
| 91 |
+
width: 100%;
|
| 92 |
+
}
|
| 93 |
+
</style>
|
| 94 |
+
""", unsafe_allow_html=True)
|
| 95 |
+
|
| 96 |
+
# Initialize session state
|
| 97 |
+
if 'query_submitted' not in st.session_state:
|
| 98 |
+
st.session_state.query_submitted = False
|
| 99 |
+
if 'chat_history' not in st.session_state:
|
| 100 |
+
st.session_state.chat_history = []
|
| 101 |
+
if 'show_welcome' not in st.session_state:
|
| 102 |
+
st.session_state.show_welcome = True
|
| 103 |
+
|
| 104 |
+
# Main content
|
| 105 |
+
st.title("AI Search Engine 🔍")
|
| 106 |
+
st.write("Ask any question and get detailed answers with sources.")
|
| 107 |
+
|
| 108 |
+
# Welcome popover
|
| 109 |
+
if st.session_state.show_welcome:
|
| 110 |
+
with st.popover("Instructions"):
|
| 111 |
+
st.markdown("""
|
| 112 |
+
1. Only the first search performs a web search.
|
| 113 |
+
2. The follow-up chat is for curating or modifying the response based on the initial search results.
|
| 114 |
+
3. Follow-up questions do not perform additional web searches.
|
| 115 |
+
4. To perform a new web search, click "Start New Search" at the bottom.
|
| 116 |
+
""")
|
| 117 |
+
st.markdown("Enjoy using AI Search Engine!")
|
| 118 |
+
if st.button("Let's Search"):
|
| 119 |
+
st.session_state.show_welcome = False
|
| 120 |
+
st.rerun()
|
| 121 |
+
|
| 122 |
+
# Search input (only show if query hasn't been submitted)
|
| 123 |
+
if not st.session_state.query_submitted:
|
| 124 |
+
query = st.text_input(
|
| 125 |
+
label="Search Query",
|
| 126 |
+
placeholder="Enter your question and press Enter",
|
| 127 |
+
key="search_input",
|
| 128 |
+
label_visibility="collapsed"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
if query:
|
| 132 |
+
st.session_state.query_submitted = True
|
| 133 |
+
st.session_state.chat_history = [] # Reset chat history
|
| 134 |
+
st.rerun()
|
| 135 |
+
|
| 136 |
+
# Main content area
|
| 137 |
+
if st.session_state.query_submitted:
|
| 138 |
+
# Create two columns: one for the answer and chat, one for thumbnails
|
| 139 |
+
col1, col2 = st.columns([3, 1])
|
| 140 |
+
|
| 141 |
+
with col1:
|
| 142 |
+
if not st.session_state.chat_history:
|
| 143 |
+
# Initial query processing
|
| 144 |
+
with st.spinner("Searching and analyzing..."):
|
| 145 |
+
try:
|
| 146 |
+
search_results = perform_web_search(st.session_state.search_input)
|
| 147 |
+
web_content = load_web_content([result['url'] for result in search_results])
|
| 148 |
+
detailed_explanation = generate_detailed_explanation(st.session_state.search_input, web_content)
|
| 149 |
+
st.session_state.chat_history.append((st.session_state.search_input, detailed_explanation))
|
| 150 |
+
st.session_state.search_results = search_results
|
| 151 |
+
except Exception as e:
|
| 152 |
+
st.error(f"An error occurred: {str(e)}")
|
| 153 |
+
st.error("Please try again or rephrase your question.")
|
| 154 |
+
st.session_state.query_submitted = False
|
| 155 |
+
st.rerun()
|
| 156 |
+
|
| 157 |
+
# Display chat history
|
| 158 |
+
for i, (q, a) in enumerate(st.session_state.chat_history):
|
| 159 |
+
if i > 0:
|
| 160 |
+
st.markdown('<div class="separator"></div>', unsafe_allow_html=True)
|
| 161 |
+
st.markdown(f'<div class="query">{q}</div>', unsafe_allow_html=True)
|
| 162 |
+
st.markdown(f'<div class="answer">{a}</div>', unsafe_allow_html=True)
|
| 163 |
+
|
| 164 |
+
# Follow-up question input
|
| 165 |
+
follow_up_key = f"follow_up_{len(st.session_state.chat_history)}"
|
| 166 |
+
follow_up_query = st.text_input("Ask a follow-up question:", key=follow_up_key)
|
| 167 |
+
|
| 168 |
+
if follow_up_query:
|
| 169 |
+
with st.spinner("Generating response..."):
|
| 170 |
+
try:
|
| 171 |
+
follow_up_context = "\n".join([f"Q: {q}\nA: {a}" for q, a in st.session_state.chat_history[-5:]])
|
| 172 |
+
follow_up_prompt = f"""Based on the following context and the new question, provide a detailed answer. If the new question is not related to the previous context, answer it independently.
|
| 173 |
+
|
| 174 |
+
Context:
|
| 175 |
+
{follow_up_context}
|
| 176 |
+
|
| 177 |
+
New Question: {follow_up_query}
|
| 178 |
+
|
| 179 |
+
Answer:"""
|
| 180 |
+
|
| 181 |
+
follow_up_response = generate_detailed_explanation(follow_up_query, follow_up_prompt)
|
| 182 |
+
st.session_state.chat_history.append((follow_up_query, follow_up_response))
|
| 183 |
+
st.rerun() # Rerun to display the new response
|
| 184 |
+
except Exception as e:
|
| 185 |
+
st.error(f"An error occurred while generating the follow-up response: {str(e)}")
|
| 186 |
+
|
| 187 |
+
# Add a button to start a new search
|
| 188 |
+
if st.button("Start New Search"):
|
| 189 |
+
st.session_state.query_submitted = False
|
| 190 |
+
st.session_state.chat_history = []
|
| 191 |
+
st.rerun()
|
| 192 |
+
|
| 193 |
+
with col2:
|
| 194 |
+
# Display related images and sources
|
| 195 |
+
st.markdown("### Related Images")
|
| 196 |
+
for result in st.session_state.search_results:
|
| 197 |
+
if result['thumbnail']:
|
| 198 |
+
st.markdown(f"<a href='{result['url']}' target='_blank'><img src='{result['thumbnail']}' class='thumbnail-image'></a>", unsafe_allow_html=True)
|
| 199 |
+
st.markdown(f"<a href='{result['url']}' target='_blank' style='text-decoration: none; color: inherit;'><div class='thumbnail-title'>{result['title']}</div></a>", unsafe_allow_html=True)
|
| 200 |
+
st.markdown(f"""
|
| 201 |
+
<div class='source-info'>
|
| 202 |
+
<span>Source: </span>
|
| 203 |
+
<a href='https://{result['hostname']}' target='_blank' style='text-decoration: none; color: #888;'>
|
| 204 |
+
{result['hostname']}
|
| 205 |
+
</a>
|
| 206 |
+
</div>
|
| 207 |
+
""", unsafe_allow_html=True)
|
| 208 |
+
st.markdown("---")
|