Arhashmi commited on
Commit
137b962
1 Parent(s): a759dbe

Upload 3 files

Browse files
Files changed (3) hide show
  1. app1.py +102 -0
  2. htmlTemplates.py +44 -0
  3. requirements.txt +0 -0
app1.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 HuggingFaceInstructEmbeddings
6
+ from langchain.vectorstores import FAISS
7
+ from langchain.memory import ConversationBufferMemory
8
+ from langchain.chains import ConversationalRetrievalChain
9
+ from htmlTemplates import css, bot_template, user_template
10
+ from langchain.llms import HuggingFaceHub
11
+
12
+ def get_pdf_text(pdf_docs):
13
+ text = ""
14
+ for pdf in pdf_docs:
15
+ pdf_reader = PdfReader(pdf)
16
+ for page in pdf_reader.pages:
17
+ text += page.extract_text()
18
+ return text
19
+
20
+
21
+ def get_text_chunks(text):
22
+ text_splitter = RecursiveCharacterTextSplitter(
23
+ chunk_size=900,
24
+ chunk_overlap=0,
25
+ separators="\n",
26
+ add_start_index = True,
27
+ length_function= len
28
+ )
29
+ chunks = text_splitter.split_text(text)
30
+ return chunks
31
+
32
+
33
+ def get_vectorstore(text_chunks):
34
+ embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
35
+ vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
36
+ return vectorstore
37
+
38
+
39
+ def get_conversation_chain(vectorstore):
40
+ llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.2, "max_length":1024})
41
+
42
+ memory = ConversationBufferMemory(
43
+ memory_key='chat_history', return_messages=True)
44
+ conversation_chain = ConversationalRetrievalChain.from_llm(
45
+ llm=llm,
46
+ retriever=vectorstore.as_retriever(),
47
+ memory=memory
48
+ )
49
+ return conversation_chain
50
+
51
+
52
+ def handle_userinput(user_question):
53
+ response = st.session_state.conversation({'question': user_question})
54
+ st.session_state.chat_history = response['chat_history']
55
+
56
+ for i, message in enumerate(st.session_state.chat_history):
57
+ if i % 2 == 0:
58
+ st.write(user_template.replace(
59
+ "{{MSG}}", message.content), unsafe_allow_html=True)
60
+ else:
61
+ st.write(bot_template.replace(
62
+ "{{MSG}}", message.content), unsafe_allow_html=True)
63
+
64
+
65
+ def main():
66
+ load_dotenv()
67
+ st.set_page_config(page_title="ChatBot",
68
+ page_icon=":books:")
69
+ st.write(css, unsafe_allow_html=True)
70
+
71
+ if "conversation" not in st.session_state:
72
+ st.session_state.conversation = None
73
+ if "chat_history" not in st.session_state:
74
+ st.session_state.chat_history = None
75
+
76
+ st.header("Chat Bot")
77
+ user_question = st.text_input("Ask a question:")
78
+ if user_question:
79
+ handle_userinput(user_question)
80
+
81
+ with st.sidebar:
82
+ st.subheader("Your documents")
83
+ pdf_docs = st.file_uploader(
84
+ "Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
85
+ if st.button("Process"):
86
+ with st.spinner("Processing"):
87
+ # get pdf text
88
+ raw_text = get_pdf_text(pdf_docs)
89
+
90
+ # get the text chunks
91
+ text_chunks = get_text_chunks(raw_text)
92
+
93
+ # create vector store
94
+ vectorstore = get_vectorstore(text_chunks)
95
+
96
+ # create conversation chain
97
+ st.session_state.conversation = get_conversation_chain(
98
+ vectorstore)
99
+
100
+
101
+ if __name__ == '__main__':
102
+ main()
htmlTemplates.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://cdn1.iconfinder.com/data/icons/customer-service-66/32/chatbot_chat_bot_assistant_chat_laptop-56-64.png" style="max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;">
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://cdn0.iconfinder.com/data/icons/leto-ui-generic-1/64/leto-04-64.png">
41
+ </div>
42
+ <div class="message">{{MSG}}</div>
43
+ </div>
44
+ '''
requirements.txt ADDED
Binary file (3.38 kB). View file