In this short tutorial we’ve worked on Google Colab, so go get an account here if you don’t have one already.
Here’s a link to this small project, where we basically translated the text from English to Croatian language with a Facebook NLLB-200 model. This is a machine translation model primarily intended for research in machine translation, – especially for low-resource languages. It allows for single sentence translation among 200 languages.
Let’s quickly ramble through the code, but if you enter the link to the project you will also notice there are comments interpreting every line of the code.
We started by installing transfomers and torch:
!pip install transformers
!pip install torch
The following piece of code is using the Transformers library, which is commonly used for natural language processing tasks like text classification, translation, and question answering, among others.
from transformers.utils import logging
logging.set_verbosity_error()
The line from transformers.utils import logging imports the logging module from the Transformers library. This module provides utilities for logging information during the execution of the code, which can be useful for debugging and monitoring.
The next line logging.set_verbosity_error() sets the logging verbosity level to “error”. This means that only error messages will be displayed in the log output, suppressing lower severity messages like warnings or informational messages. This can be helpful if you want to reduce the amount of information displayed during the execution of your code, especially if you’re primarily interested in errors or critical issues.
This code snippet imports two libraries: transformers and torch.
from transformers import pipeline
import torch
from transformers import pipeline: This line imports the pipeline module from the transformers library. The transformers library is a popular open-source library developed by Hugging Face, primarily used for natural language processing (NLP) tasks such as text classification, named entity recognition, question answering, and more. The pipeline module allows you to easily construct pipelines for various NLP tasks, abstracting away much of the complexity involved in setting up and using pre-trained models for these tasks.
import torch: This line imports the torch library. torch is the core library for PyTorch, an open-source machine learning framework primarily developed by Facebook’s AI Research lab (FAIR). PyTorch is widely used for building and training deep learning models, including those for NLP tasks. In the context of using the transformers library, PyTorch is often used as the backend for implementing neural network architectures and performing computations efficiently, especially on GPUs. By importing torch, you have access to its functionalities, including tensor operations, neural network building blocks, and GPU acceleration.
This code creates a translation pipeline using the Hugging Face Transformers library.
translator = pipeline(
task="translation",
model="facebook/nllb-200-distilled-600M",
torch_dtype=torch.bfloat16)
Let’s break it down:
pipeline: This function is used to create a processing pipeline for various natural language processing (NLP) tasks. It abstracts away many of the complexities of working with transformer models, making it easier to use pre-trained models for specific tasks.
task=”translation”: This argument specifies the task that the pipeline will perform, which in this case is translation. The pipeline will be configured to translate text from one language to another.
model=”facebook/nllb-200-distilled-600M”: This argument specifies the pre-trained model that the pipeline will use for translation. In this case, it’s using a model called “facebook/nllb-200-distilled-600M”. This model is likely a transformer-based neural network trained by Facebook AI, specifically designed for translation tasks. The “nllb” stands for “no language left behind”, The “200” refers to the number of languages the model is trained on and distilled 600M variant.
torch_dtype=torch.bfloat16: This argument specifies the data type to be used for computations within the pipeline. In this case, it’s set to torch.bfloat16, which is a 16-bit floating-point representation provided by PyTorch. Using bfloat16 can help reduce memory usage and accelerate computations, especially on hardware that supports it, such as certain GPUs.
Next, we insert a random text we want translated:
text = """Insert whatever text you want to translate"""
And we specify we want to translate the text from source language which is English to target language, which is in our case Croatian. You can of course choose other languages, – you can find the other language codes on this page
text_translated = translator(text,
src_lang=”eng_Latn”,
tgt_lang=”hrv_Latn”)
At the end, we come to the results of translation, which come in a form of a following code:
print(text_translated)
In conclusion
The tutorial walks through the process of setting up a translation pipeline using the Hugging Face Transformers library on Google Colab, while demonstrating translation from English to Croatian with a pre-trained model Facebook NLLB-200 model, designed for machine translation tasks, particularly for low-resource languages. The tutorial includes explanations and comments to guide you through each step of the process.