Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,78 @@
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
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 |
+
import os
|
12 |
+
|
13 |
+
|
14 |
+
st.set_page_config(page_title="RAG Demo - Evan Perez", layout ="wide")
|
15 |
+
|
16 |
+
api_key = 'AIzaSyCvXRggpO2yNwIpZmoMy_5Xhm2bDyD-pOo'
|
17 |
+
|
18 |
+
|
19 |
+
def get_pdf_text(pdf_docs):
|
20 |
+
text = ""
|
21 |
+
for pdf in pdf_docs:
|
22 |
+
pdf_reader = PdfReader(pdf)
|
23 |
+
for page in pdf_reader.pages:
|
24 |
+
text += page.extract_text()
|
25 |
+
return text
|
26 |
+
|
27 |
+
def get_text_chunks(text):
|
28 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=450, chunk_overlap=50)
|
29 |
+
chunks = text_splitter.split_text(text)
|
30 |
+
return chunks
|
31 |
+
|
32 |
+
def get_vector_store(text_chunks, api_key):
|
33 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
34 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
35 |
+
vector_store.save_local("faiss_index")
|
36 |
+
|
37 |
+
def get_conversational_chain():
|
38 |
+
prompt_template = """
|
39 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
40 |
+
provided context just say, "answer is not available in the context", don't provide the wrong answer. When giving an answer, try to include all mentionings of the subject being asked and include this within your response\n\n
|
41 |
+
Context:\n {context}?\n
|
42 |
+
Question: \n{question}\n
|
43 |
+
|
44 |
+
Answer:
|
45 |
+
"""
|
46 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.2, google_api_key=api_key)
|
47 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
48 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
49 |
+
return chain
|
50 |
+
|
51 |
+
def user_input(user_question, api_key):
|
52 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
53 |
+
new_db = FAISS.load_local("faiss_index", embeddings)
|
54 |
+
docs = new_db.similarity_search(user_question)
|
55 |
+
chain = get_conversational_chain()
|
56 |
+
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
57 |
+
st.write("Reply: ", response["output_text"])
|
58 |
+
|
59 |
+
def main():
|
60 |
+
st.header("RAG based LLM Applicatoin")
|
61 |
+
|
62 |
+
user_question = st.text_input("Ask a Question from the PDF Files", key="user_question")
|
63 |
+
|
64 |
+
if user_question and api_key: # Ensure API key and user question are provided
|
65 |
+
user_input(user_question, api_key)
|
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, key="pdf_uploader")
|
70 |
+
if st.button("Submit & Process", key="process_button") and api_key: # Check if API key is provided before processing
|
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, api_key)
|
75 |
+
st.success("Done")
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
main()
|