cryogenic22 commited on
Commit
40dbd2c
·
verified ·
1 Parent(s): d1f5e00

Create components/document_store.py

Browse files
components/components/document_store.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # components/document_store.py
2
+
3
+ import streamlit as st
4
+ from typing import Dict, List, Optional, Any
5
+ from utils.database import (
6
+ get_all_documents,
7
+ get_collections,
8
+ get_collection_documents,
9
+ create_collection,
10
+ add_document_to_collection,
11
+ remove_from_collection
12
+ )
13
+
14
+ def display_documents_tab():
15
+ """Display the documents management interface."""
16
+ col1, col2 = st.columns([1, 2])
17
+
18
+ with col1:
19
+ st.header("Collections")
20
+
21
+ # Create new collection
22
+ with st.expander("Create New Collection", expanded=False):
23
+ col_name = st.text_input("Collection Name")
24
+ col_desc = st.text_area("Description")
25
+ if st.button("Create Collection"):
26
+ if col_name:
27
+ if create_collection(st.session_state.db_conn, col_name, col_desc):
28
+ st.success(f"Collection '{col_name}' created!")
29
+ st.rerun()
30
+
31
+ # List all collections
32
+ collections = get_collections(st.session_state.db_conn)
33
+ if collections:
34
+ for col in collections:
35
+ if st.button(f"📁 {col['name']} ({col['doc_count']} documents)",
36
+ key=f"col_{col['id']}", use_container_width=True):
37
+ st.session_state.selected_collection = col['id']
38
+ st.rerun()
39
+
40
+ with col2:
41
+ st.header("Documents")
42
+
43
+ # Filter controls
44
+ col_filter, doc_search = st.columns([1, 2])
45
+ with col_filter:
46
+ filter_option = st.selectbox(
47
+ "View",
48
+ ["All Documents", "Uncategorized", "Selected Collection"]
49
+ )
50
+ with doc_search:
51
+ search_query = st.text_input("Search documents", placeholder="Enter search terms...")
52
+
53
+ # Get and display documents based on filter
54
+ if filter_option == "Selected Collection" and st.session_state.get('selected_collection'):
55
+ documents = get_collection_documents(st.session_state.db_conn, st.session_state.selected_collection)
56
+ else:
57
+ documents = get_all_documents(st.session_state.db_conn)
58
+ if filter_option == "Uncategorized":
59
+ documents = [doc for doc in documents if not doc['collections']]
60
+
61
+ # Display documents
62
+ for doc in documents:
63
+ with st.expander(f"📄 {doc['name']}", expanded=False):
64
+ # Document metadata
65
+ st.write(f"Uploaded: {doc['upload_date']}")
66
+ if doc.get('collections'):
67
+ st.write(f"Collections: {', '.join(doc['collections'])}")
68
+
69
+ # Document actions
70
+ col1, col2 = st.columns(2)
71
+ with col1:
72
+ # Collection management
73
+ if st.session_state.get('selected_collection'):
74
+ if st.session_state.selected_collection not in [c['id'] for c in doc.get('collections', [])]:
75
+ if st.button("Add to Collection", key=f"add_{doc['id']}"):
76
+ add_document_to_collection(
77
+ st.session_state.db_conn,
78
+ doc['id'],
79
+ st.session_state.selected_collection
80
+ )
81
+ st.rerun()
82
+ else:
83
+ if st.button("Remove from Collection", key=f"remove_{doc['id']}"):
84
+ remove_from_collection(
85
+ st.session_state.db_conn,
86
+ doc['id'],
87
+ st.session_state.selected_collection
88
+ )
89
+ st.rerun()
90
+
91
+ with col2:
92
+ # Preview/Chat actions
93
+ if st.button("Use for Chat", key=f"chat_{doc['id']}"):
94
+ st.session_state.current_document = doc['id']
95
+ st.session_state.chat_ready = True
96
+ st.rerun()