Deploying ML models straight from Jupyter Notebooks
Tags: machinelearning, jupyter, deployment, mlops
Published: 2023-01-26 16:10:10

Winter is a time of magic 🧙‍♂️. Everyone is waiting for something special at this time, and Data Scientists aren’t different. It is not in the power of software developer to be a magician, but I can help you deploy your models with literally a **single command** right from your Jupyter notebook (and basically from any place like your command line or Python script).

Sounds like magic? It is! 💫

![Streamlit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aiw7ge5lf9fqiqz29n5d.gif)

To get some winter season vibes, let’s do some magic ourselves first. Let’s do something that will help us prepare some fun for our friends for the weekends.

To do so, we’ll create a model that **translates lyrics to emojis**. With all due respect to recent advances in NLP and LLM algorithms, it’s still both easier and more fun to convince your friends to do the backward translation:

![ChatGPT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4eo79nq2obti4hiiqei.png)

Ok, I’m sure humans are up to the challenge!

Alright, just before we get into the actual coding, everything described in this blog post is available on this [Google Colab notebook](https://colab.research.google.com/drive/1JNQ1Ntv9kbKTZ82p9cw-VThf44HQ4a1Y?usp=sharing). Now, let's get to it!

First, let’s load [an emoji dataset](https://www.kaggle.com/datasets/aadityasiva/emojis-dataset). We need something to base our model on, right?

![Load emoji dataset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n32hb0vbm0rjrui2rc2j.png)

The secret sauce to creating our emoji language is using a [pretrained Distilbert model](https://huggingface.co/distilbert-base-uncased) to tokenize and create [embeddings](https://towardsdatascience.com/a-guide-to-word-embeddings-8a23817ab60f) which represent our emoji dictionary:

![Turn emojis into embeddings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f24ui4cp5zgfjsdmnj3w.png)

We can now similarly embed any word and replace it with its “closest” emoji embedding to create our text→emoji translator. Using that, “Jingle bells” should become something like “🔔🔔”:

![Find the closest emoji for each word](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmfjki82o1lff1g3tn7a.png)

Good start - it guessed half of the emojis correctly!

Our part of magic is done, now to the **single command deployment** I promised in the beginning. Before we go rogue and deploy it to the cloud, let’s run a [Streamlit](https://streamlit.io) app locally to test things out:

![Serving with Streamlit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c42sg3pg69w6mgk6whyq.png)

What happened here? That innocent looking `mlem.api.save` method ~~inspected the model object to find all python packages to install~~ did the magic of preparing the model to be used! 🪄✨

Now you should have a Streamlit app at `localhost:80` that looks just like this:

![Streamlit app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bw9wbopp3zfxu8so3os.gif)

Once we finished playing around with the model locally, let’s cast our final spell for the day 🧙‍♂️ and deploy the model to [fly.io](http://fly.io):

![Deployment to fly.io](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wbc9wtoppu5x6455xt1.png)

Some elvish gibberish is printed to the command line, and you get a deployment up and ready.

Now, before you go, remember that these powers extends to serving models as [REST API applications](https://mlem.ai/doc/user-guide/serving/fastapi/), [Streamlit apps](https://mlem.ai/doc/user-guide/serving/streamlit/), building [Docker Images](https://mlem.ai/doc/user-guide/building/docker/) and [Python packages](https://mlem.ai/doc/user-guide/building/pip/), and deploying them to [Heroku](https://mlem.ai/doc/user-guide/deploying/heroku/), [Flyio](https://iterative.ai/blog/mlem-cv-model-deployment/), [Kubernetes](https://mlem.ai/doc/user-guide/deploying/kubernetes/), and [AWS Sagemaker](https://mlem.ai/doc/user-guide/deploying/sagemaker/).

Or just [go here](https://mlem.ai/doc/get-started/) to get a crash course :)