1

For this function in UBUNTU Terminal which works well for my dockerized container (made using Tensorflow Serving):

curl -d '{"instances": [[ 1.18730158, -0.70909592,  1.21082918, -0.15897608, -0.87693017],
       [-0.3015204 , -0.44457891,  0.67090776,  1.1188499 , -0.87693017],
       [-0.52579254, -0.05989349,  0.40094705,  1.62998029,  1.13982729],
       [ 0.13322252,  0.58198159, -0.94885648,  1.1188499 , -0.87693017],
       [ 0.77441082, -0.68638116, -0.13897436,  0.35215431,  1.13982729],
       [ 0.69102759,  1.49057189, -0.13897436, -0.67010647,  1.13982729],
       [-0.25436574, -0.58819479,  0.13098635, -0.15897608,  1.13982729],
       [ 1.54671206,  2.58088026,  2.0207113 , -1.56458465,  1.13982729],
       [-0.97261165, -0.6292279 ,  0.40094705, -1.56458465, -0.87693017],
       [-0.36190136,  0.14893573,  0.40094705,  0.09658912,  1.13982729]]}'     -X POST http://localhost:8501/v1/models/model:predict

I have created a curl function in PHP:

    <?php 
        $endpoint = "http://localhost:8501/v1/models/model:predict";
$inputData = array(
    "instances" => 
       [[ 1.18730158, -0.70909592,  1.21082918, -0.15897608, -0.87693017],
       [-0.3015204 , -0.44457891,  0.67090776,  1.1188499 , -0.87693017],
       [-0.52579254, -0.05989349,  0.40094705,  1.62998029,  1.13982729],
       [ 0.13322252,  0.58198159, -0.94885648,  1.1188499 , -0.87693017],
       [ 0.77441082, -0.68638116, -0.13897436,  0.35215431,  1.13982729],
       [ 0.69102759,  1.49057189, -0.13897436, -0.67010647,  1.13982729],
       [-0.25436574, -0.58819479,  0.13098635, -0.15897608,  1.13982729],
       [ 1.54671206,  2.58088026,  2.0207113 , -1.56458465,  1.13982729],
       [-0.97261165, -0.6292279 ,  0.40094705, -1.56458465, -0.87693017],
       [-0.36190136,  0.14893573,  0.40094705,  0.09658912,  1.13982729]]
)
        
        $jsonData = array(
        "data" => $inputData,
        );
        $ch = 
        
        curl_init($endpoint);
        curl_setopt_array($ch, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json"
        ),
        
        
        CURLOPT_POSTFIELDS => json_encode($jsonData)
        ));
        
        $response = curl_exec($ch);
        ?> 

But I keep getting the error:

{'error': "Missing 'inputs' or 'instances' key"}

I am sure I am not giving the inputs correctly but I do not know where to make the change. Any help would be greatly appreciated.

1 Answer 1

2

I think you are passing extra "data" => $inputData key inside your $jsonData. Try this way without data,

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8501/v1/models/model:predict');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"instances\": [[ 1.18730158, -0.70909592,  1.21082918, -0.15897608, -0.87693017],\n       [-0.3015204 , -0.44457891,  0.67090776,  1.1188499 , -0.87693017],\n       [-0.52579254, -0.05989349,  0.40094705,  1.62998029,  1.13982729],\n       [ 0.13322252,  0.58198159, -0.94885648,  1.1188499 , -0.87693017],\n       [ 0.77441082, -0.68638116, -0.13897436,  0.35215431,  1.13982729],\n       [ 0.69102759,  1.49057189, -0.13897436, -0.67010647,  1.13982729],\n       [-0.25436574, -0.58819479,  0.13098635, -0.15897608,  1.13982729],\n       [ 1.54671206,  2.58088026,  2.0207113 , -1.56458465,  1.13982729],\n       [-0.97261165, -0.6292279 ,  0.40094705, -1.56458465, -0.87693017],\n       [-0.36190136,  0.14893573,  0.40094705,  0.09658912,  1.13982729]]}");

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

EDIT: You can pass directly the $inputData like below

where

$inputData = [
    "instances" => [
       [1.18730158, -0.70909592,  1.21082918, -0.15897608, -0.87693017],
       [-0.3015204 , -0.44457891,  0.67090776,  1.1188499 , -0.87693017],
       [-0.52579254, -0.05989349,  0.40094705,  1.62998029,  1.13982729],
       [ 0.13322252,  0.58198159, -0.94885648,  1.1188499 , -0.87693017],
       [ 0.77441082, -0.68638116, -0.13897436,  0.35215431,  1.13982729],
       [ 0.69102759,  1.49057189, -0.13897436, -0.67010647,  1.13982729],
       [-0.25436574, -0.58819479,  0.13098635, -0.15897608,  1.13982729],
       [ 1.54671206,  2.58088026,  2.0207113 , -1.56458465,  1.13982729],
       [-0.97261165, -0.6292279 ,  0.40094705, -1.56458465, -0.87693017],
       [-0.36190136,  0.14893573,  0.40094705,  0.09658912,  1.13982729]
     ]
    ];

and then CURLOPT_POSTFIELDS => json_encode($inputData)

Sign up to request clarification or add additional context in comments.

5 Comments

This works, but I need to store my input in a variable first because that will come from a database query. What should I do? @Always Sunny
@RachelWatson my bad no need to add the [] between $inputData. see edit again
or pass the $inputData directly
@RachelWatson your $inputData had some issue, please have a look the edit again
Yes, I also figured that out from how you had pasted the data within the text and edited the question. You have done me a great favor! I am obliged

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.