Prajwal3009 commited on
Commit
c488bf3
·
verified ·
1 Parent(s): 9f1ae22

Update documentchat.py

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