Anne31415 commited on
Commit
5ea80a9
1 Parent(s): 1b661bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -4
app.py CHANGED
@@ -200,8 +200,119 @@ def page1():
200
 
201
 
202
  def page2():
203
- st.title("New Page")
204
- st.write("This is the content of the new page.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
 
206
  def main():
207
  # Sidebar content
@@ -214,9 +325,9 @@ def main():
214
  st.write('Made with ❤️ by BinDoc GmbH')
215
 
216
  # Main area content based on page selection
217
- if page == "Page 1":
218
  page1()
219
- elif page == "Page 2":
220
  page2()
221
 
222
  if __name__ == "__main__":
 
200
 
201
 
202
  def page2():
203
+ try:
204
+ hide_streamlit_style = """
205
+ <style>
206
+ #MainMenu {visibility: hidden;}
207
+ footer {visibility: hidden;}
208
+ </style>
209
+ """
210
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
211
+
212
+ # Create columns for layout
213
+ col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
214
+
215
+ with col1:
216
+ st.title("Kodieren statt Frustrieren!")
217
+
218
+ with col2:
219
+ # Load and display the image in the right column, which will be the top-right corner of the page
220
+ image = Image.open('BinDoc Logo (Quadratisch).png')
221
+ st.image(image, use_column_width='always')
222
+
223
+
224
+ # Start tracking user interactions
225
+ with streamlit_analytics.track():
226
+
227
+ if not os.path.exists(pdf_path2):
228
+ st.error("File not found. Please check the file path.")
229
+ return
230
+
231
+ VectorStore = load_vector_store(pdf_path2, "my_vector_store", force_reload=False)
232
+
233
+
234
+ if "chat_history" not in st.session_state:
235
+ st.session_state['chat_history'] = []
236
+
237
+ display_chat_history(st.session_state['chat_history'])
238
+
239
+ st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
240
+ st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
241
+ st.write("<!-- End Spacer -->", unsafe_allow_html=True)
242
+
243
+ new_messages_placeholder = st.empty()
244
+
245
+ query = st.text_input("Ask questions about your PDF file (in any preferred language):")
246
+
247
+ add_vertical_space(2) # Adjust as per the desired spacing
248
+
249
+ # Create two columns for the buttons
250
+ col1, col2 = st.columns(2)
251
+
252
+ with col1:
253
+ if st.button("Was kann ich mit dem Prognose-Analyse-Tool machen?"):
254
+ query = "Was kann ich mit dem Prognose-Analyse-Tool machen?"
255
+ if st.button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"):
256
+ query = "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"
257
+ if st.button("Ich habe mein Meta-Password vergessen, wie kann ich es zurücksetzen?"):
258
+ query = "Ich habe mein Meta-Password vergessen, wie kann ich es zurücksetzen?"
259
+
260
+
261
+ with col2:
262
+ if st.button("Dies ist eine reine Test Frage, welche aber eine ausreichende Länge hat."):
263
+ query = "Dies ist eine reine Test Frage, welche aber eine ausreichende Länge hat."
264
+ if st.button("Was sagt mir denn generell die wundervolle Bevölkerungsentwicklung?"):
265
+ query = "Was sagt mir denn generell die wundervolle Bevölkerungsentwicklung?"
266
+ if st.button("Ob ich hier wohl viel schreibe, dass die Fragen vom Layout her passen?"):
267
+ query = "Ob ich hier wohl viel schreibe, dass die Fragen vom Layout her passen?"
268
+
269
+
270
+ if query:
271
+ st.session_state['chat_history'].append(("User", query, "new"))
272
+
273
+ # Start timing
274
+ start_time = time.time()
275
+
276
+ with st.spinner('Bot is thinking...'):
277
+ # Use the VectorStore loaded at the start from the session state
278
+ chain = load_chatbot()
279
+ docs = VectorStore.similarity_search(query=query, k=3)
280
+ with get_openai_callback() as cb:
281
+ response = chain.run(input_documents=docs, question=query)
282
+
283
+
284
+ # Stop timing
285
+ end_time = time.time()
286
+
287
+ # Calculate duration
288
+ duration = end_time - start_time
289
+
290
+ # You can use Streamlit's text function to display the timing
291
+ st.text(f"Response time: {duration:.2f} seconds")
292
+
293
+ st.session_state['chat_history'].append(("Bot", response, "new"))
294
+
295
+
296
+ # Display new messages at the bottom
297
+ new_messages = st.session_state['chat_history'][-2:]
298
+ for chat in new_messages:
299
+ background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
300
+ 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)
301
+
302
+
303
+ # Clear the input field after the query is made
304
+ query = ""
305
+
306
+ # Mark all messages as old after displaying
307
+ st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
308
+
309
+ except Exception as e:
310
+ st.error(f"Upsi, an unexpected error occurred: {e}")
311
+ # Optionally log the exception details to a file or error tracking service
312
+
313
+
314
+
315
+
316
 
317
  def main():
318
  # Sidebar content
 
325
  st.write('Made with ❤️ by BinDoc GmbH')
326
 
327
  # Main area content based on page selection
328
+ if page == "Main ChatBot":
329
  page1()
330
+ elif page == "Kodierhilfe":
331
  page2()
332
 
333
  if __name__ == "__main__":