Spaces:
Sleeping
Sleeping
sankar12345
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,104 +1,111 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
from
|
4 |
-
from
|
5 |
-
from langchain.
|
6 |
-
from langchain.
|
7 |
-
from langchain.
|
8 |
-
from langchain.
|
9 |
-
from langchain.
|
10 |
-
from
|
11 |
-
from
|
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 |
-
return
|
38 |
-
|
39 |
-
|
40 |
-
def
|
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 |
-
st.
|
79 |
-
|
80 |
-
if
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
main()
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from PyPDF2 import PdfReader
|
5 |
+
from langchain.text_splitter import CharacterTextSplitter
|
6 |
+
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain.chat_models import ChatOpenAI
|
9 |
+
from langchain.memory import ConversationBufferMemory
|
10 |
+
from langchain.chains import ConversationalRetrievalChain
|
11 |
+
from htmlTemplates import css, bot_template, user_template
|
12 |
+
from langchain.llms import HuggingFaceHub
|
13 |
+
|
14 |
+
# Load environment variables from .env file
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
# Get Hugging Face token from environment variables
|
18 |
+
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
19 |
+
|
20 |
+
def get_pdf_text(pdf_docs):
|
21 |
+
text = ""
|
22 |
+
for pdf in pdf_docs:
|
23 |
+
pdf_reader = PdfReader(pdf)
|
24 |
+
for page in pdf_reader.pages:
|
25 |
+
text += page.extract_text()
|
26 |
+
return text
|
27 |
+
|
28 |
+
|
29 |
+
def get_text_chunks(text):
|
30 |
+
text_splitter = CharacterTextSplitter(
|
31 |
+
separator="\n",
|
32 |
+
chunk_size=1000,
|
33 |
+
chunk_overlap=200,
|
34 |
+
length_function=len
|
35 |
+
)
|
36 |
+
chunks = text_splitter.split_text(text)
|
37 |
+
return chunks
|
38 |
+
|
39 |
+
|
40 |
+
def get_vectorstore(text_chunks):
|
41 |
+
# embeddings = OpenAIEmbeddings()
|
42 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
|
43 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
44 |
+
return vectorstore
|
45 |
+
|
46 |
+
|
47 |
+
def get_conversation_chain(vectorstore):
|
48 |
+
# llm = ChatOpenAI()
|
49 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
|
50 |
+
|
51 |
+
memory = ConversationBufferMemory(
|
52 |
+
memory_key='chat_history', return_messages=True)
|
53 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
54 |
+
llm=llm,
|
55 |
+
retriever=vectorstore.as_retriever(),
|
56 |
+
memory=memory
|
57 |
+
)
|
58 |
+
return conversation_chain
|
59 |
+
|
60 |
+
|
61 |
+
def handle_userinput(user_question):
|
62 |
+
response = st.session_state.conversation({'question': user_question})
|
63 |
+
st.session_state.chat_history = response['chat_history']
|
64 |
+
|
65 |
+
for i, message in enumerate(st.session_state.chat_history):
|
66 |
+
if i % 2 == 0:
|
67 |
+
st.write(user_template.replace(
|
68 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
69 |
+
else:
|
70 |
+
st.write(bot_template.replace(
|
71 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
72 |
+
|
73 |
+
|
74 |
+
def main():
|
75 |
+
load_dotenv()
|
76 |
+
st.set_page_config(page_title="Chat with multiple PDFs",
|
77 |
+
page_icon=":books:")
|
78 |
+
st.write(css, unsafe_allow_html=True)
|
79 |
+
|
80 |
+
if "conversation" not in st.session_state:
|
81 |
+
st.session_state.conversation = None
|
82 |
+
if "chat_history" not in st.session_state:
|
83 |
+
st.session_state.chat_history = None
|
84 |
+
|
85 |
+
st.header("Chat with multiple PDFs :books:")
|
86 |
+
user_question = st.text_input("Ask a question about your documents:")
|
87 |
+
if user_question:
|
88 |
+
handle_userinput(user_question)
|
89 |
+
|
90 |
+
with st.sidebar:
|
91 |
+
st.subheader("Your documents")
|
92 |
+
pdf_docs = st.file_uploader(
|
93 |
+
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
94 |
+
if st.button("Process"):
|
95 |
+
with st.spinner("Processing"):
|
96 |
+
# get pdf text
|
97 |
+
raw_text = get_pdf_text(pdf_docs)
|
98 |
+
|
99 |
+
# get the text chunks
|
100 |
+
text_chunks = get_text_chunks(raw_text)
|
101 |
+
|
102 |
+
# create vector store
|
103 |
+
vectorstore = get_vectorstore(text_chunks)
|
104 |
+
|
105 |
+
# create conversation chain
|
106 |
+
st.session_state.conversation = get_conversation_chain(
|
107 |
+
vectorstore)
|
108 |
+
|
109 |
+
|
110 |
+
if __name__ == '__main__':
|
111 |
main()
|