--- dataset_info: features: - name: emoji dtype: string - name: message dtype: string - name: embed sequence: float64 splits: - name: train num_bytes: 30665042 num_examples: 3722 download_size: 24682308 dataset_size: 30665042 configs: - config_name: default data_files: - split: train path: data/train-* license: mit task_categories: - feature-extraction language: - en tags: - semantic-search - embeddings - emoji size_categories: - 1K=2.7.0 from sentence_transformers import SentenceTransformer model = SentenceTransformer('Alibaba-NLP/gte-large-en-v1.5', trust_remote_code=True) ``` define a minimal semantic search inference function:
Click me to expand the inference function code ```py import numpy as np import pandas as pd from sentence_transformers import SentenceTransformer from sentence_transformers.util import semantic_search def get_top_emojis( query: str, emoji_df: pd.DataFrame, model, top_k: int = 5, num_digits: int = 4, ) -> list: """ Performs semantic search to find the most relevant emojis for a given query. Args: query (str): The search query. emoji_df (pd.DataFrame): DataFrame containing emoji metadata and embeddings. model (SentenceTransformer): The sentence transformer model for encoding. top_k (int): Number of top results to return. num_digits (int): Number of digits to round scores to Returns: list: A list of dicts, where each dict represents a top match. Each dict has keys 'emoji', 'message', and 'score' """ query_embed = model.encode(query) embeddings_array = np.vstack(emoji_df.embed.values, dtype=np.float32) hits = semantic_search(query_embed, embeddings_array, top_k=top_k)[0] # Extract the top hits + metadata results = [ { "emoji": emoji_df.loc[hit["corpus_id"], "emoji"], "message": emoji_df.loc[hit["corpus_id"], "message"], "score": round(hit["score"], num_digits), } for hit in hits ] return results ```
run inference! ```py import pprint as pp query_text = "that is flames" top_emojis = get_top_emojis(query_text, df, model, top_k=5) pp.pprint(top_emojis, indent=2) # [ {'emoji': '❤\u200d🔥', 'message': 'heart on fire', 'score': 0.7043}, # {'emoji': '🥵', 'message': 'hot face', 'score': 0.694}, # {'emoji': '😳', 'message': 'flushed face', 'score': 0.6794}, # {'emoji': '🔥', 'message': 'fire', 'score': 0.6744}, # {'emoji': '🧨', 'message': 'firecracker', 'score': 0.663}] ```