Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -37,7 +37,7 @@ if "OPENAI_API_KEY" in os.environ:
|
|
37 |
loader2 = PyPDFLoader("Tbank resources.pdf")
|
38 |
data1 = loader1.load()
|
39 |
data2 = loader2.load()
|
40 |
-
data= data1 + data2
|
41 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
42 |
all_splits = text_splitter.split_documents(data)
|
43 |
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
|
@@ -80,14 +80,17 @@ if "OPENAI_API_KEY" in os.environ:
|
|
80 |
]
|
81 |
)
|
82 |
|
83 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
84 |
document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
|
85 |
|
86 |
-
return retriever, document_chain
|
87 |
|
88 |
# Load components
|
89 |
with st.spinner("Initializing Tbank Assistant..."):
|
90 |
-
retriever, document_chain
|
|
|
|
|
|
|
|
|
91 |
|
92 |
# Chat interface
|
93 |
st.subheader("Chat with Tbank Assistant")
|
@@ -118,7 +121,7 @@ if "OPENAI_API_KEY" in os.environ:
|
|
118 |
response = document_chain.invoke(
|
119 |
{
|
120 |
"context": docs,
|
121 |
-
"chat_history": memory.load_memory_variables({})["chat_history"],
|
122 |
"messages": [
|
123 |
HumanMessage(content=prompt)
|
124 |
],
|
@@ -133,7 +136,7 @@ if "OPENAI_API_KEY" in os.environ:
|
|
133 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
134 |
|
135 |
# Update memory
|
136 |
-
memory.save_context({"input": prompt}, {"output": full_response})
|
137 |
|
138 |
else:
|
139 |
st.warning("Please enter your OpenAI API Key in the sidebar to start the chatbot.")
|
|
|
37 |
loader2 = PyPDFLoader("Tbank resources.pdf")
|
38 |
data1 = loader1.load()
|
39 |
data2 = loader2.load()
|
40 |
+
data = data1 + data2
|
41 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
42 |
all_splits = text_splitter.split_documents(data)
|
43 |
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
|
|
|
80 |
]
|
81 |
)
|
82 |
|
|
|
83 |
document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
|
84 |
|
85 |
+
return retriever, document_chain
|
86 |
|
87 |
# Load components
|
88 |
with st.spinner("Initializing Tbank Assistant..."):
|
89 |
+
retriever, document_chain = initialize_components()
|
90 |
+
|
91 |
+
# Initialize memory for each session
|
92 |
+
if "memory" not in st.session_state:
|
93 |
+
st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
94 |
|
95 |
# Chat interface
|
96 |
st.subheader("Chat with Tbank Assistant")
|
|
|
121 |
response = document_chain.invoke(
|
122 |
{
|
123 |
"context": docs,
|
124 |
+
"chat_history": st.session_state.memory.load_memory_variables({})["chat_history"],
|
125 |
"messages": [
|
126 |
HumanMessage(content=prompt)
|
127 |
],
|
|
|
136 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
137 |
|
138 |
# Update memory
|
139 |
+
st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
|
140 |
|
141 |
else:
|
142 |
st.warning("Please enter your OpenAI API Key in the sidebar to start the chatbot.")
|