File size: 1,631 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace App\Services\Ai;

use App\Helpers\Classes\Helper;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;

class FalAI
{
    public static function generate($prompt, string $model = 'flux-pro')
    {
        $model = 'fal-ai/' . (setting('fal_ai_default_model') ?: $model);

        $http = Http::withHeaders([
            'Content-Type'  => 'application/json',
            'Accept'        => 'application/json',
            'Authorization' => 'Key ' . Helper::setFalAIKey(),
        ])
            ->post('https://queue.fal.run/' . $model, [
                'prompt' => $prompt,
            ]);

        if ($http->status() == 200) {
            if ($request_id = $http->json('request_id')) {
                return $request_id;
            }
        }

        return null;
    }

    public static function check($uuid, string $model = 'flux-pro')
    {
        $model = 'fal-ai/' . (setting('fal_ai_default_model') ?: $model);

        $url = 'https://queue.fal.run/' . $model . '/requests/' . $uuid;

        $http = Http::withHeaders([
            'Content-Type'  => 'application/json',
            'Accept'        => 'application/json',
            'Authorization' => 'Key ' . Helper::setFalAIKey(),
        ])
            ->get($url);

        if ($images = $http->json('images')) {
            if (is_array($images)) {
                $image = Arr::first($images);

                return [
                    'image' => $image,
                    'size'  => data_get($image, 'width') . 'x' . data_get($image, 'height'),
                ];
            }
        }

        return null;
    }
}