ImranzamanML
commited on
Commit
•
60656dd
1
Parent(s):
a03a6c3
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,7 @@ from langchain.chains.question_answering import load_qa_chain
|
|
9 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
10 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
11 |
|
12 |
-
def process_pdf_files(pdf_files, embedding_model_name):
|
13 |
text = ""
|
14 |
for pdf in pdf_files:
|
15 |
reader = PdfReader(pdf)
|
@@ -17,27 +17,27 @@ def process_pdf_files(pdf_files, embedding_model_name):
|
|
17 |
text += page.extract_text()
|
18 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=500)
|
19 |
text_chunks = text_splitter.split_text(text)
|
20 |
-
embeddings = GoogleGenerativeAIEmbeddings(model=embedding_model_name)
|
21 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
22 |
vector_store.save_local("pdf_database")
|
23 |
return vector_store
|
24 |
|
25 |
-
def setup_qa_chain(chat_model_name):
|
26 |
prompt_template = """
|
27 |
Give answer to the asked question using the provided custom knowledge or given context only and if there is no related content then simply say "Your document dont contain related context to answer". Make sure to not answer incorrect.\n\n
|
28 |
Context:\n{context}\n
|
29 |
Question:\n{question}\n
|
30 |
Answer:
|
31 |
"""
|
32 |
-
model = ChatGoogleGenerativeAI(model=chat_model_name, temperature=0.3)
|
33 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
34 |
return load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
35 |
|
36 |
-
def get_response(user_question, chat_model_name, embedding_model_name):
|
37 |
-
embeddings = GoogleGenerativeAIEmbeddings(model=embedding_model_name)
|
38 |
vector_store = FAISS.load_local("pdf_database", embeddings, allow_dangerous_deserialization=True)
|
39 |
docs = vector_store.similarity_search(user_question)
|
40 |
-
chain = setup_qa_chain(chat_model_name)
|
41 |
response = chain(
|
42 |
{"input_documents": docs, "question": user_question},
|
43 |
return_only_outputs=True
|
@@ -88,7 +88,7 @@ def main():
|
|
88 |
if st.button("Submit data") and pdf_files:
|
89 |
if embedding_model_name:
|
90 |
with st.spinner("Processing the data . . ."):
|
91 |
-
process_pdf_files(pdf_files, embedding_model_name)
|
92 |
st.success("Files submitted successfully")
|
93 |
else:
|
94 |
st.warning("Please select or enter an embedding model.")
|
@@ -100,7 +100,7 @@ def main():
|
|
100 |
|
101 |
if user_question:
|
102 |
with st.spinner("Generating response..."):
|
103 |
-
response = get_response(user_question, chat_model_name, embedding_model_name)
|
104 |
st.write("**Reply:** ", response)
|
105 |
|
106 |
else:
|
|
|
9 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
10 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
11 |
|
12 |
+
def process_pdf_files(pdf_files, embedding_model_name, api_key):
|
13 |
text = ""
|
14 |
for pdf in pdf_files:
|
15 |
reader = PdfReader(pdf)
|
|
|
17 |
text += page.extract_text()
|
18 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=500)
|
19 |
text_chunks = text_splitter.split_text(text)
|
20 |
+
embeddings = GoogleGenerativeAIEmbeddings(model=embedding_model_name, google_api_key=api_key)
|
21 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
22 |
vector_store.save_local("pdf_database")
|
23 |
return vector_store
|
24 |
|
25 |
+
def setup_qa_chain(chat_model_name, api_key):
|
26 |
prompt_template = """
|
27 |
Give answer to the asked question using the provided custom knowledge or given context only and if there is no related content then simply say "Your document dont contain related context to answer". Make sure to not answer incorrect.\n\n
|
28 |
Context:\n{context}\n
|
29 |
Question:\n{question}\n
|
30 |
Answer:
|
31 |
"""
|
32 |
+
model = ChatGoogleGenerativeAI(model=chat_model_name, temperature=0.3, google_api_key=api_key)
|
33 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
34 |
return load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
35 |
|
36 |
+
def get_response(user_question, chat_model_name, embedding_model_name, api_key):
|
37 |
+
embeddings = GoogleGenerativeAIEmbeddings(model=embedding_model_name, google_api_key=api_key)
|
38 |
vector_store = FAISS.load_local("pdf_database", embeddings, allow_dangerous_deserialization=True)
|
39 |
docs = vector_store.similarity_search(user_question)
|
40 |
+
chain = setup_qa_chain(chat_model_name, api_key)
|
41 |
response = chain(
|
42 |
{"input_documents": docs, "question": user_question},
|
43 |
return_only_outputs=True
|
|
|
88 |
if st.button("Submit data") and pdf_files:
|
89 |
if embedding_model_name:
|
90 |
with st.spinner("Processing the data . . ."):
|
91 |
+
process_pdf_files(pdf_files, embedding_model_name, api_key)
|
92 |
st.success("Files submitted successfully")
|
93 |
else:
|
94 |
st.warning("Please select or enter an embedding model.")
|
|
|
100 |
|
101 |
if user_question:
|
102 |
with st.spinner("Generating response..."):
|
103 |
+
response = get_response(user_question, chat_model_name, embedding_model_name, api_key)
|
104 |
st.write("**Reply:** ", response)
|
105 |
|
106 |
else:
|