Update app.py
Browse files
app.py
CHANGED
@@ -43,8 +43,8 @@ def get_text_chunks(text):
|
|
43 |
|
44 |
|
45 |
|
46 |
-
def get_vector_store(text_chunks):
|
47 |
-
api_key = switch_api_key()
|
48 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
49 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
50 |
vector_store.save_local("faiss_index")
|
@@ -52,8 +52,8 @@ def get_vector_store(text_chunks):
|
|
52 |
|
53 |
|
54 |
|
55 |
-
def get_conversational_chain():
|
56 |
-
api_key = switch_api_key()
|
57 |
prompt_template = """
|
58 |
You are a helpful assistant that only answers based on the context provided from the PDF documents.
|
59 |
Do not use any external knowledge or assumptions. If the answer is not found in the context below, reply with "I don't know."
|
@@ -77,13 +77,11 @@ def get_conversational_chain():
|
|
77 |
|
78 |
|
79 |
|
80 |
-
def user_input(user_question):
|
81 |
-
api_key = switch_api_key()
|
82 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
83 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
84 |
docs = new_db.similarity_search(user_question)
|
85 |
-
chain = get_conversational_chain()
|
86 |
-
|
87 |
|
88 |
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
89 |
st.write("Reply: ", response["output_text"])
|
@@ -95,22 +93,25 @@ def user_input(user_question):
|
|
95 |
def main():
|
96 |
st.set_page_config("Chat PDF")
|
97 |
st.header("CSC 121: Computers and Scientific Thinking (Chatbot)")
|
98 |
-
st.subheader("Ask a question ONLY from the CSC 121 textbook of Dr. Reed",divider=True)
|
99 |
|
|
|
100 |
|
101 |
user_question = st.text_input("Ask a question")
|
102 |
|
103 |
-
|
104 |
if user_question:
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
107 |
|
108 |
pdf_docs = st.file_uploader("Upload PDF files", accept_multiple_files=True)
|
109 |
if st.button("Submit & Process"):
|
110 |
with st.spinner("Processing..."):
|
111 |
raw_text = get_pdf_text(pdf_docs)
|
112 |
text_chunks = get_text_chunks(raw_text)
|
113 |
-
get_vector_store(text_chunks)
|
114 |
st.success("Done")
|
115 |
|
116 |
|
|
|
43 |
|
44 |
|
45 |
|
46 |
+
def get_vector_store(text_chunks, user_api_key=None):
|
47 |
+
api_key = user_api_key if user_api_key else switch_api_key()
|
48 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
49 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
50 |
vector_store.save_local("faiss_index")
|
|
|
52 |
|
53 |
|
54 |
|
55 |
+
def get_conversational_chain(api_key):
|
56 |
+
#api_key = switch_api_key()
|
57 |
prompt_template = """
|
58 |
You are a helpful assistant that only answers based on the context provided from the PDF documents.
|
59 |
Do not use any external knowledge or assumptions. If the answer is not found in the context below, reply with "I don't know."
|
|
|
77 |
|
78 |
|
79 |
|
80 |
+
def user_input(user_question, api_key):
|
|
|
81 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
82 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
83 |
docs = new_db.similarity_search(user_question)
|
84 |
+
chain = get_conversational_chain(api_key)
|
|
|
85 |
|
86 |
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
87 |
st.write("Reply: ", response["output_text"])
|
|
|
93 |
def main():
|
94 |
st.set_page_config("Chat PDF")
|
95 |
st.header("CSC 121: Computers and Scientific Thinking (Chatbot)")
|
96 |
+
st.subheader("Ask a question ONLY from the CSC 121 textbook of Dr. Reed", divider=True)
|
97 |
|
98 |
+
user_api_key = st.text_input("Enter your API key (optional)")
|
99 |
|
100 |
user_question = st.text_input("Ask a question")
|
101 |
|
|
|
102 |
if user_question:
|
103 |
+
if user_api_key:
|
104 |
+
api_key = user_api_key
|
105 |
+
else:
|
106 |
+
api_key = switch_api_key()
|
107 |
+
user_input(user_question, api_key)
|
108 |
|
109 |
pdf_docs = st.file_uploader("Upload PDF files", accept_multiple_files=True)
|
110 |
if st.button("Submit & Process"):
|
111 |
with st.spinner("Processing..."):
|
112 |
raw_text = get_pdf_text(pdf_docs)
|
113 |
text_chunks = get_text_chunks(raw_text)
|
114 |
+
get_vector_store(text_chunks, user_api_key)
|
115 |
st.success("Done")
|
116 |
|
117 |
|