sadak commited on
Commit
f19506e
1 Parent(s): d54a145

Upload 2 files

Browse files
Files changed (2) hide show
  1. requirements.txt +15 -0
  2. test.py +91 -0
requirements.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ chromadb
7
+ faiss-cpu
8
+ pdf2image
9
+ langchain
10
+ PyPDF2
11
+ chromadb
12
+ faiss-cpu
13
+ langchain_google_genai
14
+ pdfplumber
15
+ pickle
test.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv() # Load all env variables
3
+
4
+ import streamlit as st
5
+ from PyPDF2 import PdfReader
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ import os
8
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
9
+ import google.generativeai as genai
10
+ from langchain.vectorstores import FAISS
11
+ from langchain_google_genai import ChatGoogleGenerativeAI
12
+ from langchain.chains.question_answering import load_qa_chain
13
+ from langchain.prompts import PromptTemplate
14
+
15
+ ## Function to load gemini pro model and get responses
16
+ model = genai.GenerativeModel("gemini-pro")
17
+ def get_gemini_response(question):
18
+ response=model.generate_content(question)
19
+ return response.text
20
+
21
+ def get_pdf_text(pdf_docs):
22
+ text=""
23
+ for pdf in pdf_docs:
24
+ pdf_reader= PdfReader(pdf)
25
+ for page in pdf_reader.pages:
26
+ text+= page.extract_text()
27
+ return text
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
+ def get_vector_store(text_chunks):
35
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
36
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
37
+ vector_store.save_local("faiss_index")
38
+
39
+ def get_conversational_chain():
40
+ prompt_template = """
41
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
42
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
43
+ Context:\n {context}?\n
44
+ Question: \n{question}\n
45
+
46
+ Answer:
47
+ """
48
+ model = ChatGoogleGenerativeAI(model="gemini-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,allow_dangerous_deserialization=True)
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
+ return response["output_text"]
60
+
61
+ def main():
62
+ st.set_page_config(page_title='Q&A Demo')
63
+ st.header("Combined Application")
64
+
65
+ app_mode = st.sidebar.selectbox("Choose the App Mode", ["Gemini Q&A", "PDF Q&A"])
66
+
67
+ if app_mode == "Gemini Q&A":
68
+ st.subheader("Gemini LLM Application")
69
+ user_question = st.text_input("Input")
70
+ if st.button("Ask the question"):
71
+ response = get_gemini_response(user_question)
72
+ st.subheader("The Response is :")
73
+ st.write(response)
74
+
75
+ elif app_mode == "PDF Q&A":
76
+ st.subheader("Chat with PDF using Gemini💁")
77
+ user_question = st.text_input("Ask a Question from the PDF Files")
78
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
79
+ if st.button("Submit & Process"):
80
+ with st.spinner("Processing..."):
81
+ raw_text = get_pdf_text(pdf_docs)
82
+ text_chunks = get_text_chunks(raw_text)
83
+ get_vector_store(text_chunks)
84
+ st.success("Done")
85
+
86
+ if user_question:
87
+ response = user_input(user_question)
88
+ st.write("Reply: ", response)
89
+
90
+ if __name__ == "__main__":
91
+ main()