Anne31415 commited on
Commit
6741ab6
·
1 Parent(s): 35f13e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -28
app.py CHANGED
@@ -136,11 +136,11 @@ def load_chatbot():
136
 
137
  def main():
138
  hide_streamlit_style = """
139
- <style>
140
- #MainMenu {visibility: hidden;}
141
- footer {visibility: hidden;}
142
- </style>
143
- """
144
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
145
 
146
  st.title("Welcome to BinDocs ChatBot! 🤖")
@@ -153,9 +153,6 @@ def main():
153
  if "chat_history" not in st.session_state:
154
  st.session_state['chat_history'] = []
155
 
156
- if "button_clicked" not in st.session_state:
157
- st.session_state['button_clicked'] = None
158
-
159
  display_chat_history(st.session_state['chat_history'])
160
 
161
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
@@ -163,7 +160,7 @@ def main():
163
  st.write("<!-- End Spacer -->", unsafe_allow_html=True)
164
 
165
  if pdf_path is not None:
166
- query = st.text_input("Ask questions about your PDF file (in any preferred language):")
167
 
168
  cloud_buttons = [
169
  ("Was genau ist ein Belegarzt?", "Was genau ist ein Belegarzt?", "1"),
@@ -174,20 +171,23 @@ def main():
174
  for label, query, color in cloud_buttons:
175
  cloud_button(label, query, color=color)
176
 
177
- # Handle button click from cloud buttons
178
- if st.session_state['button_clicked']:
179
- query = st.session_state['button_clicked']['query']
180
- st.session_state['chat_history'].append(("User", query, "new"))
181
- st.session_state['button_clicked'] = None
182
- handle_response(query, pdf_path)
183
 
184
- # Handle text input submission
185
- if st.button("Ask") or (query and query != st.session_state.get('last_query')):
186
- st.session_state['chat_history'].append(("User", query, "new"))
187
- st.session_state['last_query'] = query
188
- handle_response(query, pdf_path)
189
 
190
- def handle_response(query, pdf_path):
 
 
 
 
 
 
 
 
 
191
  loading_message = st.empty()
192
  loading_message.text('Bot is thinking...')
193
 
@@ -198,14 +198,9 @@ def handle_response(query, pdf_path):
198
  response = chain.run(input_documents=docs, question=query)
199
 
200
  st.session_state['chat_history'].append(("Bot", response, "new"))
201
- new_messages = st.session_state['chat_history'][-2:]
202
- for chat in new_messages:
203
- background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
204
- 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)
205
-
206
- st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
207
  loading_message.empty()
208
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
209
 
210
  if __name__ == "__main__":
211
- main()
 
136
 
137
  def main():
138
  hide_streamlit_style = """
139
+ <style>
140
+ #MainMenu {visibility: hidden;}
141
+ footer {visibility: hidden;}
142
+ </style>
143
+ """
144
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
145
 
146
  st.title("Welcome to BinDocs ChatBot! 🤖")
 
153
  if "chat_history" not in st.session_state:
154
  st.session_state['chat_history'] = []
155
 
 
 
 
156
  display_chat_history(st.session_state['chat_history'])
157
 
158
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
 
160
  st.write("<!-- End Spacer -->", unsafe_allow_html=True)
161
 
162
  if pdf_path is not None:
163
+ query = st.text_input("Ask questions about your PDF file (in any preferred language):", key="user_query")
164
 
165
  cloud_buttons = [
166
  ("Was genau ist ein Belegarzt?", "Was genau ist ein Belegarzt?", "1"),
 
171
  for label, query, color in cloud_buttons:
172
  cloud_button(label, query, color=color)
173
 
174
+ user_input = st.empty()
 
 
 
 
 
175
 
176
+ if "button_clicked" in st.session_state and st.session_state.button_clicked:
177
+ user_input = st.session_state.button_clicked["query"]
178
+ handle_query(user_input, pdf_path)
179
+ st.session_state.button_clicked = None
 
180
 
181
+ if st.button("Ask"):
182
+ user_input = st.session_state.user_query
183
+ handle_query(user_input, pdf_path)
184
+
185
+ def handle_query(query, pdf_path):
186
+ if not query:
187
+ st.warning("Please enter a query.")
188
+ return
189
+
190
+ st.session_state['chat_history'].append(("User", query, "new"))
191
  loading_message = st.empty()
192
  loading_message.text('Bot is thinking...')
193
 
 
198
  response = chain.run(input_documents=docs, question=query)
199
 
200
  st.session_state['chat_history'].append(("Bot", response, "new"))
201
+ display_chat_history(st.session_state['chat_history'][-2:])
 
 
 
 
 
202
  loading_message.empty()
203
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
204
 
205
  if __name__ == "__main__":
206
+ main()