Downloads · 30 days
17
15% of all-time downloads
theshubhamgoel/seq2seq-en-sp-translation
seq2seq-en-sp-translation is a translation model from theshubhamgoel. Use it when you need text moved from one language to another. It is set up for keras. The card lists the license as mit.
This is a word-level Recurrent Neural Network (RNN) using an Encoder-Decoder architecture trained on the English-to-Spanish translation corpus. It uses a Bidirectional GRU in the Encoder to sum forward and backward st…
Downloads · 30 days
17
15% of all-time downloads
All-time downloads
116
Public
Repo size
418 MB
Likes
0
Public
Click a slice to open those files.
.keras418 MB · 100%
From the Hugging Face model README
This is a word-level Recurrent Neural Network (RNN) using an Encoder-Decoder architecture trained on the English-to-Spanish translation corpus. It uses a Bidirectional GRU in the Encoder to sum forward and backward states, and a Unidirectional GRU in the Decoder conditioned directly on the Encoder's bottleneck representation.
You can easily pull the model and vocabulary programmatically from this Hugging Face repository and translate English sentences locally.
pip install keras numpy tensorflow huggingface_hub
import os
os.environ["KERAS_BACKEND"] = "jax"
import numpy as np
import keras
from keras import layers
import tensorflow as tf
import json
from huggingface_hub import hf_hub_download
# 1. Download Model and Vocabularies
repo_id = "theshubhamgoel/seq2seq-en-sp-translation"
model_path = hf_hub_download(repo_id=repo_id, filename="seq2seq_en_sp_translation.keras")
eng_vocab_path = hf_hub_download(repo_id=repo_id, filename="seq2seq_en_vocab.json")
spa_vocab_path = hf_hub_download(repo_id=repo_id, filename="seq2seq_sp_vocab.json")
# 2. Re-create tokenization helpers
with open(eng_vocab_path, "r", encoding="utf-8") as f:
eng_data = json.load(f)
eng_vocab = eng_data["id_to_word"].values()
with open(spa_vocab_path, "r", encoding="utf-8") as f:
spa_data = json.load(f)
spa_vocab = spa_data["id_to_word"].values()
spa_index_lookup = {int(k): v for k, v in spa_data["id_to_word"].items()}
# Preprocessing standardization
strip_chars = string.punctuation + "¿"
strip_chars = strip_chars.replace("[", "").replace("]", "")
def custom_standardization(input_string):
lowercase = tf.strings.lower(input_string)
return tf.strings.regex_replace(lowercase, f"[{re.escape(strip_chars)}]", "")
english_vectorizer = layers.TextVectorization(
max_tokens=15000, output_mode="int", output_sequence_length=20
)
spanish_vectorizer = layers.TextVectorization(
max_tokens=15000, output_mode="int", output_sequence_length=21, standardize=custom_standardization
)
english_vectorizer.set_vocabulary(list(eng_vocab))
spanish_vectorizer.set_vocabulary(list(spa_vocab))
# 3. Load Model
model = keras.saving.load_model(model_path)
def translate(sentence):
tokenized_input = english_vectorizer([sentence])
decoded_sentence = "[start]"
for i in range(20):
tokenized_target = spanish_vectorizer([decoded_sentence])
predictions = model.predict([tokenized_input, tokenized_target], verbose=0)
sampled_token_index = np.argmax(predictions[0, i, :])
sampled_token = spa_index_lookup.get(sampled_token_index, "[UNK]")
decoded_sentence += " " + sampled_token
if sampled_token == "[end]":
break
return decoded_sentence
# Run translation!
print(translate("I think they are happy."))