Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ from langchain.document_loaders import PyPDFLoader
|
|
| 16 |
st.set_page_config(page_title="Enterprise document search + chat", layout="wide")
|
| 17 |
|
| 18 |
# Streamlit app header
|
| 19 |
-
st.title("Enterprise document
|
| 20 |
|
| 21 |
# Sidebar for API Key input
|
| 22 |
with st.sidebar:
|
|
@@ -33,49 +33,48 @@ if "OPENAI_API_KEY" in os.environ:
|
|
| 33 |
dotenv.load_dotenv()
|
| 34 |
chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
|
| 35 |
|
|
|
|
| 36 |
# loader1 = PyPDFLoader("Tbank resources.pdf")
|
| 37 |
# loader2 = PyPDFLoader("International Banking Services.pdf")
|
| 38 |
# data1 = loader1.load()
|
| 39 |
# data2 = loader2.load()
|
| 40 |
# data = data1 + data2
|
| 41 |
st.header('Multiple File Upload')
|
| 42 |
-
uploaded_files = st.file_uploader('Upload your files',accept_multiple_files=True, type=['txt', 'pdf','csv','ppt','doc','xls','pptx','xlsx'])
|
| 43 |
if uploaded_files:
|
| 44 |
all_documents = []
|
| 45 |
for file in uploaded_files:
|
| 46 |
documents = load_document(file)
|
| 47 |
all_documents.extend(documents)
|
| 48 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 49 |
-
all_splits = text_splitter.split_documents(
|
| 50 |
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
|
| 51 |
vectorstore = Chroma.from_documents(documents=all_splits, embedding=embeddings)
|
| 52 |
retriever = vectorstore.as_retriever(k=4)
|
| 53 |
|
| 54 |
SYSTEM_TEMPLATE = """
|
| 55 |
-
You are
|
| 56 |
-
|
| 57 |
-
1.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
Your primary goal is to assist users with information directly related to Tbank, using only the website content and provided PDF documents. Avoid speculation and stick strictly to the provided information.
|
| 79 |
|
| 80 |
<context>
|
| 81 |
{context}
|
|
@@ -99,59 +98,61 @@ if "OPENAI_API_KEY" in os.environ:
|
|
| 99 |
document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
|
| 100 |
|
| 101 |
return retriever, document_chain
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
with st.spinner("Initializing Assistant..."):
|
| 105 |
retriever, document_chain = initialize_components()
|
| 106 |
|
| 107 |
-
|
| 108 |
if "memory" not in st.session_state:
|
| 109 |
st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
st.subheader("Chat
|
| 113 |
|
| 114 |
-
|
| 115 |
if "messages" not in st.session_state:
|
| 116 |
st.session_state.messages = []
|
| 117 |
|
| 118 |
-
|
| 119 |
for message in st.session_state.messages:
|
| 120 |
-
|
| 121 |
st.markdown(message["content"])
|
| 122 |
|
| 123 |
-
|
| 124 |
if prompt := st.chat_input("What would you like to know about Document?"):
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
full_response = response
|
| 149 |
message_placeholder.markdown(full_response)
|
| 150 |
-
|
| 151 |
-
|
| 152 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 153 |
-
|
| 154 |
-
|
| 155 |
st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
|
| 156 |
|
| 157 |
else:
|
|
|
|
| 16 |
st.set_page_config(page_title="Enterprise document search + chat", layout="wide")
|
| 17 |
|
| 18 |
# Streamlit app header
|
| 19 |
+
st.title("Enterprise document helpdesk")
|
| 20 |
|
| 21 |
# Sidebar for API Key input
|
| 22 |
with st.sidebar:
|
|
|
|
| 33 |
dotenv.load_dotenv()
|
| 34 |
chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
|
| 35 |
|
| 36 |
+
# #loader1 = WebBaseLoader("https://www.tbankltd.com/")
|
| 37 |
# loader1 = PyPDFLoader("Tbank resources.pdf")
|
| 38 |
# loader2 = PyPDFLoader("International Banking Services.pdf")
|
| 39 |
# data1 = loader1.load()
|
| 40 |
# data2 = loader2.load()
|
| 41 |
# data = data1 + data2
|
| 42 |
st.header('Multiple File Upload')
|
| 43 |
+
uploaded_files = st.file_uploader('Upload your files',accept_multiple_files=True, type=['txt', 'pdf', 'csv', 'ppt', 'doc', 'xls', 'pptx', 'xlsx'])
|
| 44 |
if uploaded_files:
|
| 45 |
all_documents = []
|
| 46 |
for file in uploaded_files:
|
| 47 |
documents = load_document(file)
|
| 48 |
all_documents.extend(documents)
|
| 49 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 50 |
+
all_splits = text_splitter.split_documents(data)
|
| 51 |
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
|
| 52 |
vectorstore = Chroma.from_documents(documents=all_splits, embedding=embeddings)
|
| 53 |
retriever = vectorstore.as_retriever(k=4)
|
| 54 |
|
| 55 |
SYSTEM_TEMPLATE = """
|
| 56 |
+
You are an advanced AI assistant designed for document search and chatbot functionality. Your primary functions are:
|
| 57 |
+
|
| 58 |
+
1. Process and structure multiple documents in various formats, including:
|
| 59 |
+
.txt, .pdf, .csv, .ppt, .doc, .xls, .pptx, and .xlsx
|
| 60 |
+
|
| 61 |
+
2. Extract and organize information from these unstructured documents into a coherent, searchable format.
|
| 62 |
+
|
| 63 |
+
3. Retrieve relevant information from the processed documents based on user queries.
|
| 64 |
+
|
| 65 |
+
4. Act as a chatbot, engaging in conversations about the content of the documents.
|
| 66 |
+
|
| 67 |
+
5. Provide accurate and contextual responses to user questions, drawing solely from the information contained within the processed documents.
|
| 68 |
+
|
| 69 |
+
6. If a user's question is not related to the content of the provided documents, politely inform them that you can only answer questions based on the information in the given documents.
|
| 70 |
+
|
| 71 |
+
7. When answering, cite the specific document or section where the information was found, if possible.
|
| 72 |
+
|
| 73 |
+
8. If there's ambiguity in a query, ask for clarification to ensure you provide the most relevant information.
|
| 74 |
+
|
| 75 |
+
9. Maintain confidentiality and do not share or discuss information from one user's documents with other users.
|
| 76 |
+
|
| 77 |
+
Remember, your knowledge is limited to the content of the documents you've been given to process. Do not provide information or answer questions that are outside the scope of these documents. Always strive for accuracy and relevance in your responses.
|
|
|
|
|
|
|
| 78 |
|
| 79 |
<context>
|
| 80 |
{context}
|
|
|
|
| 98 |
document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
|
| 99 |
|
| 100 |
return retriever, document_chain
|
| 101 |
+
else:
|
| 102 |
+
st.warning("Please Upload File to Continue")
|
| 103 |
|
| 104 |
+
# Load components
|
| 105 |
with st.spinner("Initializing Assistant..."):
|
| 106 |
retriever, document_chain = initialize_components()
|
| 107 |
|
| 108 |
+
# Initialize memory for each session
|
| 109 |
if "memory" not in st.session_state:
|
| 110 |
st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 111 |
|
| 112 |
+
# Chat interface
|
| 113 |
+
st.subheader("Chat with Tbank Assistant")
|
| 114 |
|
| 115 |
+
# Initialize chat history
|
| 116 |
if "messages" not in st.session_state:
|
| 117 |
st.session_state.messages = []
|
| 118 |
|
| 119 |
+
# Display chat messages from history on app rerun
|
| 120 |
for message in st.session_state.messages:
|
| 121 |
+
with st.chat_message(message["role"]):
|
| 122 |
st.markdown(message["content"])
|
| 123 |
|
| 124 |
+
# React to user input
|
| 125 |
if prompt := st.chat_input("What would you like to know about Document?"):
|
| 126 |
+
# Display user message in chat message container
|
| 127 |
+
st.chat_message("user").markdown(prompt)
|
| 128 |
+
# Add user message to chat history
|
| 129 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 130 |
+
|
| 131 |
+
with st.chat_message("assistant"):
|
| 132 |
+
message_placeholder = st.empty()
|
| 133 |
+
|
| 134 |
+
# Retrieve relevant documents
|
| 135 |
+
docs = retriever.get_relevant_documents(prompt)
|
| 136 |
+
|
| 137 |
+
# Generate response
|
| 138 |
+
response = document_chain.invoke(
|
| 139 |
+
{
|
| 140 |
+
"context": docs,
|
| 141 |
+
"chat_history": st.session_state.memory.load_memory_variables({})["chat_history"],
|
| 142 |
+
"messages": [
|
| 143 |
+
HumanMessage(content=prompt)
|
| 144 |
+
],
|
| 145 |
+
}
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# The response is already a string, so we can use it directly
|
| 149 |
full_response = response
|
| 150 |
message_placeholder.markdown(full_response)
|
| 151 |
+
|
| 152 |
+
# Add assistant response to chat history
|
| 153 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 154 |
+
|
| 155 |
+
# Update memory
|
| 156 |
st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
|
| 157 |
|
| 158 |
else:
|