clarifai/clarifai-php-grpc

Clarifai PHP gRPC 客户端


README

Clarifai logo

Clarifai PHP gRPC 客户端

这是官方 Clarifai gRPC PHP 客户端,用于与我们的强大识别 API 交互。Clarifai 为数据科学家、开发者、研究人员和企业提供了一个平台,以掌握整个人工智能生命周期。使用计算机视觉和自然语言处理从图像、视频和文本中收集有价值的业务洞察。

Latest Stable Version

安装

composer require clarifai/clarifai-php-grpc

另请参阅 如何将 gRPC 添加到您的 PHP 安装中

版本控制

此库不使用语义版本控制。前两个版本号(X.YX.Y.Z)遵循 API(后端)版本控制,并且每当 API 更新时,此库都会跟随。

Z(从 X.Y.Z)是此库用于库特定改进和错误修复的独立发布版本号。

入门

构建客户端并在元数据变量中设置 API 密钥或个人访问令牌。

require ‘vendor/autoload.php’;
use Clarifai\ClarifaiClient;

$client = ClarifaiClient::grpc();
$metadata = ['Authorization' => ['Key {MY_CLARIFAI_API_KEY_OR_PAT}']];

预测图像中的概念

use Clarifai\Api\Data;
use Clarifai\Api\Image;
use Clarifai\Api\Input;
use Clarifai\Api\PostModelOutputsRequest;
use Clarifai\Api\Status\StatusCode;

[$response, $status] = $client->PostModelOutputs(
    new PostModelOutputsRequest([
        'model_id' => 'aaa03c23b3724a16a56b629203edc62c',  // This is the ID of the publicly available General model.
        'inputs' => [
            new Input([
                'data' => new Data([
                    'image' => new Image([
                        'url' => 'https://samples.clarifai.com/dog2.jpeg'
                    ])
                ])
            ])
        ]
    ]),
    $metadata
)->wait();

if ($status->code !== 0) throw new Exception("Error: {$status->details}");
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
    throw new Exception("Failure response: " . $response->getStatus()->getDescription() . " " .
        $response->getStatus()->getDetails());
}

echo "Predicted concepts:\n";
foreach ($response->getOutputs()[0]->getData()->getConcepts() as $concept) {
    echo $concept->getName() . ": " . number_format($concept->getValue(), 2) . "\n";
}