-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
93 lines (69 loc) · 3.26 KB
/
app.py
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_nvidia_ai_endpoints import NVIDIAEmbeddings, ChatNVIDIA
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.embeddings import OllamaEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain.chains import create_retrieval_chain
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import PyPDFDirectoryLoader
import time
import tempfile
from dotenv import load_dotenv
load_dotenv()
## load the Groq API key
os.environ['NVIDIA_API_KEY']=os.getenv("NVIDIA_API_KEY")
def vector_embedding(loader):
if "vectors" not in st.session_state:
st.session_state.embeddings=NVIDIAEmbeddings()
st.session_state.loader=loader ## Data Ingestion
st.session_state.docs=st.session_state.loader.load() ## Document Loading
st.session_state.text_splitter=RecursiveCharacterTextSplitter(chunk_size=700,chunk_overlap=50) ## Chunk Creation
st.session_state.final_documents=st.session_state.text_splitter.split_documents(st.session_state.docs[:30]) #splitting
st.session_state.vectors=FAISS.from_documents(st.session_state.final_documents,st.session_state.embeddings) #vector OpenAI embeddings
st.title("Nvidia NIM Demo")
llm = ChatNVIDIA(model="mistralai/mixtral-8x7b-instruct-v0.1")
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}
"""
)
uploaded_files = st.file_uploader("Upload PDF",type="pdf", accept_multiple_files=True)
# Check if files were uploaded
if st.button("Documents Embedding") and uploaded_files:
# Create a temporary directory to save the uploaded PDFs
with tempfile.TemporaryDirectory() as temp_dir:
for uploaded_file in uploaded_files:
# Save each uploaded file to the temporary directory
with open(os.path.join(temp_dir, uploaded_file.name), "wb") as f:
f.write(uploaded_file.getbuffer())
# Initialize PyPDFDirectoryLoader with the path to the temporary directory
loader = PyPDFDirectoryLoader(temp_dir)
vector_embedding(loader)
st.write("Vector Store DB Is Ready")
prompt1=st.text_input("Enter Your Question From Documents")
#if st.button("Documents Embedding"):
# vector_embedding()
# st.write("Vector Store DB Is Ready")
if prompt1:
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})
print("Response time :",time.process_time()-start)
st.write(response['answer'])
# With a streamlit expander
with st.expander("Document Similarity Search"):
# Find the relevant chunks
for i, doc in enumerate(response["context"]):
st.write(doc.page_content)
st.write("--------------------------------")