qorgh346
commited on
Commit
•
f11d54b
1
Parent(s):
273c862
1023-commit
Browse files- app.py +160 -0
- htmlTemplates.py +44 -0
- requirements.txt +13 -0
app.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings # General embeddings from HuggingFace models.
|
6 |
+
from langchain.memory import ConversationBufferMemory
|
7 |
+
from langchain.chains import ConversationalRetrievalChain
|
8 |
+
from htmlTemplates import css, bot_template, user_template
|
9 |
+
from langchain.llms import LlamaCpp # For loading transformer models.
|
10 |
+
from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
|
11 |
+
from tempfile import NamedTemporaryFile
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
|
14 |
+
|
15 |
+
def get_pdf_text(pdf_docs):
|
16 |
+
with NamedTemporaryFile() as temp_file:
|
17 |
+
temp_file.write(pdf_docs.getvalue())
|
18 |
+
temp_file.seek(0)
|
19 |
+
pdf_loader = PyPDFLoader(temp_file.name)
|
20 |
+
pdf_doc = pdf_loader.load()
|
21 |
+
return pdf_doc
|
22 |
+
|
23 |
+
|
24 |
+
def get_text_file(docs):
|
25 |
+
with NamedTemporaryFile() as temp_file:
|
26 |
+
temp_file.write(docs.getvalue())
|
27 |
+
temp_file.seek(0)
|
28 |
+
text_loader = TextLoader(temp_file.name)
|
29 |
+
text_doc = text_loader.load()
|
30 |
+
|
31 |
+
return text_doc
|
32 |
+
|
33 |
+
|
34 |
+
def get_csv_file(docs):
|
35 |
+
with NamedTemporaryFile() as temp_file:
|
36 |
+
temp_file.write(docs.getvalue())
|
37 |
+
temp_file.seek(0)
|
38 |
+
text_loader = CSVLoader(temp_file.name)
|
39 |
+
text_doc = text_loader.load()
|
40 |
+
|
41 |
+
return text_doc
|
42 |
+
|
43 |
+
|
44 |
+
def get_json_file(docs):
|
45 |
+
with NamedTemporaryFile() as temp_file:
|
46 |
+
temp_file.write(docs.getvalue())
|
47 |
+
temp_file.seek(0)
|
48 |
+
json_loader = JSONLoader(temp_file.name,
|
49 |
+
jq_schema='.scans[].relationships',
|
50 |
+
text_content=False)
|
51 |
+
json_doc = json_loader.load()
|
52 |
+
|
53 |
+
return json_doc
|
54 |
+
|
55 |
+
|
56 |
+
def get_text_chunks(documents):
|
57 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
58 |
+
chunk_size=1000,
|
59 |
+
chunk_overlap=200,
|
60 |
+
length_function=len
|
61 |
+
)
|
62 |
+
|
63 |
+
documents = text_splitter.split_documents(documents)
|
64 |
+
return documents
|
65 |
+
|
66 |
+
|
67 |
+
def get_vectorstore(text_chunks):
|
68 |
+
# Load the desired embeddings model.
|
69 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
|
70 |
+
model_kwargs={'device': 'cpu'})
|
71 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
72 |
+
return vectorstore
|
73 |
+
|
74 |
+
|
75 |
+
def get_conversation_chain(vectorstore):
|
76 |
+
model_name_or_path = 'TheBloke/Llama-2-7B-chat-GGUF'
|
77 |
+
model_basename = 'llama-2-7b-chat.Q2_K.gguf'
|
78 |
+
model_path = hf_hub_download(repo_id=model_name_or_path, filename=model_basename)
|
79 |
+
|
80 |
+
llm = LlamaCpp(model_path=model_path,
|
81 |
+
n_ctx=4086,
|
82 |
+
input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
|
83 |
+
verbose=True, )
|
84 |
+
memory = ConversationBufferMemory(
|
85 |
+
memory_key='chat_history', return_messages=True)
|
86 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
87 |
+
llm=llm,
|
88 |
+
retriever=vectorstore.as_retriever(),
|
89 |
+
memory=memory
|
90 |
+
)
|
91 |
+
return conversation_chain
|
92 |
+
|
93 |
+
|
94 |
+
def handle_userinput(user_question):
|
95 |
+
print('user_question => ', user_question)
|
96 |
+
response = st.session_state.conversation({'question': user_question})
|
97 |
+
st.session_state.chat_history = response['chat_history']
|
98 |
+
|
99 |
+
for i, message in enumerate(st.session_state.chat_history):
|
100 |
+
if i % 2 == 0:
|
101 |
+
st.write(user_template.replace(
|
102 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
103 |
+
else:
|
104 |
+
st.write(bot_template.replace(
|
105 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
106 |
+
|
107 |
+
|
108 |
+
def main():
|
109 |
+
load_dotenv()
|
110 |
+
st.set_page_config(page_title="Chat with multiple PDFs",
|
111 |
+
page_icon=":books:")
|
112 |
+
st.write(css, unsafe_allow_html=True)
|
113 |
+
|
114 |
+
if "conversation" not in st.session_state:
|
115 |
+
st.session_state.conversation = None
|
116 |
+
if "chat_history" not in st.session_state:
|
117 |
+
st.session_state.chat_history = None
|
118 |
+
|
119 |
+
st.header("Chat with multiple PDFs :books:")
|
120 |
+
user_question = st.text_input("Ask a question about your documents:")
|
121 |
+
if user_question:
|
122 |
+
handle_userinput(user_question)
|
123 |
+
|
124 |
+
with st.sidebar:
|
125 |
+
st.subheader("Your documents")
|
126 |
+
docs = st.file_uploader(
|
127 |
+
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
128 |
+
if st.button("Process"):
|
129 |
+
with st.spinner("Processing"):
|
130 |
+
# get pdf text
|
131 |
+
doc_list = []
|
132 |
+
|
133 |
+
for file in docs:
|
134 |
+
print('file - type : ', file.type)
|
135 |
+
if file.type == 'text/plain':
|
136 |
+
# file is .txt
|
137 |
+
doc_list.extend(get_text_file(file))
|
138 |
+
elif file.type in ['application/octet-stream', 'application/pdf']:
|
139 |
+
# file is .pdf
|
140 |
+
doc_list.extend(get_pdf_text(file))
|
141 |
+
elif file.type == 'text/csv':
|
142 |
+
# file is .csv
|
143 |
+
doc_list.extend(get_csv_file(file))
|
144 |
+
elif file.type == 'application/json':
|
145 |
+
# file is .json
|
146 |
+
doc_list.extend(get_json_file(file))
|
147 |
+
|
148 |
+
# get the text chunks
|
149 |
+
text_chunks = get_text_chunks(doc_list)
|
150 |
+
|
151 |
+
# create vector store
|
152 |
+
vectorstore = get_vectorstore(text_chunks)
|
153 |
+
|
154 |
+
# create conversation chain
|
155 |
+
st.session_state.conversation = get_conversation_chain(
|
156 |
+
vectorstore)
|
157 |
+
|
158 |
+
|
159 |
+
if __name__ == '__main__':
|
160 |
+
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://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.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
llama-cpp-python
|
3 |
+
PyPDF2==3.0.1
|
4 |
+
faiss-cpu==1.7.4
|
5 |
+
ctransformers
|
6 |
+
pypdf
|
7 |
+
chromadb
|
8 |
+
tiktoken
|
9 |
+
pysqlite3-binary
|
10 |
+
streamlit-extras
|
11 |
+
InstructorEmbedding
|
12 |
+
sentence-transformers
|
13 |
+
jq
|