@@ -49,11 +49,14 @@ CLARIFAI_USER=username
4949### 3. Create a service at ` app/Services/ClarifAI.php ` .
5050``` php
5151namespace App\Services;
52+
5253use Illuminate\Support\Facades\Http;
54+
5355class 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
8287namespace App\Workflows;
88+
8389use Workflow\ActivityStub;
8490use Workflow\SignalMethod;
8591use 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
144155namespace App\Workflows;
156+
145157use App\Services\ClarifAI;
146158use Illuminate\Support\Facades\Storage;
147159use Workflow\Activity;
160+
148161class AutomatedImageCheckActivity extends Activity
149162{
150163 public function execute($imagePath)
@@ -158,8 +171,10 @@ class AutomatedImageCheckActivity extends Activity
158171### Logging Unsafe Images
159172``` php
160173namespace App\Workflows;
174+
161175use Illuminate\Support\Facades\Log;
162176use Workflow\Activity;
177+
163178class LogUnsafeImageActivity extends Activity
164179{
165180 public function execute($imagePath)
@@ -172,8 +187,10 @@ class LogUnsafeImageActivity extends Activity
172187### Deleting Images
173188``` php
174189namespace App\Workflows;
190+
175191use Illuminate\Support\Facades\Storage;
176192use Workflow\Activity;
193+
177194class DeleteImageActivity extends Activity
178195{
179196 public function execute($imagePath)
0 commit comments