Blog #2: Finding and Using Pretrained Models from HuggingFace

Tag: Python, Machine Learning

Finding a Decent Pre-trained Model from HuggingFace

HuggingFace is currently the best place to find open-source pre-trained machine learning models. It's sort of like a popular code repository for machine learning models.

We can use HuggingFace to tune the pre-trained model and deploy it on serverless services or use their inference API for prototyping. However, our goal is to run it locally.

My Requirements

After going through the models' documentation, I chose: Helsinki-NLP/opus-mt-mul-en.

There are filters on the model pages. Make sure to filter based on your requirement and remember to check the option for the correct license suitable for you.

Running a Machine Learning Model Locally

We will use Python to run the model. Keep in mind that I am using WSL2 with Ubuntu 22 LTS and Python 3.10

Local Python Environment

Before we start, it is recommended to create a virtual env for our project. Why ? Because , the more Python projects we have, the more likely it is that we need to work with different versions of Python libraries, or even Python itself. Different versions of libraries for one project can break compatibility in another project. I tend to use venv within the project folder and use pip freeze to check the project's packages. Alternatively, Conda is another option.

Find out how to create, activate, and deactivate a virtual environment here.

Deep Learning Library

We have many DLL today.Popular ones include PyTorch and TensorFlow. HuggingFace initially supported PyTorch but at present supports TensorFlow as well. (We will use TensorFlow, as I want to explore TensorFlow Lite later for its better Android support)

We will be using a CPU only version of TensorFlow (I have an older NVIDIA GTX card which is not officially supported).

Installing TensorFlow

Installation details for TensorFlow can be found here.

Note: This will install the latest TensorFlow onto the system and this will cause some or the other backward compatibility issue with HuggingFace Transformer. Check out the ‘Installing HugginFace Transformers’ section to install a Transformer with compactable TensorFlow.

Note: Avoid using Conda for TensorFlow as it may not have the latest stable version.

For CPU users:

pip install --upgrade pip
pip install tensorflow
Terminal output showing pip successfully installed TensorFlow along with its dependencies such as keras, numpy and tensorboard
Verifying CPU-only Installation of TensorFlow
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

If a tensor is returned, the installation is successful.

Terminal output printing tf.Tensor(335.95593, shape=(), dtype=float32), confirming the TensorFlow installation works

Installing HuggingFace Transformers

For CPU-support only, we can conveniently install HugginFace Transformers and TensorFlow 2.0 using the below command (This is my recommendation to install TensorFlow and HuggingFace Transformer on local which require CPU only support of TensorFlow):

pip install 'transformers[tf-cpu]'

Finally, check if HuggingFace Transformers has been properly installed by running the following command. (It will download a pretrained model and run it)

python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"
Terminal output of the transformers sentiment-analysis test pipeline printing label POSITIVE with a score of 0.999

Running the Helsinki-NLP/Opus-mt-mul-en Model Locally

Note: You can find the code used in this blog here.

We can copy the template for the HuggingFace Pipeline which simplifies using models for inference. According to HuggingFace documentation, pipelines abstract complex code, providing a simple API for several tasks. Even if we don't have experience with a specific modality or aren't familiar with the underlying code behind the models, you can still use them for inference with the pipeline().

Note: Remember that the code provided will most likely be set to use PyTorch.

Coding it up as a transformer

Code :

Python code loading the Helsinki-NLP/opus-mt-mul-en tokenizer and TFAutoModelForSeq2SeqLM, tokenizing a French sample text and generating and decoding a translation

Output :

Terminal output showing the tokenizer's input_ids and attention_mask followed by a repetitive, inaccurate generated translation of the French sample text

Note: Details on TFAutoModel can be followed up here.

Note: Details on AutoTokenizer can be followed up here. The tokenizer returns a dictionary with three important items:

Improving Translation Accuracy ?

Finetuning the model is beyond the scope of this blog. But if we prepend the language to the input text as below we will get a more accurate result.

Python code line prepending the source language token >>fra<< to the French sample text

However, based on the MarianMT Model documentation, we don’t need to prepend the source language in multilingual mode, leading to possible confusion or a bug ? Unfortunately I was not able to find an answer for this.

Coding it up using HuggingFace Pipeline

Code :

Python code using langdetect to detect the input language, prepending the language token to the sample text and translating it with a HuggingFace translation pipeline

(I have prepend the language to the input text to improve the translation.)

Output :

Terminal output showing the pipeline's result: translation_text 'I know where the bus stops?'

In conclusion , using the pipeline is easier for inference than the traditional method, although the latter offers more tweaking flexibility.