Created
November 21, 2018 11:05
-
-
Save Fed0t/bdd736c0b0abe9fe00ecb51592bf7f18 to your computer and use it in GitHub Desktop.
nude detector laravel job deepai api guzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Jobs; | |
use App\File; | |
use Illuminate\Queue\SerializesModels; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use GuzzleHttp\Client; | |
class CheckNudityJob extends Job { | |
private $deepAiKey = '192d2d888-712ca-42251-93bb-2df708c36462223'; | |
private $api = 'https://api.deepai.org/api/nsfw-detector'; | |
private $url; | |
private $image_id; | |
private $nudeMax = 0.3; | |
public function __construct($url, $id) | |
{ | |
$this->url = $url; | |
$this->image_id = $id; | |
} | |
/** | |
* Execute the job. | |
* | |
* @return void | |
*/ | |
public function handle() | |
{ | |
$client = new Client(); | |
$options = [ | |
'headers' => [ | |
'Api-Key' => $this->deepAiKey | |
], | |
'form_params' => ['image' => $this->url], | |
]; | |
$res = $client->request('POST', $this->api, $options); | |
$responseJson = $res->getBody()->getContents(); | |
$responseData = json_decode($responseJson, true); | |
$scoreResponse = ($responseData['output']) ? round($responseData['output']['nsfw_score'], 2) : null; | |
if (is_float($scoreResponse)): | |
$image = File::find($this->image_id); | |
$image->score_nude = $scoreResponse; | |
$image->verified_nude = 1; | |
if (bccomp($scoreResponse, $this->nudeMax, 3) == 1) $image->gallery_id = 2; | |
$image->save(); | |
endif; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment