Defkhan5960 commited on
Commit
855f7a4
1 Parent(s): 67cae79

Upload 3 files

Browse files
Files changed (3) hide show
  1. .env +1 -0
  2. app.py +103 -0
  3. requirements.txt +9 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GOOGLE_API_KEY="AIzaSyAbKZgDxZ-lL-vNXStTl0S4vZftDSYu890"
app.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.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
+ load_dotenv()
14
+ os.getenv("GOOGLE_API_KEY")
15
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
+
17
+
18
+
19
+
20
+ def get_pdf_text(pdf_docs):
21
+ text=""
22
+ for pdf in pdf_docs:
23
+ pdf_reader= PdfReader(pdf)
24
+ for page in pdf_reader.pages:
25
+ text+= page.extract_text()
26
+ return text
27
+
28
+
29
+
30
+ def get_text_chunks(text):
31
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
32
+ chunks = text_splitter.split_text(text)
33
+ return chunks
34
+
35
+
36
+ def get_vector_store(text_chunks):
37
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
38
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
39
+ vector_store.save_local("faiss_index")
40
+
41
+
42
+ def get_conversational_chain():
43
+
44
+ prompt_template = """
45
+
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()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ chromadb
7
+ faiss-cpu
8
+ langchain_google_genai
9
+ langchain-community