Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,114 +1,113 @@
|
|
1 |
-
from langchain_nvidia import ChatNVIDIA
|
2 |
-
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings
|
3 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
4 |
-
from langchain.vectorstores import FAISS
|
5 |
-
from langchain_community.document_loaders import PyPDFDirectoryLoader,PyPDFLoader
|
6 |
-
from langchain_core.prompts import ChatMessagePromptTemplate,ChatPromptTemplate
|
7 |
-
from langchain.prompts import PromptTemplate
|
8 |
-
from dotenv import load_dotenv
|
9 |
-
from
|
10 |
-
from langchain.chains.
|
11 |
-
from langchain.chains
|
12 |
-
from langchain.chains import
|
13 |
-
|
14 |
-
import
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
os.environ[
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
st.session_state.
|
40 |
-
#st.session_state.
|
41 |
-
|
42 |
-
st.session_state.
|
43 |
-
st.session_state.
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
similar
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
st.
|
68 |
-
st.title(
|
69 |
-
st.
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
st.
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
st.
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
st.
|
97 |
-
|
98 |
-
|
99 |
-
if
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
st.write(
|
111 |
-
|
112 |
-
|
113 |
-
else:
|
114 |
st.write("context is not provide in the response")
|
|
|
1 |
+
from langchain_nvidia import ChatNVIDIA
|
2 |
+
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings
|
3 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
from langchain_community.document_loaders import PyPDFDirectoryLoader,PyPDFLoader
|
6 |
+
from langchain_core.prompts import ChatMessagePromptTemplate,ChatPromptTemplate
|
7 |
+
from langchain.prompts import PromptTemplate
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
from langchain.chains.history_aware_retriever import create_history_aware_retriever
|
10 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
11 |
+
from langchain.chains import create_retrieval_chain
|
12 |
+
from langchain.chains import LLMChain
|
13 |
+
import streamlit as st
|
14 |
+
import os
|
15 |
+
|
16 |
+
load_dotenv()
|
17 |
+
os.environ["NVIDIA_API_KEY"] = os.getenv("NVIDIA_API_KEY")
|
18 |
+
os.environ['HF_TOKEN'] = os.getenv("HF_TOKEN")
|
19 |
+
|
20 |
+
|
21 |
+
def file_process(file_uploader):
|
22 |
+
|
23 |
+
all_docs = []
|
24 |
+
if file_uploader is not None:
|
25 |
+
|
26 |
+
for file_uploader in file_uploader:
|
27 |
+
with open(f"./temp/{file_uploader.name}",'wb') as f:
|
28 |
+
f.write(file_uploader.getbuffer())
|
29 |
+
loader = PyPDFLoader(f"./temp/{file_uploader.name}")
|
30 |
+
docs = loader.load()
|
31 |
+
all_docs.extend(docs)
|
32 |
+
|
33 |
+
return all_docs
|
34 |
+
|
35 |
+
def vectore_function(docs):
|
36 |
+
if "vectore" not in st.session_state:
|
37 |
+
|
38 |
+
st.session_state.embedding = NVIDIAEmbeddings()
|
39 |
+
#st.session_state.loader = PyPDFDirectoryLoader("./pdf")
|
40 |
+
#st.session_state.pdf = st.session_state.loader.load()
|
41 |
+
st.session_state.text_spliter = RecursiveCharacterTextSplitter(chunk_size=700,chunk_overlap=70)
|
42 |
+
st.session_state.spliter = st.session_state.text_spliter.split_documents(docs[:30])
|
43 |
+
st.session_state.vectore = FAISS.from_documents(st.session_state.spliter,st.session_state.embedding)
|
44 |
+
|
45 |
+
|
46 |
+
template_prompt = ChatPromptTemplate.from_template(
|
47 |
+
|
48 |
+
"""
|
49 |
+
your work to give the answer to the user question from using
|
50 |
+
context. please try to use attractive emoji or give the most accurcate and
|
51 |
+
similar answer from the context if answer is seems
|
52 |
+
similar so write the Sorry we don't have a answer yet!.
|
53 |
+
|
54 |
+
<context>
|
55 |
+
{context}
|
56 |
+
<context>
|
57 |
+
|
58 |
+
Question:{input}
|
59 |
+
|
60 |
+
"""
|
61 |
+
)
|
62 |
+
|
63 |
+
|
64 |
+
llm = ChatNVIDIA(model="nvidia/llama-3.1-nemotron-70b-instruct")
|
65 |
+
|
66 |
+
st.set_page_config(page_title="Langchain: Question/Answering Chat bot",page_icon="🦜")
|
67 |
+
st.title('🦜LangChain')
|
68 |
+
st.title("Title: Question/Answering Chat bot")
|
69 |
+
st.subheader("How to use:")
|
70 |
+
|
71 |
+
st.write("Step1: Upload your document. Please ensure that the document is in PDF format.")
|
72 |
+
|
73 |
+
st.write("""Step2: Click on the 'Embed Document' button and wait. During this step,the entire document will be embedded and stored in the database.
|
74 |
+
Wait until the database setup is complete.""")
|
75 |
+
|
76 |
+
st.write("""Step3: Once the database is ready, a text box will appear where you can
|
77 |
+
ask question based on the stored document.""")
|
78 |
+
|
79 |
+
st.write("Step4: Enter your question in the text box and press Enter to receive answers.")
|
80 |
+
|
81 |
+
st.write("⚠️Step 1: Upload the document then go to another steps")
|
82 |
+
file_uploader = st.file_uploader("Upload the only pdf",type='pdf',accept_multiple_files=True)
|
83 |
+
|
84 |
+
if file_uploader is not None:
|
85 |
+
#st.write(f"file name:{file_uploader.name}")
|
86 |
+
os.makedirs(f"./temp",exist_ok=True)
|
87 |
+
upload_docs = file_process(file_uploader)
|
88 |
+
st.write(f"length of the uploaded document: {len(upload_docs)}")
|
89 |
+
|
90 |
+
st.write("⚠️Step2: Click on the 'Embed document' button and wait for the database is successfully ready")
|
91 |
+
if st.button("Embed the document"):
|
92 |
+
vectore_function(upload_docs)
|
93 |
+
st.write("Sucessfully database is ready")
|
94 |
+
|
95 |
+
st.write("⚠️After completing all the requirements then you ask your question")
|
96 |
+
user_input = st.text_area("Ask you any question from the documentation")
|
97 |
+
|
98 |
+
if st.button("👉Generate the answer"):
|
99 |
+
if user_input:
|
100 |
+
chain_stuff = create_stuff_documents_chain(llm=llm,prompt=template_prompt)
|
101 |
+
retriever = st.session_state.vectore.as_retriever()
|
102 |
+
chain_retriever = create_retrieval_chain(retriever,chain_stuff)
|
103 |
+
response = chain_retriever.invoke({"input":user_input})
|
104 |
+
st.write(response['answer'])
|
105 |
+
|
106 |
+
if "context" in response:
|
107 |
+
with st.expander("Similar document:"):
|
108 |
+
for i,doc in enumerate(response['context']):
|
109 |
+
st.write(doc.page_content)
|
110 |
+
st.write("---------------------")
|
111 |
+
|
112 |
+
else:
|
|
|
113 |
st.write("context is not provide in the response")
|