|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fab\Clarifai; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
|
|
7
|
|
|
class Client |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var HttpClient |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $client; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The Clarifai client id. |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $clientId; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The Clarifai client secret. |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $clientSecret; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The Clarifai access token. |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $accessToken; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The Clarifai API base URL. |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $baseUrl = 'https://api.clarifai.com/v2'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Instantiate a new clarifai client. |
|
40
|
|
|
* |
|
41
|
|
|
* @param HttpClient $client |
|
42
|
|
|
* @param string $clientId |
|
43
|
|
|
* @param string $clientSecret |
|
44
|
|
|
*/ |
|
45
|
5 |
|
public function __construct($client, $clientId, $clientSecret) |
|
46
|
|
|
{ |
|
47
|
5 |
|
$this->client = $client; |
|
48
|
5 |
|
$this->clientId = $clientId; |
|
49
|
5 |
|
$this->clientSecret = $clientSecret; |
|
50
|
5 |
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
public function withAccessToken($accessToken) |
|
53
|
|
|
{ |
|
54
|
3 |
|
$this->accessToken = $accessToken; |
|
55
|
|
|
|
|
56
|
3 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Predict the content of an image using the Clarifai API. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $urls |
|
63
|
|
|
* @param string $modelId |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function predict(array $urls, $modelId = Models::GENERAL) |
|
67
|
|
|
{ |
|
68
|
3 |
|
$body = []; |
|
69
|
|
|
|
|
70
|
3 |
|
foreach ($urls as $url) { |
|
71
|
3 |
|
$body['inputs'][] = Input::fromUrl($url)->format(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
$response = $this->client->request('POST', $this->baseUrl.'/models/'.$modelId.'/outputs', [ |
|
75
|
|
|
'headers' => [ |
|
76
|
3 |
|
'Accept' => 'application/json', |
|
77
|
3 |
|
'Content-Type' => 'application/json', |
|
78
|
3 |
|
'Authorization' => 'Bearer ' . $this->accessToken, |
|
79
|
|
|
], |
|
80
|
3 |
|
'json' => $body, |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
3 |
|
return json_decode($response->getBody(), true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retrieve a new access token. |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function accessToken() |
|
92
|
|
|
{ |
|
93
|
1 |
|
$response = $this->client->request('POST', $this->baseUrl . '/token', [ |
|
94
|
|
|
'headers' => [ |
|
95
|
|
|
'Accept' => 'application/json', |
|
96
|
|
|
'Content-Type' => 'application/json', |
|
97
|
1 |
|
], |
|
98
|
1 |
|
'auth' => [$this->clientId, $this->clientSecret], |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
1 |
|
return json_decode($response->getBody(), true); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|