ikhlamal commited on
Commit
62b15b6
·
1 Parent(s): cc202bb

first commit

Browse files
Files changed (3) hide show
  1. app.py +99 -0
  2. htmltemp.py +44 -0
  3. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ api_key = st.secrets['api_key']
13
+
14
+ def main():
15
+ load_dotenv()
16
+ st.set_page_config(page_title="PDF Chatbot")
17
+ st.image("https://huggingface.co/spaces/wiwaaw/summary/resolve/main/banner.png")
18
+
19
+ if "conversation" not in st.session_state:
20
+ st.session_state.conversation = None
21
+ if "chat_history" not in st.session_state:
22
+ st.session_state.chat_history = None
23
+
24
+ st.title("Chat with Multiple PDFs using FLAN-T5")
25
+ user_question = st.text_input("Ask a question about your documents:")
26
+ if user_question:
27
+ handle_userinput(user_question)
28
+
29
+ with st.sidebar:
30
+ st.subheader("Your PDFs")
31
+ pdf_docs = st.file_uploader(
32
+ "Upload your PDFs here", accept_multiple_files=True
33
+ )
34
+ if st.button("Process"):
35
+ with st.spinner("Processing"):
36
+ #get pdf text
37
+ raw_text = get_pdf_text(pdf_docs)
38
+
39
+ #get text chunks
40
+ text_chunks = get_text_chunks(raw_text)
41
+
42
+ #create vector store
43
+ vectorstore = get_vectorstore(text_chunks)
44
+
45
+ #create conversation chain
46
+ st.session_state.conversation = get_conversation_chain(vectorstore)
47
+ st.success("file uploaded")
48
+
49
+ def get_pdf_text(pdf_docs):
50
+ text = ""
51
+ for pdf in pdf_docs:
52
+ pdf_reader = PdfReader(pdf)
53
+ for page in pdf_reader.pages:
54
+ text += page.extract_text()
55
+ return text
56
+
57
+ def get_text_chunks(text):
58
+ text_splitter = RecursiveCharacterTextSplitter(
59
+ separators = ["\n\n", "\n", "."], chunk_size=900, chunk_overlap=200, length_function=len
60
+ )
61
+ chunks = text_splitter.split_text(text)
62
+ return chunks
63
+
64
+ def get_vectorstore(text_chunks):
65
+ embeddings = HuggingFaceBgeEmbeddings(model_name="BAAI/bge-base-en-v1.5")
66
+ vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
67
+ return vectorstore
68
+
69
+ def get_conversation_chain(vectorestore):
70
+ llm = HuggingFaceHub(
71
+ repo_id="google/flan-t5-large",
72
+ model_kwargs={"temperature": 0.5, "max_length": 1024},
73
+ huggingfacehub_api_token=api_key
74
+ )
75
+
76
+ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
77
+ conversation_chain = ConversationalRetrievalChain.from_llm(
78
+ llm = llm, retriever = vectorestore.as_retriever(), memory=memory
79
+ )
80
+ return conversation_chain
81
+
82
+ def handle_userinput(user_question):
83
+ response = st.session_state.conversation({"question": user_question})
84
+ st.session_state.chat_history = response["chat_history"]
85
+
86
+ for i, message in enumerate(st.session_state.chat_history):
87
+ if i%2 ==0:
88
+ st.write(
89
+ user_template.replace("{{MSG}}", message.content),
90
+ unsafe_allow_html=True
91
+ )
92
+ else:
93
+ st.write(
94
+ bot_template.replace("{{MSG}}", message.content),
95
+ unsafe_allow_html=True
96
+ )
97
+
98
+ if __name__ == "__main__":
99
+ 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
+ '''
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ python-dotenv
2
+ PyPDF2
3
+ langchain
4
+ sentence_transformers
5
+ transformers
6
+ faiss-cpu
7
+ htmltemplate