Spaces:
Sleeping
Sleeping
Delete pages/chat_rag.py
Browse files- pages/chat_rag.py +0 -76
pages/chat_rag.py
DELETED
@@ -1,76 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from langchain_core.messages import AIMessage, HumanMessage
|
3 |
-
from functions.gptResponse import get_response
|
4 |
-
from functions.sidebar import sidebar
|
5 |
-
from functions.web_chain import vectorize, loadPdfData, loadUrlData
|
6 |
-
import asyncio
|
7 |
-
|
8 |
-
async def main():
|
9 |
-
sidebar()
|
10 |
-
st.title("Upload Data")
|
11 |
-
|
12 |
-
uploaded_files = st.file_uploader("Upload PDFs", accept_multiple_files=True)
|
13 |
-
url = st.text_input("Enter a website link")
|
14 |
-
if st.button('Process URL and Files'):
|
15 |
-
if url:
|
16 |
-
try:
|
17 |
-
urlData = loadUrlData(url)
|
18 |
-
st.session_state.data.extend(urlData)
|
19 |
-
except Exception as e:
|
20 |
-
st.error(f"Failed to load URL: {e}")
|
21 |
-
if uploaded_files:
|
22 |
-
try:
|
23 |
-
print(uploaded_files)
|
24 |
-
pdfData = loadPdfData(uploaded_files)
|
25 |
-
print(pdfData)
|
26 |
-
st.session_state.data.extend(pdfData)
|
27 |
-
except Exception as e:
|
28 |
-
st.error(f"Failed to load URL: {e}")
|
29 |
-
with st.spinner("Processing..."):
|
30 |
-
try:
|
31 |
-
if "data" in st.session_state:
|
32 |
-
st.session_state.retriever = vectorize(st.session_state.data)
|
33 |
-
st.success("Done")
|
34 |
-
except Exception as e:
|
35 |
-
st.error(f"Failed to process Data: {e}")
|
36 |
-
|
37 |
-
|
38 |
-
if "chat_history" not in st.session_state:
|
39 |
-
st.session_state.chat_history = [AIMessage(content="Hello, I am a bot. How can I help you?")]
|
40 |
-
|
41 |
-
st.title("RAG CHAT")
|
42 |
-
for message in st.session_state.chat_history:
|
43 |
-
if isinstance(message, AIMessage):
|
44 |
-
with st.chat_message("AI"):
|
45 |
-
st.write(message.content)
|
46 |
-
elif isinstance(message, HumanMessage):
|
47 |
-
with st.chat_message("Human"):
|
48 |
-
st.write(message.content)
|
49 |
-
|
50 |
-
user_query = st.text_input("Type your message here...", key="chat_input")
|
51 |
-
if user_query:
|
52 |
-
st.session_state.chat_history.append(HumanMessage(content=user_query))
|
53 |
-
with st.chat_message("Human"):
|
54 |
-
st.write(user_query)
|
55 |
-
|
56 |
-
if 'retriever' in st.session_state:
|
57 |
-
try:
|
58 |
-
with st.spinner("Retrieving Information..."):
|
59 |
-
ragAnswer = await st.session_state.retriever.amax_marginal_relevance_search(user_query, k=2, fetch_k=10)
|
60 |
-
context = []
|
61 |
-
for i, doc in enumerate(ragAnswer):
|
62 |
-
print(f"{i}: {doc.page_content}")
|
63 |
-
context.append(doc.page_content)
|
64 |
-
with st.spinner("Generating Response"):
|
65 |
-
response = get_response(user_query, st.session_state.chat_history, context)
|
66 |
-
if response:
|
67 |
-
st.session_state.chat_history.append(AIMessage(content=response))
|
68 |
-
with st.chat_message("AI"):
|
69 |
-
st.write(response)
|
70 |
-
else:
|
71 |
-
st.write("No response received.")
|
72 |
-
except Exception as e:
|
73 |
-
st.error(f"Error during retrieval or response generation: {e}")
|
74 |
-
|
75 |
-
if __name__ == "__main__":
|
76 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|