Views
No views yet
all-MiniLM-L6-v2.tflite: The standard Float32 model.all-MiniLM-L6-v2-quant.tflite: An INT8 quantized version which is ~4x smaller and significantly faster on CPU, making it ideal for mobile and edge applications.apache-2.0sentence-transformers/all-MiniLM-L6-v2all-MiniLM-L6-v2. The original model was trained on a large corpus of text from the internet and may reflect the societal and historical biases present in that data. Users should be aware of this when using the model in downstream applications.tensorflow-lite Python library or directly in a mobile application (Android/iOS).1# 1. Install necessary libraries
2!pip install tensorflow
3!pip install huggingface_hub
4!pip install tokenizers
5
6# 2. Import libraries
7import tensorflow as tf
8from huggingface_hub import hf_hub_download
9from tokenizers import Tokenizer
10import numpy as np
11
12# 3. Download model and tokenizer from the Hub
13REPO_ID = "YourUsername/YourModelRepoName" # <-- Change this to your repo ID!
14TFLITE_MODEL_FILENAME = "all-MiniLM-L6-v2-quant.tflite"
15TOKENIZER_FILENAME = "tokenizer.json" # Make sure you upload the tokenizer.json file!
16
17model_path = hf_hub_download(repo_id=REPO_ID, filename=TFLITE_MODEL_FILENAME)
18tokenizer_path = hf_hub_download(repo_id=REPO_ID, filename=TOKENIZER_FILENAME)
19
20# 4. Load the TFLite model and tokenizer
21tokenizer = Tokenizer.from_file(tokenizer_path)
22tokenizer.enable_padding(pad_id=0, pad_token="[PAD]", length=128)
23interpreter = tf.lite.Interpreter(model_path=model_path)
24
25# 5. Prepare and run inference
26sentences = ["This is an example sentence.", "Here is another one."]
27
28# Tokenize input and get shapes
29encoded_input = [tokenizer.encode(s) for s in sentences]
30input_ids = tf.constant([e.ids for e in encoded_input])
31attention_mask = tf.constant([e.attention_mask for e in encoded_input])
32
33# Resize interpreter inputs and allocate tensors
34input_details = interpreter.get_input_details()
35interpreter.resize_tensor_input(input_details['index'], attention_mask.shape)
36interpreter.resize_tensor_input(input_details['index'], input_ids.shape)
37interpreter.allocate_tensors()
38
39# Set input tensors
40interpreter.set_tensor(input_details['index'], attention_mask)
41interpreter.set_tensor(input_details['index'], input_ids)
42
43# Run inference
44interpreter.invoke()
45
46# Get output and normalize
47output_details = interpreter.get_output_details()
48embeddings = interpreter.get_tensor(output_details['index'])
49normalized_embeddings = tf.math.l2_normalize(embeddings, axis=1).numpy()
50
51print("Embeddings generated successfully!")
52print(f"Shape: {normalized_embeddings.shape}")
53print(f"First embedding: {normalized_embeddings[:5]}...")