Anne31415 commited on
Commit
5b2cc8a
·
verified ·
1 Parent(s): f89b5b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -53
app.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -17,8 +18,6 @@ from langchain.callbacks import get_openai_callback
17
  import os
18
  import uuid
19
  import json
20
-
21
-
22
  import pandas as pd
23
  import pydeck as pdk
24
  from urllib.error import URLError
@@ -50,6 +49,8 @@ index_name = "canopy--document-uploader" # Replace with your chosen index name
50
 
51
  index = pc.Index(name=index_name)
52
 
 
 
53
 
54
  # Step 1: Clone the Dataset Repository
55
  repo = Repository(
@@ -254,16 +255,11 @@ def query_pinecone(vector, index, top_k=5):
254
  query_results = index.query(vector=vector, top_k=top_k)
255
  return query_results["matches"]
256
 
257
-
258
- from sentence_transformers import SentenceTransformer
259
-
260
- # Initialize the model
261
- model = SentenceTransformer('all-MiniLM-L6-v2')
262
-
263
  def text_to_vector(text):
264
- # Convert input text to vector
265
- embedding = model.encode(text)
266
- return embedding.tolist() # Convert embedding to list
 
267
 
268
 
269
  def page1():
@@ -510,68 +506,48 @@ def page2():
510
 
511
  def page3():
512
  try:
513
- # Hide Streamlit's default UI elements
514
- hide_streamlit_style = """
515
  <style>
516
  #MainMenu {visibility: hidden;}
517
  footer {visibility: hidden;}
518
  </style>
519
- """
520
- st.markdown(hide_streamlit_style, unsafe_allow_html=True)
521
 
522
- # Create columns for layout
523
  col1, col2 = st.columns([3, 1])
524
-
525
  with col1:
526
  st.title("Kosten- und Strukturdaten der Krankenhäuser")
527
-
528
  with col2:
529
- # Display an image in the right column
530
- image = Image.open('BinDoc Logo (Quadratisch).png')
531
- st.image(image, use_column_width='always')
532
-
533
- # Check if the PDF file exists
534
- pdf_path3 = "Private_Book/Kosten_Strukturdaten_RAG_vorbereited.pdf"
535
- if not os.path.exists(pdf_path3):
536
- st.error("File not found. Please check the file path.")
537
- return
538
 
539
  display_chat_history(st.session_state['chat_history_page3'])
540
 
541
- # Spacer
542
- st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
543
- st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
544
- st.write("<!-- End Spacer -->", unsafe_allow_html=True)
545
-
546
- # New messages placeholder
547
- new_messages_placeholder = st.empty()
548
-
549
- # User query input
550
  query = st.text_input("Geben Sie hier Ihre Frage ein / Enter your question here:")
551
 
552
- # Query buttons
553
- col1, col2 = st.columns(2)
554
- # Define buttons and their queries here as before
555
-
556
-
557
  if query:
558
- # Convert the query text to a vector
559
- query_vector = text_to_vector(query)
560
-
561
- # Query Pinecone with the vector
562
- matches = query_pinecone(query_vector, index, top_k=5)
563
 
564
- # Assuming query_pinecone is defined elsewhere and connects to Pinecone correctly
565
- # Display the results
566
  for match in matches:
567
- matched_text = match["metadata"].get("text", "No text available")
568
  similarity_score = match["score"]
569
- st.write(f"Matched Text: {matched_text} - Score: {similarity_score}")
570
 
571
- # Your existing logic to handle chat history and responses...
 
572
 
 
 
 
 
 
 
573
  except Exception as e:
574
- st.error(f"Upsi, an unexpected error occurred: {e}")
575
 
576
 
577
 
 
1
  import streamlit as st
2
  from PIL import Image
3
+ import pinecone
4
+ import pinecone_text
5
  import random
6
  import time
7
  from dotenv import load_dotenv
 
18
  import os
19
  import uuid
20
  import json
 
 
21
  import pandas as pd
22
  import pydeck as pdk
23
  from urllib.error import URLError
 
49
 
50
  index = pc.Index(name=index_name)
51
 
52
+ # Initialize Pinecone Text Client for embedding
53
+ encoder = pinecone_text.OpenAIEncoder()
54
 
55
  # Step 1: Clone the Dataset Repository
56
  repo = Repository(
 
255
  query_results = index.query(vector=vector, top_k=top_k)
256
  return query_results["matches"]
257
 
 
 
 
 
 
 
258
  def text_to_vector(text):
259
+ # Convert input text to vector using Pinecone Text Client
260
+ embedding = encoder.encode([text])[0] # Assume single text input; adjust accordingly
261
+ return embedding
262
+
263
 
264
 
265
  def page1():
 
506
 
507
  def page3():
508
  try:
509
+ st.markdown("""
 
510
  <style>
511
  #MainMenu {visibility: hidden;}
512
  footer {visibility: hidden;}
513
  </style>
514
+ """, unsafe_allow_html=True)
 
515
 
 
516
  col1, col2 = st.columns([3, 1])
 
517
  with col1:
518
  st.title("Kosten- und Strukturdaten der Krankenhäuser")
 
519
  with col2:
520
+ image_path = 'BinDoc Logo (Quadratisch).png'
521
+ if os.path.exists(image_path):
522
+ image = Image.open(image_path)
523
+ st.image(image, use_column_width='always')
 
 
 
 
 
524
 
525
  display_chat_history(st.session_state['chat_history_page3'])
526
 
 
 
 
 
 
 
 
 
 
527
  query = st.text_input("Geben Sie hier Ihre Frage ein / Enter your question here:")
528
 
 
 
 
 
 
529
  if query:
530
+ # Convert the query text to a vector using Pinecone Text Client
531
+ query_vector = encoder.encode([query])[0]
532
+ matches = query_pinecone(query_vector, top_k=5)
 
 
533
 
534
+ response_messages = []
 
535
  for match in matches:
536
+ matched_text = match["metadata"].get("summary", "Detailed information not available.")
537
  similarity_score = match["score"]
538
+ response_messages.append((f"Pinecone Match - Score: {similarity_score:.2f}", matched_text))
539
 
540
+ # Update session state with the new chat and response
541
+ st.session_state['chat_history_page3'].extend([(query, "User Query")] + response_messages)
542
 
543
+ # Display the new responses
544
+ display_chat_history([(query, "User Query")] + response_messages)
545
+
546
+ # Save the updated conversation to a file
547
+ save_conversation(st.session_state['chat_history_page3'], st.session_state['session_id'])
548
+
549
  except Exception as e:
550
+ st.error(f"An unexpected error occurred: {e}")
551
 
552
 
553