-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
437 lines (393 loc) · 17.2 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import streamlit as st
import pandas as pd
import os
from dotenv import load_dotenv
from PIL import Image
import base64
import io
from llama_index.core.llms import ChatMessage, MessageRole
from llama_index.core.schema import NodeRelationship
import re
import tempfile
from vector_search import *
# ==========================================================
# Section: Page Config
# ==========================================================
st.set_page_config(
page_title="DeepKnowledge.net - Your intelligent Q&A AI",
page_icon="./assets/favicon.ico",
layout="wide",
initial_sidebar_state="expanded",
)
# ==========================================================
# Section: CSS Styling
# ==========================================================
# Layout
st.markdown("""
<style>
div[data-testid="stToolbar"] {
visibility: hidden;
height: 0%;
position: fixed;
}
div[data-testid="stDecoration"] {
visibility: hidden;
height: 0%;
position: fixed;
}
div[data-testid="stStatusWidget"] {
visibility: hidden;
height: 0%;
position: fixed;
}
#MainMenu {
visibility: hidden;
height: 0%;
}
header {
visibility: hidden;
height: 0%;
}
footer {
visibility: hidden;
height: 0%;
}
.css-15zrgzn {display: none}
#root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 0rem;}
/* Remove blank space at top and bottom */
.block-container {
padding-top: 0rem;
padding-bottom: 0rem;
}
/* Remove blank space at the center canvas */
.st-emotion-cache-z5fcl4 {
position: relative;
top: -62px;
}
/* Make the toolbar transparent and the content below it clickable */
.st-emotion-cache-18ni7ap {
pointer-events: none;
background: rgb(255 255 255 / 0%)
}
.st-emotion-cache-zq5wmm {
pointer-events: auto;
background: rgb(255 255 255);
border-radius: 5px;
}
</style>
""", unsafe_allow_html=True)
# Footer
st.markdown("""
<style>
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #232323;
color: #FFFFFF;
text-align: center;
padding: 0px 0;
font-size: 15px;
height: 35px;
line-height: 30px;
}
.footer a {
color: #6464ef;
text-decoration: none;
}
</style>
<div class="footer">
<p>Made by <a href='https://github.com/ErnestAroozoo' target='_blank'>Ernest Aroozoo</a> | <a href='https://github.com/ErnestAroozoo/DeepKnowledge.net' target='_blank'>View on GitHub</a></p>
</div>
""", unsafe_allow_html=True)
# Title
def image_to_base64(img_path):
img = Image.open(img_path)
img_data = io.BytesIO()
img.save(img_data, format='PNG')
img_base64 = base64.b64encode(img_data.getvalue()).decode('utf-8')
return img_base64
custom_title = """
<style>
.custom-title {{
display: flex;
align-items: center;
font-family: Arial, sans-serif;
color: #FFFFFF;
}}
.custom-title img {{
height: 55px;
margin-right: 3px;
position: relative;
top: -8px;
}}
.custom-title h1 {{
font-size: 2.5rem;
margin: 0;
}}
</style>
<div class="custom-title">
<img src="data:image/png;base64,{logo}" alt="Logo">
<h2>DeepKnowledge.net</h2>
</div>
"""
logo_base64 = image_to_base64('./assets/logo.png')
custom_title = custom_title.format(logo=logo_base64)
st.markdown(custom_title, unsafe_allow_html=True)
st.html("<style>[data-testid='stHeaderActionElements'] {display: none;}</style>")
# ==========================================================
# Section: Streamlit UI and Logic
# ==========================================================
def is_valid_url(url):
"""
Function to validate whether the string is a valid URL
"""
regex = re.compile(
r'^(https?://)' # http:// or https://
r'(([a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,})' # domain
r'(/[a-zA-Z0-9@:%._\+~#=/-]*)*' # path
r'(\?[a-zA-Z0-9&=_%-]*)?' # query string
r'(#.*)?$' # fragment locator
)
return re.match(regex, url) is not None
def get_all_sources_from_index(index):
"""
Get unified list of all sources (websites and documents) from index
Returns list of dicts with 'Type' and 'Source' keys
"""
sources = []
for node_id in index.docstore.docs.keys():
node = index.docstore.get_node(node_id)
# Check for document first
if 'file_name' in node.metadata:
sources.append({
'Type': 'Document',
'Source': node.metadata['file_name']
})
# Then check for website URL
else:
source_relation = node.relationships.get(NodeRelationship.SOURCE)
if source_relation and source_relation.node_id:
sources.append({
'Type': 'Website',
'Source': source_relation.node_id
})
# Remove duplicates while preserving order
seen = set()
return [x for x in sources if not (x['Source'] in seen or seen.add(x['Source']))]
def get_urls_from_index(index):
"""
Get unique website URLs from index (ONLY web sources)
"""
urls = set()
for node_id in index.docstore.docs.keys():
node = index.docstore.get_node(node_id)
# Only consider nodes that DON'T have document metadata
if 'file_name' not in node.metadata:
source_relation = node.relationships.get(NodeRelationship.SOURCE)
if source_relation and source_relation.node_id:
urls.add(source_relation.node_id)
return sorted(urls)
def get_file_names_from_index(index):
"""
Get unique document filenames from index (ONLY document sources)
"""
file_names = set()
for node_id in index.docstore.docs.keys():
node = index.docstore.get_node(node_id)
# Only consider nodes with document metadata
if 'file_name' in node.metadata:
file_names.add(node.metadata['file_name'])
return sorted(file_names)
# Initialize default URLs to be added to vector store
if "index" not in st.session_state:
default_urls = [
"https://www.apple.com/newsroom/2024/02/apple-reports-first-quarter-results/",
"https://www.apple.com/newsroom/2024/05/apple-reports-second-quarter-results/",
"https://www.apple.com/newsroom/2024/08/apple-reports-third-quarter-results/",
"https://www.apple.com/newsroom/2024/10/apple-reports-fourth-quarter-results/"
]
st.session_state.documents = load_web_data(default_urls)
st.session_state.index = create_vector_store(st.session_state.documents)
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Initial assistant message
initial_message = "Hi! I'm your AI assistant, ready to help answer your questions using the documents or websites you've added to the knowledge base. Ask me anything, and I'll provide accurate, relevant answers based on the information available!"
st.session_state.messages.append(ChatMessage(role="assistant", content=initial_message))
@st.fragment()
def knowledge_base_layout():
"""
Fragmented UI component for Knowledge Base
"""
st.write("") # Empty padding
# Knowledge Base
with st.expander(":material/database: Knowledge Base", expanded=True):
col1, col2 = st.columns(2)
# Add Data
with col1:
st.subheader(":material/library_add: Add Data")
# Website URL Input
with st.form("url_form", clear_on_submit=True, border=False):
website_url = st.text_input(
label="Website",
placeholder="Type a website URL here (e.g. https://website.com)"
)
submitted_url = st.form_submit_button(":material/upload: Add website")
# Process submitted URL
if submitted_url and len(website_url) > 0:
# Check if URL is valid using regex
if is_valid_url(website_url):
# Get EXISTING website URLs only
current_urls = get_urls_from_index(st.session_state.index)
# Check if URL already exists
if website_url not in current_urls:
try:
# Load ONLY the new URL (not reloading existing ones)
new_web_docs = load_web_data([website_url])
# Merge with existing documents
updated_documents = st.session_state.documents + new_web_docs
# Update session state
st.session_state.documents = updated_documents
st.session_state.index = create_vector_store(updated_documents)
st.success("Added 1 website to the knowledge base.", icon=":material/task_alt:")
except Exception as e:
st.error(f"Error loading website.", icon=":material/error:")
else:
st.warning("Website already exists in the knowledge base.", icon=":material/warning:")
else:
st.error("Invalid URL. Please enter a valid website link.", icon=":material/error:")
st.write("") # Empty padding
# Document Uploader
with st.form("document_form", clear_on_submit=True, border=False):
uploaded_files = st.file_uploader(
"Document",
type=["pdf", "docx"],
accept_multiple_files=True
)
submitted_file = st.form_submit_button(":material/upload: Add files")
# Process uploaded document
if submitted_file and len(uploaded_files) > 0:
try:
# Get existing DOCUMENT sources only
existing_files = get_file_names_from_index(st.session_state.index)
# Filter new files
new_files = [f for f in uploaded_files if f.name not in existing_files]
# Check if file already exists
if new_files:
with tempfile.TemporaryDirectory() as temp_dir:
# Save and process new files
saved_paths = []
for file in new_files:
file_path = os.path.join(temp_dir, file.name)
with open(file_path, "wb") as f:
f.write(file.getvalue())
saved_paths.append(file_path)
# Load new documents
new_docs = load_document_data(temp_dir)
# Update state
updated_documents = st.session_state.documents + new_docs
st.session_state.documents = updated_documents
st.session_state.index = create_vector_store(updated_documents)
st.success(f"Added {len(new_files)} file(s) to the knowledge base.", icon=":material/task_alt:")
else:
st.warning("Files already exist in knowledge base.", icon=":material/warning:")
except Exception as e:
st.error(f"Error processing files.", icon=":material/error:")
# Data Source Display
with col2:
st.subheader(":material/database: Knowledge Base")
# Get properly separated sources
sources = get_all_sources_from_index(st.session_state.index)
vector_store_df = pd.DataFrame(sources)
# Display sources in vector store
st.dataframe(
vector_store_df,
hide_index=True,
use_container_width=True
)
knowledge_base_layout()
@st.fragment
def chat_layout():
"""
Fragmented UI component for Chat
"""
with st.container(height=515, border=False):
col1, col2 = st.columns(2)
# Chatbox
with col1:
st.subheader(":material/forum: Chat")
# Create a placeholder container that holds all the messages
messages_placeholder = st.container(height=393, border=False)
# Display chat messages from history on app rerun
with messages_placeholder:
for message in st.session_state.messages:
with st.chat_message(message.role):
st.markdown(message.content)
# User Chat Input
if user_message := st.chat_input("Type your message here..."):
# Display user message in Chat Display
with messages_placeholder:
with st.chat_message("user"):
st.markdown(user_message)
# Generate AI response
assistant_response, sources = chat_response(user_message, st.session_state.messages, st.session_state.index)
# Append "user" message to chat history
st.session_state.messages.append(ChatMessage(role="user", content=user_message))
# Append "assistant" message to chat history
st.session_state.messages.append(ChatMessage(role="assistant", content=assistant_response))
# Store sources in session state for display in Sources section
st.session_state.sources = sources
# Display assistant message in Chat Display
with messages_placeholder:
with st.chat_message("assistant"):
st.markdown(assistant_response)
# Document Sources Table
with col2:
st.subheader(":material/fact_check: Relevant Sources")
# Display the sources if available
if 'sources' in st.session_state:
if len(st.session_state.sources) == 0:
st.warning("No relevant documents or websites found in knowledge base.", icon=":material/warning:")
else:
# Create a DataFrame from the sources dict object
sources_df = pd.DataFrame(st.session_state.sources)
sources_df = sources_df.rename(columns={
'score': 'Relevance',
'source': 'Source',
'text': 'Text'
})
# Display DataFrame
st.dataframe(sources_df, hide_index=True, use_container_width=True)
else:
st.info("Relevant documents or websites from the knowledge base will appear here once you start asking questions.", icon=":material/info:")
chat_layout()
# ==========================================================
# Section: CSS Footer
# ==========================================================
# Footer
st.markdown("""
<style>
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #232323;
color: #FFFFFF;
text-align: center;
padding: 0px 0;
font-size: 15px;
height: 35px;
line-height: 30px;
}
.footer a {
color: #6464ef;
text-decoration: none;
}
</style>
<div class="footer">
<p>Made by <a href='https://github.com/ErnestAroozoo' target='_blank'>Ernest Aroozoo</a> | <a href='https://github.com/ErnestAroozoo/DeepKnowledge.net' target='_blank'>View on GitHub</a></p>
</div>
""", unsafe_allow_html=True)