IshmamF commited on
Commit
090ed3a
·
1 Parent(s): 6fb0b9d

minor tweaks

Browse files
Files changed (1) hide show
  1. pages/ragChat.py +38 -33
pages/ragChat.py CHANGED
@@ -11,9 +11,13 @@ async def main():
11
  st.title("Upload Data")
12
 
13
  uploaded_files = st.file_uploader("Upload PDFs", accept_multiple_files=True)
 
14
  url = st.text_input("Enter a website link")
 
 
15
  if st.button('Process URL and Files'):
16
- with st.spinner("Vectorizing Data, wait times vary depending size..."):
 
17
  if url:
18
  try:
19
  if "retriever" not in st.session_state:
@@ -33,42 +37,43 @@ async def main():
33
  except Exception as e:
34
  st.error(f"Failed to load PDF: {e}")
35
  st.success("Data is ready to be queried!")
 
 
 
 
36
 
37
- if "chat_history" not in st.session_state:
38
- st.session_state.chat_history = [AIMessage(content="Hello, I am a bot. How can I help you?")]
 
 
 
 
 
 
39
 
40
- st.title("RAG CHAT")
41
- for message in st.session_state.chat_history:
42
- if isinstance(message, AIMessage):
43
- with st.chat_message("AI"):
44
- st.write(message.content)
45
- elif isinstance(message, HumanMessage):
46
  with st.chat_message("Human"):
47
- st.write(message.content)
48
 
49
- user_query = st.chat_input("Type your message here...", key="chat_input")
50
- if user_query:
51
- st.session_state.chat_history.append(HumanMessage(content=user_query))
52
- with st.chat_message("Human"):
53
- st.write(user_query)
54
-
55
- if 'retriever' in st.session_state:
56
- try:
57
- ragAnswer = await st.session_state.retriever.amax_marginal_relevance_search(user_query, k=2, fetch_k=10)
58
- context = []
59
- for i, doc in enumerate(ragAnswer):
60
- print(f"{i}: {doc.page_content}")
61
- context.append(doc.page_content)
62
- with st.spinner("Generating Response"):
63
- response = get_response(user_query, st.session_state.chat_history, context)
64
- if response:
65
- st.session_state.chat_history.append(AIMessage(content=response))
66
- with st.chat_message("AI"):
67
- st.write(response)
68
- else:
69
- st.write("No response received.")
70
- except Exception as e:
71
- st.error(f"Error during retrieval or response generation: {e}")
72
 
73
  if __name__ == "__main__":
74
  asyncio.run(main())
 
11
  st.title("Upload Data")
12
 
13
  uploaded_files = st.file_uploader("Upload PDFs", accept_multiple_files=True)
14
+ st.warning("If you plan to add more files, after processing initial files, make sure the uploaded files you already processed are removed")
15
  url = st.text_input("Enter a website link")
16
+ if "button_pressed" not in st.session_state:
17
+ st.session_state.button_pressed = False
18
  if st.button('Process URL and Files'):
19
+ st.session_state.button_pressed = True
20
+ with st.spinner("Vectorizing Data, wait times vary depending on size..."):
21
  if url:
22
  try:
23
  if "retriever" not in st.session_state:
 
37
  except Exception as e:
38
  st.error(f"Failed to load PDF: {e}")
39
  st.success("Data is ready to be queried!")
40
+
41
+ if st.session_state.button_pressed:
42
+ if "chat_history" not in st.session_state:
43
+ st.session_state.chat_history = [AIMessage(content="Hello, I am a bot. How can I help you?")]
44
 
45
+ st.title("RAG CHAT")
46
+ for message in st.session_state.chat_history:
47
+ if isinstance(message, AIMessage):
48
+ with st.chat_message("AI"):
49
+ st.write(message.content)
50
+ elif isinstance(message, HumanMessage):
51
+ with st.chat_message("Human"):
52
+ st.write(message.content)
53
 
54
+ user_query = st.chat_input("Type your message here...", key="chat_input")
55
+ if user_query:
56
+ st.session_state.chat_history.append(HumanMessage(content=user_query))
 
 
 
57
  with st.chat_message("Human"):
58
+ st.write(user_query)
59
 
60
+ if 'retriever' in st.session_state:
61
+ try:
62
+ ragAnswer = await st.session_state.retriever.amax_marginal_relevance_search(user_query, k=4, fetch_k=10)
63
+ context = []
64
+ for i, doc in enumerate(ragAnswer):
65
+ print(f"{i}: {doc.page_content}")
66
+ context.append(doc.page_content)
67
+ with st.spinner("Generating Response"):
68
+ response = get_response(user_query, st.session_state.chat_history, context)
69
+ if response:
70
+ st.session_state.chat_history.append(AIMessage(content=response))
71
+ with st.chat_message("AI"):
72
+ st.write(response)
73
+ else:
74
+ st.write("No response received.")
75
+ except Exception as e:
76
+ st.error(f"Error during retrieval or response generation: {e}")
 
 
 
 
 
 
77
 
78
  if __name__ == "__main__":
79
  asyncio.run(main())