Anne31415 commited on
Commit
c5ffa05
·
verified ·
1 Parent(s): dc06e01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -87
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  from PIL import Image
 
3
  import random
4
  import time
5
  from dotenv import load_dotenv
@@ -38,6 +39,18 @@ if 'session_id' not in st.session_state:
38
 
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Step 1: Clone the Dataset Repository
42
  repo = Repository(
43
  local_dir="Private_Book", # Local directory to clone the repository
@@ -235,6 +248,22 @@ def display_session_id():
235
  st.sidebar.markdown(f"**Ihre Session ID:** `{session_id}`")
236
  st.sidebar.markdown("Verwenden Sie diese ID als Referenz bei Mitteilungen oder Rückmeldungen.")
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
  def page1():
240
  try:
@@ -480,125 +509,65 @@ def page2():
480
 
481
  def page3():
482
  try:
 
483
  hide_streamlit_style = """
484
- <style>
485
- #MainMenu {visibility: hidden;}
486
- footer {visibility: hidden;}
487
- </style>
488
- """
489
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
490
 
491
- # Create columns for layout
492
- col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
493
 
494
  with col1:
495
  st.title("Kosten- und Strukturdaten der Krankenhäuser")
496
 
497
  with col2:
498
- # Load and display the image in the right column, which will be the top-right corner of the page
499
  image = Image.open('BinDoc Logo (Quadratisch).png')
500
  st.image(image, use_column_width='always')
501
 
502
-
503
- if not os.path.exists(pdf_path2):
 
504
  st.error("File not found. Please check the file path.")
505
  return
506
 
507
- VectorStore = load_vector_store(pdf_path3, "Kosten_Str_2301", force_reload=True)
508
-
509
-
510
-
511
  display_chat_history(st.session_state['chat_history_page3'])
512
 
 
513
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
514
  st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
515
  st.write("<!-- End Spacer -->", unsafe_allow_html=True)
516
 
 
517
  new_messages_placeholder = st.empty()
518
 
 
519
  query = st.text_input("Geben Sie hier Ihre Frage ein / Enter your question here:")
520
 
521
- add_vertical_space(2) # Adjust as per the desired spacing
522
-
523
- # Create two columns for the buttons
524
  col1, col2 = st.columns(2)
525
-
526
- with col1:
527
- if st.button("Wie hat sich die Bettenanzahl in den letzten 10 Jahren entwickelt?"):
528
- query = "Wie hat sich die Bettenanzahl in den letzten 10 Jahren entwickelt?"
529
- if st.button("Wie viele Patienten wurden im Jahr 2017 vollstationär behandelt?"):
530
- query = ("Wie viele Patienten wurden im Jahr 2017 vollstationär behandelt?")
531
- if st.button("Wie viele Vollkräfte arbeiten in Summe 2021 in deutschen Krankenhäusern?"):
532
- query = "Wie viele Vollkräfte arbeiten in Summe 2021 in deutschen Krankenhäusern? "
533
-
534
-
535
- with col2:
536
- if st.button("Welche unterschiedlichen Personalkosten gibt es im Krankenhaus?"):
537
- query = "Welche unterschiedlichen Personalkosten gibt es im Krankenhaus?"
538
- if st.button("Welche Sachkosten werden in Krankenhäusern unterschieden?"):
539
- query = "Welche Sachkosten werden in Krankenhäusern unterschieden? "
540
- if st.button("Wie hoch sind die Gesamtkosten der Krankenhäuser pro Jahr: 2019, 2020, 2021?"):
541
- query = "Wie hoch sind die Gesamtkosten der Krankenhäuser pro Jahr: 2019, 2020, 2021?"
542
-
543
-
544
 
545
  if query:
546
- full_query = ask_bot(query)
547
- st.session_state['chat_history_page3'].append(("User", query, "new"))
548
-
549
- # Start timing
550
- start_time = time.time()
551
-
552
- # Create a placeholder for the response time
553
- response_time_placeholder = st.empty()
554
-
555
- with st.spinner('Eve denkt über Ihre Frage nach...'):
556
- chain = load_chatbot()
557
- docs = VectorStore.similarity_search(query=query, k=5)
558
- with get_openai_callback() as cb:
559
- response = chain.run(input_documents=docs, question=full_query)
560
- response = handle_no_answer(response) # Process the response through the new function
561
 
 
 
 
 
 
562
 
563
-
564
- # Stop timing
565
- end_time = time.time()
566
-
567
- # Calculate duration
568
- duration = end_time - start_time
569
-
570
- st.session_state['chat_history_page3'].append(("Eve", response, "new"))
571
-
572
- # Combine chat histories from all pages
573
- all_chat_histories = [
574
- st.session_state['chat_history_page1'],
575
- st.session_state['chat_history_page2'],
576
- st.session_state['chat_history_page3']
577
- ]
578
-
579
- # Save the combined chat histories
580
- save_conversation(all_chat_histories, st.session_state['session_id'])
581
-
582
-
583
- # Display new messages at the bottom
584
- new_messages = st.session_state['chat_history_page3'][-2:]
585
- for chat in new_messages:
586
- background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
587
- 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)
588
-
589
- # Update the response time placeholder after the messages are displayed
590
- response_time_placeholder.text(f"Response time: {duration:.2f} seconds")
591
-
592
-
593
- # Clear the input field after the query is made
594
- query = ""
595
-
596
- # Mark all messages as old after displaying
597
- st.session_state['chat_history_page3'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page3']]
598
 
599
  except Exception as e:
600
  st.error(f"Upsi, an unexpected error occurred: {e}")
601
- # Optionally log the exception details to a file or error tracking service
602
 
603
  def page4():
604
  try:
 
1
  import streamlit as st
2
  from PIL import Image
3
+ from pinecone import Pinecone
4
  import random
5
  import time
6
  from dotenv import load_dotenv
 
39
 
40
 
41
 
42
+
43
+ # Initialize Pinecone client
44
+ api_key = os.environ["api_key"]
45
+
46
+ # Consider securely storing and accessing this
47
+ Pinecone.init(api_key=api_key)
48
+ index_name = "canopy--document-uploader" # Replace with your chosen index name
49
+
50
+ index = Pinecone.Index(name=index_name)
51
+
52
+
53
+
54
  # Step 1: Clone the Dataset Repository
55
  repo = Repository(
56
  local_dir="Private_Book", # Local directory to clone the repository
 
248
  st.sidebar.markdown(f"**Ihre Session ID:** `{session_id}`")
249
  st.sidebar.markdown("Verwenden Sie diese ID als Referenz bei Mitteilungen oder Rückmeldungen.")
250
 
251
+
252
+ def query_pinecone(user_input, index, top_k=5):
253
+ # Assuming 'user_input' is the text from the user you want to query against the vector database
254
+ # And 'index' is your initialized Pinecone index
255
+
256
+ # Query Pinecone index for similar vectors
257
+ query_results = index.query(
258
+ vector=user_input, # Pinecone automatically handles embedding if set up to do so
259
+ top_k=top_k,
260
+ include_metadata=True # Set to True if you want to include metadata in the response
261
+ )
262
+
263
+ # Process query results
264
+ matches = query_results["matches"]
265
+ return matches
266
+
267
 
268
  def page1():
269
  try:
 
509
 
510
  def page3():
511
  try:
512
+ # Hide Streamlit's default UI elements
513
  hide_streamlit_style = """
514
+ <style>
515
+ #MainMenu {visibility: hidden;}
516
+ footer {visibility: hidden;}
517
+ </style>
518
+ """
519
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
520
 
521
+ # Create columns for layout
522
+ col1, col2 = st.columns([3, 1])
523
 
524
  with col1:
525
  st.title("Kosten- und Strukturdaten der Krankenhäuser")
526
 
527
  with col2:
528
+ # Display an image in the right column
529
  image = Image.open('BinDoc Logo (Quadratisch).png')
530
  st.image(image, use_column_width='always')
531
 
532
+ # Check if the PDF file exists
533
+ pdf_path3 = "Private_Book/Kosten_Strukturdaten_RAG_vorbereited.pdf"
534
+ if not os.path.exists(pdf_path3):
535
  st.error("File not found. Please check the file path.")
536
  return
537
 
 
 
 
 
538
  display_chat_history(st.session_state['chat_history_page3'])
539
 
540
+ # Spacer
541
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
542
  st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
543
  st.write("<!-- End Spacer -->", unsafe_allow_html=True)
544
 
545
+ # New messages placeholder
546
  new_messages_placeholder = st.empty()
547
 
548
+ # User query input
549
  query = st.text_input("Geben Sie hier Ihre Frage ein / Enter your question here:")
550
 
551
+ # Query buttons
 
 
552
  col1, col2 = st.columns(2)
553
+ # Define buttons and their queries here as before
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
554
 
555
  if query:
556
+ # Query Pinecone index
557
+ matches = query_pinecone(query, index, top_k=5)
558
+ # Assuming query_pinecone is a function that sends the query to Pinecone and retrieves matches
 
 
 
 
 
 
 
 
 
 
 
 
559
 
560
+ for match in matches:
561
+ # Display matched texts and their scores
562
+ matched_text = match["metadata"].get("text", "No text available")
563
+ similarity_score = match["score"]
564
+ st.write(f"Matched Text: {matched_text} - Score: {similarity_score}")
565
 
566
+ # Append the user query to chat history and process it as before
567
+ process_and_display_chat_history(query, matches) # You would need to implement this based on your application logic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
 
569
  except Exception as e:
570
  st.error(f"Upsi, an unexpected error occurred: {e}")
 
571
 
572
  def page4():
573
  try: