You can easily pull the model and vocabulary programmatically from this Hugging Face repository and translate English sentences locally.
1import os
2os.environ["KERAS_BACKEND"] = "jax"
3
4import numpy as np
5import keras
6from keras import layers
7import tensorflow as tf
8import json
9from huggingface_hub import hf_hub_download
10
11# 1. Download Model and Vocabularies
12repo_id = "theshubhamgoel/seq2seq-en-sp-translation"
13model_path = hf_hub_download(repo_id=repo_id, filename="seq2seq_en_sp_translation.keras")
14eng_vocab_path = hf_hub_download(repo_id=repo_id, filename="seq2seq_en_vocab.json")
15spa_vocab_path = hf_hub_download(repo_id=repo_id, filename="seq2seq_sp_vocab.json")
16
17# 2. Re-create tokenization helpers
18with open(eng_vocab_path, "r", encoding="utf-8") as f:
19 eng_data = json.load(f)
20eng_vocab = eng_data["id_to_word"].values()
21
22with open(spa_vocab_path, "r", encoding="utf-8") as f:
23 spa_data = json.load(f)
24spa_vocab = spa_data["id_to_word"].values()
25spa_index_lookup = {int(k): v for k, v in spa_data["id_to_word"].items()}
26
27# Preprocessing standardization
28strip_chars = string.punctuation + "¿"
29strip_chars = strip_chars.replace("[", "").replace("]", "")
30
31def custom_standardization(input_string):
32 lowercase = tf.strings.lower(input_string)
33 return tf.strings.regex_replace(lowercase, f"[{re.escape(strip_chars)}]", "")
34
35english_vectorizer = layers.TextVectorization(
36 max_tokens=15000, output_mode="int", output_sequence_length=20
37)
38spanish_vectorizer = layers.TextVectorization(
39 max_tokens=15000, output_mode="int", output_sequence_length=21, standardize=custom_standardization
40)
41
42english_vectorizer.set_vocabulary(list(eng_vocab))
43spanish_vectorizer.set_vocabulary(list(spa_vocab))
44
45# 3. Load Model
46model = keras.saving.load_model(model_path)
47
48def translate(sentence):
49 tokenized_input = english_vectorizer([sentence])
50 decoded_sentence = "[start]"
51 for i in range(20):
52 tokenized_target = spanish_vectorizer([decoded_sentence])
53 predictions = model.predict([tokenized_input, tokenized_target], verbose=0)
54 sampled_token_index = np.argmax(predictions[0, i, :])
55 sampled_token = spa_index_lookup.get(sampled_token_index, "[UNK]")
56 decoded_sentence += " " + sampled_token
57 if sampled_token == "[end]":
58 break
59 return decoded_sentence
60
61# Run translation!
62print(translate("I think they are happy."))