Anne31415 commited on
Commit
38ca6b6
·
verified ·
1 Parent(s): 3ad7e67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -40
app.py CHANGED
@@ -27,6 +27,9 @@ if 'chat_history_page1' not in st.session_state:
27
  if 'chat_history_page2' not in st.session_state:
28
  st.session_state['chat_history_page2'] = []
29
 
 
 
 
30
 
31
 
32
  # Step 1: Clone the Dataset Repository
@@ -39,11 +42,11 @@ repo = Repository(
39
  repo.git_pull() # Pull the latest changes (if any)
40
 
41
  # Step 2: Load the PDF File
42
- pdf_path = "Private_Book/09012024_Kombi_2.pdf" # Replace with your PDF file path
43
 
44
- # Step 2: Load the PDF File
45
- pdf_path2 = "Private_Book/Deutsche_Kodierrichtlinien_23.pdf" # Replace with your PDF file path
46
 
 
47
 
48
  api_key = os.getenv("OPENAI_API_KEY")
49
  # Retrieve the API key from st.secrets
@@ -72,7 +75,7 @@ def load_vector_store(file_path, store_name, force_reload=False):
72
  # Serialize the vector store
73
  with open(vector_store_path, "wb") as f:
74
  pickle.dump(VectorStore, f)
75
- st.text(f"Created and saved vector store at {vector_store_path}")
76
 
77
  # Change working directory for Git operations
78
  original_dir = os.getcwd()
@@ -80,14 +83,14 @@ def load_vector_store(file_path, store_name, force_reload=False):
80
 
81
  try:
82
  # Check current working directory and list files for debugging
83
- st.text(f"Current working directory: {os.getcwd()}")
84
- st.text(f"Files in current directory: {os.listdir()}")
85
 
86
  # Adjusted file path for Git command
87
  repo.git_add(f"{store_name}.pkl") # Use just the file name
88
  repo.git_commit(f"Update vector store: {store_name}")
89
  repo.git_push()
90
- st.text("Committed and pushed vector store to repository.")
91
  except Exception as e:
92
  st.error(f"Error during Git operations: {e}")
93
  finally:
@@ -183,7 +186,7 @@ def page1():
183
  col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
184
 
185
  with col1:
186
- st.title("Welcome to BinDocs AI!")
187
 
188
  with col2:
189
  # Load and display the image in the right column, which will be the top-right corner of the page
@@ -197,7 +200,7 @@ def page1():
197
  st.error("File not found. Please check the file path.")
198
  return
199
 
200
- VectorStore = load_vector_store(pdf_path, "vector_store_page1", force_reload=False)
201
 
202
  display_chat_history(st.session_state['chat_history_page1'])
203
 
@@ -215,21 +218,22 @@ def page1():
215
  col1, col2 = st.columns(2)
216
 
217
  with col1:
218
- if st.button("Welche Geräte müssen für die LG Geriatrie vorgehalten werden? "):
219
- query = "Welche Geräte müssen für die LG Geriatrie vorgehalten werden? "
220
- if st.button("Welche ärztlichen Vorgaben gibt es für die LG Palliativmedizin?"):
221
- query = "Welche ärztlichen Vorgaben gibt es für die LG Palliativmedizin?"
222
- if st.button("Wie haben sich die DiGA in den letzten Jahren entwickelt? Kannst du mir Daten nennen?"):
223
- query = "Wie haben sich die DiGA in den letzten Jahren entwickelt? Kannst du mir Daten nennen?"
224
 
225
 
226
  with col2:
227
- if st.button("Dies ist eine reine Test Frage, welche aber eine ausreichende Länge hat."):
228
- query = "Dies ist eine reine Test Frage, welche aber eine ausreichende Länge hat."
229
- if st.button("Was sagt mir denn generell die wundervolle Bevölkerungsentwicklung?"):
230
- query = "Was sagt mir denn generell die wundervolle Bevölkerungsentwicklung?"
231
- if st.button("Ob ich hier wohl viel schreibe, dass die Fragen vom Layout her passen?"):
232
- query = "Ob ich hier wohl viel schreibe, dass die Fragen vom Layout her passen?"
 
233
 
234
 
235
  if query:
@@ -240,7 +244,7 @@ def page1():
240
 
241
  with st.spinner('Bot is thinking...'):
242
  chain = load_chatbot()
243
- docs = VectorStore.similarity_search(query=query, k=3)
244
  with get_openai_callback() as cb:
245
  response = chain.run(input_documents=docs, question=query)
246
  response = handle_no_answer(response) # Process the response through the new function
@@ -293,7 +297,7 @@ def page2():
293
  col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
294
 
295
  with col1:
296
- st.title("Kodieren statt Frustrieren!")
297
 
298
  with col2:
299
  # Load and display the image in the right column, which will be the top-right corner of the page
@@ -308,7 +312,7 @@ def page2():
308
  st.error("File not found. Please check the file path.")
309
  return
310
 
311
- VectorStore = load_vector_store(pdf_path2, "vector_store_page2", force_reload=False)
312
 
313
 
314
 
@@ -328,21 +332,21 @@ def page2():
328
  col1, col2 = st.columns(2)
329
 
330
  with col1:
331
- if st.button("Wann kodiere ich etwas als Hauptdiagnose und wann als Nebendiagnose?"):
332
- query = "Wann kodiere ich etwas als Hauptdiagnose und wann als Nebendiagnose?"
333
- if st.button("Ein Patient wird mit Aszites bei bekannter Leberzirrhose stationär aufgenommen. Es wird nur der Aszites durch eine Punktion behandelt.Wie kodiere ich das?"):
334
- query = ("Ein Patient wird mit Aszites bei bekannter Leberzirrhose stationär aufgenommen. Es wird nur der Aszites durch eine Punktion behandelt.Wie kodiere ich das?")
335
- if st.button("Hauptdiagnose: Hirntumor wie kodiere ich das?"):
336
- query = "Hauptdiagnose: Hirntumor wie kodiere ich das?"
337
 
338
 
339
  with col2:
340
- if st.button("Welche Prozeduren werden normalerweise nicht verschlüsselt?"):
341
- query = "Welche Prozeduren werden normalerweise nicht verschlüsselt?"
342
- if st.button("Was muss ich bei der Kodierung der Folgezusänden von Krankheiten beachten?"):
343
- query = "Was muss ich bei der Kodierung der Folgezusänden von Krankheiten beachten?"
344
- if st.button("Was mache ich bei einer Verdachtsdiagnose, wenn mein Patien nach Hause entlassen wird?"):
345
- query = "Was mache ich bei einer Verdachtsdiagnose, wenn mein Patien nach Hause entlassen wird?"
346
 
347
 
348
  if query:
@@ -353,7 +357,7 @@ def page2():
353
 
354
  with st.spinner('Bot is thinking...'):
355
  chain = load_chatbot()
356
- docs = VectorStore.similarity_search(query=query, k=3)
357
  with get_openai_callback() as cb:
358
  response = chain.run(input_documents=docs, question=query)
359
  response = handle_no_answer(response) # Process the response through the new function
@@ -391,6 +395,116 @@ def page2():
391
 
392
 
393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
 
395
 
396
  def main():
@@ -399,15 +513,17 @@ def main():
399
  st.title('BinDoc GmbH')
400
  st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
401
  add_vertical_space(1)
402
- page = st.sidebar.selectbox("Choose a page", ["Document Analysis Bot", "Coding Assistance Bot"])
403
  add_vertical_space(1)
404
  st.write('Made with ❤️ by BinDoc GmbH')
405
 
406
  # Main area content based on page selection
407
- if page == "Document Analysis Bot":
408
  page1()
409
- elif page == "Coding Assistance Bot":
410
  page2()
 
 
411
 
412
 
413
  if __name__ == "__main__":
 
27
  if 'chat_history_page2' not in st.session_state:
28
  st.session_state['chat_history_page2'] = []
29
 
30
+ if 'chat_history_page3' not in st.session_state:
31
+ st.session_state['chat_history_page3'] = []
32
+
33
 
34
 
35
  # Step 1: Clone the Dataset Repository
 
42
  repo.git_pull() # Pull the latest changes (if any)
43
 
44
  # Step 2: Load the PDF File
45
+ pdf_path = "Private_Book/KH_Reform_230124.pdf" # Replace with your PDF file path
46
 
47
+ pdf_path2 = "Private_Book/Buch_23012024.pdf"
 
48
 
49
+ pdf_path3 = "Private_Book/Kosten_Grunddaten_KH_230124.pdf"
50
 
51
  api_key = os.getenv("OPENAI_API_KEY")
52
  # Retrieve the API key from st.secrets
 
75
  # Serialize the vector store
76
  with open(vector_store_path, "wb") as f:
77
  pickle.dump(VectorStore, f)
78
+ #st.text(f"Created and saved vector store at {vector_store_path}")
79
 
80
  # Change working directory for Git operations
81
  original_dir = os.getcwd()
 
83
 
84
  try:
85
  # Check current working directory and list files for debugging
86
+ #st.text(f"Current working directory: {os.getcwd()}")
87
+ #st.text(f"Files in current directory: {os.listdir()}")
88
 
89
  # Adjusted file path for Git command
90
  repo.git_add(f"{store_name}.pkl") # Use just the file name
91
  repo.git_commit(f"Update vector store: {store_name}")
92
  repo.git_push()
93
+ #st.text("Committed and pushed vector store to repository.")
94
  except Exception as e:
95
  st.error(f"Error during Git operations: {e}")
96
  finally:
 
186
  col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
187
 
188
  with col1:
189
+ st.title("KH_reform!")
190
 
191
  with col2:
192
  # Load and display the image in the right column, which will be the top-right corner of the page
 
200
  st.error("File not found. Please check the file path.")
201
  return
202
 
203
+ VectorStore = load_vector_store(pdf_path, "KH_Reform_2301", force_reload=False)
204
 
205
  display_chat_history(st.session_state['chat_history_page1'])
206
 
 
218
  col1, col2 = st.columns(2)
219
 
220
  with col1:
221
+ if st.button("Wie viele Ärzte benötigt eine Klinik in der Leistungsgruppe Pädiatrie?"):
222
+ query = "Wie viele Ärzte benötigt eine Klinik in der Leistungsgruppe Pädiatrie?"
223
+ if st.button("Wie viele Leistungsgruppen gibt es?"):
224
+ query = ("Wie viele Leistungsgruppen gibt es?")
225
+ if st.button("Was sind die hauptsächlichen Änderungsvorhaben der Krankenhausreform?"):
226
+ query = "Was sind die hauptsächlichen Änderungsvorhaben der Krankenhausreform?"
227
 
228
 
229
  with col2:
230
+ if st.button("Welche und wieviele Fachärzte benötige ich für die Leistungsgruppe Gastroenterologie? "):
231
+ query = "Welche und wieviele Fachärzte benötige ich für die Leistungsgruppe Gastroenterologie"
232
+ if st.button("Was soll die Reform der Notfallversorgung beinhalten?"):
233
+ query = "Was soll die Reform der Notfallversorgung beinhalten?"
234
+ if st.button("Was bedeutet die Vorhaltefinanzierung?"):
235
+ query = "Was bedeutet die Vorhaltefinanzierung?"
236
+
237
 
238
 
239
  if query:
 
244
 
245
  with st.spinner('Bot is thinking...'):
246
  chain = load_chatbot()
247
+ docs = VectorStore.similarity_search(query=query, k=5)
248
  with get_openai_callback() as cb:
249
  response = chain.run(input_documents=docs, question=query)
250
  response = handle_no_answer(response) # Process the response through the new function
 
297
  col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
298
 
299
  with col1:
300
+ st.title("Kennzahlenbuch 100 Kennzahlen!")
301
 
302
  with col2:
303
  # Load and display the image in the right column, which will be the top-right corner of the page
 
312
  st.error("File not found. Please check the file path.")
313
  return
314
 
315
+ VectorStore = load_vector_store(pdf_path2, "Buch_2301", force_reload=False)
316
 
317
 
318
 
 
332
  col1, col2 = st.columns(2)
333
 
334
  with col1:
335
+ if st.button("Nenne mir 5 wichtige Personalkennzahlen im Krankenhaus."):
336
+ query = "Nenne mir 5 wichtige Personalkennzahlen im Krankenhaus."
337
+ if st.button("Wie ist die durchschnittliche Bettenauslastung eines Krankenhauses?"):
338
+ query = ("Wie ist die durchschnittliche Bettenauslastung eines Krankenhauses?")
339
+ if st.button("Welches sind die häufigsten DRGs, die von den Krankenhäusern abgerechnet werden?"):
340
+ query = "Welches sind die häufigsten DRGs, die von den Krankenhäusern abgerechnet werden? "
341
 
342
 
343
  with col2:
344
+ if st.button("Wie viel Casemixpunkte werden im Median von einer ärztlichen Vollkraft erbracht?"):
345
+ query = "Wie viel Casemixpunkte werden im Median von einer ärztlichen Vollkraft erbracht?"
346
+ if st.button("Bitte erstelle mir einer Übersicht der wichtiger Strukturkennzahlen eines Krankenhauses der Grund- und Regelversorgung."):
347
+ query = "Bitte erstelle mir einer Übersicht der wichtiger Strukturkennzahlen eines Krankenhauses der Grund- und Regelversorgung."
348
+ if st.button("Wie viele Patienten eines Grund- und Regelversorgers kommen aus welcher Fahrzeitzone?"):
349
+ query = "Wie viele Patienten eines Grund- und Regelversorgers kommen aus welcher Fahrzeitzone?"
350
 
351
 
352
  if query:
 
357
 
358
  with st.spinner('Bot is thinking...'):
359
  chain = load_chatbot()
360
+ docs = VectorStore.similarity_search(query=query, k=5)
361
  with get_openai_callback() as cb:
362
  response = chain.run(input_documents=docs, question=query)
363
  response = handle_no_answer(response) # Process the response through the new function
 
395
 
396
 
397
 
398
+ def page3():
399
+ try:
400
+ hide_streamlit_style = """
401
+ <style>
402
+ #MainMenu {visibility: hidden;}
403
+ footer {visibility: hidden;}
404
+ </style>
405
+ """
406
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
407
+
408
+ # Create columns for layout
409
+ col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
410
+
411
+ with col1:
412
+ st.title("Kosten- und Strukturdaten der Krankenhäuser")
413
+
414
+ with col2:
415
+ # Load and display the image in the right column, which will be the top-right corner of the page
416
+ image = Image.open('BinDoc Logo (Quadratisch).png')
417
+ st.image(image, use_column_width='always')
418
+
419
+
420
+ # Start tracking user interactions
421
+ with streamlit_analytics.track():
422
+
423
+ if not os.path.exists(pdf_path2):
424
+ st.error("File not found. Please check the file path.")
425
+ return
426
+
427
+ VectorStore = load_vector_store(pdf_path3, "Kosten_Str_2301", force_reload=False)
428
+
429
+
430
+
431
+ display_chat_history(st.session_state['chat_history_page3'])
432
+
433
+ st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
434
+ st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
435
+ st.write("<!-- End Spacer -->", unsafe_allow_html=True)
436
+
437
+ new_messages_placeholder = st.empty()
438
+
439
+ query = st.text_input("Ask questions about your PDF file (in any preferred language):")
440
+
441
+ add_vertical_space(2) # Adjust as per the desired spacing
442
+
443
+ # Create two columns for the buttons
444
+ col1, col2 = st.columns(2)
445
+
446
+ with col1:
447
+ if st.button("Wie hat sich die Bettenanzahl in den letzten 10 Jahren entwickelt?"):
448
+ query = "Wie hat sich die Bettenanzahl in den letzten 10 Jahren entwickelt?"
449
+ if st.button("Wie viele Patienten werden pro Jahr vollstationär behandelt?"):
450
+ query = ("Wie viele Patienten werden pro Jahr vollstationär behandelt?")
451
+ if st.button("Wie viele Vollkräfte arbeiten in Summe in deutschen Krankenhäusern?"):
452
+ query = "Wie viele Vollkräfte arbeiten in Summe in deutschen Krankenhäusern? "
453
+
454
+
455
+ with col2:
456
+ if st.button("Welche unterschiedlichen Personalkosten gibt es im Krankenhaus?"):
457
+ query = "Welche unterschiedlichen Personalkosten gibt es im Krankenhaus?"
458
+ if st.button("Welche Sachkosten werden in Krankenhäusern unterschieden?"):
459
+ query = "Welche Sachkosten werden in Krankenhäusern unterschieden? "
460
+ if st.button("Wie hoch sind die Gesamtkosten der Krankenhäuser pro Jahr?"):
461
+ query = "Wie hoch sind die Gesamtkosten der Krankenhäuser pro Jahr?"
462
+
463
+
464
+ if query:
465
+ st.session_state['chat_history_page3'].append(("User", query, "new"))
466
+
467
+ # Start timing
468
+ start_time = time.time()
469
+
470
+ with st.spinner('Bot is thinking...'):
471
+ chain = load_chatbot()
472
+ docs = VectorStore.similarity_search(query=query, k=5)
473
+ with get_openai_callback() as cb:
474
+ response = chain.run(input_documents=docs, question=query)
475
+ response = handle_no_answer(response) # Process the response through the new function
476
+
477
+
478
+
479
+ # Stop timing
480
+ end_time = time.time()
481
+
482
+ # Calculate duration
483
+ duration = end_time - start_time
484
+
485
+ # You can use Streamlit's text function to display the timing
486
+ st.text(f"Response time: {duration:.2f} seconds")
487
+
488
+ st.session_state['chat_history_page3'].append(("Bot", response, "new"))
489
+
490
+
491
+ # Display new messages at the bottom
492
+ new_messages = st.session_state['chat_history_page3'][-2:]
493
+ for chat in new_messages:
494
+ background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
495
+ 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)
496
+
497
+
498
+ # Clear the input field after the query is made
499
+ query = ""
500
+
501
+ # Mark all messages as old after displaying
502
+ st.session_state['chat_history_page3'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page3']]
503
+
504
+ except Exception as e:
505
+ st.error(f"Upsi, an unexpected error occurred: {e}")
506
+ # Optionally log the exception details to a file or error tracking service
507
+
508
 
509
 
510
  def main():
 
513
  st.title('BinDoc GmbH')
514
  st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
515
  add_vertical_space(1)
516
+ page = st.sidebar.selectbox("Choose a page", ["KH_Reform", "Kennzahlenbuch 100 Kennzahlen", "Kosten- und Strukturdaten der Krankenhäuser"])
517
  add_vertical_space(1)
518
  st.write('Made with ❤️ by BinDoc GmbH')
519
 
520
  # Main area content based on page selection
521
+ if page == "KH_Reform":
522
  page1()
523
+ elif page == "Kennzahlenbuch 100 Kennzahlen":
524
  page2()
525
+ elif page == "Kosten- und Strukturdaten der Krankenhäuser":
526
+ page3()
527
 
528
 
529
  if __name__ == "__main__":