|
import tensorflow as tf
|
|
import sentencepiece as spm
|
|
import numpy as np
|
|
|
|
|
|
tflite_model_path = r"C:\Users\dejan\AppData\Local\Google\Chrome SxS\User Data\optimization_guide_model_store\43\E6DC4029A1E4B4C1\EF94C116CBE73994\model.tflite"
|
|
spm_model_path = r"C:\Users\dejan\AppData\Local\Google\Chrome SxS\User Data\optimization_guide_model_store\43\E6DC4029A1E4B4C1\EF94C116CBE73994\sentencepiece.model"
|
|
|
|
|
|
sp = spm.SentencePieceProcessor()
|
|
sp.load(spm_model_path)
|
|
|
|
|
|
def preprocess_text(text, sp):
|
|
|
|
input_ids = sp.encode(text, out_type=int)
|
|
|
|
return np.array(input_ids, dtype=np.int32).reshape(1, -1)
|
|
|
|
|
|
interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
|
|
interpreter.allocate_tensors()
|
|
|
|
|
|
input_details = interpreter.get_input_details()
|
|
output_details = interpreter.get_output_details()
|
|
|
|
|
|
def generate_embeddings(text):
|
|
|
|
input_data = preprocess_text(text, sp)
|
|
|
|
|
|
interpreter.resize_tensor_input(input_details[0]['index'], input_data.shape)
|
|
interpreter.allocate_tensors()
|
|
|
|
|
|
interpreter.set_tensor(input_details[0]['index'], input_data)
|
|
|
|
|
|
interpreter.invoke()
|
|
|
|
|
|
embedding = interpreter.get_tensor(output_details[0]['index'])
|
|
return embedding
|
|
|
|
|
|
text = "Sample passage for embedding generation"
|
|
embedding = generate_embeddings(text)
|
|
print("Generated Embedding:", embedding)
|
|
|