Spaces:
Sleeping
Sleeping
abstracting code
#2
by
LordFarquaad42
- opened
- .gitignore +2 -0
- .streamlit/config.toml +11 -1
- app.py +3 -4
- functions/gptResponse.py +14 -9
- functions/sidebar.py +4 -4
- pages/home.py +16 -0
- pages/ragChat.py +69 -41
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
env/
|
2 |
+
*/__pycache__
|
.streamlit/config.toml
CHANGED
@@ -1,2 +1,12 @@
|
|
1 |
[client]
|
2 |
-
showSidebarNavigation = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
[client]
|
2 |
+
showSidebarNavigation = false
|
3 |
+
showErrorDetails = false
|
4 |
+
|
5 |
+
[theme]
|
6 |
+
base="dark"
|
7 |
+
primaryColor = "#702963"
|
8 |
+
font="serif"
|
9 |
+
|
10 |
+
[server]
|
11 |
+
fileWatcherType = "auto"
|
12 |
+
runOnSave = true
|
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
-
|
2 |
-
|
3 |
|
4 |
-
|
5 |
-
st.title('Home')
|
|
|
1 |
+
from pages.home import home
|
2 |
+
# idea: instead of "redirecting" - you can have redirects render different pages on main page
|
3 |
|
4 |
+
home()
|
|
functions/gptResponse.py
CHANGED
@@ -5,7 +5,11 @@ from dotenv import load_dotenv
|
|
5 |
import os
|
6 |
|
7 |
load_dotenv()
|
8 |
-
openai_key = os.getenv(
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def get_response(user_query, chat_history, context):
|
11 |
template = """
|
@@ -18,20 +22,21 @@ def get_response(user_query, chat_history, context):
|
|
18 |
User question: {user_question}
|
19 |
"""
|
20 |
|
21 |
-
|
22 |
llm = ChatOpenAI(api_key=openai_key)
|
23 |
try:
|
24 |
prompt = ChatPromptTemplate.from_template(template)
|
25 |
|
26 |
llm = ChatOpenAI(api_key=openai_key)
|
27 |
-
|
28 |
chain = prompt | llm | StrOutputParser()
|
29 |
-
|
30 |
-
value = chain.stream(
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
if value:
|
36 |
response = " ".join([part for part in value])
|
37 |
return response
|
|
|
5 |
import os
|
6 |
|
7 |
load_dotenv()
|
8 |
+
openai_key = os.getenv(
|
9 |
+
"OPENAI_API_KEY"
|
10 |
+
) # may wanna ask user for this or handle error when its not there
|
11 |
+
# if not openai_key:
|
12 |
+
# raise ValueError("OpenAI API key not found in environment variables.")
|
13 |
|
14 |
def get_response(user_query, chat_history, context):
|
15 |
template = """
|
|
|
22 |
User question: {user_question}
|
23 |
"""
|
24 |
|
|
|
25 |
llm = ChatOpenAI(api_key=openai_key)
|
26 |
try:
|
27 |
prompt = ChatPromptTemplate.from_template(template)
|
28 |
|
29 |
llm = ChatOpenAI(api_key=openai_key)
|
30 |
+
|
31 |
chain = prompt | llm | StrOutputParser()
|
32 |
+
|
33 |
+
value = chain.stream(
|
34 |
+
{
|
35 |
+
"chat_history": chat_history,
|
36 |
+
"context": context,
|
37 |
+
"user_question": user_query,
|
38 |
+
}
|
39 |
+
)
|
40 |
if value:
|
41 |
response = " ".join([part for part in value])
|
42 |
return response
|
functions/sidebar.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
def sidebar():
|
4 |
-
|
5 |
-
st.sidebar.page_link("app.py", label="Home")
|
6 |
-
st.sidebar.page_link("pages/ragChat.py", label="RAG CHAT")
|
7 |
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
def sidebar():
|
5 |
+
st.sidebar.title("Navigation")
|
6 |
+
st.sidebar.page_link("pages/home.py", label="Home", icon="🏠")
|
7 |
+
st.sidebar.page_link("pages/ragChat.py", label="RAG CHAT", icon="💬")
|
pages/home.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from functions.sidebar import sidebar
|
3 |
+
|
4 |
+
|
5 |
+
def home():
|
6 |
+
sidebar()
|
7 |
+
st.header("Home")
|
8 |
+
st.write("add a desc here")
|
9 |
+
|
10 |
+
st.subheader("Goto Chat")
|
11 |
+
st.page_link(page="pages/ragChat.py", label="RAG CHAT", icon="💬")
|
12 |
+
|
13 |
+
|
14 |
+
# if page is called directly (/home)
|
15 |
+
if __name__ == "__main__":
|
16 |
+
home()
|
pages/ragChat.py
CHANGED
@@ -6,24 +6,26 @@ from functions.web_chain import vectorize, loadUrlData, get_pdf_text
|
|
6 |
import asyncio
|
7 |
|
8 |
|
9 |
-
async def
|
10 |
-
sidebar()
|
11 |
st.title("Upload Data")
|
12 |
|
13 |
uploaded_files = st.file_uploader("Upload PDFs", accept_multiple_files=True)
|
14 |
-
st.warning(
|
|
|
|
|
15 |
url = st.text_input("Enter a website link")
|
16 |
-
|
17 |
-
|
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:
|
24 |
-
st.session_state.retriever = vectorize(
|
|
|
|
|
25 |
except Exception as e:
|
26 |
st.error(f"Failed to load URL: {e}")
|
|
|
27 |
if uploaded_files:
|
28 |
try:
|
29 |
texts = get_pdf_text(uploaded_files)
|
@@ -36,44 +38,70 @@ async def main():
|
|
36 |
st.error("PDF has no meta data text")
|
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 |
-
|
42 |
-
|
43 |
-
st.session_state.chat_history = [AIMessage(content="Hello, I am a bot. How can I help you?")]
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
elif isinstance(message, HumanMessage):
|
51 |
-
with st.chat_message("Human"):
|
52 |
-
st.write(message.content)
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
with st.chat_message("Human"):
|
58 |
-
st.write(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
6 |
import asyncio
|
7 |
|
8 |
|
9 |
+
async def add_data():
|
|
|
10 |
st.title("Upload Data")
|
11 |
|
12 |
uploaded_files = st.file_uploader("Upload PDFs", accept_multiple_files=True)
|
13 |
+
st.warning(
|
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:
|
22 |
if "retriever" not in st.session_state:
|
23 |
+
st.session_state.retriever = vectorize(
|
24 |
+
loadUrlData(url), "document"
|
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 |
+
return False
|
45 |
+
|
|
|
46 |
|
47 |
+
async def rag_chat():
|
48 |
+
if "chat_history" not in st.session_state:
|
49 |
+
st.session_state.chat_history = [
|
50 |
+
AIMessage(content="Hello, I am a bot. How can I help you?")
|
51 |
+
]
|
|
|
|
|
|
|
52 |
|
53 |
+
st.title("RAG CHAT")
|
54 |
+
for message in st.session_state.chat_history:
|
55 |
+
if isinstance(message, AIMessage):
|
56 |
+
with st.chat_message("AI"):
|
57 |
+
st.write(message.content)
|
58 |
+
elif isinstance(message, HumanMessage):
|
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(user_query)
|
67 |
+
|
68 |
+
if "retriever" in st.session_state:
|
69 |
+
try:
|
70 |
+
ragAnswer = (
|
71 |
+
await st.session_state.retriever.amax_marginal_relevance_search(
|
72 |
+
user_query, k=4, fetch_k=10
|
73 |
+
)
|
74 |
+
)
|
75 |
+
context = []
|
76 |
+
for i, doc in enumerate(ragAnswer):
|
77 |
+
print(f"{i}: {doc.page_content}")
|
78 |
+
context.append(doc.page_content)
|
79 |
+
with st.spinner("Generating Response"):
|
80 |
+
response = get_response(
|
81 |
+
user_query, st.session_state.chat_history, context
|
82 |
+
)
|
83 |
+
if response:
|
84 |
+
st.session_state.chat_history.append(
|
85 |
+
AIMessage(content=response)
|
86 |
+
)
|
87 |
+
with st.chat_message("AI"):
|
88 |
+
st.write(response)
|
89 |
+
else:
|
90 |
+
st.write("No response received.")
|
91 |
+
except Exception as e:
|
92 |
+
st.error(f"Error during retrieval or response generation: {e}")
|
93 |
+
|
94 |
+
|
95 |
+
async def main():
|
96 |
+
if st.session_state.data_hungry:
|
97 |
+
st.session_state.data_hungry = (
|
98 |
+
await add_data()
|
99 |
+
)
|
100 |
+
else:
|
101 |
+
await rag_chat()
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
+
st.session_state.data_hungry = st.toggle("Add Custom Data", False)
|
106 |
+
sidebar()
|
107 |
asyncio.run(main())
|