Skip to content

Commit

Permalink
chore: add php examples
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Sep 26, 2022
1 parent 96a1ab9 commit eb71f1a
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 176 deletions.
12 changes: 6 additions & 6 deletions .bazelignore
@@ -1,7 +1,7 @@
java/src/main/java/com/bentoml
kotlin/src/main/kotlin/com/bentoml
js/bentoml
go/bentoml
swift/Sources/bentoml
grpc-client/java/src/main/java/com/bentoml
grpc-client/kotlin/src/main/kotlin/com/bentoml
grpc-client/js/bentoml
grpc-client/go/bentoml
grpc-client/swift/Sources/bentoml
**/*/node_modules
thirdparty
grpc-client/thirdparty
12 changes: 9 additions & 3 deletions .gitignore
Expand Up @@ -124,7 +124,7 @@ cpp/bentoml
go/bentoml
node/bentoml
node_modules
thirdparty/*
thirdparty

# Swift-related
swift/**/*/bentoml
Expand All @@ -140,6 +140,12 @@ DerivedData/
Package.resolved

# Java-Kotlin
java/**/*/bentoml
kotlin/**/*/bentoml
**/*/java/**/*/bentoml
**/*/kotlin/**/*/bentoml
.gradle

# PHP generated stubs
**/*/php/Bentoml
**/*/php/GPBMetadata
composer.lock
vendor
5 changes: 5 additions & 0 deletions MANIFEST.in
Expand Up @@ -5,6 +5,9 @@ exclude *.yml *.yaml
exclude .git*
exclude bentoml/grpc/buf.yaml
exclude bentoml/_internal/frameworks/FRAMEWORK_TEMPLATE_PY
exclude .bazelignore
exclude .bazelrc
exclude BUILD WORKSPACE

# Directories to exclude in PyPI package
prune .devcontainer
Expand All @@ -20,6 +23,8 @@ prune */__pycache__
prune */.DS_Store
prune */.ipynb_checkpoints
prune **/*/README*
prune bazel-*
prune grpc-client

# Patterns to exclude from any directory
global-exclude *.py[cod]
5 changes: 5 additions & 0 deletions WORKSPACE
Expand Up @@ -61,6 +61,11 @@ go_register_toolchains(version = "1.19")
# Currently c3714eced8c51db9092e0adc2a1dfb715655c795 address
# some build issues with upb for C++.
# TODO: Update this to v1.50.0 when 1.50.0 is out.
http_archive(
name = "com_google_protobuf",
strip_prefix = "protobuf-3.19.4",
urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.19.4.zip"],
)
http_archive(
name = "com_github_grpc_grpc",
strip_prefix = "grpc-c3714eced8c51db9092e0adc2a1dfb715655c795",
Expand Down
19 changes: 10 additions & 9 deletions grpc-client/README.md
Expand Up @@ -26,15 +26,16 @@ bazel build ...

The following table contains command to run clients:

| Language | Command |
| ------------------ | --------------------------------------- |
| [Python](./python) | `bazel run //grpc-client/python:client` |
| [C++](./cpp) | `bazel run //grpc-client/cpp:client` |
| [Go](./go) | `bazel run //grpc-client/go:client` |
| [Java](./java) | `bazel run //grpc-client/java:client` |
| [Kotlin](./kotlin) | `bazel run //grpc-client/kotlin:client` |
| [Swift](./swift) | `pushd swift && client && popd` |
| [Node.js](./node) | `pushd node && yarn client && popd` |
| Language | Command |
| ------------------ | -------------------------------------------- |
| [Python](./python) | `bazel run //grpc-client/python:client` |
| [C++](./cpp) | `bazel run //grpc-client/cpp:client` |
| [Go](./go) | `bazel run //grpc-client/go:client` |
| [Java](./java) | `bazel run //grpc-client/java:client` |
| [Kotlin](./kotlin) | `bazel run //grpc-client/kotlin:client` |
| [Swift](./swift) | `pushd swift && client && popd` |
| [Node.js](./node) | `pushd node && yarn client && popd` |
| [PHP](./php) | `pushd php && ./codegen && ./client && popd` |

> For Swift client, make sure to compile gRPC Swift `protoc` beforehand to generate the client stubs.
Expand Down
34 changes: 34 additions & 0 deletions grpc-client/php/BentoServiceClient.php
@@ -0,0 +1,34 @@
<?php

use Bentoml\Grpc\V1alpha1\BentoServiceClient;
use Bentoml\Grpc\V1alpha1\NDArray;
use Bentoml\Grpc\V1alpha1\Request;

require dirname(__FILE__) . '/vendor/autoload.php';

function call()
{
$hostname = 'localhost:3000';
$apiName = "classify";
$to_parsed = array("3.5", "2.4", "7.8", "5.1");
$data = array_map("floatval", $to_parsed);
$shape = array(1, 4);
$client = new BentoServiceClient($hostname, [
'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
$request = new Request();
$request->setApiName($apiName);
$payload = new NDArray();
$payload->setShape($shape);
$payload->setFloatValues($data);
$payload->setDtype(\Bentoml\Grpc\V1alpha1\NDArray\DType::DTYPE_FLOAT);

list($response, $status) = $client->Call($request)->wait();
if ($status->code !== Grpc\STATUS_OK) {
echo "ERROR: " . $status->code . ", " . $status->details . PHP_EOL;
exit(1);
}
echo $response->getMessage() . PHP_EOL;
}

call();
19 changes: 19 additions & 0 deletions grpc-client/php/client
@@ -0,0 +1,19 @@
#!/bin/bash

set -e

if ! $(which php >/dev/null); then
echo "PHP is required."
exit 0
fi

if ! $(which composer >/dev/null); then
echo "composer is required."
exit 0
fi

[[ -f ./composer.lock ]] && composer update || composer install

echo "Running PHP client"

php -d extension=grpc -d max_execution_time=300 BentoServiceClient.php
28 changes: 28 additions & 0 deletions grpc-client/php/codegen
@@ -0,0 +1,28 @@
#!/bin/bash

set -e

pushd .. >/dev/null

! [[ -d "thirdparty" ]] && mkdir thirdparty

pushd thirdparty >/dev/null
! [[ -d "protobuf" ]] && git clone -b v3.19.4 https://github.com/protocolbuffers/protobuf.git
popd
pushd .. >/dev/null
# We will use BentoML tools/bazel.rc
echo "Building shared C++ gRPC..."
bazel build @com_github_grpc_grpc//:all
bazel build @com_google_protobuf//:protoc
if ! [ -f "bazel-bin/src/compiler/grpc_php_plugin" ]; then
echo "We will compile grpc_php_plugin from source."
bazel build @com_github_grpc_grpc//src/compiler:grpc_php_plugin
fi

PROTOC=$(pwd)/bazel-bin/external/com_google_protobuf/protoc
PLUGIN=protoc-gen-grpc=$(pwd)/bazel-bin/src/compiler/grpc_php_plugin
popd

"$PROTOC" -I . -I ./thirdparty/protobuf/src --php_out=php --grpc_out=php --plugin="$PLUGIN" bentoml/grpc/v1alpha1/service.proto

popd
20 changes: 20 additions & 0 deletions grpc-client/php/composer.json
@@ -0,0 +1,20 @@
{
"name": "bentoml/grpc-php-client",
"description": "BentoML gRPC client for PHP",
"require": {
"grpc/grpc": "1.42.0",
"google/protobuf": "3.19.4"
},
"license": "Apache-2.0",
"autoload": {
"psr-4": {
"GBPMetadata\\": "GPBMetadata/",
"Bentoml\\": "Bentoml"
}
},
"authors": [
{
"name": "BentoML Team"
}
]
}

0 comments on commit eb71f1a

Please sign in to comment.