How to deploy Machine Learning Model in Laravel Application

Call model API from Algorithmia & predict in Laravel

Davis David
Towards Data Science

--

Photo by Pixabay from Pexels

The topic of Machine Learning Model Deployment is not new nowadays, but many AI practitioners especially beginners find it difficult to deploy their models into production. In this article, we are going to learn how to call our model API from the Algorithmia platform to the Laravel application and make predictions.

Before moving on I recommend you read my previous article where I explained in detail all about the Algorithmia platform and how you can deploy your model and call it for prediction in the programming language of your choice(Python, PHP, javascript, java, c#, NodeJS, Go, Ruby e.t.c).

In the previous article, you will learn

  • How to create an NLP model that detects spam SMS text messages.
  • How to use Algorithmia, a MLOps platform.
  • How to deploy your model into the Algorithmia platform.
  • How to use your deployed NLP model in any Python application(Flask or Django).

Why Algorithmia?
Algorithmia helps to separate Machine Learning concerns from the rest of your application (example, e-commerce site or android app). By calling your model and making predictions as an API call, your application can be free of the concerns of a machine learning environment. All you need to do is create correctly formatted input.

What is Laravel 💻

Laravel is one of the most popular open-source PHP web frameworks for the development of web applications, created by Taylor Otwell. It follows the model-view-controller architectural pattern and based on Symfony.

If you are not familiar with Laravel, I recommend you take this latest Laravel Crash course 2020 from Travesy Media Youtube Channel.

In this article, we will create a simple Laravel app with two web pages. The first page will have a simple HTML form to write a text message and the second page will show the model prediction result if the text message provided is a spam message or a normal message.

Let’s get started… 🚀

Install Laravel 7

To install the laravel framework, your machine must meet the following few requirements.

  • PHP >= 7.2.5
  • BCMath PHP Extension
  • Ctype PHP Extension
  • Fileinfo PHP extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Laravel uses Composer to manage its dependencies. Make sure you have Composer installed on your machine before using Laravel.

Then you can install laravel 7 by typing the following command.

composer create-project --prefer-dist laravel/laravel:^7.0 laravel-sms-spam-detection

The command mentioned above will make Laravel installed on the specific directory called laravel-sms-spam-detection.

The next thing after installing Laravel is to set your application key to a random string by running the following command.

php artisan key:generate

Install Algorthimia PHP client

The next step is to install the algorithmia PHP client. To install run the following command in the laravel-sms-spam-detection directory.

composer require algorithmia/algorithmia

Then run this command to regenerate the list of all classes that need to be included in this project. This will also include new classes from the algorithimia package we have installed.

composer dumpautoload

Now we have successfully install algorithmia for PHP in our Laravel project. 👍

Create Controller

The following command will create a simple controller called SpamController.

 php artisan make:controller SpamController

YOu can then access this new controller in the “app/Http/Controller” directory. The controller will have two methods.

The first method will be responsible to show the blade file with a simple HTML form to write a text message(we will create this blade file in the next steps). The name of the method is show_form()

The above method will show a view from the sms_spam_form blade file.

The second method will receive the text message and make a prediction by using Algorithimia PHP Client if the text message provided is a normal message or spam message. The name of the method is detect().

NB: Click the “API Key” tab on your algorithm panel to collect the API key that will help you call the code that runs the model.

To call the algorithm, we need to add the name of the algorithm with its version to the client object we have created. The name of the algorithm is “Davis/spam_detection/0.2.0”.The above method will then make a prediction and show the results in the sms_spam_result blade file.

Here is the complete source code in the SpamController PHP file.

Create Routes

Add the following route code in the “routes/web.php” file for the Laravel application.

The first route will show a simple HTML form with sample normal and spam messages and the second route will show a result page with model prediction results.

Create Blade files

We then create two blade files, the first blade file is sms_spam_form.blade.php in the “resources/views/” folder directory. It will have a simple form with a text-area form attribute to write a text message and a submission button. It will also have some examples of a normal message and spam message you can try to use.

The second blade file is sms_spam_result.blade.php in the “resources/views/” folder directory. It will show the text message provided in the HTML form, the prediction results, and a button to try again.

Run Our Laravel Application

We can start the server by using the below command from the project directory.

 php artisan serve

Now we can open our laravel application with the following Url in the browser.

http://127.0.0.1:8000/

Then you can see our first page with HTML form.

home page

Make Prediction

To make a prediction write a text message in the textarea and click the predict button.
Example of the normal text message: “Haven't mus ask if u can 1st wat. Of meet 4 lunch den u n him meet can already lor. Or u wan 2 go ask da ge 1st then confirm w me asap?”

After clicking the predict button the Laravel app will show the result page with the prediction result.

normal message

Our model predicts the text message provided is a normal message.

Let’s try another text message.

Text Message: “This weeks SavaMob member offers are now accessible. Just call 08709501522 for details! SavaMob, POBOX 139, LA3 2WU. Only £1.50/week. SavaMob — offers mobile!

Then click the predict button.

spam message

Our model predicts the text message provided is a spam message.

Wrapping Up

In this article, you have learned how to deploy your model from Algorithmia to a Laravel application by using Algorithmia PHP Client. You can also deploy in different applications from different programming languages, you just need to install the specific algorithmia package for the language of your choice and call the API for prediction.

You can download the source code for this laravel application here: https://github.com/Davisy/Laravel-SMS-Spam-Detection

Congratulations, you have made it to the end of this article!

If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post! I can also be reached on Twitter @Davis_McDavid

One last thing: Read more articles like this in the following links.

--

--

Data Scientist 📊 | Software Developer | Technical Writer 📝 | ML Course Author 👨🏽‍💻 | Giving talks. Contact me to collaborate https://davisdavid.com/