Pranav4datasc commited on
Commit
d7e954c
·
verified ·
1 Parent(s): 5f5f653

Upload 3 files

Browse files
Files changed (3) hide show
  1. .env +1 -0
  2. app.py +98 -0
  3. requirements.txt +12 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GOOGLE_API_KEY="AIzaSyDXQW8-SKLROfSGvJcQOXOl-iE9rvKV2Zk"
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pypdf import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ import os
5
+
6
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
7
+ from langchain_google_genai import ChatGoogleGenerativeAI
8
+ import google.generativeai as genai
9
+
10
+ from langchain_community.vectorstores import FAISS
11
+
12
+ from langchain.chains.question_answering import load_qa_chain
13
+ from langchain.prompts import PromptTemplate
14
+ from dotenv import load_dotenv
15
+
16
+ load_dotenv()
17
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
18
+
19
+ print('Here am I ...1')
20
+ def get_pdf_text(pdf_docs):
21
+ text =""
22
+ print('Here am I ...1.1')
23
+ for pdf in pdf_docs:
24
+ print('Here am I ...1.2')
25
+ pdf_reader=PdfReader(pdf)
26
+ print('Here am I ...1.3')
27
+ for page in pdf_reader.pages:
28
+ print('for pages in...text+=')
29
+ text+=page.extract_text()
30
+ print('Here am I ...2')
31
+ return text
32
+
33
+ def get_text_chunks(text):
34
+ print('Here am I ...3')
35
+ text_splitter=RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
36
+ chunks=text_splitter.split_text(text)
37
+ return chunks
38
+
39
+ def get_vector_store(text_chunks):
40
+ embeddings=GoogleGenerativeAIEmbeddings(model="models/embeddings-001")
41
+ vector_stores=FAISS.from_texts(text_chunks,embedding=embeddings)
42
+ vector_stores.save_local("faiss_index")
43
+
44
+ def get_conversational_chain():
45
+ Prompt_template="""
46
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details.
47
+ if the answer is not in the provided context just say "answer is not available in the context",dont provide the wrong answer.\n\n_
48
+ Context:\n{context}?\n
49
+ Question:\n{question}\n
50
+
51
+ Answer:
52
+ """
53
+ # print('Here am I ...4')
54
+ model=ChatGoogleGenerativeAI(model="gemini-pro",temperature=0.3)
55
+ PromptTemplate(template=Prompt_template,input_variables=["context","question"])
56
+ chain=load_qa_chain(model,chain_type="stuff",prompt=prompt)
57
+ return chain
58
+
59
+ def user_input(user_question):
60
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embeddings-001")
61
+
62
+ new_db = FAISS.load_local("faiss_index",embeddings)
63
+ docs = new_db.similarity_search(user_question)
64
+
65
+ chain = get_conversational_chain()
66
+
67
+ response = chain(
68
+ {"input_documents":docs,"question":user_question},
69
+ return_only_outputs=True)
70
+
71
+ print(response)
72
+ st.write("Reply:", response["Output_text"])
73
+ # print('Here am I ...just bef main')
74
+ def main():
75
+ # print('Here am I ...inside main')
76
+ st.set_page_config("Chat with multiple PDF")
77
+ st.header("Chat with multiple PDF using Gemini AI")
78
+ print('Here am I ...5')
79
+ user_question = st.text_input("Ask a question from a PDF files")
80
+
81
+ if user_question:
82
+ user_input(user_question)
83
+ print('Here am I ...6')
84
+ with st.sidebar:
85
+ st.title("Menu:")
86
+ pdf_docs = st.file_uploader("Upload your PDF files and click on the submit button")
87
+ if st.button("Submit and Process"):
88
+ with st.spinner("Processing..."):
89
+ print('Just before get pdf text call...')
90
+ raw_text = get_pdf_text(pdf_docs)
91
+ print('Just bef get text chunks cal...')
92
+ text_chunks = get_text_chunks(raw_text)
93
+ get_vector_store(text_chunks)
94
+ st.success("Done")
95
+ st.print("In main st.print")
96
+ #print('Here am I ...the end')
97
+ if __name__ == "__main__":
98
+ main()
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF
6
+ chromadb
7
+ faiss-cpu
8
+ langchain_google_genai
9
+ langchain_community
10
+
11
+
12
+