Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
from PyPDF2 import PdfReader
|
|
|
|
|
|
|
| 4 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 5 |
import google.generativeai as genai
|
| 6 |
from langchain.vectorstores import FAISS
|
|
@@ -8,32 +9,40 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
| 8 |
from langchain.chains.question_answering import load_qa_chain
|
| 9 |
from langchain.prompts import PromptTemplate
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
-
import os
|
| 12 |
|
| 13 |
load_dotenv()
|
|
|
|
| 14 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def get_pdf_text(pdf_docs):
|
| 19 |
-
text
|
| 20 |
for pdf in pdf_docs:
|
| 21 |
-
pdf_reader
|
| 22 |
for page in pdf_reader.pages:
|
| 23 |
-
text
|
| 24 |
-
return
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def get_text_chunks(text):
|
| 27 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
| 28 |
chunks = text_splitter.split_text(text)
|
| 29 |
return chunks
|
| 30 |
|
|
|
|
| 31 |
def get_vector_store(text_chunks):
|
| 32 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
| 33 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
| 34 |
vector_store.save_local("faiss_index")
|
| 35 |
|
|
|
|
| 36 |
def get_conversational_chain():
|
|
|
|
| 37 |
prompt_template = """
|
| 38 |
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
| 39 |
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
|
@@ -41,25 +50,57 @@ def get_conversational_chain():
|
|
| 41 |
Question: \n{question}\n
|
| 42 |
Answer:
|
| 43 |
"""
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
|
|
|
| 47 |
return chain
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
@app.post("/ask/")
|
| 58 |
-
async def ask_question(question: str):
|
| 59 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
| 60 |
-
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 61 |
-
docs = new_db.similarity_search(question)
|
| 62 |
chain = get_conversational_chain()
|
| 63 |
-
response = chain({"input_documents": docs, "question": question}, return_only_outputs=True)
|
| 64 |
-
return {"response": response["output_text"]}
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
| 2 |
from PyPDF2 import PdfReader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
import os
|
| 5 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 6 |
import google.generativeai as genai
|
| 7 |
from langchain.vectorstores import FAISS
|
|
|
|
| 9 |
from langchain.chains.question_answering import load_qa_chain
|
| 10 |
from langchain.prompts import PromptTemplate
|
| 11 |
from dotenv import load_dotenv
|
|
|
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
+
os.getenv("GOOGLE_API_KEY")
|
| 15 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 16 |
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
|
| 22 |
def get_pdf_text(pdf_docs):
|
| 23 |
+
text=""
|
| 24 |
for pdf in pdf_docs:
|
| 25 |
+
pdf_reader= PdfReader(pdf)
|
| 26 |
for page in pdf_reader.pages:
|
| 27 |
+
text+= page.extract_text()
|
| 28 |
+
return text
|
| 29 |
+
|
| 30 |
+
|
| 31 |
|
| 32 |
def get_text_chunks(text):
|
| 33 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
| 34 |
chunks = text_splitter.split_text(text)
|
| 35 |
return chunks
|
| 36 |
|
| 37 |
+
|
| 38 |
def get_vector_store(text_chunks):
|
| 39 |
+
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
| 40 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
| 41 |
vector_store.save_local("faiss_index")
|
| 42 |
|
| 43 |
+
|
| 44 |
def get_conversational_chain():
|
| 45 |
+
|
| 46 |
prompt_template = """
|
| 47 |
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
| 48 |
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
|
|
|
| 50 |
Question: \n{question}\n
|
| 51 |
Answer:
|
| 52 |
"""
|
| 53 |
+
|
| 54 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
| 55 |
+
temperature=0.3)
|
| 56 |
+
|
| 57 |
+
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
| 58 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
| 59 |
+
|
| 60 |
return chain
|
| 61 |
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def user_input(user_question):
|
| 65 |
+
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
| 66 |
+
|
| 67 |
+
new_db = FAISS.load_local("faiss_index", embeddings,allow_dangerous_deserialization=True)
|
| 68 |
+
docs = new_db.similarity_search(user_question)
|
| 69 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
chain = get_conversational_chain()
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
|
| 73 |
+
response = chain(
|
| 74 |
+
{"input_documents":docs, "question": user_question}
|
| 75 |
+
, return_only_outputs=True)
|
| 76 |
+
|
| 77 |
+
print(response)
|
| 78 |
+
st.write("Reply: ", response["output_text"])
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def main():
|
| 84 |
+
st.set_page_config("Chat PDF")
|
| 85 |
+
st.header("Chat with PDF using Gemini💁")
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
user_question = st.text_input("Ask a Question from the PDF Files")
|
| 89 |
+
|
| 90 |
+
if user_question:
|
| 91 |
+
user_input(user_question)
|
| 92 |
+
|
| 93 |
+
with st.sidebar:
|
| 94 |
+
st.title("Menu:")
|
| 95 |
+
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
|
| 96 |
+
if st.button("Submit & Process"):
|
| 97 |
+
with st.spinner("Processing..."):
|
| 98 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 99 |
+
text_chunks = get_text_chunks(raw_text)
|
| 100 |
+
get_vector_store(text_chunks)
|
| 101 |
+
st.success("Done")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|