Use TensorFlow Serving in Laravel

Use TensorFlow Serving in Laravel

Machine learning has become an increasingly popular tool for building artificial intelligence applications. Deploying and serving machine learning models in a scalable and efficient way is a challenging task in building machine learning applications. That's where TensorFlow Serving comes in. TensorFlow Serving is a high-performance serving system for machine learning models that allows you to deploy and serve models in a production environment. In this post, we'll explore how to use TensorFlow Serving with Laravel.

Prepare environment

Make sure you have installed TensorFlow Serving on your system. You can read the post how to install TensorFlow Serving inside Docker container.


Open the .env file, add new environment variable TENSORFLOW_SERVING_URL.



Create a new file config/tensorflow_serving.php file for storing TensorFlow Serving configuration.



return [
    'url' => env('TENSORFLOW_SERVING_URL'),

Clear configuration cache using command:

php artisan config:cache

Here's an example of how you can use TensorFlow Serving with Laravel:



namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;
use Symfony\Component\HttpFoundation\Response;

class TestController
    public function index(): Response
        $url = config('tensorflow_serving.url').'/half_plus_two:predict';

        $requestData = [
            'instances' => [
                [3.0, 5.0, 7.0],

        $response = Http::post($url, $requestData);
        $responseData = $response->json('predictions');

        return response()->json($responseData); // Output: [[3.5,4.5,5.5]]

Here's a breakdown of the code:

  • We define the URL of the TensorFlow Serving, which includes the name of the model (half_plus_two) and the predict endpoint.
  • We define the input data for the model as a multidimensional array. In this example, we have one instance of input data with three features.
  • We use Http facade to send the POST request. Data is automatically encoded as JSON and sent using the application/json content type.
  • We decode the response from JSON into an associative array.
  • Finally, we return the prediction results as a JSON response. You can modify this to suit your needs, such as rendering a view or returning a different type of response.

Leave a Comment

Cancel reply

Your email address will not be published.