Relocating to another country has become a viable reality for many. If you’re considering a move, you might find this article interesting https://www.msn.com/en-gb/family-and-relationships/parenting/thinking-of-moving-abroad-with-kids-this-gulf-country-is-surprisingly-family-friendly/ar-AA1QeYOV. Beyond cultural adjustments, overcoming the language barrier is often the greatest challenge. Fortunately, modern technology simplifies this process.

In this guide, we’ll build a free, open-source translation app. Using the Hugging Face platform and the mBART multilingual model, we will create a tool capable of translating directly between multiple languages.

What is Hugging Face?

Hugging Face is a platform that provides open-source, state-of-the-art machine learning models for tasks like sentiment analysis and chatbots. For this project, we’ll use its Transformers library, which allows us to interact with advanced AI models in just a few lines of Python.

Introducing the model

We will use facebook/mbart-large-50-many-to-many-mmt, a model developed by Facebook AI. It is part of the mBART (Multilingual Bidirectional and Auto-Regressive Transformers) series.

This “many-to-many” model translates directly between dozens of languages without using English as an intermediary. Supporting over 50 languages, it ensures high speed and accuracy by avoiding the “bridge” language step.

How does the model work?

The mBART model uses a transformer architecture, the same technology behind GPT and BERT. Transformers excel at understanding context, which is vital for accurate translation.

The process is straightforward:

  1. Input: Provide the text to be translated.
  2. Specify Languages: Use codes like en_XX (English) or fr_XX (French).
  3. Generate: The model outputs the translated text.

Why uses this model?

Unlike paid services like Google Translate, using this Hugging Face model offers:

  1. No cost for high-volume usage.
  2. Full control over the model and translation process.
  3. Customization to integrate into your own apps.

Setting up

To use this model, we need two components:

  1. A Tokenizer to convert text into a format the model understands.
  2. The Model itself to generate the translation.

Code

Here’s the full code to get you started. We’ll break it down step-by-step.

Step 1: Set up the environment

First, install the Transformers, Torch, and Streamlit libraries:

pip install transformers torch streamlit

Step 2: Import libraries

“`python

from transformers import MBartForConditionalGeneration, MBart50TokenizerFast

import torch

import streamlit as st

“`

  1. MBartForConditionalGeneration handles the translation.
  2. MBart50TokenizerFast processes the text.
  3. torch manages computations and GPU acceleration.
  4. Streamlit helps turn Python scripts into interactive web apps.

Step 3: Load the model and tokenizer

“`python

model_name = “facebook/mbart-large-50-many-to-many-mmt”

tokenizer = MBart50TokenizerFast.from_pretrained(model_name)

model = MBartForConditionalGeneration.from_pretrained(model_name)

Use GPU if available

device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)

model.to(device)

“`

Step 4: Create the Translation Function

“`python

def translate_text(text, source_lang, target_lang):

tokenizer.src_lang = source_lang

encoded_text = tokenizer(text, return_tensors=”pt”).to(device)

generated_tokens = model.generate(

**encoded_text,

forced_bos_token_id=tokenizer.lang_code_to_id[target_lang]

)

return tokenizer.decode(generated_tokens[0], skip_special_tokens=True)

“`

Translator app
Translator app

Step 5: Test the Function

“`python

source_language = “en_XX”

target_language = “hi_IN”

text = “Hello, how are you?”

translation = translate_text(text, source_language, target_language)

print(f”Original: {text}”)

print(f”Translated: {translation}”)

“`

Build the App

Follow these steps to create a functional and user-friendly application from start to finish:

Step 1: Design the UI

“`python

st.title(“🌍 Language Translation App”)

st.markdown(“Translate text using AI-powered models from Hugging Face!”)

text = st.text_area(“Enter text to translate”, “”)

Add more language options as needed

lang_options = [“en_XX”, “fr_XX”, “es_XX”, “hi_IN”]

source_language = st.selectbox(“Source language”, options=lang_options)

target_language = st.selectbox(“Target language”, options=lang_options)

“`

Step 2: Connect the function

“`python

if st.button(“Translate”):

if not text.strip():

st.warning(“Please enter some text.”)

else:

with st.spinner(“Translating…”):

translation = translate_text(text, source_language, target_language)

st.success(“Done!”)

st.write(“### Translated Text:”)

st.write(translation)

“`

Step 3: Add final touches

You can add a sidebar for examples and a footer for credits.

“`python

st.sidebar.header(“Examples”)

st.sidebar.markdown(“- English to French: ‘Hello, how are you?'”)

st.markdown(“—“)

st.markdown(“Built with ❤️ using Hugging Face and Streamlit”)

“`

Step 4: Run the app

Launch your app from the terminal by running:

streamlit run app.py

You now have a functional, professional-grade translation app ready to use.