File size: 6,779 Bytes
cf9bec7 676d600 1cb9bbc 676d600 54f139e 1cb9bbc a07e56d 1cb9bbc a07e56d 1cb9bbc a07e56d 1cb9bbc a07e56d 1cb9bbc cf9bec7 04d1254 cf9bec7 04d1254 cf9bec7 04d1254 cf9bec7 04d1254 cf9bec7 04d1254 |
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 |
# components/collection_manager.py
import streamlit as st # Import Streamlit here
from utils.database import (
get_all_documents,
create_collection,
add_document_to_collection,
get_collections,
search_documents,
get_collection_documents,
)
st.session_state.reinitialize_chat = True
def show_collection_creation_dialog():
"""Show dialog for creating a new collection."""
with st.form("create_collection"):
st.subheader("Create New Collection")
name = st.text_input("Collection Name", key="new_col_name")
description = st.text_area("Description", key="new_col_desc")
# Document selection
existing_docs = get_all_documents(st.session_state.db_conn)
if existing_docs:
st.subheader("Add Existing Documents")
selected_docs = st.multiselect(
"Select documents to add",
options=[doc['id'] for doc in existing_docs],
format_func=lambda x: next(doc['name'] for doc in existing_docs if doc['id'] == x)
)
submitted = st.form_submit_button("Create Collection")
if submitted and name:
collection_id = create_collection(st.session_state.db_conn, name, description)
if collection_id and selected_docs:
for doc_id in selected_docs:
add_document_to_collection(st.session_state.db_conn, doc_id, collection_id)
st.success(f"Collection '{name}' created!")
st.session_state.show_collection_dialog = False
st.rerun()
def enhanced_document_preview(doc):
"""Enhanced document preview with actions."""
col1, col2 = st.columns([3, 1])
with col1:
st.text_area(
"Preview",
value=doc['content'][:1000] + "..." if len(doc['content']) > 1000 else doc['content'],
height=200,
disabled=True
)
with col2:
st.button("ποΈ Remove", key=f"remove_{doc['id']}", use_container_width=True)
st.button("π Copy", key=f"copy_{doc['id']}", use_container_width=True)
st.button("π¬ Chat", key=f"chat_{doc['id']}", use_container_width=True)
def display_enhanced_collections():
"""Enhanced collections management interface."""
col1, col2 = st.columns([1, 2])
with col1:
st.subheader("Collections")
# Search collections
search_query = st.text_input("π Search collections", key="collection_search")
# Create new collection button
if st.button("+ New Collection", type="primary", use_container_width=True):
st.session_state.show_collection_dialog = True
# Collection list with stats
collections = get_collections(st.session_state.db_conn)
if search_query:
collections = [c for c in collections
if search_query.lower() in c['name'].lower()]
st.divider()
for collection in collections:
with st.container():
col_button = st.button(
f"π {collection['name']}",
key=f"col_{collection['id']}",
use_container_width=True
)
# Show collection stats
stats_cols = st.columns(3)
with stats_cols[0]:
st.caption(f"π {collection['doc_count']}")
with stats_cols[1]:
st.caption(f"π
{collection['created_at'][:10]}")
with stats_cols[2]:
st.caption("βοΈ Manage")
if col_button:
st.session_state.selected_collection = collection
st.rerun()
with col2:
if st.session_state.get('selected_collection'):
display_collection_content(st.session_state.selected_collection)
else:
st.info("Select a collection to view its contents")
def display_collection_content(collection):
"""Display enhanced collection contents."""
st.subheader(collection['name'])
# Collection actions
col1, col2, col3, col4 = st.columns(4)
with col1:
st.button("π₯ Add Documents", use_container_width=True)
with col2:
st.button("βοΈ Edit Collection", use_container_width=True)
with col3:
st.button("π€ Export", use_container_width=True)
with col4:
st.button("ποΈ Delete", use_container_width=True)
# Document search within collection
search_query = st.text_input("π Search documents in collection")
# Filters
with st.expander("Filters"):
filter_cols = st.columns(3)
with filter_cols[0]:
st.date_input("Date Range")
with filter_cols[1]:
st.selectbox("Sort By", ["Name", "Date", "Size"])
with filter_cols[2]:
st.selectbox("View", ["List", "Grid"])
# Document list with enhanced display
documents = get_collection_documents(st.session_state.db_conn, collection['id'])
if search_query:
documents = [doc for doc in documents
if search_query.lower() in doc['name'].lower() or
search_query.lower() in doc.get('content', '').lower()]
# Display documents in a clean, modern format
for doc in documents:
with st.container():
doc_cols = st.columns([0.1, 2, 0.5])
with doc_cols[0]:
st.checkbox("", key=f"select_{doc['id']}")
with doc_cols[1]:
st.markdown(f"**{doc['name']}**")
st.caption(f"Uploaded: {doc.get('upload_date', 'N/A')}")
with doc_cols[2]:
st.button("Preview", key=f"preview_{doc['id']}", use_container_width=True)
st.button("Remove", key=f"remove_{doc['id']}", use_container_width=True)
# Document preview expander
if st.session_state.get(f"preview_{doc['id']}", False):
with st.expander("Document Preview", expanded=True):
st.text_area(
"Content",
value=doc['content'][:1000] + "..." if len(doc['content']) > 1000 else doc['content'],
height=200,
disabled=True
)
def show_document_preview(document):
"""Show document preview in a modal."""
with st.expander(f"Preview: {document['name']}", expanded=True):
st.text_area(
"Content",
value=document['content'][:1000] + "..." if len(document['content']) > 1000 else document['content'],
height=300,
disabled=True
) |