prthgo commited on
Commit
c2ec0ab
1 Parent(s): f1d396e

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +106 -0
  2. htmltemp.py +44 -0
  3. requirments.txt +5 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from dotenv import load_dotenv
3
+ from PyPDF2 import PdfReader
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain.embeddings import HuggingFaceBgeEmbeddings
6
+ from langchain.vectorstores import FAISS
7
+ from langchain.memory import ConversationBufferMemory
8
+ from langchain.chains import ConversationalRetrievalChain
9
+ from htmltemp import css, bot_template, user_template
10
+ from langchain.llms import HuggingFaceHub
11
+
12
+
13
+ def main():
14
+ load_dotenv()
15
+ st.set_page_config(page_title="PDF Chatbot", page_icon="📚")
16
+ st.write(css, unsafe_allow_html=True)
17
+
18
+ if "conversation" not in st.session_state:
19
+ st.session_state.conversation = None
20
+ if "chat_history" not in st.session_state:
21
+ st.session_state.chat_history = None
22
+
23
+ st.header("Chat with your PDFs 📚")
24
+ user_question = st.text_input("Ask a question about your documents:")
25
+ if user_question:
26
+ handle_userinput(user_question)
27
+
28
+ with st.sidebar:
29
+ st.sidebar.info("""Note: I haven't used any GPU for this project so It can take
30
+ long time to process large PDFs. Also this is POC project and can be easily upgraded
31
+ with better model and resources. """)
32
+
33
+ st.subheader("Your PDFs")
34
+ pdf_docs = st.file_uploader(
35
+ "Upload your PDFs here", accept_multiple_files=True
36
+ )
37
+ if st.button("Process"):
38
+ with st.spinner("Processing"):
39
+ # get pdf text
40
+ raw_text = get_pdf_text(pdf_docs)
41
+
42
+ # get the text chunks
43
+ text_chunks = get_text_chunks(raw_text)
44
+
45
+ # create vector store
46
+ vectorstore = get_vectorstore(text_chunks)
47
+
48
+ # create conversation chain
49
+ st.session_state.conversation = get_conversation_chain(vectorstore)
50
+
51
+
52
+ def get_pdf_text(pdf_docs):
53
+ text = ""
54
+ for pdf in pdf_docs:
55
+ pdf_reader = PdfReader(pdf)
56
+ for page in pdf_reader.pages:
57
+ text += page.extract_text()
58
+ return text
59
+
60
+
61
+ def get_text_chunks(text):
62
+ text_splitter = RecursiveCharacterTextSplitter(
63
+ separators=["\n\n", "\n", "."], chunk_size=900, chunk_overlap=200, length_function=len
64
+ )
65
+ chunks = text_splitter.split_text(text)
66
+ return chunks
67
+
68
+
69
+ def get_vectorstore(text_chunks):
70
+ embeddings = HuggingFaceBgeEmbeddings(model_name="BAAI/bge-base-en-v1.5")
71
+ vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
72
+ return vectorstore
73
+
74
+
75
+ def get_conversation_chain(vectorstore):
76
+ llm = HuggingFaceHub(
77
+ repo_id="google/flan-t5-xxl",
78
+ model_kwargs={"temperature": 0.5, "max_length": 1024},
79
+
80
+ )
81
+
82
+ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
83
+ conversation_chain = ConversationalRetrievalChain.from_llm(
84
+ llm=llm, retriever=vectorstore.as_retriever(), memory=memory
85
+ )
86
+ return conversation_chain
87
+
88
+
89
+ def handle_userinput(user_question):
90
+ response = st.session_state.conversation({"question": user_question})
91
+ st.session_state.chat_history = response["chat_history"]
92
+
93
+ for i, message in enumerate(st.session_state.chat_history):
94
+ if i % 2 == 0:
95
+ st.write(
96
+ user_template.replace("{{MSG}}", message.content),
97
+ unsafe_allow_html=True,
98
+ )
99
+ else:
100
+ st.write(
101
+ bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True
102
+ )
103
+
104
+
105
+ if __name__ == "__main__":
106
+ main()
htmltemp.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ css = '''
2
+ <style>
3
+ .chat-message {
4
+ padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
5
+ }
6
+ .chat-message.user {
7
+ background-color: #2b313e
8
+ }
9
+ .chat-message.bot {
10
+ background-color: #475063
11
+ }
12
+ .chat-message .avatar {
13
+ width: 20%;
14
+ }
15
+ .chat-message .avatar img {
16
+ max-width: 78px;
17
+ max-height: 78px;
18
+ border-radius: 50%;
19
+ object-fit: cover;
20
+ }
21
+ .chat-message .message {
22
+ width: 80%;
23
+ padding: 0 1.5rem;
24
+ color: #fff;
25
+ }
26
+ '''
27
+
28
+ bot_template = '''
29
+ <div class="chat-message bot">
30
+ <div class="avatar">
31
+ <img src="https://i.ibb.co/1shjLH8/chat-bot.jpg">
32
+ </div>
33
+ <div class="message">{{MSG}}</div>
34
+ </div>
35
+ '''
36
+
37
+ user_template = '''
38
+ <div class="chat-message user">
39
+ <div class="avatar">
40
+ <img src="https://i.ibb.co/D8gZPkX/f1624829-f27e-4d21-b691-43cff60c0539.jpg" >
41
+ </div>
42
+ <div class="message">{{MSG}}</div>
43
+ </div>
44
+ '''
requirments.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ python-dotenv
2
+ PyPDF2
3
+ langchain
4
+ sentence_transformers
5
+ faiss-cpu