Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
mvectors
Browse files- pages/Multimodal_Conversational_Search.py +0 -1
- pages/Semantic_Search.py +8 -1
- semantic_search/all_search_execute.py +41 -31
- utilities/mvectors.py +105 -0
pages/Multimodal_Conversational_Search.py
CHANGED
|
@@ -249,7 +249,6 @@ def render_answer(question,answer,index,res_img):
|
|
| 249 |
unsafe_allow_html=True
|
| 250 |
)
|
| 251 |
st.image(res_img[i]['file'])
|
| 252 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 253 |
else:
|
| 254 |
if(res_img[i]['file'].lower()!='none' and idx < 1):
|
| 255 |
col3,col4,col5 = st.columns([33,33,33])
|
|
|
|
| 249 |
unsafe_allow_html=True
|
| 250 |
)
|
| 251 |
st.image(res_img[i]['file'])
|
|
|
|
| 252 |
else:
|
| 253 |
if(res_img[i]['file'].lower()!='none' and idx < 1):
|
| 254 |
col3,col4,col5 = st.columns([33,33,33])
|
pages/Semantic_Search.py
CHANGED
|
@@ -671,7 +671,14 @@ if(search_all_type == True or 1==1):
|
|
| 671 |
########################## enable for query_rewrite ########################
|
| 672 |
if rewrite_query:
|
| 673 |
st.session_state.input_is_rewrite_query = 'enabled'
|
| 674 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 675 |
st.subheader(':blue[Hybrid Search]')
|
| 676 |
with st.expander("Set query Weightage:"):
|
| 677 |
st.number_input("Keyword %", min_value=0, max_value=100, value=100, step=5, key='input_Keyword-weight', help=None)
|
|
|
|
| 671 |
########################## enable for query_rewrite ########################
|
| 672 |
if rewrite_query:
|
| 673 |
st.session_state.input_is_rewrite_query = 'enabled'
|
| 674 |
+
st.subheader(':blue[Vector Search]')
|
| 675 |
+
|
| 676 |
+
mvector_rerank = st.checkbox("Search and Re-rank with Token level vectors",key = 'mvector_rerank',help = "Enabling this option uses 'all-MiniLM-L6-v2' model's token level embeddings to retrieve documents and MaxSim to re-rank documents.\n\n Hugging Face Model: https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2")
|
| 677 |
+
|
| 678 |
+
if(mvector_rerank):
|
| 679 |
+
st.session_state.input_mvector_rerank = True
|
| 680 |
+
else:
|
| 681 |
+
st.session_state.input_mvector_rerank = False
|
| 682 |
st.subheader(':blue[Hybrid Search]')
|
| 683 |
with st.expander("Set query Weightage:"):
|
| 684 |
st.number_input("Keyword %", min_value=0, max_value=100, value=100, step=5, key='input_Keyword-weight', help=None)
|
semantic_search/all_search_execute.py
CHANGED
|
@@ -18,10 +18,7 @@ from requests.auth import HTTPBasicAuth
|
|
| 18 |
from datetime import datetime
|
| 19 |
import boto3
|
| 20 |
import streamlit as st
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
current_date_time = (datetime.now()).isoformat()
|
| 26 |
today_ = datetime.today().strftime('%Y-%m-%d')
|
| 27 |
|
|
@@ -213,13 +210,19 @@ def handler(input_,session_id):
|
|
| 213 |
}
|
| 214 |
}]
|
| 215 |
del keyword_payload['match']
|
| 216 |
-
|
| 217 |
-
# print(keyword_payload)
|
| 218 |
-
|
| 219 |
-
|
| 220 |
hybrid_payload["query"]["hybrid"]["queries"].append(keyword_payload)
|
| 221 |
|
| 222 |
if('Vector Search' in search_types):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
# path3 = "_plugins/_ml/models/"+BEDROCK_TEXT_MODEL_ID+"/_predict"
|
| 225 |
|
|
@@ -249,7 +252,8 @@ def handler(input_,session_id):
|
|
| 249 |
# }
|
| 250 |
|
| 251 |
#using neural query
|
| 252 |
-
|
|
|
|
| 253 |
"neural": {
|
| 254 |
"product_description_vector": {
|
| 255 |
"query_text": query,
|
|
@@ -373,15 +377,8 @@ def handler(input_,session_id):
|
|
| 373 |
if(st.session_state.input_price!=(0,0)):
|
| 374 |
sparse_payload['bool']['filter'].append({"range": {"price": {"gte": st.session_state.input_price[0],"lte": st.session_state.input_price[1] }}})
|
| 375 |
|
| 376 |
-
|
| 377 |
-
# print("sparse_payload**************")
|
| 378 |
-
# print(sparse_payload)
|
| 379 |
-
|
| 380 |
-
|
| 381 |
###### end of efficient filter applying #####
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
#print(sparse_payload)
|
| 385 |
|
| 386 |
# sparse_payload = {
|
| 387 |
|
|
@@ -409,21 +406,34 @@ def handler(input_,session_id):
|
|
| 409 |
r = requests.post(url, auth=awsauth, json=payload, headers=headers)
|
| 410 |
|
| 411 |
if(len(hybrid_payload["query"]["hybrid"]["queries"])==1):
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
hybrid_payload["query"] = single_query
|
| 415 |
-
if(st.session_state.re_ranker == 'true' and st.session_state.input_reranker == 'Cohere Rerank'):
|
| 416 |
-
path = "demostore-search-index/_search?search_pipeline=rerank_pipeline"
|
| 417 |
url = host + path
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
|
| 428 |
|
| 429 |
else:
|
|
|
|
| 18 |
from datetime import datetime
|
| 19 |
import boto3
|
| 20 |
import streamlit as st
|
| 21 |
+
import utilities.mvectors as cb
|
|
|
|
|
|
|
|
|
|
| 22 |
current_date_time = (datetime.now()).isoformat()
|
| 23 |
today_ = datetime.today().strftime('%Y-%m-%d')
|
| 24 |
|
|
|
|
| 210 |
}
|
| 211 |
}]
|
| 212 |
del keyword_payload['match']
|
| 213 |
+
|
|
|
|
|
|
|
|
|
|
| 214 |
hybrid_payload["query"]["hybrid"]["queries"].append(keyword_payload)
|
| 215 |
|
| 216 |
if('Vector Search' in search_types):
|
| 217 |
+
if(st.session_state.input_mvector_rerank):
|
| 218 |
+
query_vector = cb.vectorise(query,False)
|
| 219 |
+
vector_field = "description_vector"
|
| 220 |
+
print("-------------COLBERT-----1-------------------------------------------------")
|
| 221 |
+
vector_payload = {"knn": {}}
|
| 222 |
+
vector_payload["knn"][vector_field]= {
|
| 223 |
+
"vector":query_vector,
|
| 224 |
+
"k": k_
|
| 225 |
+
}
|
| 226 |
|
| 227 |
# path3 = "_plugins/_ml/models/"+BEDROCK_TEXT_MODEL_ID+"/_predict"
|
| 228 |
|
|
|
|
| 252 |
# }
|
| 253 |
|
| 254 |
#using neural query
|
| 255 |
+
else:
|
| 256 |
+
vector_payload = {
|
| 257 |
"neural": {
|
| 258 |
"product_description_vector": {
|
| 259 |
"query_text": query,
|
|
|
|
| 377 |
if(st.session_state.input_price!=(0,0)):
|
| 378 |
sparse_payload['bool']['filter'].append({"range": {"price": {"gte": st.session_state.input_price[0],"lte": st.session_state.input_price[1] }}})
|
| 379 |
|
| 380 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
###### end of efficient filter applying #####
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
# sparse_payload = {
|
| 384 |
|
|
|
|
| 406 |
r = requests.post(url, auth=awsauth, json=payload, headers=headers)
|
| 407 |
|
| 408 |
if(len(hybrid_payload["query"]["hybrid"]["queries"])==1):
|
| 409 |
+
if(st.session_state.input_mvector_rerank and 'Vector Search' in search_types):
|
| 410 |
+
path = "retail-search-colbert-description/_search"
|
|
|
|
|
|
|
|
|
|
| 411 |
url = host + path
|
| 412 |
+
r = requests.get(url, auth=awsauth, json=hybrid_payload, headers=headers)
|
| 413 |
+
print(r.status_code)
|
| 414 |
+
#print(r.text)
|
| 415 |
+
response_ = json.loads(r.text)
|
| 416 |
+
print("-------------colbert ---- 3-----------")
|
| 417 |
+
#print(response_)
|
| 418 |
+
docs = response_['hits']['hits']
|
| 419 |
+
docs = cb.search(docs)
|
| 420 |
+
print("-------------COLBERT------------5------------------------------------------")
|
| 421 |
+
else:
|
| 422 |
+
single_query = hybrid_payload["query"]["hybrid"]["queries"][0]
|
| 423 |
+
del hybrid_payload["query"]["hybrid"]
|
| 424 |
+
hybrid_payload["query"] = single_query
|
| 425 |
+
if(st.session_state.re_ranker == 'true' and st.session_state.input_reranker == 'Cohere Rerank'):
|
| 426 |
+
path = "demostore-search-index/_search?search_pipeline=rerank_pipeline"
|
| 427 |
+
url = host + path
|
| 428 |
+
hybrid_payload["ext"] = {"rerank": {
|
| 429 |
+
"query_context": {
|
| 430 |
+
"query_text": query
|
| 431 |
+
}
|
| 432 |
+
}}
|
| 433 |
+
|
| 434 |
+
r = requests.get(url, auth=awsauth, json=hybrid_payload, headers=headers)
|
| 435 |
+
response_ = json.loads(r.text)
|
| 436 |
+
docs = response_['hits']['hits']
|
| 437 |
|
| 438 |
|
| 439 |
else:
|
utilities/mvectors.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModel
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import boto3
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
runtime = boto3.client('sagemaker-runtime',region_name='us-east-1')
|
| 10 |
+
# Load model from HuggingFace Hub
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
| 12 |
+
endpoint_name = 'huggingface-pytorch-inference-2025-05-21-16-31-07-967'
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def mean_pooling(token_embeddings, attention_mask):
|
| 16 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 17 |
+
return torch.sum(token_embeddings * input_mask_expanded, dim=1) / \
|
| 18 |
+
torch.clamp(input_mask_expanded.sum(dim=1), min=1e-9)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def vectorise(sentence,token_level_vectors):
|
| 22 |
+
print("-------------colbert ---- 2-----------")
|
| 23 |
+
encoded_input = tokenizer(sentence, padding=True, truncation=True, return_tensors='pt')
|
| 24 |
+
# Get input IDs (token IDs)
|
| 25 |
+
input_ids = encoded_input['input_ids'][0]
|
| 26 |
+
|
| 27 |
+
# Convert IDs to tokens
|
| 28 |
+
tokens = tokenizer.convert_ids_to_tokens(input_ids)
|
| 29 |
+
model_output = runtime.invoke_endpoint(
|
| 30 |
+
EndpointName=endpoint_name,
|
| 31 |
+
ContentType="application/json",
|
| 32 |
+
Body=json.dumps({"inputs": sentence})
|
| 33 |
+
)
|
| 34 |
+
token_vectors = json.loads(model_output['Body'].read().decode())
|
| 35 |
+
if(token_level_vectors):
|
| 36 |
+
return tokens,token_vectors
|
| 37 |
+
|
| 38 |
+
token_vectors_tensor = torch.tensor(token_vectors)
|
| 39 |
+
attention_mask = encoded_input['attention_mask']
|
| 40 |
+
|
| 41 |
+
# Perform pooling
|
| 42 |
+
sentence_embeddings = mean_pooling(token_vectors_tensor, attention_mask)
|
| 43 |
+
|
| 44 |
+
# Normalize embeddings
|
| 45 |
+
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
|
| 46 |
+
|
| 47 |
+
return sentence_embeddings[0].tolist()
|
| 48 |
+
|
| 49 |
+
def search(hits):
|
| 50 |
+
print("-------------COLBERT------------4------------------------------------------")
|
| 51 |
+
tokens,token_vectors = vectorise(st.session_state.input_text,True)
|
| 52 |
+
final_docs = []
|
| 53 |
+
for ind,j in enumerate(hits):
|
| 54 |
+
max_score_dict_list = []
|
| 55 |
+
doc={"_source":
|
| 56 |
+
{
|
| 57 |
+
"description":j["_source"]["description"],"caption":j["_source"]["title"],
|
| 58 |
+
"image_s3_url":j["_source"]["image_s3_url"],"price":j["_source"]["price"],
|
| 59 |
+
"style":j["_source"]["style"],"category":j["_source"]["category"]},"_id":j["_id"],"_score":j["_score"]}
|
| 60 |
+
|
| 61 |
+
if("gender_affinity" in j["_source"]):
|
| 62 |
+
doc["_source"]["gender_affinity"] = j["_source"]["gender_affinity"]
|
| 63 |
+
else:
|
| 64 |
+
doc["_source"]["gender_affinity"] = ""
|
| 65 |
+
#print(j["_source"]["title"])
|
| 66 |
+
source_doc_token_keys = list(j["_source"].keys())
|
| 67 |
+
with_s = [x for x in source_doc_token_keys if x.startswith("description-token-")]
|
| 68 |
+
add_score = 0
|
| 69 |
+
|
| 70 |
+
for index,i in enumerate(token_vectors[0]):
|
| 71 |
+
token = tokens[index]
|
| 72 |
+
if(token!='[SEP]' and token!='[CLS]'):
|
| 73 |
+
query_token_vector = np.array(i)
|
| 74 |
+
print("query token: "+token)
|
| 75 |
+
print("-----------------")
|
| 76 |
+
scores = []
|
| 77 |
+
for m in with_s:
|
| 78 |
+
m_arr = m.split("-")
|
| 79 |
+
if(m_arr[-1]!='[SEP]' and m_arr[-1]!='[CLS]'):
|
| 80 |
+
#print("document token: "+m_arr[3])
|
| 81 |
+
doc_token_vector = np.array(j["_source"][m])
|
| 82 |
+
score = np.dot(query_token_vector,doc_token_vector)
|
| 83 |
+
scores.append({"doc_token":m_arr[3],"score":score})
|
| 84 |
+
#print({"doc_token":m_arr[3],"score":score})
|
| 85 |
+
|
| 86 |
+
newlist = sorted(scores, key=lambda d: d['score'], reverse=True)
|
| 87 |
+
max_score = newlist[0]['score']
|
| 88 |
+
add_score+=max_score
|
| 89 |
+
max_score_dict_list.append(newlist[0])
|
| 90 |
+
print(newlist[0])
|
| 91 |
+
max_score_dict_list_sorted = sorted(max_score_dict_list, key=lambda d: d['score'], reverse=True)
|
| 92 |
+
print(max_score_dict_list_sorted)
|
| 93 |
+
# print(add_score)
|
| 94 |
+
doc["total_score"] = add_score
|
| 95 |
+
doc['max_score_dict_list_sorted'] = max_score_dict_list_sorted
|
| 96 |
+
final_docs.append(doc)
|
| 97 |
+
final_docs_sorted = sorted(final_docs, key=lambda d: d['total_score'], reverse=True)
|
| 98 |
+
print("-------------COLBERT-----final--------")
|
| 99 |
+
print(final_docs_sorted)
|
| 100 |
+
return final_docs_sorted
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|