Spaces:
Sleeping
Sleeping
minor tweaks
Browse files- 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 |
-
|
|
|
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 |
-
|
38 |
-
st.session_state.chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
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(
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
st.
|
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())
|