Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -113,60 +113,60 @@ def main():
|
|
113 |
os.unlink(f.name)
|
114 |
os.path.exists(f.name)
|
115 |
if len(data) > 0 :
|
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 |
if __name__ == '__main__':
|
|
|
113 |
os.unlink(f.name)
|
114 |
os.path.exists(f.name)
|
115 |
if len(data) > 0 :
|
116 |
+
embeddings = load_embeddings()
|
117 |
+
sp_docs = split_docs(documents = data)
|
118 |
+
st.write(f"This document have {len(sp_docs)} chunks")
|
119 |
+
sp_docs_list.extend(sp_docs)
|
120 |
+
try:
|
121 |
+
db = FAISS.from_documents(sp_docs_list, embeddings)
|
122 |
+
memory = ConversationBufferMemory(memory_key="chat_history",
|
123 |
+
return_messages=True,
|
124 |
+
input_key="query",
|
125 |
+
output_key="result")
|
126 |
+
qa_chain = RetrievalQA.from_chain_type(
|
127 |
+
llm = llm,
|
128 |
+
chain_type = "stuff",
|
129 |
+
retriever = db.as_retriever(search_kwargs = {'k':3}),
|
130 |
+
return_source_documents = True,
|
131 |
+
memory = memory,
|
132 |
+
chain_type_kwargs = {"prompt":qa_prompt})
|
133 |
+
for message in st.session_state.messages:
|
134 |
+
with st.chat_message(message["role"]):
|
135 |
+
st.markdown(message["content"])
|
136 |
+
|
137 |
+
# Accept user input
|
138 |
+
if query := st.chat_input("What is up?"):
|
139 |
+
# Display user message in chat message container
|
140 |
+
with st.chat_message("user"):
|
141 |
+
st.markdown(query)
|
142 |
+
# Add user message to chat history
|
143 |
+
st.session_state.messages.append({"role": "user", "content": query})
|
144 |
+
|
145 |
+
start = time.time()
|
146 |
+
|
147 |
+
response = qa_chain({'query': query})
|
148 |
+
|
149 |
+
with st.chat_message("assistant"):
|
150 |
+
st.markdown(response['result'])
|
151 |
+
|
152 |
+
end = time.time()
|
153 |
+
st.write("Respone time:",int(end-start),"sec")
|
154 |
+
print(response)
|
155 |
+
|
156 |
+
# Add assistant response to chat history
|
157 |
+
st.session_state.messages.append({"role": "assistant", "content": response['result']})
|
158 |
+
|
159 |
+
with st.expander("See the related documents"):
|
160 |
+
for count, url in enumerate(response['source_documents']):
|
161 |
+
st.write(str(count+1)+":", url)
|
162 |
+
|
163 |
+
clear_button = st.button("Start new convo")
|
164 |
+
if clear_button :
|
165 |
+
st.session_state.messages = []
|
166 |
+
qa_chain.memory.chat_memory.clear()
|
167 |
+
|
168 |
+
except:
|
169 |
+
st.write("Plaese upload your pdf file.")
|
170 |
|
171 |
|
172 |
if __name__ == '__main__':
|