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