Manasa1 commited on
Commit
0bff161
ยท
verified ยท
1 Parent(s): 9618ca5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -98
app.py CHANGED
@@ -1,98 +1,98 @@
1
- from dotenv import load_dotenv
2
- import streamlit as st
3
- from langchain_community.document_loaders import UnstructuredPDFLoader
4
- from langchain_text_splitters.character import CharacterTextSplitter
5
- from langchain_community.vectorstores import FAISS
6
- from langchain_community.embeddings import HuggingFaceEmbeddings
7
- from langchain_groq import ChatGroq
8
- from langchain.memory import ConversationBufferMemory
9
- from langchain.chains import ConversationalRetrievalChain
10
- import os
11
- import nltk
12
- nltk.download('punkt')
13
- nltk_data_dir = os.getenv("NLTK_DATA")
14
-
15
-
16
- load_dotenv()
17
-
18
- working_dir = os.path.dirname(os.path.abspath(__file__))
19
-
20
- def load_documents(file_path):
21
- loader = UnstructuredPDFLoader(file_path)
22
- documents = loader.load()
23
- return documents
24
-
25
- def setup_vectorstore(documents):
26
- embeddings = HuggingFaceEmbeddings
27
- text_splitter = CharacterTextSplitter(
28
- separator="/n",
29
- chunk_size = 1000,
30
- chunk_overlap = 200
31
- )
32
- doc_chunks = text_splitter.split_documents(documents)
33
- vectorstores = FAISS.from_documents(doc_chunks,embeddings)
34
- return vectorstores
35
-
36
- def create_chain(vectorstores):
37
- llm = ChatGroq(
38
- model="llama-3.1-70b-versatile",
39
- temperature=0
40
- )
41
- retriever = vectorstores.as_retriever()
42
- memory = ConversationBufferMemory(
43
- llm = llm,
44
- output_key= "answer",
45
- memory_key = "chat_history",
46
- return_messages=True
47
-
48
- )
49
- chain = ConversationalRetrievalChain.from_llm(
50
- llm = llm,
51
- retriever = retriever,
52
- memory = memory,
53
- verbose = True
54
- )
55
- return chain
56
-
57
- st.set_page_config(
58
- page_title= "Chat with your documents",
59
- page_icon= "๐Ÿ“‘",
60
- layout="centered"
61
-
62
- )
63
-
64
- st.title("๐Ÿ“Chat With your docs ๐Ÿ˜Ž")
65
-
66
- if "chat_history" not in st.session_state:
67
- st.session_state.chat_history = []
68
-
69
- uploaded_file = st.file_uploader(label="Upload your PDF")
70
-
71
- if uploaded_file:
72
- file_path = f"{working_dir}{uploaded_file.name}"
73
- with open(file_path,"wb") as f:
74
- f.write(uploaded_file.getbuffer())
75
-
76
- if "vectorstores" not in st.session_state:
77
- st.session_state.vectorstores = setup_vectorstore(load_documents(file_path))
78
-
79
- if "conversation_chain" not in st.session_state:
80
- st.session_state.conversation_chain = create_chain(st.session_state.vectorstores)
81
-
82
-
83
- for message in st.session_state.chat_history:
84
- with st.chat_message(message["role"]):
85
- st.markdown(message["content"])
86
-
87
- user_input = st.chat_input("Ask any questions relevant to uploaded pdf")
88
-
89
- if user_input:
90
- st.session_state.chat_history.append({"role":"user","content":user_input})
91
- with st.chat_message("user"):
92
- st.markdown(user_input)
93
-
94
- with st.chat_message("assistant"):
95
- response = st.session_state.conversation_chain({"question":user_input})
96
- assistant_response = response["answer"]
97
- st.markdown(assistant_response)
98
- st.session_state.chat_history.append({"role":"assistant","content":assistant_response})
 
1
+ from dotenv import load_dotenv
2
+ import streamlit as st
3
+ from langchain_community.document_loaders import UnstructuredPDFLoader
4
+ from langchain_text_splitters.character import CharacterTextSplitter
5
+ from langchain_community.vectorstores import FAISS
6
+ from langchain_community.embeddings import HuggingFaceEmbeddings
7
+ from langchain_groq import ChatGroq
8
+ from langchain.memory import ConversationBufferMemory
9
+ from langchain.chains import ConversationalRetrievalChain
10
+ import os
11
+ import nltk
12
+ nltk.download('punkt')
13
+ nltk_data_dir = os.getenv("NLTK_DATA")
14
+
15
+
16
+ load_dotenv()
17
+
18
+ working_dir = os.path.dirname(os.path.abspath(__file__))
19
+
20
+ def load_documents(file_path):
21
+ loader = UnstructuredPDFLoader(file_path)
22
+ documents = loader.load()
23
+ return documents
24
+
25
+ def setup_vectorstore(documents):
26
+ embeddings = HuggingFaceEmbeddings()
27
+ text_splitter = CharacterTextSplitter(
28
+ separator="/n",
29
+ chunk_size = 1000,
30
+ chunk_overlap = 200
31
+ )
32
+ doc_chunks = text_splitter.split_documents(documents)
33
+ vectorstores = FAISS.from_documents(doc_chunks,embeddings)
34
+ return vectorstores
35
+
36
+ def create_chain(vectorstores):
37
+ llm = ChatGroq(
38
+ model="llama-3.1-70b-versatile",
39
+ temperature=0
40
+ )
41
+ retriever = vectorstores.as_retriever()
42
+ memory = ConversationBufferMemory(
43
+ llm = llm,
44
+ output_key= "answer",
45
+ memory_key = "chat_history",
46
+ return_messages=True
47
+
48
+ )
49
+ chain = ConversationalRetrievalChain.from_llm(
50
+ llm = llm,
51
+ retriever = retriever,
52
+ memory = memory,
53
+ verbose = True
54
+ )
55
+ return chain
56
+
57
+ st.set_page_config(
58
+ page_title= "Chat with your documents",
59
+ page_icon= "๐Ÿ“‘",
60
+ layout="centered"
61
+
62
+ )
63
+
64
+ st.title("๐Ÿ“Chat With your docs ๐Ÿ˜Ž")
65
+
66
+ if "chat_history" not in st.session_state:
67
+ st.session_state.chat_history = []
68
+
69
+ uploaded_file = st.file_uploader(label="Upload your PDF")
70
+
71
+ if uploaded_file:
72
+ file_path = f"{working_dir}{uploaded_file.name}"
73
+ with open(file_path,"wb") as f:
74
+ f.write(uploaded_file.getbuffer())
75
+
76
+ if "vectorstores" not in st.session_state:
77
+ st.session_state.vectorstores = setup_vectorstore(load_documents(file_path))
78
+
79
+ if "conversation_chain" not in st.session_state:
80
+ st.session_state.conversation_chain = create_chain(st.session_state.vectorstores)
81
+
82
+
83
+ for message in st.session_state.chat_history:
84
+ with st.chat_message(message["role"]):
85
+ st.markdown(message["content"])
86
+
87
+ user_input = st.chat_input("Ask any questions relevant to uploaded pdf")
88
+
89
+ if user_input:
90
+ st.session_state.chat_history.append({"role":"user","content":user_input})
91
+ with st.chat_message("user"):
92
+ st.markdown(user_input)
93
+
94
+ with st.chat_message("assistant"):
95
+ response = st.session_state.conversation_chain({"question":user_input})
96
+ assistant_response = response["answer"]
97
+ st.markdown(assistant_response)
98
+ st.session_state.chat_history.append({"role":"assistant","content":assistant_response})