shrut123 commited on
Commit
a19ad68
1 Parent(s): cb1057f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -4,7 +4,7 @@ from pinecone import Pinecone
4
  from sentence_transformers import SentenceTransformer
5
 
6
  # Title of the Streamlit App
7
- st.title("Medical Hybrid Search")
8
 
9
  # Initialize Pinecone globally
10
  index = None
@@ -14,8 +14,7 @@ def initialize_pinecone():
14
  api_key = os.getenv('PINECONE_API_KEY') # Get Pinecone API key from environment variable
15
  if api_key:
16
  # Initialize Pinecone client using the new class instance method
17
- pc = Pinecone(api_key=api_key)
18
- return pc
19
  else:
20
  st.error("Pinecone API key not found! Please set the PINECONE_API_KEY environment variable.")
21
  return None
@@ -23,11 +22,9 @@ def initialize_pinecone():
23
  # Function to connect to the 'pubmed-splade' index
24
  def connect_to_index(pc):
25
  index_name = 'pubmed-splade' # Hardcoded index name
26
- # Connect to the 'pubmed-splade' index
27
  if index_name in pc.list_indexes().names():
28
- #st.info(f"Successfully connected to index '{index_name}'")
29
- index = pc.Index(index_name)
30
- return index
31
  else:
32
  st.error(f"Index '{index_name}' not found!")
33
  return None
@@ -41,10 +38,9 @@ pc = initialize_pinecone()
41
 
42
  # If Pinecone initialized successfully, proceed with index management
43
  if pc:
44
- # Connect directly to 'pubmed-splade' index
45
  index = connect_to_index(pc)
46
 
47
- # Model for query encoding
48
  model = SentenceTransformer('msmarco-bert-base-dot-v5')
49
 
50
  # Query input
@@ -54,17 +50,25 @@ if pc:
54
  if st.button("Search Query"):
55
  if query_text and index:
56
  dense_vector = encode_query(model, query_text)
57
- #st.write(f"Encoded Query Vector: {dense_vector}")
58
-
59
- # Search the index (sparse values can be added here as well)
60
  results = index.query(
61
  vector=dense_vector,
62
- top_k=3,
63
  include_metadata=True
64
  )
65
 
66
- st.write("Search Results:")
67
- for match in results.matches:
68
- st.write(f"ID: {match.id}, Score: {match.score}, Metadata: {match.metadata}")
 
 
 
 
 
 
 
 
 
69
  else:
70
  st.error("Please enter a query and ensure the index is initialized.")
 
4
  from sentence_transformers import SentenceTransformer
5
 
6
  # Title of the Streamlit App
7
+ st.title("Pinecone Query Search on 'pubmed-splade' Index")
8
 
9
  # Initialize Pinecone globally
10
  index = None
 
14
  api_key = os.getenv('PINECONE_API_KEY') # Get Pinecone API key from environment variable
15
  if api_key:
16
  # Initialize Pinecone client using the new class instance method
17
+ return Pinecone(api_key=api_key)
 
18
  else:
19
  st.error("Pinecone API key not found! Please set the PINECONE_API_KEY environment variable.")
20
  return None
 
22
  # Function to connect to the 'pubmed-splade' index
23
  def connect_to_index(pc):
24
  index_name = 'pubmed-splade' # Hardcoded index name
 
25
  if index_name in pc.list_indexes().names():
26
+ st.info(f"Successfully connected to index '{index_name}'")
27
+ return pc.Index(index_name)
 
28
  else:
29
  st.error(f"Index '{index_name}' not found!")
30
  return None
 
38
 
39
  # If Pinecone initialized successfully, proceed with index management
40
  if pc:
 
41
  index = connect_to_index(pc)
42
 
43
+ # Load model for query encoding
44
  model = SentenceTransformer('msmarco-bert-base-dot-v5')
45
 
46
  # Query input
 
50
  if st.button("Search Query"):
51
  if query_text and index:
52
  dense_vector = encode_query(model, query_text)
53
+
54
+ # Search the index
 
55
  results = index.query(
56
  vector=dense_vector,
57
+ top_k=5,
58
  include_metadata=True
59
  )
60
 
61
+ st.write("### Search Results:")
62
+ if results.matches:
63
+ for match in results.matches:
64
+ score = match.score
65
+ context = match.metadata.get("context", "No context available")
66
+
67
+ # Display score and context in a formatted way
68
+ st.markdown(f"**Score**: `{score}`")
69
+ st.markdown(f"**Context**: {context}")
70
+ st.markdown("---") # Divider for each result
71
+ else:
72
+ st.warning("No results found for this query.")
73
  else:
74
  st.error("Please enter a query and ensure the index is initialized.")