Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,17 +20,22 @@ with st.sidebar:
|
|
| 20 |
|
| 21 |
st.markdown("**BinDocs Chat App**.")
|
| 22 |
|
|
|
|
| 23 |
st.markdown("Harnessing the power of a Large Language Model and AI technology,")
|
|
|
|
|
|
|
|
|
|
| 24 |
st.markdown("this innovative platform redefines PDF engagement,")
|
| 25 |
|
| 26 |
st.markdown("enabling dynamic conversations that bridge the gap between")
|
| 27 |
st.markdown("human and machine intelligence.")
|
| 28 |
|
|
|
|
|
|
|
| 29 |
add_vertical_space(3) # Add more vertical space between text blocks
|
| 30 |
st.write('Made with ❤️ by Anne')
|
| 31 |
|
| 32 |
-
|
| 33 |
-
pdf_path = None # Initialize pdf_path as None
|
| 34 |
|
| 35 |
def load_pdf(file_path):
|
| 36 |
pdf_reader = PdfReader(file_path)
|
|
@@ -45,7 +50,7 @@ def load_pdf(file_path):
|
|
| 45 |
)
|
| 46 |
chunks = text_splitter.split_text(text=text)
|
| 47 |
|
| 48 |
-
store_name
|
| 49 |
|
| 50 |
if os.path.exists(f"{store_name}.pkl"):
|
| 51 |
with open(f"{store_name}.pkl", "rb") as f:
|
|
@@ -58,71 +63,58 @@ def load_pdf(file_path):
|
|
| 58 |
|
| 59 |
return VectorStore
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
openai_config = {
|
| 64 |
-
"api_key": openai_api_key
|
| 65 |
-
}
|
| 66 |
-
return load_qa_chain(llm=OpenAI(config=openai_config), chain_type="stuff")
|
| 67 |
-
|
| 68 |
|
| 69 |
def main():
|
| 70 |
st.title("BinDocs Chat App")
|
| 71 |
|
| 72 |
-
|
| 73 |
-
uploaded_pdf = st.file_uploader("Upload a PDF file:", type=["pdf"])
|
| 74 |
-
|
| 75 |
-
if uploaded_pdf is not None:
|
| 76 |
-
pdf_path = uploaded_pdf
|
| 77 |
-
|
| 78 |
|
| 79 |
if "chat_history" not in st.session_state:
|
| 80 |
st.session_state['chat_history'] = []
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
display_chat_history(st.session_state['chat_history'])
|
| 83 |
|
| 84 |
st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
|
| 85 |
st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
|
| 86 |
st.write("<!-- End Spacer -->", unsafe_allow_html=True)
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
-
if st.button("Ask")
|
| 94 |
-
st.session_state['
|
|
|
|
| 95 |
|
| 96 |
loading_message = st.empty()
|
| 97 |
loading_message.text('Bot is thinking...')
|
| 98 |
|
| 99 |
-
VectorStore = load_pdf(
|
| 100 |
chain = load_chatbot()
|
| 101 |
-
docs = VectorStore.similarity_search(query=
|
| 102 |
with get_openai_callback() as cb:
|
| 103 |
-
response = chain.run(input_documents=docs, question=
|
| 104 |
-
|
| 105 |
-
st.session_state['chat_history'].append(("Bot", response, "new"))
|
| 106 |
|
| 107 |
-
# Display
|
| 108 |
-
|
| 109 |
-
for chat in new_messages:
|
| 110 |
-
background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
|
| 111 |
-
new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
|
| 112 |
-
|
| 113 |
-
# Scroll to the latest response using JavaScript
|
| 114 |
st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
|
| 115 |
|
| 116 |
loading_message.empty()
|
| 117 |
|
| 118 |
-
# Clear the input field by setting the query variable to an empty string
|
| 119 |
-
query = ""
|
| 120 |
-
|
| 121 |
# Mark all messages as old after displaying
|
| 122 |
st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
|
| 123 |
|
| 124 |
|
| 125 |
-
|
| 126 |
def display_chat_history(chat_history):
|
| 127 |
for chat in chat_history:
|
| 128 |
background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
|
|
@@ -130,4 +122,3 @@ def display_chat_history(chat_history):
|
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
main()
|
| 133 |
-
|
|
|
|
| 20 |
|
| 21 |
st.markdown("**BinDocs Chat App**.")
|
| 22 |
|
| 23 |
+
|
| 24 |
st.markdown("Harnessing the power of a Large Language Model and AI technology,")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
st.markdown("this innovative platform redefines PDF engagement,")
|
| 29 |
|
| 30 |
st.markdown("enabling dynamic conversations that bridge the gap between")
|
| 31 |
st.markdown("human and machine intelligence.")
|
| 32 |
|
| 33 |
+
|
| 34 |
+
|
| 35 |
add_vertical_space(3) # Add more vertical space between text blocks
|
| 36 |
st.write('Made with ❤️ by Anne')
|
| 37 |
|
| 38 |
+
load_dotenv()
|
|
|
|
| 39 |
|
| 40 |
def load_pdf(file_path):
|
| 41 |
pdf_reader = PdfReader(file_path)
|
|
|
|
| 50 |
)
|
| 51 |
chunks = text_splitter.split_text(text=text)
|
| 52 |
|
| 53 |
+
store_name = file_path.name[:-4]
|
| 54 |
|
| 55 |
if os.path.exists(f"{store_name}.pkl"):
|
| 56 |
with open(f"{store_name}.pkl", "rb") as f:
|
|
|
|
| 63 |
|
| 64 |
return VectorStore
|
| 65 |
|
| 66 |
+
def load_chatbot():
|
| 67 |
+
return load_qa_chain(llm=OpenAI(), chain_type="stuff")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def main():
|
| 70 |
st.title("BinDocs Chat App")
|
| 71 |
|
| 72 |
+
pdf = st.file_uploader("Upload your PDF", type="pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
if "chat_history" not in st.session_state:
|
| 75 |
st.session_state['chat_history'] = []
|
| 76 |
|
| 77 |
+
if "current_input" not in st.session_state:
|
| 78 |
+
st.session_state['current_input'] = ""
|
| 79 |
+
|
| 80 |
+
if "processing_input" not in st.session_state:
|
| 81 |
+
st.session_state['processing_input'] = ""
|
| 82 |
+
|
| 83 |
display_chat_history(st.session_state['chat_history'])
|
| 84 |
|
| 85 |
st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
|
| 86 |
st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
|
| 87 |
st.write("<!-- End Spacer -->", unsafe_allow_html=True)
|
| 88 |
|
| 89 |
+
if pdf is not None:
|
| 90 |
+
query = st.text_input("Ask questions about your PDF file (in any preferred language):", value=st.session_state['current_input'])
|
| 91 |
|
| 92 |
+
if query != st.session_state['current_input']:
|
| 93 |
+
st.session_state['current_input'] = query
|
| 94 |
|
| 95 |
+
if st.button("Ask"):
|
| 96 |
+
st.session_state['processing_input'] = st.session_state['current_input']
|
| 97 |
+
st.session_state['chat_history'].append(("User", st.session_state['processing_input'], "new"))
|
| 98 |
|
| 99 |
loading_message = st.empty()
|
| 100 |
loading_message.text('Bot is thinking...')
|
| 101 |
|
| 102 |
+
VectorStore = load_pdf(pdf)
|
| 103 |
chain = load_chatbot()
|
| 104 |
+
docs = VectorStore.similarity_search(query=st.session_state['processing_input'], k=3)
|
| 105 |
with get_openai_callback() as cb:
|
| 106 |
+
response = chain.run(input_documents=docs, question=st.session_state['processing_input'])
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# Display the bot's response immediately using JavaScript
|
| 109 |
+
st.write(f"<div id='response' style='background-color: #caf; padding: 10px; border-radius: 10px; margin: 10px;'>Bot: {response}</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
|
| 111 |
|
| 112 |
loading_message.empty()
|
| 113 |
|
|
|
|
|
|
|
|
|
|
| 114 |
# Mark all messages as old after displaying
|
| 115 |
st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
|
| 116 |
|
| 117 |
|
|
|
|
| 118 |
def display_chat_history(chat_history):
|
| 119 |
for chat in chat_history:
|
| 120 |
background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
|
|
|
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
main()
|
|
|