Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app (1).py +194 -0
- gitattributes +35 -0
- htmlTemplates (1).py +44 -0
- requirements (1).txt +16 -0
app (1).py
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Question Answering with Retrieval QA and LangChain Language Models featuring FAISS vector stores.
|
3 |
+
This script uses the LangChain Language Model API to answer questions using Retrieval QA
|
4 |
+
and FAISS vector stores. It also uses the Mistral huggingface inference endpoint to
|
5 |
+
generate responses.
|
6 |
+
"""
|
7 |
+
|
8 |
+
import os
|
9 |
+
import streamlit as st
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
from PyPDF2 import PdfReader
|
12 |
+
from langchain.text_splitter import CharacterTextSplitter
|
13 |
+
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
14 |
+
from langchain.vectorstores import FAISS
|
15 |
+
from langchain.chat_models import ChatOpenAI
|
16 |
+
from langchain.memory import ConversationBufferMemory
|
17 |
+
from langchain.chains import ConversationalRetrievalChain
|
18 |
+
from htmlTemplates import css, bot_template, user_template
|
19 |
+
from langchain.llms import HuggingFaceHub
|
20 |
+
|
21 |
+
|
22 |
+
def get_pdf_text(pdf_docs):
|
23 |
+
"""
|
24 |
+
Extract text from a list of PDF documents.
|
25 |
+
|
26 |
+
Parameters
|
27 |
+
----------
|
28 |
+
pdf_docs : list
|
29 |
+
List of PDF documents to extract text from.
|
30 |
+
|
31 |
+
Returns
|
32 |
+
-------
|
33 |
+
str
|
34 |
+
Extracted text from all the PDF documents.
|
35 |
+
|
36 |
+
"""
|
37 |
+
text = ""
|
38 |
+
for pdf in pdf_docs:
|
39 |
+
pdf_reader = PdfReader(pdf)
|
40 |
+
for page in pdf_reader.pages:
|
41 |
+
text += page.extract_text()
|
42 |
+
return text
|
43 |
+
|
44 |
+
|
45 |
+
def get_text_chunks(text):
|
46 |
+
"""
|
47 |
+
Split the input text into chunks.
|
48 |
+
|
49 |
+
Parameters
|
50 |
+
----------
|
51 |
+
text : str
|
52 |
+
The input text to be split.
|
53 |
+
|
54 |
+
Returns
|
55 |
+
-------
|
56 |
+
list
|
57 |
+
List of text chunks.
|
58 |
+
|
59 |
+
"""
|
60 |
+
text_splitter = CharacterTextSplitter(
|
61 |
+
separator="\n", chunk_size=1500, chunk_overlap=300, length_function=len
|
62 |
+
)
|
63 |
+
chunks = text_splitter.split_text(text)
|
64 |
+
return chunks
|
65 |
+
|
66 |
+
|
67 |
+
def get_vectorstore(text_chunks):
|
68 |
+
"""
|
69 |
+
Generate a vector store from a list of text chunks using HuggingFace BgeEmbeddings.
|
70 |
+
|
71 |
+
Parameters
|
72 |
+
----------
|
73 |
+
text_chunks : list
|
74 |
+
List of text chunks to be embedded.
|
75 |
+
|
76 |
+
Returns
|
77 |
+
-------
|
78 |
+
FAISS
|
79 |
+
A FAISS vector store containing the embeddings of the text chunks.
|
80 |
+
|
81 |
+
"""
|
82 |
+
model = "BAAI/bge-base-en-v1.5"
|
83 |
+
encode_kwargs = {
|
84 |
+
"normalize_embeddings": True
|
85 |
+
} # set True to compute cosine similarity
|
86 |
+
embeddings = HuggingFaceBgeEmbeddings(
|
87 |
+
model_name=model, encode_kwargs=encode_kwargs, model_kwargs={"device": "cpu"}
|
88 |
+
)
|
89 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
90 |
+
return vectorstore
|
91 |
+
|
92 |
+
|
93 |
+
def get_conversation_chain(vectorstore):
|
94 |
+
"""
|
95 |
+
Create a conversational retrieval chain using a vector store and a language model.
|
96 |
+
|
97 |
+
Parameters
|
98 |
+
----------
|
99 |
+
vectorstore : FAISS
|
100 |
+
A FAISS vector store containing the embeddings of the text chunks.
|
101 |
+
|
102 |
+
Returns
|
103 |
+
-------
|
104 |
+
ConversationalRetrievalChain
|
105 |
+
A conversational retrieval chain for generating responses.
|
106 |
+
|
107 |
+
"""
|
108 |
+
llm = HuggingFaceHub(
|
109 |
+
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
110 |
+
model_kwargs={"temperature": 0.5, "max_length": 1048},
|
111 |
+
)
|
112 |
+
# llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
|
113 |
+
|
114 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
115 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
116 |
+
llm=llm, retriever=vectorstore.as_retriever(), memory=memory
|
117 |
+
)
|
118 |
+
return conversation_chain
|
119 |
+
|
120 |
+
|
121 |
+
def handle_userinput(user_question):
|
122 |
+
"""
|
123 |
+
Handle user input and generate a response using the conversational retrieval chain.
|
124 |
+
Parameters
|
125 |
+
----------
|
126 |
+
user_question : str
|
127 |
+
The user's question.
|
128 |
+
"""
|
129 |
+
response = st.session_state.conversation({"question": user_question})
|
130 |
+
st.session_state.chat_history = response["chat_history"]
|
131 |
+
|
132 |
+
for i, message in enumerate(st.session_state.chat_history):
|
133 |
+
if i % 2 == 0:
|
134 |
+
st.write("//_^ User: " + message.content)
|
135 |
+
else:
|
136 |
+
st.write("🤖 ChatBot: " + message.content)
|
137 |
+
|
138 |
+
|
139 |
+
def main():
|
140 |
+
"""
|
141 |
+
Putting it all together.
|
142 |
+
"""
|
143 |
+
st.set_page_config(
|
144 |
+
page_title="Chat with a Bot that tries to answer questions about multiple PDFs",
|
145 |
+
page_icon=":books:",
|
146 |
+
)
|
147 |
+
|
148 |
+
st.markdown("# Chat with a Bot")
|
149 |
+
st.markdown("This bot tries to answer questions about multiple PDFs. Let the processing of the PDF finish before adding your question. 🙏🏾")
|
150 |
+
|
151 |
+
st.write(css, unsafe_allow_html=True)
|
152 |
+
|
153 |
+
# set huggingface hub token in st.text_input widget
|
154 |
+
# then hide the input
|
155 |
+
huggingface_token = st.text_input("Enter your HuggingFace Hub token", type="password")
|
156 |
+
#openai_api_key = st.text_input("Enter your OpenAI API key", type="password")
|
157 |
+
|
158 |
+
# set this key as an environment variable
|
159 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = huggingface_token
|
160 |
+
#os.environ["OPENAI_API_KEY"] = openai_api_key
|
161 |
+
|
162 |
+
|
163 |
+
if "conversation" not in st.session_state:
|
164 |
+
st.session_state.conversation = None
|
165 |
+
if "chat_history" not in st.session_state:
|
166 |
+
st.session_state.chat_history = None
|
167 |
+
|
168 |
+
st.header("Chat with a Bot 🤖��� that tries to answer questions about multiple PDFs :books:")
|
169 |
+
user_question = st.text_input("Ask a question about your documents:")
|
170 |
+
if user_question:
|
171 |
+
handle_userinput(user_question)
|
172 |
+
|
173 |
+
with st.sidebar:
|
174 |
+
st.subheader("Your documents")
|
175 |
+
pdf_docs = st.file_uploader(
|
176 |
+
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True
|
177 |
+
)
|
178 |
+
if st.button("Process"):
|
179 |
+
with st.spinner("Processing"):
|
180 |
+
# get pdf text
|
181 |
+
raw_text = get_pdf_text(pdf_docs)
|
182 |
+
|
183 |
+
# get the text chunks
|
184 |
+
text_chunks = get_text_chunks(raw_text)
|
185 |
+
|
186 |
+
# create vector store
|
187 |
+
vectorstore = get_vectorstore(text_chunks)
|
188 |
+
|
189 |
+
# create conversation chain
|
190 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
191 |
+
|
192 |
+
|
193 |
+
if __name__ == "__main__":
|
194 |
+
main()
|
gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
htmlTemplates (1).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/cN0nmSj/Screenshot-2023-05-28-at-02-37-21.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://i.ibb.co/rdZC7LZ/Photo-logo-1.png">
|
41 |
+
</div>
|
42 |
+
<div class="message">{{MSG}}</div>
|
43 |
+
</div>
|
44 |
+
"""
|
requirements (1).txt
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.0.335
|
2 |
+
PyPDF2==3.0.1
|
3 |
+
python-dotenv==1.0.0
|
4 |
+
streamlit==1.28.2
|
5 |
+
openai==1.2.4
|
6 |
+
faiss-cpu==1.7.4
|
7 |
+
altair==5.1.2
|
8 |
+
tiktoken==0.5.1
|
9 |
+
black==23.11.0
|
10 |
+
# uncomment to use huggingface llms
|
11 |
+
huggingface-hub==0.17.3
|
12 |
+
|
13 |
+
# uncomment to use instructor embeddings
|
14 |
+
InstructorEmbedding==1.0.1
|
15 |
+
sentence-transformers==2.2.2
|
16 |
+
transformers
|