🎭 English-to-Spanish Sequence-to-Sequence Translation Model

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.

πŸ“Š Model Details

  • Developed by: Shubham Goel
  • Model Type: Word-Level RNN Encoder-Decoder (Seq2Seq)
  • Framework: Keras 3 (using JAX backend)
  • Training Dataset: English-to-Spanish translation corpus (50,000 sentence pairs)
  • Vocabulary Size: 15,000 words for both English and Spanish
  • Max Length: 20 words (English source), 20 words (Spanish target)
  • License: MIT

Architecture Specs

  • Embedding Dimension: 256
  • GRU Hidden Units: 1024 (summed in Bidirectional Encoder)
  • Output Projection: 15,000-way Dense softmax
  • Total Parameters: 34,869,912 (~35M parameters)

🐍 How to Load and Translate Text in Python

You can easily pull the model and vocabulary programmatically from this Hugging Face repository and translate English sentences locally.

Prerequisites

pip install keras numpy tensorflow huggingface_hub

Python Inference Script

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."))
Downloads last month
20
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Space using theshubhamgoel/seq2seq-en-sp-translation 1