Update app.py
Browse files
app.py
CHANGED
@@ -1,104 +1,41 @@
|
|
1 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
2 |
from PyPDF2 import PdfReader
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
5 |
from langchain.vectorstores import FAISS
|
6 |
from langchain.chains.question_answering import load_qa_chain
|
7 |
from langchain.prompts import PromptTemplate
|
8 |
-
import streamlit as st
|
9 |
-
import os
|
10 |
-
import sqlite3
|
11 |
-
import google.generativeai as genai
|
12 |
|
13 |
-
# Load
|
14 |
-
load_dotenv()
|
15 |
-
|
16 |
-
# Configure Genai Key
|
17 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
18 |
|
19 |
-
# Initialize Streamlit app
|
20 |
-
st.set_page_config(page_title="Q&A Demo")
|
21 |
-
|
22 |
-
st.header("Gemini LLM Application")
|
23 |
-
|
24 |
-
# Initialize session state for chat history if it doesn't exist
|
25 |
-
if 'chat_history' not in st.session_state:
|
26 |
-
st.session_state['chat_history'] = []
|
27 |
-
|
28 |
-
# Load Google Gemini Model
|
29 |
-
model = genai.GenerativeModel("gemini-pro")
|
30 |
-
chat = model.start_chat(history=[])
|
31 |
-
|
32 |
-
# Function to get response from Gemini model
|
33 |
-
def get_gemini_response(question):
|
34 |
-
response = chat.send_message(question)
|
35 |
-
return response
|
36 |
-
|
37 |
-
# Function to read SQL query from database
|
38 |
-
def read_sql_query(sql, db):
|
39 |
-
conn = sqlite3.connect(db)
|
40 |
-
cur = conn.cursor()
|
41 |
-
cur.execute(sql)
|
42 |
-
rows = cur.fetchall()
|
43 |
-
conn.commit()
|
44 |
-
conn.close()
|
45 |
-
return rows
|
46 |
-
|
47 |
-
# Define prompt for Gemini model
|
48 |
-
prompt = """
|
49 |
-
You are an expert in converting English questions to SQL query!
|
50 |
-
The SQL database has the name STUDENT and has the following columns - NAME, CLASS, SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
|
51 |
-
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
|
52 |
-
\nExample 2 - Tell me all the students studying in Data Science class?,
|
53 |
-
the SQL command will be something like this SELECT * FROM STUDENT
|
54 |
-
where CLASS="Data Science"; And show me the data in tabular format if possible.
|
55 |
-
also the sql code should not have ``` in beginning or end and sql word in output
|
56 |
-
"""
|
57 |
-
|
58 |
-
# Streamlit UI
|
59 |
-
input_text = st.text_area("Input: ", key="input")
|
60 |
-
submit_button = st.button("Ask the question")
|
61 |
-
|
62 |
-
if submit_button and input_text:
|
63 |
-
# Get response from Gemini model
|
64 |
-
response = get_gemini_response(input_text)
|
65 |
-
|
66 |
-
# Add user query and response to session state chat history
|
67 |
-
st.session_state['chat_history'].append(("You", input_text))
|
68 |
-
st.subheader("The Response is")
|
69 |
-
for chunk in response:
|
70 |
-
st.write(chunk.text)
|
71 |
-
st.session_state['chat_history'].append(("Bot", chunk.text))
|
72 |
-
|
73 |
-
# Display chat history
|
74 |
-
st.subheader("The Chat History is")
|
75 |
-
for role, text in st.session_state['chat_history']:
|
76 |
-
st.write(f"{role}: {text}")
|
77 |
|
78 |
-
# Function to get text from PDF
|
79 |
def get_pdf_text(pdf_docs):
|
80 |
-
text
|
81 |
for pdf in pdf_docs:
|
82 |
-
pdf_reader
|
83 |
for page in pdf_reader.pages:
|
84 |
-
text
|
85 |
-
return
|
|
|
86 |
|
87 |
-
# Function to split text into chunks
|
88 |
def get_text_chunks(text):
|
89 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
90 |
chunks = text_splitter.split_text(text)
|
91 |
return chunks
|
92 |
|
93 |
-
|
94 |
def get_vector_store(text_chunks):
|
95 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
96 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
97 |
vector_store.save_local("faiss_index")
|
98 |
|
99 |
-
|
100 |
def get_conversational_chain():
|
101 |
-
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
102 |
prompt_template = """
|
103 |
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
104 |
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
@@ -107,11 +44,14 @@ def get_conversational_chain():
|
|
107 |
|
108 |
Answer:
|
109 |
"""
|
|
|
|
|
110 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
111 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
|
|
112 |
return chain
|
113 |
|
114 |
-
|
115 |
def user_input(user_question):
|
116 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
117 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
@@ -120,22 +60,70 @@ def user_input(user_question):
|
|
120 |
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
121 |
st.write("Reply: ", response["output_text"])
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from dotenv import load_dotenv
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
import google.generativeai as genai
|
5 |
+
from PIL import Image
|
6 |
from PyPDF2 import PdfReader
|
7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
9 |
from langchain.vectorstores import FAISS
|
10 |
from langchain.chains.question_answering import load_qa_chain
|
11 |
from langchain.prompts import PromptTemplate
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
load_dotenv() # Load all env variables
|
|
|
|
|
|
|
14 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
17 |
def get_pdf_text(pdf_docs):
|
18 |
+
text=""
|
19 |
for pdf in pdf_docs:
|
20 |
+
pdf_reader= PdfReader(pdf)
|
21 |
for page in pdf_reader.pages:
|
22 |
+
text+= page.extract_text()
|
23 |
+
return text
|
24 |
+
|
25 |
|
|
|
26 |
def get_text_chunks(text):
|
27 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
28 |
chunks = text_splitter.split_text(text)
|
29 |
return chunks
|
30 |
|
31 |
+
|
32 |
def get_vector_store(text_chunks):
|
33 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
34 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
35 |
vector_store.save_local("faiss_index")
|
36 |
|
37 |
+
|
38 |
def get_conversational_chain():
|
|
|
39 |
prompt_template = """
|
40 |
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
41 |
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
|
|
44 |
|
45 |
Answer:
|
46 |
"""
|
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 |
+
|
52 |
return chain
|
53 |
|
54 |
+
|
55 |
def user_input(user_question):
|
56 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
57 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
|
|
60 |
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
61 |
st.write("Reply: ", response["output_text"])
|
62 |
|
63 |
+
|
64 |
+
## function to load Gemini Pro model and get responses
|
65 |
+
model = genai.GenerativeModel("gemini-pro-vision")
|
66 |
+
def get_gemini_response(input,image):
|
67 |
+
if input != "":
|
68 |
+
response=model.generate_content([input,image])
|
69 |
+
else:
|
70 |
+
response=model.generate_content(image)
|
71 |
+
return response.text
|
72 |
+
|
73 |
+
|
74 |
+
## Initialize our Streamlit app
|
75 |
+
|
76 |
+
st.set_page_config(page_title='Combined Streamlit Application')
|
77 |
+
st.header("Streamlit Application")
|
78 |
+
|
79 |
+
# PDF Chat Section
|
80 |
+
user_question = st.text_input("Ask a Question from the PDF Files")
|
81 |
+
if user_question:
|
82 |
+
user_input(user_question)
|
83 |
+
|
84 |
+
with st.sidebar:
|
85 |
+
st.title("Menu:")
|
86 |
+
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
|
87 |
+
if st.button("Submit & Process"):
|
88 |
+
with st.spinner("Processing..."):
|
89 |
+
raw_text = get_pdf_text(pdf_docs)
|
90 |
+
text_chunks = get_text_chunks(raw_text)
|
91 |
+
get_vector_store(text_chunks)
|
92 |
+
st.success("Done")
|
93 |
+
|
94 |
+
# Image Chat Section
|
95 |
+
input_text = st.text_input("Input for Gemini Pro:", key="input_gemini")
|
96 |
+
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
|
97 |
+
if uploaded_file is not None:
|
98 |
+
image = Image.open(uploaded_file)
|
99 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
100 |
+
|
101 |
+
submit_gemini = st.button("Ask Gemini Pro")
|
102 |
+
|
103 |
+
if submit_gemini:
|
104 |
+
response_gemini = get_gemini_response(input_text, image)
|
105 |
+
st.subheader("Gemini Pro Response:")
|
106 |
+
st.write(response_gemini)
|
107 |
+
|
108 |
+
# Q&A Chat Section
|
109 |
+
st.header("Q&A Chat Section")
|
110 |
+
|
111 |
+
# Initialize session state for chat history if it doesn't exist
|
112 |
+
if 'chat_history' not in st.session_state:
|
113 |
+
st.session_state['chat_history'] = []
|
114 |
+
|
115 |
+
input_qa = st.text_area("Input for Q&A:", key="input_qa")
|
116 |
+
submit_qa = st.button("Ask the question")
|
117 |
+
|
118 |
+
if submit_qa and input_qa:
|
119 |
+
response_qa = get_gemini_response(input_qa)
|
120 |
+
# Add user query and response to session state chat history
|
121 |
+
st.session_state['chat_history'].append(("You", input_qa))
|
122 |
+
st.subheader("Q&A Response:")
|
123 |
+
for chunk in response_qa:
|
124 |
+
st.write(chunk.text)
|
125 |
+
st.session_state['chat_history'].append(("Gemini Pro", chunk.text))
|
126 |
+
|
127 |
+
st.subheader("Q&A Chat History:")
|
128 |
+
for role, text in st.session_state['chat_history']:
|
129 |
+
st.write(f"{role}: {text}")
|