Deploy Machine Learning Web App on Azure | by Sagnik Chattopadhyaya | Towards Data Science

As a Machine Learning Engineer you role isn’t limited to building a model, but it also covers to make Models Deployable. Which on later stage is handled by DevOps team. In this Story,I will show you step by step how you can deploy an ML Classification Web App on Azure. This article is a part of the MSP Developer Stories initiative by the Microsoft Student Partners (India) program.
To start with, you need to build an web app first. For that you can use Streamlit. It is a python based open sourced framework that would help you to display text, charts, maps, dataframe, build interactive interface and many more and all that in few hours. As a first step you would build only an app.py file on your local machine and make sure it looks as expected.
Launch your Streamlit app by running streamlit run app.pyin a shell from the folder where the app.py file is saved. Whenever the app.py file is saved, the local web app will update automatically.
After you have completed building it on your local machine, next task is to deploy it to the actual internet for others to view it anytime. Now you cannot use GitHub pages as Streamlit web app is an non static site. but you can surely use a GitHub Repository for deploying it to Azure.
Have a look at my code from GitHub ,you will understand how simple it is to make an Streamlit web app.
Streamlit’s Getting Started tutorial would help to learn the API, so I will jump straight to deployment of the Streamlit web app.
For this example, I will publish an Binary Classification model showing whether Mushroom are Edible or Poisonous. I will use the Mushrooms dataset , you can find the dataset also on my GitHub.
After you have your web app ready to publish, you will create an Docker Container on the Azure Container Registry. This container will have all the files required to run the web app.
I will use Azure Shell through out but you can use Azure Portal or install the Azure CLI on your local machine. using Azure Shell or Azure CLI would be exactly same, difference is one you will use from Browser and the other you will use from Terminal.
Once you open Azure Shell you will be asked to sign in to your Azure Account. But if you are using Azure CLI, then type the following command:
az login
After you sign in to your shell you can see something like this.
Next, you need a resource Group to keep your Container Registry and Web App service. To do so type the following
az group create -l eastus -n MushroomApp
If you are using Azure Portal then follow their documentation to build resource group .
Here the location is East Us and name MushroomApp .You can name it differently but make sure to make appropriate changes in upcoming steps.
Then you will build Container Registry under the resource group.
az acr create --name MushroomAppRegistry --resource-group MushroomApp --sku basic --admin-enabled true
If you are using portal make sure to select the MushroomApp resource group created in last step.
Move back to your GitHub repository and add few more files that would help in functioning of the web app. First we will make the Dockerfile. This would be the instructions for Docker to run.
For best practices of writing Dockerfile see their official documentation.
docs.docker.com
The code for the Dockerfile you can use for this project is given below.
For the configuration files you would need config.toml and credentials.toml . I recommend copying MarcSkovMadsen’s config.toml and credentials.toml file from his awesome-streamlit repository.
Next you would need requirements.txt to add the libraries that needs to be installed for running your Web App. Below is the requirements.txt that I will use in this for this story.
streamlitpandasnumpyscikit-learnmatplotlib
Make sure you have all these files in your GitHub repository before moving to next step.
Next we will build the docker image and save to Azure Container Registry.
az acr build --registry MushroomAppRegistry --resource-group MushroomApp --image mushroom-app <SOURCE_LOCATION>
Replace <SOURCE_LOCATION> in the above code with your Git clone link. For this story the link I will use is https://github.com/sagnik20/ML-WebApp-using-Streamlit.git .
If the Build is successful, you could see output similar to: Run ID: ca1 was successful after 2m26s.
If you wish to see the image in your Container Registry, then log in to your Azure portal and navigate to your resource group, click on your container registry and under the Services Blade, click on Repositories.
You need to create an App Service Plan for your Web app. This will determine the size of the Virtual machine which will run your Web App. The default value of sku is B1 just the first tier above Free. We will use that.
All the pricing tiers available are: F1(Free), D1(Shared), B1(Basic Small), B2(Basic Medium), B3(Basic Large), S1(Standard Small), P1V2(Premium V2 Small), PC2 (Premium Container Small), PC3 (Premium Container Medium), PC4 (Premium Container Large), I1 (Isolated Small), I2 (Isolated Medium), I3 (Isolated Large). Choose one that you need.
az appservice plan create -g MushroomApp -n MushroomAppServicePlan -l eastus --is-linux --sku B1
Run the above code to create your App Service Plan.
Create the Azure Web App from the docker container in the Container Registry by running the below command.
az webapp create -g MushroomApp -p MushroomAppServicePlan -n census-web-app -i mushroomappregistry.azurecr.io/mushroom-app:latest
Or you can do the same from the portal too.
You should now see 3 resources in your Resource Group.
To view your new website, go to mushroom-web-app resource and click on the URL on the top right shown in picture below.
The first time you view it, there will be a cold-start delay and will likely become unresponsive after about 5 minutes. Go back to the resource and click click on the URL again, and your web app should appear in your browser!
If you need to make any changes to your app.py file, then you can easily view your changes by redeploying your web app. For that again run the code below by replacing <SOURCE_LOCATION> again. You have already did it once above.
az acr build --registry MushroomAppRegistry --resource-group MushroomApp --image mushroom-app <SOURCE_LOCATION>
You can add functionality or make changes and simply redeploy the webapp to visualise the changes.
The final web app that I build looks something like this 👇
You can also view and interact with it from : https://mushroom-web-app.azurewebsites.net/ (Sorry temporarily the link is inactive).
We have successfully deployed our Machine Learning model as a Web Application simply using Docker and Azure. Full code can be found on my GitHub Repo.
Thank You for reading!
I’m Sagnik Chattopadhyaya, a Microsoft Student Partner. You can Visit my Website to know more about me. Twitter: @sagnik_20 . YouTube: Learn Overflow
Hope you got to learn from this story. ❤
Happy Learning! 🐱‍💻