zakerytclarke commited on
Commit
51db61d
·
verified ·
1 Parent(s): 3939908

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -30
app.py CHANGED
@@ -276,7 +276,15 @@ def query_teapot(prompt, context, user_input, teapot_ai):
276
  return response
277
 
278
  @log_time
279
- def handle_chat(user_input, teapot_ai):
 
 
 
 
 
 
 
 
280
  results = brave_search(user_input)
281
 
282
  documents = [desc.replace('<strong>','').replace('</strong>','') for _, desc, _ in results]
@@ -299,28 +307,15 @@ def suggestion_button(suggestion_text, teapot_ai):
299
  if st.button(suggestion_text):
300
  handle_chat(suggestion_text, teapot_ai)
301
 
302
- @log_time
303
- def hash_documents(documents):
304
- return hashlib.sha256("\n".join(documents).encode("utf-8")).hexdigest()
305
 
306
  def main():
307
  st.set_page_config(page_title="TeapotAI Chat", page_icon=":robot_face:", layout="wide")
308
 
309
  st.sidebar.header("Retrieval Augmented Generation")
310
- user_documents = st.sidebar.text_area("Enter documents, each on a new line", value="\n".join(default_documents))
311
-
312
- documents = [doc.strip() for doc in user_documents.split("\n") if doc.strip()]
313
- new_documents_hash = hash_documents(documents)
314
 
315
- if "documents_hash" not in st.session_state or st.session_state.documents_hash != new_documents_hash:
316
- with st.spinner('Loading Model and Embeddings...'):
317
- start_time = time.time()
318
- teapot_ai = TeapotAI(documents=documents or default_documents, settings=TeapotAISettings(rag_num_results=3,log_level="debug"))
319
- end_time = time.time()
320
- print(f"Model loaded in {end_time - start_time:.4f} seconds")
321
-
322
- st.session_state.documents_hash = new_documents_hash
323
- st.session_state.teapot_ai = teapot_ai
324
  else:
325
  teapot_ai = st.session_state.teapot_ai
326
 
@@ -335,25 +330,16 @@ def main():
335
 
336
  s1, s2, s3 = st.columns([1, 2, 3])
337
  with s1:
338
- suggestion_button("Tell me about TeapotLLM.", teapot_ai)
339
  with s2:
340
- suggestion_button("Who invented quantum mechanics", teapot_ai)
341
  with s3:
342
  suggestion_button("Extract the year Google was founded", teapot_ai)
343
 
344
  if user_input:
345
- with st.chat_message("user"):
346
- st.markdown(user_input)
347
-
348
- st.session_state.messages.append({"role": "user", "content": user_input})
349
  with st.spinner('Generating Response...'):
350
- response = handle_chat(user_input, teapot_ai)
351
-
352
- with st.chat_message("assistant"):
353
- st.markdown(response)
354
-
355
- st.session_state.messages.append({"role": "assistant", "content": response})
356
- st.markdown("### Suggested Questions")
357
 
358
  if __name__ == "__main__":
359
  main()
 
276
  return response
277
 
278
  @log_time
279
+ def handle_chat(prompt, user_input, teapot_ai):
280
+ with st.chat_message("user"):
281
+ st.markdown(user_input)
282
+ st.session_state.messages.append({"role": "user", "content": user_input})
283
+
284
+ with st.chat_message("assistant"):
285
+ st.markdown(response)
286
+ st.session_state.messages.append({"role": "assistant", "content": response})
287
+
288
  results = brave_search(user_input)
289
 
290
  documents = [desc.replace('<strong>','').replace('</strong>','') for _, desc, _ in results]
 
307
  if st.button(suggestion_text):
308
  handle_chat(suggestion_text, teapot_ai)
309
 
 
 
 
310
 
311
  def main():
312
  st.set_page_config(page_title="TeapotAI Chat", page_icon=":robot_face:", layout="wide")
313
 
314
  st.sidebar.header("Retrieval Augmented Generation")
315
+ user_prompt = st.sidebar.text_area("Enter prompt, leave empty for search", value="\n".join(default_documents))
 
 
 
316
 
317
+ teapot_ai = TeapotAI(documents=[], settings=TeapotAISettings(rag_num_results=3, log_level="debug"))
318
+
 
 
 
 
 
 
 
319
  else:
320
  teapot_ai = st.session_state.teapot_ai
321
 
 
330
 
331
  s1, s2, s3 = st.columns([1, 2, 3])
332
  with s1:
333
+ suggestion_button("Tell me about teapotllm", teapot_ai)
334
  with s2:
335
+ suggestion_button("Who invented quantum mechanics?", teapot_ai)
336
  with s3:
337
  suggestion_button("Extract the year Google was founded", teapot_ai)
338
 
339
  if user_input:
 
 
 
 
340
  with st.spinner('Generating Response...'):
341
+ response = handle_chat(user_prompt, user_input, teapot_ai)
342
+
 
 
 
 
 
343
 
344
  if __name__ == "__main__":
345
  main()