-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
93 lines (79 loc) · 3.69 KB
/
app.py
File metadata and controls
93 lines (79 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import streamlit as st
import os
from langchain_groq import ChatGroq
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import create_retrieval_chain
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import PyPDFDirectoryLoader
from dotenv import load_dotenv
import time
# Load environment variables
load_dotenv()
# Load the Groq and Openai API Key
# os.environ['OPENAI_API_KEY'] =os.getenv('OPENAI_API_KEY')
groq_api_key = os.getenv('GROQ_API_KEY')
# Streamlit app title
st.title('DocuQuery using Llama3 and Groq')
# Initialize the language model
llm = ChatGroq(groq_api_key = groq_api_key,
model_name = "Llama3-8b-8192")
# Define the prompt template
prompt = ChatPromptTemplate.from_template(
"""
Answer the questions based on the provided context only.
Please provide the most accurate response based on the question
<context>
{context}
<context>
Questions:{input}
"""
)
#@st.cache # Cache for later use by the client code when the template is loaded
def init_faiss_index():
if "embeddings" not in st.session_state:
st.session_state.embeddings = OpenAIEmbeddings()
if os.path.exists("faiss_index"):
st.session_state.vectors = FAISS.load_local("faiss_index", st.session_state.embeddings, allow_dangerous_deserialization=True)
st.write("Loaded vectors from FAISS index.")
else:
st.session_state.embeddings = OpenAIEmbeddings()
st.session_state.loader = PyPDFDirectoryLoader("./PDFdocs") # Data Ingestion
st.session_state.docs =st.session_state.loader.load() # Document Loading
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) # Chunck creation
st.session_state.final_documents = st.session_state.text_splitter.split_documents(st.session_state.docs[:50]) # Splitting
# Embed documents and create FAISS index
st.session_state.vectors = FAISS.from_documents(st.session_state.final_documents, st.session_state.embeddings)
st.session_state.vectors.save_local("faiss_index")
st.write("Created and saved vectors to FAISS index.")
# Input field for user questions
prompt1 = st.text_input("Enter Your Question From Documents")
# Button to trigger document embedding
if st.button("Documents Embedding"):
with st.spinner("Embedding documents..."):
try:
init_faiss_index()
st.write("Vector Store DB Is Ready")
except Exception as e:
st.error(f"An error occurred during vector embedding: {e}")
# Processing user question and fetching relevant documents
if prompt1:
try:
document_chain =create_stuff_documents_chain(llm, prompt)
retriever = st.session_state.vectors.as_retriever()
retrieval_chain = create_retrieval_chain(retriever, document_chain)
start = time.process_time()
response = retrieval_chain.invoke({'input': prompt1})
response_time = time.process_time() - start
st.write(f"Response time: {response_time:.2f} seconds")
st.write(response['answer'])
# Display similar documents in an expander
with st.expander("Document Similarity Search"):
# Find the relevant chunks
for i, doc in enumerate(response['context']):
st.info(doc.page_content)
st.write("--------------------------------")
except Exception as e:
st.error(f"An error occurred during document retrieval: {e}")