Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -120,9 +120,38 @@ def main():
|
|
| 120 |
loading_message = st.empty()
|
| 121 |
loading_message.text('Bot is thinking...')
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
my_component()
|
| 126 |
|
|
|
|
| 127 |
if __name__ == "__main__":
|
| 128 |
main()
|
|
|
|
| 120 |
loading_message = st.empty()
|
| 121 |
loading_message.text('Bot is thinking...')
|
| 122 |
|
| 123 |
+
VectorStore = load_pdf(pdf)
|
| 124 |
+
max_tokens = 120
|
| 125 |
+
chain = load_chatbot(max_tokens=max_tokens)
|
| 126 |
+
docs = VectorStore.similarity_search(query=query, k=2)
|
| 127 |
|
| 128 |
+
with get_openai_callback() as cb:
|
| 129 |
+
response = chain.run(input_documents=docs, question=query)
|
| 130 |
+
|
| 131 |
+
# Post-processing to remove incomplete sentences and redundant information
|
| 132 |
+
filtered_response = remove_incomplete_sentences(response)
|
| 133 |
+
filtered_response = remove_redundant_information(filtered_response)
|
| 134 |
+
|
| 135 |
+
st.session_state['chat_history'].append(("Bot", filtered_response, "new"))
|
| 136 |
+
|
| 137 |
+
new_messages = st.session_state['chat_history'][-2:]
|
| 138 |
+
for chat in new_messages:
|
| 139 |
+
background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
|
| 140 |
+
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)
|
| 141 |
+
|
| 142 |
+
st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
|
| 143 |
+
|
| 144 |
+
loading_message.empty()
|
| 145 |
+
|
| 146 |
+
query = ""
|
| 147 |
+
else:
|
| 148 |
+
st.warning("Please upload a PDF file before asking questions.")
|
| 149 |
+
|
| 150 |
+
st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
|
| 151 |
+
|
| 152 |
+
|
| 153 |
my_component()
|
| 154 |
|
| 155 |
+
|
| 156 |
if __name__ == "__main__":
|
| 157 |
main()
|