Machine Learning is here. Researchers have been programming and using decision trees and neural networks for decades now, but it’s only recently (be it because of an advance in computational power, the ubiquity of data sources, or simply because of the appeal of self-driving cars) that Machine Learning has reached business in all segments. It has taken a role in our daily lives and is slowly starting to represent something concrete in the minds of the public.
Still, don’t let the relative recency of its wide adoption fool you into thinking that implementing a Machine Learning solution is cutting edge or only for businesses that have money to spare on the latest technology, the reality is quite different. Simply put, ignoring Machine Learning use cases where they clearly present themselves, is a waste of resources.
It’s a waste of employees’ time when they perform tasks that could be automated, it’s a waste of the value of data when it’s just sitting in a production database, and it’s a waste of the final product when it’s delivered without reaching the full capacity that an easily available technology could provide it with.
But how to implement ML in a business, as a tool for engineers or as a service for clients, in a streamlined way, without requiring that everyone involved be an expert in the Machine Learning stack?
Learn more about tech trends: Redis
One practical way of doing that is with an API. Offering a Machine Learning solution through an API allows the person using it to focus on the results, while giving ML developers full control and easy maintainability of the model behind it. This efficient way of delivering ML is being extensively applied even by companies with large AI divisions such as Google (e.g. their Vision API) and Amazon.
Now, back to the advances of Machine Learning, another factor that possibly plays a big part in its present wide adoption are exceptional Python libraries such as scikit-learn, Pandas and TensorFlow and how they make it much easier for ML developers to deliver high-quality solutions.
It’s no wonder that Python is the most used programming language for Machine Learning and Data Science by a large margin. How much more practical would it be if your ML API could integrate the code that uses these libraries, seamlessly? How would you do that?
Django is an open source web framework, fully written in Python. It’s easy to get started with, it’s stable, and it integrates with all of Python’s libraries. Django was first developed because the web developers of a newspaper’s site wanted an efficient Python web framework to use for building the backend of the portal.
Since then, it has grown to be among the top four web frameworks, being used on the websites of business like Instagram and Disqus. And it is the answer to our second question. Adding to it the Django REST Framework and a web server such as Gunicorn, you can have a fully Python-based RESTful API for your Machine Learning solution quite quickly.
Django adheres to the model-view-template (MVT) architectural pattern. After installing it, you can get started by executing the command
django-admin startproject mysite
This will create most of the folder structure and configurations for your project. Then, after going into the project folder you can run
python manage.py startapp myapp
Creating then the app which will run your API. Next, you should edit your app’s models.py if you are going to use a database, and create your API’s Views.
Generally, your ML model will be accessed from within a View, in your views.py file. A good way of making the integration of the model and the server is to, after building and validating the model, save any binaries generated with Pickle and put the code into a package, which you can import in your View.
In addition, if your model is too large, it may be a good idea to load it as a global variable in your views.py, so it will only be loaded once when the server is started, and not every time a View is called.
Django provides you with a fast and reliable way of putting ML models to use, with the same programming language they were written in. With that, implementing Machine Learning solutions, as usually as any other software, appears one step closer to what it actually is – our reality.
Marcelo is an Intern at PoaTek. He is currently pursuing a BSc in Biomedical Informatics at Universidade Federal de Ciências da Saúde de Porto Alegre. He likes eighties music, churros, and the Semantic Web.