eliujl commited on
Commit
7ee9dd1
1 Parent(s): 310ca3f

Updated app UI

Browse files

Add a user Submit button, change conversions to markdown and in reverse order, and display all chat records in textarea

Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -90,6 +90,7 @@ def ingest(_all_texts, use_pinecone, _embeddings, pinecone_index_name, chroma_co
90
  else:
91
  docsearch = Chroma.from_documents(
92
  _all_texts, _embeddings, collection_name=chroma_collection_name, persist_directory=persist_directory)
 
93
  return docsearch
94
 
95
 
@@ -121,8 +122,9 @@ def setup_docsearch(use_pinecone, pinecone_index_name, embeddings, chroma_collec
121
  else:
122
  docsearch = Chroma(persist_directory=persist_directory, embedding_function=embeddings,
123
  collection_name=chroma_collection_name)
124
- n_texts = docsearch._client._count(
125
- collection_name=chroma_collection_name)
 
126
  return docsearch, n_texts
127
 
128
 
@@ -161,6 +163,9 @@ pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready,
161
  def main(pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready, directory_name):
162
  docsearch_ready = False
163
  chat_history = []
 
 
 
164
  # Get user input of whether to use Pinecone or not
165
  col1, col2, col3 = st.columns([1, 1, 1])
166
  # create the radio buttons and text input fields
@@ -218,14 +223,19 @@ def main(pinecone_index_name, chroma_collection_name, persist_directory, docsear
218
  CRqa = ConversationalRetrievalChain.from_llm(
219
  llm, retriever=retriever, return_source_documents=True)
220
 
221
- st.title('Chatbot')
222
  # Get user input
223
  query = st.text_area('Enter your question:', height=10,
224
- placeholder='Summarize the context.')
225
- if query:
 
 
 
 
 
 
 
226
  # Generate a reply based on the user input and chat history
227
- CHAT_HISTORY_FILENAME = f"chat_history/{session_name}_chat_hist.json"
228
- chat_history = load_chat_history(CHAT_HISTORY_FILENAME)
229
  chat_history = [(user, bot)
230
  for user, bot in chat_history]
231
  reply, source = get_response(query, chat_history, CRqa)
@@ -233,10 +243,17 @@ def main(pinecone_index_name, chroma_collection_name, persist_directory, docsear
233
  chat_history.append(('User', query))
234
  chat_history.append(('Bot', reply))
235
  save_chat_history(chat_history, CHAT_HISTORY_FILENAME)
236
- latest_chats = chat_history[-4:]
237
- chat_history_str = '\n'.join(
238
- [f'{x[0]}: {x[1]}' for x in latest_chats])
239
- st.text_area('Chat record:', value=chat_history_str, height=250)
 
 
 
 
 
 
 
240
  # Display sources
241
  for i, source_i in enumerate(source):
242
  if i < k_sources:
@@ -246,13 +263,15 @@ def main(pinecone_index_name, chroma_collection_name, persist_directory, docsear
246
  page_content = source_i.page_content
247
  if source_i.metadata:
248
  metadata_source = source_i.metadata['source']
249
- st.write(
250
- f"**_Source {i+1}:_** {metadata_source}: {page_content}")
251
- st.write(source_i.metadata)
252
  else:
253
- st.write(f"**_Source {i+1}:_** {page_content}")
254
-
255
 
 
 
 
 
 
256
  if __name__ == '__main__':
257
  main(pinecone_index_name, chroma_collection_name, persist_directory,
258
  docsearch_ready, directory_name)
 
90
  else:
91
  docsearch = Chroma.from_documents(
92
  _all_texts, _embeddings, collection_name=chroma_collection_name, persist_directory=persist_directory)
93
+
94
  return docsearch
95
 
96
 
 
122
  else:
123
  docsearch = Chroma(persist_directory=persist_directory, embedding_function=embeddings,
124
  collection_name=chroma_collection_name)
125
+
126
+ n_texts = docsearch._collection.count()
127
+
128
  return docsearch, n_texts
129
 
130
 
 
163
  def main(pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready, directory_name):
164
  docsearch_ready = False
165
  chat_history = []
166
+ latest_chats = []
167
+ reply = ''
168
+ source = ''
169
  # Get user input of whether to use Pinecone or not
170
  col1, col2, col3 = st.columns([1, 1, 1])
171
  # create the radio buttons and text input fields
 
223
  CRqa = ConversationalRetrievalChain.from_llm(
224
  llm, retriever=retriever, return_source_documents=True)
225
 
226
+ st.title(':blue[Chatbot]')
227
  # Get user input
228
  query = st.text_area('Enter your question:', height=10,
229
+ placeholder='''Summarize the context.
230
+ \nAfter typing your question, click on SUBMIT to send it to the bot.''')
231
+ submitted = st.button('SUBMIT')
232
+
233
+ CHAT_HISTORY_FILENAME = f"chat_history/{session_name}_chat_hist.json"
234
+ chat_history = load_chat_history(CHAT_HISTORY_FILENAME)
235
+ st.markdown('<style>.my_title { font-weight: bold; color: red; }</style>', unsafe_allow_html=True)
236
+
237
+ if query and submitted:
238
  # Generate a reply based on the user input and chat history
 
 
239
  chat_history = [(user, bot)
240
  for user, bot in chat_history]
241
  reply, source = get_response(query, chat_history, CRqa)
 
243
  chat_history.append(('User', query))
244
  chat_history.append(('Bot', reply))
245
  save_chat_history(chat_history, CHAT_HISTORY_FILENAME)
246
+ c = chat_history[-4:]
247
+ if len(chat_history) >= 4:
248
+ latest_chats = [c[2],c[3],c[0],c[1]]
249
+ else:
250
+ latest_chats = c
251
+
252
+ if latest_chats:
253
+ chat_history_str1 = '<br>'.join([f'<span class=\"my_title\">{x[0]}:</span> {x[1]}' for x in latest_chats])
254
+ st.markdown(f'<div class=\"chat-record\">{chat_history_str1}</div>', unsafe_allow_html=True)
255
+
256
+ if reply and source:
257
  # Display sources
258
  for i, source_i in enumerate(source):
259
  if i < k_sources:
 
263
  page_content = source_i.page_content
264
  if source_i.metadata:
265
  metadata_source = source_i.metadata['source']
266
+ st.markdown(f"<h3 class='my_title'>Source {i+1}: {metadata_source}</h3> <br> {page_content}", unsafe_allow_html=True)
 
 
267
  else:
268
+ st.markdown(f"<h3 class='my_title'>Source {i+1}: </h3> <br> {page_content}", unsafe_allow_html=True)
 
269
 
270
+ all_chats = chat_history
271
+ all_chat_history_str = '\n'.join(
272
+ [f'{x[0]}: {x[1]}' for x in all_chats])
273
+ st.title(':blue[All chat records]')
274
+ st.text_area('', value=all_chat_history_str, height=250, label_visibility='collapsed')
275
  if __name__ == '__main__':
276
  main(pinecone_index_name, chroma_collection_name, persist_directory,
277
  docsearch_ready, directory_name)