Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
-
from pinecone import Pinecone
|
4 |
from sentence_transformers import SentenceTransformer
|
5 |
|
6 |
# Title of the Streamlit App
|
7 |
-
st.title("Pinecone
|
8 |
|
9 |
# Initialize Pinecone globally
|
10 |
index = None
|
@@ -20,41 +20,29 @@ def initialize_pinecone():
|
|
20 |
st.error("Pinecone API key not found! Please set the PINECONE_API_KEY environment variable.")
|
21 |
return None
|
22 |
|
23 |
-
# Function to
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
spec=ServerlessSpec(cloud='aws', region='us-west-2') # Change to your cloud provider and region
|
32 |
-
)
|
33 |
-
st.success(f"Created new index '{index_name}'")
|
34 |
else:
|
35 |
-
st.
|
36 |
-
|
37 |
-
index = pc.Index(index_name)
|
38 |
-
return index
|
39 |
|
40 |
# Function to encode query using sentence transformers model
|
41 |
def encode_query(model, query_text):
|
42 |
return model.encode(query_text).tolist()
|
43 |
|
44 |
# Initialize Pinecone
|
45 |
-
pc =
|
46 |
|
47 |
# If Pinecone initialized successfully, proceed with index management
|
48 |
if pc:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
# Button to create or connect to index
|
53 |
-
if st.button("Create or Connect to Index"):
|
54 |
-
global index # Make index a global variable
|
55 |
-
index = create_or_connect_index(pc, index_name, dimension)
|
56 |
-
if index:
|
57 |
-
st.success(f"Successfully connected to index '{index_name}'")
|
58 |
|
59 |
# Model for query encoding
|
60 |
model = SentenceTransformer('msmarco-bert-base-dot-v5')
|
@@ -80,11 +68,3 @@ if pc:
|
|
80 |
st.write(f"ID: {match.id}, Score: {match.score}, Metadata: {match.metadata}")
|
81 |
else:
|
82 |
st.error("Please enter a query and ensure the index is initialized.")
|
83 |
-
|
84 |
-
# Option to delete index
|
85 |
-
if st.button("Delete Index"):
|
86 |
-
if pc and index_name in pc.list_indexes().names():
|
87 |
-
pc.delete_index(index_name)
|
88 |
-
st.success(f"Index '{index_name}' deleted successfully.")
|
89 |
-
else:
|
90 |
-
st.error("Index not found.")
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
+
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
|
|
|
20 |
st.error("Pinecone API key not found! Please set the PINECONE_API_KEY environment variable.")
|
21 |
return None
|
22 |
|
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
|
|
|
|
|
34 |
|
35 |
# Function to encode query using sentence transformers model
|
36 |
def encode_query(model, query_text):
|
37 |
return model.encode(query_text).tolist()
|
38 |
|
39 |
# Initialize Pinecone
|
40 |
+
pc = initialize_ppinecone()
|
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')
|
|
|
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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|