@@ -49,11 +49,14 @@ CLARIFAI_USER=username
49
49
### 3. Create a service at ` app/Services/ClarifAI.php ` .
50
50
``` php
51
51
namespace App\Services;
52
+
52
53
use Illuminate\Support\Facades\Http;
54
+
53
55
class ClarifAI
54
56
{
55
57
private $apiKey;
56
58
private $apiUrl;
59
+
57
60
public function __construct()
58
61
{
59
62
$app = config('services.clarifai.app');
@@ -62,12 +65,14 @@ class ClarifAI
62
65
$this->apiKey = config('services.clarifai.api_key');
63
66
$this->apiUrl = "https://api.clarifai.com/v2/users/{$user}/apps/{$app}/workflows/{$workflow}/results/";
64
67
}
68
+
65
69
public function checkImage(string $image): bool
66
70
{
67
71
$response = Http::withToken($this->apiKey, 'Key')
68
72
->post($this->apiUrl, ['inputs' => [
69
73
['data' => ['image' => ['base64' => base64_encode($image)]]],
70
74
]]);
75
+
71
76
return collect($response->json('results.0.outputs.0.data.concepts', []))
72
77
->filter(fn ($value) => $value['name'] === 'safe')
73
78
->map(fn ($value) => round((float) $value['value']) > 0)
@@ -80,6 +85,7 @@ class ClarifAI
80
85
81
86
``` php
82
87
namespace App\Workflows;
88
+
83
89
use Workflow\ActivityStub;
84
90
use Workflow\SignalMethod;
85
91
use Workflow\WorkflowStub;
@@ -105,11 +111,14 @@ class ImageModerationWorkflow extends Workflow
105
111
public function execute($imagePath)
106
112
{
107
113
$safe = yield from $this->check($imagePath);
114
+
108
115
if (! $safe) {
109
116
yield from $this->unsafe($imagePath);
110
117
return 'unsafe';
111
118
}
119
+
112
120
yield from $this->moderate($imagePath);
121
+
113
122
return $this->approved ? 'approved' : 'rejected';
114
123
}
115
124
@@ -130,7 +139,9 @@ class ImageModerationWorkflow extends Workflow
130
139
{
131
140
while (true) {
132
141
yield ActivityStub::make(NotifyImageModeratorActivity::class, $imagePath);
142
+
133
143
$signaled = yield WorkflowStub::awaitWithTimeout('24 hours', fn () => $this->approved || $this->rejected);
144
+
134
145
if ($signaled) break;
135
146
}
136
147
}
@@ -142,9 +153,11 @@ class ImageModerationWorkflow extends Workflow
142
153
### Automated Image Check
143
154
``` php
144
155
namespace App\Workflows;
156
+
145
157
use App\Services\ClarifAI;
146
158
use Illuminate\Support\Facades\Storage;
147
159
use Workflow\Activity;
160
+
148
161
class AutomatedImageCheckActivity extends Activity
149
162
{
150
163
public function execute($imagePath)
@@ -158,8 +171,10 @@ class AutomatedImageCheckActivity extends Activity
158
171
### Logging Unsafe Images
159
172
``` php
160
173
namespace App\Workflows;
174
+
161
175
use Illuminate\Support\Facades\Log;
162
176
use Workflow\Activity;
177
+
163
178
class LogUnsafeImageActivity extends Activity
164
179
{
165
180
public function execute($imagePath)
@@ -172,8 +187,10 @@ class LogUnsafeImageActivity extends Activity
172
187
### Deleting Images
173
188
``` php
174
189
namespace App\Workflows;
190
+
175
191
use Illuminate\Support\Facades\Storage;
176
192
use Workflow\Activity;
193
+
177
194
class DeleteImageActivity extends Activity
178
195
{
179
196
public function execute($imagePath)
0 commit comments