cryogenic22 commited on
Commit
04126a0
·
verified ·
1 Parent(s): a8872f2

Create components/knowledge_base.py

Browse files
Files changed (1) hide show
  1. components/knowledge_base.py +83 -0
components/knowledge_base.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # src/components/knowledge_base.py
2
+ import streamlit as st
3
+ from datetime import datetime
4
+
5
+ def handle_doc_select():
6
+ st.session_state.current_chat = True
7
+ st.session_state.chat_history = []
8
+
9
+ def handle_start_chat():
10
+ st.session_state.current_chat = True
11
+ st.session_state.chat_history = []
12
+
13
+ def display_knowledge_base(conn, backend):
14
+ st.markdown("### 📚 Knowledge Base")
15
+
16
+ if conn is not None:
17
+ try:
18
+ cursor = conn.cursor()
19
+ cursor.execute("SELECT id, name, upload_date FROM documents ORDER BY upload_date DESC")
20
+ documents_in_db = cursor.fetchall()
21
+
22
+ if documents_in_db:
23
+ st.markdown("#### Available Documents")
24
+
25
+ for doc_id, name, upload_date in documents_in_db:
26
+ col1, col2 = st.columns([3, 1])
27
+
28
+ with col1:
29
+ selected = st.checkbox(
30
+ name,
31
+ value=doc_id in st.session_state.selected_docs,
32
+ key=f"doc_{doc_id}",
33
+ on_change=handle_doc_select
34
+ )
35
+
36
+ if selected and doc_id not in st.session_state.selected_docs:
37
+ st.session_state.selected_docs.append(doc_id)
38
+ elif not selected and doc_id in st.session_state.selected_docs:
39
+ st.session_state.selected_docs.remove(doc_id)
40
+
41
+ with col2:
42
+ upload_date = datetime.strptime(upload_date, '%Y-%m-%d %H:%M:%S')
43
+ st.text(upload_date.strftime('%Y-%m-%d'))
44
+
45
+ initialize_selected_documents(conn, backend)
46
+
47
+ if st.session_state.selected_docs:
48
+ st.button("🚀 Start New Chat",
49
+ on_click=handle_start_chat,
50
+ use_container_width=True)
51
+ else:
52
+ st.info("No documents in the knowledge base. Upload some documents to get started!")
53
+
54
+ except Exception as e:
55
+ st.error(f"Error accessing knowledge base: {e}")
56
+
57
+ def initialize_selected_documents(conn, backend):
58
+ if st.session_state.selected_docs and not st.session_state.documents_initialized:
59
+ with st.spinner("Initializing document analysis..."):
60
+ selected_documents = []
61
+ selected_doc_names = []
62
+
63
+ cursor = conn.cursor()
64
+ for doc_id in st.session_state.selected_docs:
65
+ cursor.execute(
66
+ "SELECT content, name FROM documents WHERE id = ?",
67
+ (doc_id,)
68
+ )
69
+ result = cursor.fetchone()
70
+ if result:
71
+ selected_documents.append(result[0])
72
+ selected_doc_names.append(result[1])
73
+
74
+ embeddings = backend.get_embeddings_model()
75
+ if embeddings:
76
+ vector_store = backend.initialize_faiss(
77
+ embeddings,
78
+ selected_documents,
79
+ selected_doc_names
80
+ )
81
+ if vector_store:
82
+ st.session_state.qa_system = backend.initialize_qa_system(vector_store)
83
+ st.session_state.documents_initialized = True