When working in the field of data science, you usually find yourself facing a new challenge after finishing a long loop of training, fine-tuning, and improving. Congratulations on building a ML model that has good performance and answers your needs. Now the challenge you have at hand is how can you deliver this nice piece of technology to the intended users? Or maybe how can you communicate effectively your result with stakeholders in your company? Or even how can you share your model results with your colleagues effectively for better collaboration?
Deploying machine learning can be sometimes difficult since we will be using technologies and techniques out of the usual skill set needed to build the models.
In this article, we are going to discover a way to deploy machine learning models using only python. And along the way, we will build a machine translation model and a web page.
So here are the steps that we are going to see:
-
Using Huggingface machine translation models.
-
Discovering Anvil and building a web UI for our model.
-
Connecting backend and frontend and serving the world our work!
Build Machine Translation model
Huggingface is an AI community working to “democratize good machine learning”. Under this initiative, you can find many trained models on different machine learning tasks: image segmentation, text to speech, text generation… and also machine translation!
Machine translation is simply a translation task between two languages performed by a piece of software, in our case, a machine learning model built with transformers.
A transformer is a deep learning architecture based on attention. Let’s get it running on your machine!
We will use transformers, a python library to download the MT model and perform a translation.
pip install torch
pip install transformers
After installing the needed packages, import the following modules:
from transformers import MarianTokenizer, MarianMTModel
from typing import List
Let’s get a model that translates a sentence from German to English. We need the name of the model:
src= "de"
trg= "en"
mname= f'Helsinki-NLP/opus-mt-{src}-{trg}'
Now let’s import the trained model and the tokenizer using the following lines:
model = MarianMTModel.from_pretrained(mname)
tok = MarianTokenizer.from_pretrained(mname)
The download size is about 300mb, after finishing you can store the model in a local directory using the following:
model.save_pretrained("./models/de_en/")
tok.save_pretrained("./models/de_en/tok")
Let’s the model:
text="ich habe keine ahnung"
gen = model.generate(**tok.prepare_seq2seq_batch(src_texts=[text], return_tensors="pt"))
words: List[str] = tok.batch_decode(gen, skip_special_tokens=True)
print(words[0])
Now you should have the English translation of the sentence stored in words[0]
.
Discover Anvil and build a web UI
Anvil is a framework and a stack of solutions allowing you to build web applications using only python code. It has a drag and drops editor to build web UI and it allows you to connect the code from your local machine to the UI you build and host your application by giving you a link that you can share.
So let’s start with creating an application from here. Choose blank application then material design.
You should see something like this:
Now I will count on you to use the editor and build something similar to the following:
In this simple UI, we have two DropDowns for choosing the source and destination languages. We also have a TextBox to enter the source text and a richText component to display the translated text. You can also see a Button to start the translation task.
To synchronize with the code snippets that you will see below, give the same IDs to the components. Below you can find an example of where you can set the id of a component:
The IDs we are using are:
Component |
ID |
---|---|
Source language DropDown | source_lang |
Destination language DropDown | dest_lang |
Source language TextBox | source_text |
Translated text RichText | translated_text |
Onclick function
We have added a button that is used to start the translation. From your editor click on the button then scroll down in the properties panel. At the bottom, you will see an event section. In the text zone next to “click”, enter “translate” and then click on the arrow on the right of this text zone.
This will take you to the code view where you will see some auto-generated python code.
You will find that anvil has automatically added a function called translate. It will be called each time the button in our UI is clicked.
Here is what the function should look like:
def translate(self, **event_args):
"""This method is called when the button is clicked"""
src_lang=self.source_lang.selected_value #get the selected source language
dest_lang=self.dest_lang.selected_value #get the selected destination language
text=self.source_text.text #get the text written in source language
#call the server function
translated_text=anvil.server.call("translation",text,src_lang,dest_lang)
#write the translated text to the UI
self.translated_text.content=translated_text
This function performs 3 main tasks:
-
Get Information from the UI
-
Send the information to our backend using server function “translation” (we will explain it in the next section)
-
Send the translated text to the UI.
Server function
Let’s focus on this line of code:
translated_text=anvil.server.call("translation",text,src_lang,dest_lang)
We use anvil.server.call to call a server function called translation
that we will define in our backend code in the local machine.
This function will serve as a connection between the web UI and the backend code that will run on our machine translation model.
As you have noticed we also send the parameters of this function in the function anvil.server.call
.
Deploy our MT model
Let ‘s first install anvil
pip install anvil-uplink
Now we have built our web interface in anvil editor and we have basic code blocks to run our machine translation model and do a translation.
The next step is to define the server function that we have discussed in the previous section.
Here is the code of the function:
@anvil.server.callable
def translation(text,src,dest):
lang_code={"English":"en",
"German":"de",
"French":"fr",
"Spanish":"es"}
model=MarianMTModel.from_pretrained("./models/"+lang_code[src]+"_"+lang_code[dest])
tok=MarianTokenizer.from_pretrained("./models/"+lang_code[src]+"_"+lang_code[dest]+"/tok")
gen = model.generate(**tok.prepare_seq2seq_batch(src_texts=[text], return_tensors="pt"))
words: List[str] = tok.batch_decode(gen, skip_special_tokens=True)
return words[0]
The function will take the 3 parameters sent from the frontend, convert the source and destination languages to the respective language codes and then load the models and compute the translation and return the result.
The way to declare this function to anvil as a server function is by using the decorator
@anvil.server.callable
.
We have one last step to do to connect the backend code that we can run in a jupyter notebook to our anvil application.
Go to anvil online editor, click on the gear icon, and then click on “Uplink…”.
See the screen below
You will see a popup appear, then click on “Enable server uplink for this app” to get the connection code that you copy.
You paste the code into the following code line:
anvil.server.connect("code here")
This line will start a server that connects your local code script or jupyter notebook to the anvil application with the function translation
registered as a server function.
Final step
To this point, you have your backend server running with a server function that loads the machine translation model and does the translation after taking into consideration the parameters sent from the frontend. This figure summarizes what we have implemented together so far.
A final step is to run the application by clicking the run button on the top center of the anvil editor.
After running the application you see on the top right corner a button “publish this app” that will give you a link that you can share to access the application and do your translation!
Conclusion
By following this article you have been able to deploy a MT model and build a web interface to use it.
There is still much to discover on how to deploy effectively a model using anvil but now you have the basics to start your deployment journey and further the potential of your prior knowledge in Python to do much more!
Come To One Of Our Free Workshops
Start your career as a data scientist with our free workshops, which are based on an adaptable curriculum and guided by industry experts.
Master Machine Learning with Code Labs Academy! Join Our Online Bootcamp – Flexible Part-Time & Full-Time Options Available.