Spaces:
Runtime error
Runtime error
Commit ·
d6626e6
0
Parent(s):
Duplicate from Graceliying/pdf_chat
Browse filesCo-authored-by: Grace Li <Graceliying@users.noreply.huggingface.co>
- .gitattributes +36 -0
- README.md +13 -0
- Toastmasters-CC-Manual.pdf +3 -0
- app.py +120 -0
- htmlTemplates.py +55 -0
- requirements.txt +10 -0
.gitattributes
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 36 |
+
Toastmasters-CC-Manual.pdf filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Pdf Chat
|
| 3 |
+
emoji: 📉
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.21.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
duplicated_from: Graceliying/pdf_chat
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
Toastmasters-CC-Manual.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ec9fdec57c22ead344bac5319e0097990f20df8660285b75256c81427817ace8
|
| 3 |
+
size 5989158
|
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from PyPDF2 import PdfReader
|
| 6 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 7 |
+
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
|
| 8 |
+
from langchain.vectorstores import FAISS
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 11 |
+
from langchain.chat_models import ChatOpenAI
|
| 12 |
+
from htmlTemplates import css, bot_template, user_template
|
| 13 |
+
from PIL import Image
|
| 14 |
+
|
| 15 |
+
def get_pdf_text(pdf_docs):
|
| 16 |
+
text = ""
|
| 17 |
+
for pdf in pdf_docs:
|
| 18 |
+
pdf_reader = PdfReader(pdf)
|
| 19 |
+
for page in pdf_reader.pages:
|
| 20 |
+
text += page.extract_text()
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
# documentation for CharacterTextSplitter:
|
| 24 |
+
# https://python.langchain.com/en/latest/modules/indexes/text_splitters/examples/character_text_splitter.html
|
| 25 |
+
def get_text_chunk(text):
|
| 26 |
+
text_splitter = CharacterTextSplitter(
|
| 27 |
+
separator="\n",
|
| 28 |
+
chunk_size = 1000,
|
| 29 |
+
chunk_overlap = 200,
|
| 30 |
+
length_function = len
|
| 31 |
+
)
|
| 32 |
+
chunks = text_splitter.split_text(text)
|
| 33 |
+
return chunks
|
| 34 |
+
|
| 35 |
+
#embedding using openAI embedding. Warn: This will cost you money
|
| 36 |
+
def get_vectorstore_openAI(text_chunks):
|
| 37 |
+
embeddings = OpenAIEmbeddings()
|
| 38 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 39 |
+
return vectorstore
|
| 40 |
+
|
| 41 |
+
#embedding using instructor-xl with your local machine for free
|
| 42 |
+
#you can find more details at: https://huggingface.co/hkunlp/instructor-xl
|
| 43 |
+
def get_vectorstore(text_chunks):
|
| 44 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
|
| 45 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 46 |
+
return vectorstore
|
| 47 |
+
|
| 48 |
+
def get_conversation_chain(vectorstore):
|
| 49 |
+
llm = ChatOpenAI()
|
| 50 |
+
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
| 51 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 52 |
+
llm=llm,
|
| 53 |
+
retriever=vectorstore.as_retriever(),
|
| 54 |
+
memory = memory
|
| 55 |
+
)
|
| 56 |
+
return conversation_chain
|
| 57 |
+
|
| 58 |
+
def handle_userinput(user_question):
|
| 59 |
+
response = st.session_state.conversation({'question': user_question})
|
| 60 |
+
st.session_state.chat_history = response['chat_history']
|
| 61 |
+
|
| 62 |
+
for i, message in enumerate(st.session_state.chat_history):
|
| 63 |
+
if i%2 == 0:
|
| 64 |
+
st.write(user_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
|
| 65 |
+
else:
|
| 66 |
+
st.write(bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
|
| 67 |
+
|
| 68 |
+
def main():
|
| 69 |
+
##############################################################################
|
| 70 |
+
#load openai api_key from .evn
|
| 71 |
+
load_dotenv()
|
| 72 |
+
#openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 73 |
+
|
| 74 |
+
##############################################################################
|
| 75 |
+
#set up basic page
|
| 76 |
+
st.set_page_config(page_title="Chat With multiple PDFs", page_icon=":books:")
|
| 77 |
+
st.write(css, unsafe_allow_html=True)
|
| 78 |
+
|
| 79 |
+
#initial session_state in order to avoid refresh
|
| 80 |
+
if "conversation" not in st.session_state:
|
| 81 |
+
st.session_state.conversation = None
|
| 82 |
+
if "chat_history" not in st.session_state:
|
| 83 |
+
st.session_state.chat_history = None
|
| 84 |
+
|
| 85 |
+
st.header("Chat based on PDF you provided :books:")
|
| 86 |
+
user_question = st.text_input("Ask a question about your documents:")
|
| 87 |
+
|
| 88 |
+
if user_question:
|
| 89 |
+
handle_userinput(user_question)
|
| 90 |
+
|
| 91 |
+
# Define the templates
|
| 92 |
+
|
| 93 |
+
with st.sidebar:
|
| 94 |
+
st.subheader("Your PDF documents")
|
| 95 |
+
pdf_docs = st.file_uploader("Upload your pdfs here and click on 'Proces'", accept_multiple_files= True)
|
| 96 |
+
#if the button is pressed
|
| 97 |
+
if st.button("Process"):
|
| 98 |
+
with st.spinner("Processing"):
|
| 99 |
+
#get pdf text
|
| 100 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 101 |
+
print('raw_text is created')
|
| 102 |
+
|
| 103 |
+
#get the text chunks
|
| 104 |
+
text_chunks = get_text_chunk(raw_text)
|
| 105 |
+
print('text_chunks are generated')
|
| 106 |
+
|
| 107 |
+
#create vector store
|
| 108 |
+
vectorstore = get_vectorstore_openAI(text_chunks)
|
| 109 |
+
print('vectorstore is created')
|
| 110 |
+
|
| 111 |
+
#create converstion chain
|
| 112 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
| 113 |
+
print('conversation chain created')
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# to run this application, you need to run "streamlit run app.py"
|
| 119 |
+
if __name__ == '__main__':
|
| 120 |
+
main()
|
htmlTemplates.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 29 |
+
bot_template = '''
|
| 30 |
+
<div class="chat-message bot">
|
| 31 |
+
<div class="avatar">
|
| 32 |
+
<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 32 32"
|
| 33 |
+
style="max-height: 50px; max-width: 50px; border-radius: 50%; object-fit: cover;">
|
| 34 |
+
<g fill="white"><path d="M13.472 26h5.056C19.34 26 20 25.326 20 24.5s-.66-1.5-1.472-1.5h-5.056C12.66 23 12 23.674 12 24.5s.66 1.5 1.472 1.5ZM10.5 10a4.5 4.5 0 1 0 0 9h11a4.5 4.5 0 1 0 0-9h-11Zm.75 2c.69 0 1.25.56 1.25 1.25v2.5a1.25 1.25 0 1 1-2.5 0v-2.5c0-.69.56-1.25 1.25-1.25Zm8.25 1.25a1.25 1.25 0 1 1 2.5 0v2.5a1.25 1.25 0 1 1-2.5 0v-2.5Z"/><path d="M4 4.915a1.5 1.5 0 1 0-1 0v7.355a2 2 0 0 0-1 1.728v7.004c0 .736.403 1.382 1 1.729v1.319A6.945 6.945 0 0 0 9.95 31h12.1A6.943 6.943 0 0 0 29 24.06v-1.39c.597-.347 1-.994 1-1.73v-7.01c0-.736-.403-1.383-1-1.73V5.018a1.55 1.55 0 1 0-1 0v3.396A7.017 7.017 0 0 0 21.98 5h-1.065A1.5 1.5 0 0 0 19.5 3h-7a1.5 1.5 0 0 0-1.415 2H10.03C7.47 5 5.23 6.369 4 8.414v-3.5Zm1 7.115A5.03 5.03 0 0 1 10.03 7h11.95A5.028 5.028 0 0 1 27 12.03v12.03A4.943 4.943 0 0 1 22.05 29H9.95A4.945 4.945 0 0 1 5 24.05V12.03Z"/></g></svg>
|
| 35 |
+
</div>
|
| 36 |
+
<div class="message">{{MSG}}</div>
|
| 37 |
+
</div>
|
| 38 |
+
'''
|
| 39 |
+
|
| 40 |
+
user_template = '''
|
| 41 |
+
<div class="chat-message user">
|
| 42 |
+
<div class="avatar">
|
| 43 |
+
<svg
|
| 44 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 45 |
+
width="256"
|
| 46 |
+
height="256"
|
| 47 |
+
viewBox="0 0 256 256"
|
| 48 |
+
style="max-height: 50px; max-width: 50px; border-radius: 50%; object-fit: cover;">
|
| 49 |
+
<path fill="white" d="M168 96H88a40 40 0 0 0-40 40v8a40 40 0 0 0 40 40h80a40 40 0 0 0 40-40v-8a40 40 0 0 0-40-40Zm24 48a24 24 0 0 1-24 24H88a24 24 0 0 1-24-24v-8a24 24 0 0 1 24-24h80a24 24 0 0 1 24 24Zm16-112a32.06 32.06 0 0 0-31 24H79a32 32 0 0 0-63 8v80a72.08 72.08 0 0 0 72 72h80a72.08 72.08 0 0 0 72-72V64a32 32 0 0 0-32-32Zm16 112a56.06 56.06 0 0 1-56 56H88a56.06 56.06 0 0 1-56-56V64a16 16 0 0 1 32 0a8 8 0 0 0 8 8h112a8 8 0 0 0 8-8a16 16 0 0 1 32 0Zm-120-4a12 12 0 1 1-12-12a12 12 0 0 1 12 12Zm72 0a12 12 0 1 1-12-12a12 12 0 0 1 12 12Z"/>
|
| 50 |
+
</svg>
|
| 51 |
+
Download SVG
|
| 52 |
+
</div>
|
| 53 |
+
<div class="message">{{MSG}}</div>
|
| 54 |
+
</div>
|
| 55 |
+
'''
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.23.1
|
| 2 |
+
PyPDF2==3.0.1
|
| 3 |
+
langchain==0.0.197
|
| 4 |
+
openai==0.27.8
|
| 5 |
+
python-dotenv==1.0.0
|
| 6 |
+
faiss-cpu==1.7.4
|
| 7 |
+
huggingface-hub==0.15.1
|
| 8 |
+
InstructorEmbedding==1.0.1
|
| 9 |
+
sentence-transformers==2.2.2
|
| 10 |
+
tiktoken==0.4.0
|