Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -65,23 +65,27 @@ def load_pdf(file_path):
|
|
65 |
|
66 |
def load_chatbot():
|
67 |
return load_qa_chain(llm=OpenAI(), chain_type="stuff")
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
def main():
|
69 |
st.title("BinDocs Chat App")
|
70 |
|
71 |
-
|
72 |
if "chat_history" not in st.session_state:
|
73 |
st.session_state['chat_history'] = []
|
74 |
|
75 |
-
if "current_input" not in st.session_state:
|
76 |
-
st.session_state['current_input'] = ""
|
77 |
-
|
78 |
display_chat_history(st.session_state['chat_history'])
|
79 |
|
80 |
st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
|
81 |
st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
|
82 |
st.write("<!-- End Spacer -->", unsafe_allow_html=True)
|
83 |
-
|
84 |
|
|
|
|
|
|
|
85 |
pdf = st.file_uploader("Upload your PDF", type="pdf")
|
86 |
|
87 |
if pdf is not None:
|
@@ -91,7 +95,6 @@ def main():
|
|
91 |
st.session_state['last_input'] = query # Save the current query as the last input
|
92 |
st.session_state['chat_history'].append(("User", query, "new"))
|
93 |
|
94 |
-
loading_message = st.empty()
|
95 |
loading_message.text('Bot is thinking...')
|
96 |
|
97 |
VectorStore = load_pdf(pdf)
|
@@ -102,28 +105,22 @@ def main():
|
|
102 |
|
103 |
st.session_state['chat_history'].append(("Bot", response, "new"))
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
# Scroll to the latest response using JavaScript
|
112 |
-
st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
|
113 |
-
|
114 |
-
loading_message.empty()
|
115 |
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
|
120 |
-
st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
|
121 |
|
|
|
|
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
|
126 |
-
st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
|
127 |
|
128 |
if __name__ == "__main__":
|
129 |
-
main()
|
|
|
65 |
|
66 |
def load_chatbot():
|
67 |
return load_qa_chain(llm=OpenAI(), chain_type="stuff")
|
68 |
+
|
69 |
+
def display_chat_history(chat_history):
|
70 |
+
for chat in chat_history:
|
71 |
+
background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
|
72 |
+
st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
|
73 |
+
|
74 |
def main():
|
75 |
st.title("BinDocs Chat App")
|
76 |
|
|
|
77 |
if "chat_history" not in st.session_state:
|
78 |
st.session_state['chat_history'] = []
|
79 |
|
|
|
|
|
|
|
80 |
display_chat_history(st.session_state['chat_history'])
|
81 |
|
82 |
st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
|
83 |
st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
|
84 |
st.write("<!-- End Spacer -->", unsafe_allow_html=True)
|
|
|
85 |
|
86 |
+
new_messages_placeholder = st.empty()
|
87 |
+
loading_message = st.empty()
|
88 |
+
|
89 |
pdf = st.file_uploader("Upload your PDF", type="pdf")
|
90 |
|
91 |
if pdf is not None:
|
|
|
95 |
st.session_state['last_input'] = query # Save the current query as the last input
|
96 |
st.session_state['chat_history'].append(("User", query, "new"))
|
97 |
|
|
|
98 |
loading_message.text('Bot is thinking...')
|
99 |
|
100 |
VectorStore = load_pdf(pdf)
|
|
|
105 |
|
106 |
st.session_state['chat_history'].append(("Bot", response, "new"))
|
107 |
|
108 |
+
# Display new messages at the bottom
|
109 |
+
new_messages = st.session_state['chat_history'][-2:]
|
110 |
+
for chat in new_messages:
|
111 |
+
background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
|
112 |
+
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)
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
+
# Scroll to the latest response using JavaScript
|
115 |
+
st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
|
116 |
|
117 |
+
loading_message.empty()
|
|
|
118 |
|
119 |
+
# Clear the input field by setting the query variable to an empty string
|
120 |
+
query = ""
|
121 |
|
122 |
+
# Mark all messages as old after displaying
|
123 |
+
st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
|
|
|
|
|
124 |
|
125 |
if __name__ == "__main__":
|
126 |
+
main()
|