Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,93 @@
|
|
1 |
-
"""
|
2 |
-
Install the Google AI Python SDK
|
3 |
-
|
4 |
-
$ pip install google-generativeai
|
5 |
-
|
6 |
-
See the getting started guide for more information:
|
7 |
-
https://ai.google.dev/gemini-api/docs/get-started/python
|
8 |
-
"""
|
9 |
-
import streamlit as st
|
10 |
import os
|
11 |
-
import
|
12 |
-
from
|
13 |
-
|
|
|
14 |
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
load_dotenv()
|
|
|
17 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
# TODO Make these files available on the local file system
|
69 |
-
# You may need to update the file paths
|
70 |
-
files = [
|
71 |
-
upload_to_gemini("2024_25_Annex_Budget.pdf", mime_type="application/pdf"),
|
72 |
-
upload_to_gemini("2024_25_Budget_Speech.pdf", mime_type="application/pdf"),
|
73 |
-
]
|
74 |
-
|
75 |
-
# Some files have a processing delay. Wait for them to be ready.
|
76 |
-
wait_for_files_active(files)
|
77 |
-
|
78 |
-
chat_session = model.start_chat(
|
79 |
-
history=[ ]
|
80 |
-
)
|
81 |
-
|
82 |
-
response = chat_session.send_message("INSERT_INPUT_HERE")
|
83 |
-
|
84 |
-
print(response.text)
|
85 |
|
86 |
def clear_chat_history():
|
87 |
st.session_state.messages = [
|
88 |
{"role": "assistant", "content": "upload some pdfs and ask me a question"}]
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
def main():
|
91 |
st.set_page_config(
|
92 |
-
page_title="
|
93 |
page_icon="🤖"
|
94 |
)
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
5 |
+
import streamlit as st
|
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 |
+
# read all pdf files and return text
|
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 |
+
# split text into chunks
|
29 |
+
|
30 |
+
|
31 |
+
def get_text_chunks(text):
|
32 |
+
splitter = RecursiveCharacterTextSplitter(
|
33 |
+
chunk_size=10000, chunk_overlap=1000)
|
34 |
+
chunks = splitter.split_text(text)
|
35 |
+
return chunks # list of strings
|
36 |
+
|
37 |
+
# get embeddings for each chunk
|
38 |
+
|
39 |
+
|
40 |
+
def get_vector_store(chunks):
|
41 |
+
embeddings = GoogleGenerativeAIEmbeddings(
|
42 |
+
model="models/embedding-001") # type: ignore
|
43 |
+
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
|
44 |
+
vector_store.save_local("faiss_index")
|
45 |
+
|
46 |
+
|
47 |
+
def get_conversational_chain():
|
48 |
+
prompt_template = """
|
49 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
50 |
+
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
51 |
+
Context:\n {context}?\n
|
52 |
+
Question: \n{question}\n
|
53 |
+
|
54 |
+
Answer:
|
55 |
+
"""
|
56 |
+
|
57 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
58 |
+
client=genai,
|
59 |
+
temperature=0.3,
|
60 |
+
)
|
61 |
+
prompt = PromptTemplate(template=prompt_template,
|
62 |
+
input_variables=["context", "question"])
|
63 |
+
chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
|
64 |
+
return chain
|
65 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
def clear_chat_history():
|
68 |
st.session_state.messages = [
|
69 |
{"role": "assistant", "content": "upload some pdfs and ask me a question"}]
|
70 |
|
71 |
+
|
72 |
+
def user_input(user_question):
|
73 |
+
embeddings = GoogleGenerativeAIEmbeddings(
|
74 |
+
model="models/embedding-001") # type: ignore
|
75 |
+
|
76 |
+
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
77 |
+
docs = new_db.similarity_search(user_question)
|
78 |
+
|
79 |
+
chain = get_conversational_chain()
|
80 |
+
|
81 |
+
response = chain(
|
82 |
+
{"input_documents": docs, "question": user_question}, return_only_outputs=True, )
|
83 |
+
|
84 |
+
print(response)
|
85 |
+
return response
|
86 |
+
|
87 |
+
|
88 |
def main():
|
89 |
st.set_page_config(
|
90 |
+
page_title="Gemini PDF Chatbot",
|
91 |
page_icon="🤖"
|
92 |
)
|
93 |
|