shrut123 commited on
Commit
9610c39
1 Parent(s): a19ad68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
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("Pinecone Query Search on 'pubmed-splade' Index")
8
 
9
  # Initialize Pinecone globally
10
  index = None
@@ -14,7 +14,8 @@ 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
- 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,9 +23,10 @@ def initialize_pinecone():
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,9 +40,10 @@ pc = initialize_pinecone()
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,25 +53,18 @@ if pc:
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.")
 
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
  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
  # 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
+ index = pc.Index(index_name)
29
+ return index
30
  else:
31
  st.error(f"Index '{index_name}' not found!")
32
  return None
 
40
 
41
  # If Pinecone initialized successfully, proceed with index management
42
  if pc:
43
+ # Connect directly to 'pubmed-splade' index
44
  index = connect_to_index(pc)
45
 
46
+ # Model for query encoding
47
  model = SentenceTransformer('msmarco-bert-base-dot-v5')
48
 
49
  # Query input
 
53
  if st.button("Search Query"):
54
  if query_text and index:
55
  dense_vector = encode_query(model, query_text)
56
+
57
  # Search the index
58
  results = index.query(
59
  vector=dense_vector,
60
+ top_k=3,
61
  include_metadata=True
62
  )
63
 
64
  st.write("### Search Results:")
65
+ for match in results.matches:
66
+ st.markdown(f"#### Score: **{match.score:.4f}**")
67
+ st.write(f"**Context:** {match.metadata.get('context', 'No context available.')}")
68
+ st.write("---")
 
 
 
 
 
 
 
69
  else:
70
  st.error("Please enter a query and ensure the index is initialized.")