annas4421 commited on
Commit
a52e98e
Β·
verified Β·
1 Parent(s): 5b6e5d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -5
app.py CHANGED
@@ -64,13 +64,15 @@ def get_document_text(uploaded_files):
64
 
65
  # Split text into chunks
66
  def get_chunks(documents):
67
- text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
68
- return [chunk for doc in documents for chunk in text_splitter.split_text(doc.page_content)]
69
-
70
  # Create vectorstore
 
71
  def get_vectorstore(chunks):
72
  embeddings = OpenAIEmbeddings()
73
- return FAISS.from_texts(texts=chunks, embedding=embeddings)
 
74
 
75
  # Create a conversational chain
76
  def get_conversationchain(vectorstore):
@@ -78,7 +80,7 @@ def get_conversationchain(vectorstore):
78
  memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
79
  conversation_chain = ConversationalRetrievalChain.from_llm(
80
  llm=llm,
81
- retriever=vectorstore.as_retriever(search_type="similarity",search_kwargs={"k": 20}),
82
  condense_question_prompt=CUSTOM_QUESTION_PROMPT,
83
  memory=memory,
84
  combine_docs_chain_kwargs={'prompt': prompt}
@@ -100,6 +102,25 @@ def handle_question(question):
100
  else:
101
  st.markdown(f"**Bot:** {msg.content}")
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  # Main Streamlit app
104
  def main():
105
  st.set_page_config(page_title="Chat with Documents", page_icon="πŸ“š")
 
64
 
65
  # Split text into chunks
66
  def get_chunks(documents):
67
+ text_splitter = CharacterTextSplitter(separator="\n", chunk_size=600, chunk_overlap=200, length_function=len)
68
+ chunks = [chunk for doc in documents for chunk in text_splitter.split_text(doc.page_content)]
69
+ return chunks
70
  # Create vectorstore
71
+
72
  def get_vectorstore(chunks):
73
  embeddings = OpenAIEmbeddings()
74
+ vectorstore = FAISS.from_texts(texts=chunks, embedding=embeddings)
75
+ return vectorstore
76
 
77
  # Create a conversational chain
78
  def get_conversationchain(vectorstore):
 
80
  memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
81
  conversation_chain = ConversationalRetrievalChain.from_llm(
82
  llm=llm,
83
+ retriever=vectorstore.as_retriever(search_type="similarity",search_kwargs={"k": 10}),
84
  condense_question_prompt=CUSTOM_QUESTION_PROMPT,
85
  memory=memory,
86
  combine_docs_chain_kwargs={'prompt': prompt}
 
102
  else:
103
  st.markdown(f"**Bot:** {msg.content}")
104
 
105
+ def handle_question(question):
106
+ if not st.session_state.conversation:
107
+ st.warning("Please process your documents first.")
108
+ return
109
+
110
+ # Get the response from the conversation chain
111
+ response = st.session_state.conversation({'question': question})
112
+
113
+ # Update chat history
114
+ st.session_state.chat_history = response['chat_history']
115
+
116
+ # Display chat history
117
+ for i, msg in enumerate(st.session_state.chat_history):
118
+ if i % 2 == 0:
119
+ st.markdown(f"**You:** {msg.content}")
120
+ else:
121
+ st.markdown(f"**Bot:** {msg.content}")
122
+
123
+
124
  # Main Streamlit app
125
  def main():
126
  st.set_page_config(page_title="Chat with Documents", page_icon="πŸ“š")