Skip to content

Commit d8586d6

Browse files
authored
Update 2023-08-20-ai-image-moderation-with-laravel-workflow.md
1 parent 5b5c354 commit d8586d6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

blog/2023-08-20-ai-image-moderation-with-laravel-workflow.md

+17
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ CLARIFAI_USER=username
4949
### 3. Create a service at `app/Services/ClarifAI.php`.
5050
```php
5151
namespace App\Services;
52+
5253
use Illuminate\Support\Facades\Http;
54+
5355
class ClarifAI
5456
{
5557
private $apiKey;
5658
private $apiUrl;
59+
5760
public function __construct()
5861
{
5962
$app = config('services.clarifai.app');
@@ -62,12 +65,14 @@ class ClarifAI
6265
$this->apiKey = config('services.clarifai.api_key');
6366
$this->apiUrl = "https://api.clarifai.com/v2/users/{$user}/apps/{$app}/workflows/{$workflow}/results/";
6467
}
68+
6569
public function checkImage(string $image): bool
6670
{
6771
$response = Http::withToken($this->apiKey, 'Key')
6872
->post($this->apiUrl, ['inputs' => [
6973
['data' => ['image' => ['base64' => base64_encode($image)]]],
7074
]]);
75+
7176
return collect($response->json('results.0.outputs.0.data.concepts', []))
7277
->filter(fn ($value) => $value['name'] === 'safe')
7378
->map(fn ($value) => round((float) $value['value']) > 0)
@@ -80,6 +85,7 @@ class ClarifAI
8085

8186
```php
8287
namespace App\Workflows;
88+
8389
use Workflow\ActivityStub;
8490
use Workflow\SignalMethod;
8591
use Workflow\WorkflowStub;
@@ -105,11 +111,14 @@ class ImageModerationWorkflow extends Workflow
105111
public function execute($imagePath)
106112
{
107113
$safe = yield from $this->check($imagePath);
114+
108115
if (! $safe) {
109116
yield from $this->unsafe($imagePath);
110117
return 'unsafe';
111118
}
119+
112120
yield from $this->moderate($imagePath);
121+
113122
return $this->approved ? 'approved' : 'rejected';
114123
}
115124

@@ -130,7 +139,9 @@ class ImageModerationWorkflow extends Workflow
130139
{
131140
while (true) {
132141
yield ActivityStub::make(NotifyImageModeratorActivity::class, $imagePath);
142+
133143
$signaled = yield WorkflowStub::awaitWithTimeout('24 hours', fn () => $this->approved || $this->rejected);
144+
134145
if ($signaled) break;
135146
}
136147
}
@@ -142,9 +153,11 @@ class ImageModerationWorkflow extends Workflow
142153
### Automated Image Check
143154
```php
144155
namespace App\Workflows;
156+
145157
use App\Services\ClarifAI;
146158
use Illuminate\Support\Facades\Storage;
147159
use Workflow\Activity;
160+
148161
class AutomatedImageCheckActivity extends Activity
149162
{
150163
public function execute($imagePath)
@@ -158,8 +171,10 @@ class AutomatedImageCheckActivity extends Activity
158171
### Logging Unsafe Images
159172
```php
160173
namespace App\Workflows;
174+
161175
use Illuminate\Support\Facades\Log;
162176
use Workflow\Activity;
177+
163178
class LogUnsafeImageActivity extends Activity
164179
{
165180
public function execute($imagePath)
@@ -172,8 +187,10 @@ class LogUnsafeImageActivity extends Activity
172187
### Deleting Images
173188
```php
174189
namespace App\Workflows;
190+
175191
use Illuminate\Support\Facades\Storage;
176192
use Workflow\Activity;
193+
177194
class DeleteImageActivity extends Activity
178195
{
179196
public function execute($imagePath)

0 commit comments

Comments
 (0)