File size: 2,735 Bytes
ada8cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
from langchain.document_loaders import DirectoryLoader, TextLoader, PyPDFLoader
from langchain.text_splitter import CharacterTextSplitter
from app import vectorstore


st.title("Document Management")

# File uploader
uploaded_file = st.file_uploader("Choose a file", type=['txt', 'pdf', 'docx'])

if uploaded_file is not None:
    # Create a temporary directory to store the uploaded file
    temp_dir = "temp_uploads"
    os.makedirs(temp_dir, exist_ok=True)
    file_path = os.path.join(temp_dir, uploaded_file.name)
    
    # Save the uploaded file temporarily
    with open(file_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
    
    st.success(f"File {uploaded_file.name} successfully uploaded!")

    # Process the uploaded file
    if st.button("Process Document"):
        with st.spinner("Processing document..."):
            try:
                # Load the document based on file type
                if uploaded_file.type == "application/pdf":
                    loader = PyPDFLoader(file_path)
                elif uploaded_file.type == "text/plain":
                    loader = TextLoader(file_path)
                else:
                    st.error("Unsupported file type.")
                    st.stop()
                
                documents = loader.load()
                
                # Split the document into chunks
                text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
                texts = text_splitter.split_documents(documents)
                
                # Add the chunks to the vectorstore
                vectorstore.add_documents(texts)
                
                st.success(f"Document processed and added to the knowledge base!")
            except Exception as e:
                st.error(f"An error occurred: {e}")
        
        # Clean up: remove the temporary file
        os.remove(file_path)

# Display current documents in the knowledge base
# st.subheader("Current Documents in Knowledge Base")
# # This is a placeholder. You'll need to implement a method to retrieve and display
# # the list of documents currently in your Chroma database.
# st.write("Placeholder for document list")

# # Option to clear the entire knowledge base
# if st.button("Clear Knowledge Base"):
#     if st.sidebar.checkbox("Are you sure you want to clear the entire knowledge base? This action cannot be undone."):
#         try:
#             # Clear the Chroma database
#             vectorstore.delete()
#             st.success("Knowledge base cleared!")
#         except Exception as e:
#             st.error(f"An error occurred: {e}")