Spaces:
Sleeping
Sleeping
File size: 3,244 Bytes
6547fc2 | 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 | # Importing all the necessary libraries
import streamlit as st
from PyPDF2 import PdfReader
from langchain.text_splitter import RecursiveCharacterTextSplitter
import os
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
import google.generativeai as genai
from langchain_community.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv
# Load the API Key
load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Creating a function for reading PDF File
def read_pdf(pdf):
text = ""
for file in pdf:
pdf_read = PdfReader(file)
for page in pdf_read.pages:
text += page.extract_text()
return text
# Document Chunking
def get_chunks(text):
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100000, chunk_overlap=10000)
chunks = text_splitter.split_text(text)
return chunks
# Create Embedding Store
def get_vector_store(text_chunks):
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
vector_store.save_local("faiss_index")
# Create Conversation Chain
def get_conversation_chain_pdf():
prompt_template = """
Please meticulously research the question using only the information provided within the context. Provide a detailed
answer, ensuring to avoid unnecessary rambling. If the answer cannot be found within the context, simply state
'answer is not available in the context.'
Context: \n{context}?\n
Question: \n{question}?\n
Answer:
"""
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.9)
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
return chain
# Processing User Input
def user_input(user_query):
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
load_vector_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
docs = load_vector_db.similarity_search(user_query)
chain = get_conversation_chain_pdf()
response = chain(
{"input_documents": docs, "question": user_query},
return_only_outputs=True
)
print(response)
st.write("AI Response : ", response["output_text"])
def app():
#st.set_page_config("PDF Chat App", page_icon="img.png")
st.header("Chat with your PDF files using Google Gemini Pro")
user_query = st.text_input("Ast question for the PDF File?")
if user_query:
user_input(user_query)
st.title("Upload PDF")
pdf_docs = st.file_uploader("Upload your PDF File, and click Submit!", accept_multiple_files=True)
if st.button("Submit!"):
with st.spinner('Processing...'):
raw_text = read_pdf(pdf_docs)
text_chunks = get_chunks(raw_text)
get_vector_store(text_chunks)
st.success("Processing Done!")
if __name__ == "__main__":
app()
|