Repository navigation
Expand file tree
/
Copy pathpdf_chatbot.py
More file actions
114 lines (93 loc) · 3.99 KB
/
Copy pathpdf_chatbot.py
File metadata and controls
114 lines (93 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import openai
import fitz
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.indexes import VectorstoreIndexCreator
from langchain.indexes.vectorstore import VectorStoreIndexWrapper
from langchain.vectorstores import Chroma
import streamlit as st
from tempfile import NamedTemporaryFile
# Load the API key from Streamlit secrets
openai.api_key = st.secrets["OPENAI_API_KEY"]
# Enable to save to disk & reuse the model (for repeated queries on the same data)
PERSIST = False
# Set page config
st.set_page_config(page_title="Background Image Example", layout="centered")
# Inject CSS with background image
st.markdown(
"""
<style>
.stApp {
background-image: url("https://plus.unsplash.com/premium_photo-1663100722417-6e36673fe0ed?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
</style>
""",
unsafe_allow_html=True
)
def extract_text_from_pdf(file_path):
with fitz.open(file_path) as doc:
text = ""
for page in doc:
text += page.get_text()
return text
def load_files(uploaded_files):
temp_files = []
for uploaded_file in uploaded_files:
with NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
temp_file.write(uploaded_file.read())
temp_files.append(temp_file.name)
return temp_files
st.title("Conversational Retrieval Chain with LangChain")
st.write("Upload your PDF files and ask questions based on their content.")
uploaded_files = st.file_uploader("Upload PDF files", type=["pdf"], accept_multiple_files=True)
if uploaded_files:
temp_file_paths = load_files(uploaded_files)
text_data = []
for file_path in temp_file_paths:
text = extract_text_from_pdf(file_path)
with NamedTemporaryFile(delete=False, suffix=".txt") as temp_text_file:
temp_text_file.write(text.encode())
text_data.append(temp_text_file.name)
loaders = [TextLoader(file_path) for file_path in text_data]
try:
if PERSIST and os.path.exists("persist"):
st.write("Reusing index...\n")
vectorstore = Chroma(persist_directory="persist", embedding_function=OpenAIEmbeddings())
index = VectorStoreIndexWrapper(vectorstore=vectorstore)
else:
if PERSIST:
index = VectorstoreIndexCreator(
vectorstore_kwargs={"persist_directory": "persist"},
embedding=OpenAIEmbeddings()
).from_loaders(loaders)
else:
index = VectorstoreIndexCreator(
embedding=OpenAIEmbeddings()
).from_loaders(loaders)
chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(model="gpt-3.5-turbo"),
retriever=index.vectorstore.as_retriever(search_kwargs={"k": 1}),
)
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
query = st.text_input("Enter your question:")
if query:
result = chain({"question": query, "chat_history": st.session_state.chat_history})
st.write(result['answer'])
st.session_state.chat_history.append((query, result['answer']))
# Citation References
st.write("Citations:")
for doc in result['source_documents']:
st.write(f"Source: {doc.metadata['source']}")
if st.button("Clear History"):
st.session_state.chat_history = []
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.write("Please upload PDF files to proceed.")