Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,86 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
-
import
|
4 |
from sentence_transformers import SentenceTransformer
|
5 |
-
from transformers import AutoTokenizer
|
6 |
-
from splade.models.transformer_rep import Splade
|
7 |
-
import pinecone
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
pinecone.init(api_key=api_key, environment='us-east1-gcp')
|
12 |
-
index_name = 'pubmed-splade'
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
#
|
24 |
-
|
|
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
sparse_model = Splade(sparse_model_id, agg='max').to(device)
|
29 |
-
sparse_model.eval()
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
def encode(text: str):
|
36 |
-
# Dense vector
|
37 |
-
dense_vec = dense_model.encode(text).tolist()
|
38 |
-
|
39 |
-
# Sparse vector
|
40 |
-
input_ids = tokenizer(text, return_tensors='pt')
|
41 |
-
with torch.no_grad():
|
42 |
-
sparse_vec = sparse_model(d_kwargs=input_ids.to(device))['d_rep'].squeeze()
|
43 |
-
|
44 |
-
# Extract non-zero values and indices for sparse vector
|
45 |
-
indices = sparse_vec.nonzero().squeeze().cpu().tolist()
|
46 |
-
values = sparse_vec[indices].cpu().tolist()
|
47 |
-
|
48 |
-
sparse_dict = {"indices": indices, "values": values}
|
49 |
-
return dense_vec, sparse_dict
|
50 |
-
|
51 |
-
# Function for hybrid search scaling
|
52 |
-
def hybrid_scale(dense, sparse, alpha: float):
|
53 |
-
if alpha < 0 or alpha > 1:
|
54 |
-
raise ValueError("Alpha must be between 0 and 1")
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
return hdense, hsparse
|
63 |
-
|
64 |
-
# Streamlit UI
|
65 |
-
st.title("PubMed Search Application")
|
66 |
-
query = st.text_input("Enter your query:", "")
|
67 |
|
68 |
-
#
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
dense_vec, sparse_vec = encode(query)
|
74 |
-
|
75 |
-
# Scale vectors based on slider value
|
76 |
-
hdense, hsparse = hybrid_scale(dense_vec, sparse_vec, alpha)
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
+
from pinecone import Pinecone, ServerlessSpec
|
4 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
5 |
|
6 |
+
# Title of the Streamlit App
|
7 |
+
st.title("Pinecone Index Management with Streamlit")
|
|
|
|
|
8 |
|
9 |
+
# Function to initialize Pinecone
|
10 |
+
def initialize_pinecone():
|
11 |
+
api_key = os.getenv('PINECONE_API_KEY') # Get Pinecone API key from environment variable
|
12 |
+
if api_key:
|
13 |
+
# Initialize Pinecone client using the new class instance method
|
14 |
+
pc = Pinecone(api_key=api_key)
|
15 |
+
return pc
|
16 |
+
else:
|
17 |
+
st.error("Pinecone API key not found! Please set the PINECONE_API_KEY environment variable.")
|
18 |
+
return None
|
19 |
|
20 |
+
# Function to create or connect to an index
|
21 |
+
def create_or_connect_index(pc, index_name, dimension):
|
22 |
+
if index_name not in pc.list_indexes().names():
|
23 |
+
# Create index if it doesn't exist
|
24 |
+
pc.create_index(
|
25 |
+
name=index_name,
|
26 |
+
dimension=dimension,
|
27 |
+
metric='dotproduct', # Change this based on your use case
|
28 |
+
spec=ServerlessSpec(cloud='aws', region='us-west-2') # Change to your cloud provider and region
|
29 |
+
)
|
30 |
+
st.success(f"Created new index '{index_name}'")
|
31 |
+
else:
|
32 |
+
st.info(f"Index '{index_name}' already exists.")
|
33 |
+
# Connect to the index
|
34 |
+
index = pc.Index(index_name)
|
35 |
+
return index
|
36 |
|
37 |
+
# Function to encode query using sentence transformers model
|
38 |
+
def encode_query(model, query_text):
|
39 |
+
return model.encode(query_text).tolist()
|
40 |
|
41 |
+
# Initialize Pinecone
|
42 |
+
pc = initialize_pinecone()
|
|
|
|
|
43 |
|
44 |
+
# If Pinecone initialized successfully, proceed with index management
|
45 |
+
if pc:
|
46 |
+
index_name = st.text_input("Enter Index Name", "my_index")
|
47 |
+
dimension = st.number_input("Enter Vector Dimension", min_value=1, value=768)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# Button to create or connect to index
|
50 |
+
if st.button("Create or Connect to Index"):
|
51 |
+
index = create_or_connect_index(pc, index_name, dimension)
|
52 |
+
if index:
|
53 |
+
st.success(f"Successfully connected to index '{index_name}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
# Model for query encoding
|
56 |
+
model = SentenceTransformer('msmarco-bert-base-dot-v5')
|
57 |
|
58 |
+
# Query input
|
59 |
+
query_text = st.text_input("Enter a Query to Search", "Can clinicians use the PHQ-9 to assess depression?")
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
# Button to encode query and search the Pinecone index
|
62 |
+
if st.button("Search Query"):
|
63 |
+
if query_text and index:
|
64 |
+
dense_vector = encode_query(model, query_text)
|
65 |
+
st.write(f"Encoded Query Vector: {dense_vector}")
|
66 |
+
|
67 |
+
# Search the index (sparse values can be added here as well)
|
68 |
+
results = index.query(
|
69 |
+
vector=dense_vector,
|
70 |
+
top_k=5,
|
71 |
+
include_metadata=True
|
72 |
+
)
|
73 |
+
|
74 |
+
st.write("Search Results:")
|
75 |
+
for match in results.matches:
|
76 |
+
st.write(f"ID: {match.id}, Score: {match.score}, Metadata: {match.metadata}")
|
77 |
+
else:
|
78 |
+
st.error("Please enter a query and ensure the index is initialized.")
|
79 |
+
|
80 |
+
# Option to delete index
|
81 |
+
if st.button("Delete Index"):
|
82 |
+
if pc and index_name in pc.list_indexes().names():
|
83 |
+
pc.delete_index(index_name)
|
84 |
+
st.success(f"Index '{index_name}' deleted successfully.")
|
85 |
+
else:
|
86 |
+
st.error("Index not found.")
|