saas-product-matcher / example_search.py
ComparEdge's picture
Upload folder using huggingface_hub
e8b1b95 verified
Raw
History Blame Contribute Delete
1.47 kB
#!/usr/bin/env python3
"""
Standalone semantic search for ComparEdge SaaS Product Matcher.
Install: pip install sentence-transformers numpy huggingface_hub torch
Usage: python example_search.py "best CRM for small team"
"""
import sys, json
import numpy as np
import torch
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
from huggingface_hub import hf_hub_download
MODEL_ID = "ComparEdge/saas-product-matcher"
def main():
query = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else "best project management tool"
print(f"Loading {MODEL_ID}...")
model = SentenceTransformer(MODEL_ID)
emb_path = hf_hub_download(MODEL_ID, "product_embeddings.npy")
idx_path = hf_hub_download(MODEL_ID, "products_index.json")
embeddings = np.load(emb_path)
with open(idx_path) as f:
products = json.load(f)
q_emb = model.encode(query, normalize_embeddings=True)
scores = cos_sim(torch.tensor(q_emb), torch.tensor(embeddings))[0]
top_idx = scores.argsort(descending=True)[:5]
print(f"\nTop matches for: \"{query}\"")
print("-" * 60)
for i, idx in enumerate(top_idx):
p = products[idx]
print(f"{i+1}. {p['name']} ({p['category']})")
print(f" Score : {scores[idx]:.3f}")
print(f" About : {p['description']}")
print(f" Link : https://comparedge.com/tools/{p['slug']}")
print()
if __name__ == "__main__":
main()