Spaces:
Sleeping
Sleeping
Commit ·
82732e7
1
Parent(s): 7665c32
removed unused session state & fixed bug
Browse files- pages/ragChat.py +48 -49
pages/ragChat.py
CHANGED
|
@@ -14,10 +14,8 @@ async def add_data():
|
|
| 14 |
"If you plan to add more files, after processing initial files, make sure the uploaded files you already processed are removed"
|
| 15 |
)
|
| 16 |
url = st.text_input("Enter a website link")
|
| 17 |
-
|
| 18 |
-
st.session_state.button_pressed = False
|
| 19 |
if st.button("Process URL and Files"):
|
| 20 |
-
st.session_state.button_pressed = True
|
| 21 |
with st.spinner("Vectorizing Data, wait times vary depending on size..."):
|
| 22 |
if url:
|
| 23 |
try:
|
|
@@ -27,6 +25,7 @@ async def add_data():
|
|
| 27 |
)
|
| 28 |
except Exception as e:
|
| 29 |
st.error(f"Failed to load URL: {e}")
|
|
|
|
| 30 |
if uploaded_files:
|
| 31 |
try:
|
| 32 |
texts = get_pdf_text(uploaded_files)
|
|
@@ -39,67 +38,67 @@ async def add_data():
|
|
| 39 |
st.error("PDF has no meta data text")
|
| 40 |
except Exception as e:
|
| 41 |
st.error(f"Failed to load PDF: {e}")
|
|
|
|
| 42 |
st.success("Data is ready to be queried!")
|
| 43 |
-
|
| 44 |
|
| 45 |
|
| 46 |
async def rag_chat():
|
| 47 |
-
if st.session_state
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
]
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
with st.chat_message("Human"):
|
| 60 |
-
st.write(message.content)
|
| 61 |
-
|
| 62 |
-
user_query = st.chat_input("Type your message here...", key="chat_input")
|
| 63 |
-
if user_query:
|
| 64 |
-
st.session_state.chat_history.append(HumanMessage(content=user_query))
|
| 65 |
with st.chat_message("Human"):
|
| 66 |
-
st.write(
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
async def main():
|
| 96 |
-
st.session_state.data_hungry
|
| 97 |
-
|
| 98 |
-
if(st.session_state.data_hungry):
|
| 99 |
await add_data()
|
| 100 |
else:
|
| 101 |
await rag_chat()
|
| 102 |
|
|
|
|
| 103 |
if __name__ == "__main__":
|
|
|
|
| 104 |
sidebar()
|
| 105 |
asyncio.run(main())
|
|
|
|
| 14 |
"If you plan to add more files, after processing initial files, make sure the uploaded files you already processed are removed"
|
| 15 |
)
|
| 16 |
url = st.text_input("Enter a website link")
|
| 17 |
+
|
|
|
|
| 18 |
if st.button("Process URL and Files"):
|
|
|
|
| 19 |
with st.spinner("Vectorizing Data, wait times vary depending on size..."):
|
| 20 |
if url:
|
| 21 |
try:
|
|
|
|
| 25 |
)
|
| 26 |
except Exception as e:
|
| 27 |
st.error(f"Failed to load URL: {e}")
|
| 28 |
+
|
| 29 |
if uploaded_files:
|
| 30 |
try:
|
| 31 |
texts = get_pdf_text(uploaded_files)
|
|
|
|
| 38 |
st.error("PDF has no meta data text")
|
| 39 |
except Exception as e:
|
| 40 |
st.error(f"Failed to load PDF: {e}")
|
| 41 |
+
|
| 42 |
st.success("Data is ready to be queried!")
|
| 43 |
+
st.session_state.data_hungry = False
|
| 44 |
|
| 45 |
|
| 46 |
async def rag_chat():
|
| 47 |
+
if "chat_history" not in st.session_state:
|
| 48 |
+
st.session_state.chat_history = [
|
| 49 |
+
AIMessage(content="Hello, I am a bot. How can I help you?")
|
| 50 |
+
]
|
|
|
|
| 51 |
|
| 52 |
+
st.title("RAG CHAT")
|
| 53 |
+
for message in st.session_state.chat_history:
|
| 54 |
+
if isinstance(message, AIMessage):
|
| 55 |
+
with st.chat_message("AI"):
|
| 56 |
+
st.write(message.content)
|
| 57 |
+
elif isinstance(message, HumanMessage):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
with st.chat_message("Human"):
|
| 59 |
+
st.write(message.content)
|
| 60 |
|
| 61 |
+
user_query = st.chat_input("Type your message here...", key="chat_input")
|
| 62 |
+
if user_query:
|
| 63 |
+
st.session_state.chat_history.append(HumanMessage(content=user_query))
|
| 64 |
+
with st.chat_message("Human"):
|
| 65 |
+
st.write(user_query)
|
| 66 |
+
|
| 67 |
+
if "retriever" in st.session_state:
|
| 68 |
+
try:
|
| 69 |
+
ragAnswer = (
|
| 70 |
+
await st.session_state.retriever.amax_marginal_relevance_search(
|
| 71 |
+
user_query, k=4, fetch_k=10
|
| 72 |
)
|
| 73 |
+
)
|
| 74 |
+
context = []
|
| 75 |
+
for i, doc in enumerate(ragAnswer):
|
| 76 |
+
print(f"{i}: {doc.page_content}")
|
| 77 |
+
context.append(doc.page_content)
|
| 78 |
+
with st.spinner("Generating Response"):
|
| 79 |
+
response = get_response(
|
| 80 |
+
user_query, st.session_state.chat_history, context
|
| 81 |
+
)
|
| 82 |
+
if response:
|
| 83 |
+
st.session_state.chat_history.append(
|
| 84 |
+
AIMessage(content=response)
|
| 85 |
+
)
|
| 86 |
+
with st.chat_message("AI"):
|
| 87 |
+
st.write(response)
|
| 88 |
+
else:
|
| 89 |
+
st.write("No response received.")
|
| 90 |
+
except Exception as e:
|
| 91 |
+
st.error(f"Error during retrieval or response generation: {e}")
|
| 92 |
|
| 93 |
|
| 94 |
async def main():
|
| 95 |
+
if st.session_state.data_hungry:
|
|
|
|
|
|
|
| 96 |
await add_data()
|
| 97 |
else:
|
| 98 |
await rag_chat()
|
| 99 |
|
| 100 |
+
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
st.session_state.data_hungry = st.toggle("Add Custom Data", False)
|
| 103 |
sidebar()
|
| 104 |
asyncio.run(main())
|