-
Notifications
You must be signed in to change notification settings - Fork 3
/
PredictRequest.php
153 lines (136 loc) · 4.72 KB
/
PredictRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Clarifai\API\Requests\Models;
use Clarifai\API\ClarifaiHttpClientInterface;
use Clarifai\API\CustomV2Client;
use Clarifai\API\RequestMethod;
use Clarifai\API\Requests\ClarifaiRequest;
use Clarifai\DTOs\Inputs\ClarifaiInput;
use Clarifai\DTOs\Models\ModelType;
use Clarifai\DTOs\Outputs\ClarifaiOutput;
use Clarifai\DTOs\Predictions\Concept;
use Clarifai\Exceptions\ClarifaiException;
use Clarifai\Internal\_Model;
use Clarifai\Internal\_MultiOutputResponse;
use Clarifai\Internal\_Output;
use Clarifai\Internal\_OutputConfig;
use Clarifai\Internal\_OutputInfo;
use Clarifai\Internal\_PostModelOutputsRequest;
/**
* Predicts on an input (image/video) using a certain model.
*/
class PredictRequest extends ClarifaiRequest
{
private $modelID;
private $modelType;
private $input;
private $modelVersionID;
/**
* @param string $val
* @return PredictRequest
*/
public function withModelVersionID($val) { $this->modelVersionID = $val; return $this; }
private $language;
/**
* @param string $val
* @return PredictRequest
*/
public function withLanguage($val) { $this->language = $val; return $this; }
private $minValue;
/**
* @param float $val
* @return PredictRequest
*/
public function withMinValue($val) { $this->minValue = $val; return $this; }
private $maxConcepts;
/**
* @param int $val
* @return PredictRequest
*/
public function withMaxConcepts($val) { $this->maxConcepts = $val; return $this; }
private $selectConcepts;
/**
* @param Concept[] $val
* @return PredictRequest
*/
public function withSelectConcepts($val) { $this->selectConcepts = $val; return $this; }
/**
* Ctor.
* @param ClarifaiHttpClientInterface $httpClient The Clarifai HTTP client.
* @param ModelType $modelType The model type.
* @param string $modelID The model ID.
* @param ClarifaiInput $input The input.
*/
public function __construct(ClarifaiHttpClientInterface $httpClient, ModelType $modelType,
$modelID, ClarifaiInput $input)
{
parent::__construct($httpClient);
$this->modelID = $modelID;
$this->modelType = $modelType;
$this->input = $input;
}
protected function requestMethod()
{
return RequestMethod::POST;
}
protected function url()
{
if ($this->modelVersionID == null) {
return "/v2/models/$this->modelID/outputs";
} else {
return "/v2/models/$this->modelID/versions/$this->modelVersionID/outputs";
}
}
public function httpRequestBody(CustomV2Client $grpcClient) {
$postModelOutputsRequest = (new _PostModelOutputsRequest())
->setInputs([$this->input->serialize()]);
$outputConfig = (new _OutputConfig());
$anyOutputConfig = false;
if (!is_null($this->language)) {
$outputConfig->setLanguage($this->language);
$anyOutputConfig = true;
}
if (!is_null($this->minValue)) {
$outputConfig->setMinValue($this->minValue);
$anyOutputConfig = true;
}
if (!is_null($this->maxConcepts)) {
$outputConfig->setMaxConcepts($this->maxConcepts);
$anyOutputConfig = true;
}
if (!is_null($this->language)) {
$outputConfig->setLanguage($this->language);
$anyOutputConfig = true;
}
if (!is_null($this->selectConcepts)) {
$selectConcepts = [];
foreach ($this->selectConcepts as $selectConcept) {
array_push($selectConcepts, $selectConcept->serialize());
}
$outputConfig->setSelectConcepts($selectConcepts);
$anyOutputConfig = true;
}
if ($anyOutputConfig) {
$postModelOutputsRequest
->setModel((new _Model())
->setOutputInfo((new _OutputInfo())
->setOutputConfig($outputConfig)));
}
return $grpcClient->PostModelOutputs($postModelOutputsRequest);
}
/**
* @param _MultiOutputResponse $response
* @return ClarifaiOutput Our serialized object.
* @throws ClarifaiException
*/
public function unmarshaller($response) {
if (is_null($response->getOutputs()) || count($response->getOutputs()) === 0) {
return ClarifaiOutput::deserialize($this->httpClient, $this->modelType, null);
}
$outputResponse = null;
if (!is_null($response->getOutputs())) {
/** @var _Output $outputResponse */
$outputResponse = $response->getOutputs()[0];
}
return ClarifaiOutput::deserialize($this->httpClient, $this->modelType, $outputResponse);
}
}