Anne31415 commited on
Commit
cf32ffd
1 Parent(s): b9750e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  from PIL import Image
3
  import pinecone
4
  import pinecone_text
@@ -48,8 +49,6 @@ index_name = "canopy--document-uploader" # Replace with your chosen index name
48
 
49
  index = pc.Index(name=index_name)
50
 
51
- # Initialize Pinecone Text Client for embedding
52
- encoder = pinecone_text.OpenAIEncoder()
53
 
54
  # Step 1: Clone the Dataset Repository
55
  repo = Repository(
@@ -254,10 +253,15 @@ 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
  def text_to_vector(text):
258
- # Convert input text to vector using Pinecone Text Client
259
- embedding = encoder.encode([text])[0] # Assume single text input; adjust accordingly
260
- return embedding
261
 
262
 
263
 
@@ -505,6 +509,7 @@ def page2():
505
 
506
  def page3():
507
  try:
 
508
  st.markdown("""
509
  <style>
510
  #MainMenu {visibility: hidden;}
@@ -512,6 +517,7 @@ def page3():
512
  </style>
513
  """, unsafe_allow_html=True)
514
 
 
515
  col1, col2 = st.columns([3, 1])
516
  with col1:
517
  st.title("Kosten- und Strukturdaten der Krankenhäuser")
@@ -521,32 +527,38 @@ def page3():
521
  image = Image.open(image_path)
522
  st.image(image, use_column_width='always')
523
 
 
524
  display_chat_history(st.session_state['chat_history_page3'])
525
 
 
526
  query = st.text_input("Geben Sie hier Ihre Frage ein / Enter your question here:")
527
 
528
  if query:
529
- # Convert the query text to a vector using Pinecone Text Client
530
- query_vector = encoder.encode([query])[0]
531
- matches = query_pinecone(query_vector, top_k=5)
 
 
532
 
533
- response_messages = []
534
  for match in matches:
535
  matched_text = match["metadata"].get("summary", "Detailed information not available.")
536
  similarity_score = match["score"]
537
- response_messages.append((f"Pinecone Match - Score: {similarity_score:.2f}", matched_text))
 
 
 
538
 
539
- # Update session state with the new chat and response
540
- st.session_state['chat_history_page3'].extend([(query, "User Query")] + response_messages)
541
-
542
- # Display the new responses
543
- display_chat_history([(query, "User Query")] + response_messages)
544
 
545
- # Save the updated conversation to a file
546
- save_conversation(st.session_state['chat_history_page3'], st.session_state['session_id'])
 
547
 
548
  except Exception as e:
549
  st.error(f"An unexpected error occurred: {e}")
 
550
 
551
 
552
 
 
1
  import streamlit as st
2
+ from sentence_transformers import SentenceTransformer
3
  from PIL import Image
4
  import pinecone
5
  import pinecone_text
 
49
 
50
  index = pc.Index(name=index_name)
51
 
 
 
52
 
53
  # Step 1: Clone the Dataset Repository
54
  repo = Repository(
 
253
  query_results = index.query(vector=vector, top_k=top_k)
254
  return query_results["matches"]
255
 
256
+ from sentence_transformers import SentenceTransformer
257
+
258
+ # Initialize the Sentence Transformer model
259
+ model = SentenceTransformer('all-MiniLM-L6-v2')
260
+
261
  def text_to_vector(text):
262
+ # Convert input text to vector
263
+ embedding = model.encode(text)
264
+ return embedding # No need to convert to list, depending on how you use it later
265
 
266
 
267
 
 
509
 
510
  def page3():
511
  try:
512
+ # Style adjustments for the Streamlit page
513
  st.markdown("""
514
  <style>
515
  #MainMenu {visibility: hidden;}
 
517
  </style>
518
  """, unsafe_allow_html=True)
519
 
520
+ # Layout configuration
521
  col1, col2 = st.columns([3, 1])
522
  with col1:
523
  st.title("Kosten- und Strukturdaten der Krankenhäuser")
 
527
  image = Image.open(image_path)
528
  st.image(image, use_column_width='always')
529
 
530
+ # Display existing chat history
531
  display_chat_history(st.session_state['chat_history_page3'])
532
 
533
+ # Input for new user query
534
  query = st.text_input("Geben Sie hier Ihre Frage ein / Enter your question here:")
535
 
536
  if query:
537
+ # Convert the query text to a vector
538
+ query_vector = text_to_vector(query)
539
+
540
+ # Query the Pinecone index with the vector
541
+ matches = index.query(queries=[query_vector], top_k=5)["matches"][0]
542
 
543
+ # Display the results and update chat history
544
  for match in matches:
545
  matched_text = match["metadata"].get("summary", "Detailed information not available.")
546
  similarity_score = match["score"]
547
+ response_message = f"Matched Text: {matched_text} - Score: {similarity_score:.2f}"
548
+ st.write(response_message)
549
+ # Append the response to the chat history
550
+ st.session_state['chat_history_page3'].append(("Eve", response_message))
551
 
552
+ # Append the user query to the chat history
553
+ st.session_state['chat_history_page3'].append(("User", query))
 
 
 
554
 
555
+ # Save the updated chat history to a session or external storage as needed
556
+ # This is a placeholder for where you might save the chat history
557
+ # save_conversation(st.session_state['chat_history_page3'], st.session_state['session_id'])
558
 
559
  except Exception as e:
560
  st.error(f"An unexpected error occurred: {e}")
561
+
562
 
563
 
564