Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -83,29 +83,39 @@ def main():
|
|
| 83 |
st.write("<!-- End Spacer -->", unsafe_allow_html=True)
|
| 84 |
|
| 85 |
if pdf is not None:
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
st.write(f"<div id='response' style='background-color: #caf; padding: 10px; border-radius: 10px; margin: 10px;'>Bot: {response}</div>", unsafe_allow_html=True)
|
| 103 |
-
st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
def display_chat_history(chat_history):
|
| 111 |
for chat in chat_history:
|
|
|
|
| 83 |
st.write("<!-- End Spacer -->", unsafe_allow_html=True)
|
| 84 |
|
| 85 |
if pdf is not None:
|
| 86 |
+
query = st.text_input("Ask questions about your PDF file (in any preferred language):")
|
| 87 |
|
| 88 |
+
if st.button("Ask") or (query and query != st.session_state.get('last_input', '')):
|
| 89 |
+
st.session_state['last_input'] = query # Save the current query as the last input
|
| 90 |
+
st.session_state['chat_history'].append(("User", query, "new"))
|
| 91 |
|
| 92 |
+
loading_message = st.empty()
|
| 93 |
+
loading_message.text('Bot is thinking...')
|
| 94 |
|
| 95 |
+
VectorStore = load_pdf(pdf)
|
| 96 |
+
chain = load_chatbot()
|
| 97 |
+
docs = VectorStore.similarity_search(query=query, k=3)
|
| 98 |
+
with get_openai_callback() as cb:
|
| 99 |
+
response = chain.run(input_documents=docs, question=query)
|
| 100 |
|
| 101 |
+
st.session_state['chat_history'].append(("Bot", response, "new"))
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# Display new messages at the bottom
|
| 104 |
+
new_messages = st.session_state['chat_history'][-2:]
|
| 105 |
+
for chat in new_messages:
|
| 106 |
+
background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
|
| 107 |
+
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)
|
| 108 |
|
| 109 |
+
# Scroll to the latest response using JavaScript
|
| 110 |
+
st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
|
| 111 |
+
|
| 112 |
+
loading_message.empty()
|
| 113 |
+
|
| 114 |
+
# Clear the input field by setting the query variable to an empty string
|
| 115 |
+
query = ""
|
| 116 |
+
|
| 117 |
+
# Mark all messages as old after displaying
|
| 118 |
+
st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
|
| 119 |
|
| 120 |
def display_chat_history(chat_history):
|
| 121 |
for chat in chat_history:
|