Utkarsh736 commited on
Commit
023f9f0
·
verified ·
1 Parent(s): f698984

Upload main.py

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