File size: 10,114 Bytes
1501522 da7bbba 1501522 cddf7d8 1501522 cddf7d8 1501522 cddf7d8 1501522 eeaf011 da7bbba eeaf011 da7bbba eeaf011 da7bbba eeaf011 da7bbba 1501522 | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | <?php
namespace App\Services;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;
class FreepikVideoService
{
public function __construct(private ?HuggingFaceMediaStorageService $hfMediaStorage = null)
{
}
public function generate(string $prompt, int $duration = 5): array
{
$apiKey = trim((string) config('services.freepik.api_key', ''));
$timeout = max(20, (int) config('services.freepik.timeout', 90));
$cleanPrompt = trim($prompt);
$duration = in_array($duration, [5, 8, 10], true) ? $duration : 5;
if ($apiKey === '') {
throw new RuntimeException($this->missingApiKeyMessage('Video generator'));
}
if ($cleanPrompt === '') {
throw new RuntimeException('Prompt video Freepik tidak boleh kosong.');
}
$response = $this->client($apiKey, $timeout)
->post($this->videoEndpoint(), [
'prompt' => $cleanPrompt,
'ratio' => '720:1280',
'duration' => $duration,
]);
$data = $this->extractData($response, 'Freepik video request gagal');
$videoUrl = $this->extractGeneratedUrl($data);
if ($videoUrl === null) {
$taskId = trim((string) data_get($data, 'task_id', ''));
if ($taskId === '') {
throw new RuntimeException('Freepik video tidak mengembalikan task_id.');
}
$videoUrl = $this->awaitGeneratedUrl($apiKey, $timeout, $taskId);
}
$relativePath = $this->persistVideo($videoUrl, $timeout);
return [
'url' => $this->offloadPublicPath($relativePath),
'provider' => 'freepik-runway',
'aspect' => '9:16',
'duration_seconds' => $duration,
];
}
private function awaitGeneratedUrl(string $apiKey, int $timeout, string $taskId): string
{
$deadline = microtime(true) + max(35, min(240, $timeout * 2));
$lastError = null;
do {
try {
$response = $this->client($apiKey, min(25, $timeout))
->get($this->videoEndpoint().'/'.$taskId);
$data = $this->extractData($response, 'Gagal membaca status task Freepik video');
$generatedUrl = $this->extractGeneratedUrl($data);
if ($generatedUrl !== null) {
return $generatedUrl;
}
$status = strtoupper(trim((string) data_get($data, 'status', '')));
if (in_array($status, ['FAILED', 'ERROR', 'REJECTED', 'CANCELLED'], true)) {
throw new RuntimeException('Freepik video task berakhir dengan status '.$status.'.');
}
} catch (\Throwable $pollError) {
$lastError = $pollError;
}
usleep(1500000);
} while (microtime(true) < $deadline);
if ($lastError !== null) {
throw new RuntimeException('Timeout saat menunggu hasil video dari Freepik. Detail terakhir: '.$lastError->getMessage());
}
throw new RuntimeException('Timeout saat menunggu hasil video dari Freepik.');
}
private function persistVideo(string $videoUrl, int $timeout): string
{
$response = Http::connectTimeout(20)
->timeout(max(45, min(120, $timeout * 2)))
->withHeaders(['Accept' => 'video/*'])
->get($videoUrl);
if (! $response->successful()) {
throw new RuntimeException('Gagal mengambil hasil video dari Freepik: '.$response->status());
}
$binary = $response->body();
if (! is_string($binary) || $binary === '') {
throw new RuntimeException('Binary video dari Freepik kosong.');
}
$relativePath = 'videos/freepik_'.now()->format('Ymd_His').'_'.Str::lower(Str::random(8)).'.mp4';
Storage::disk('public')->makeDirectory('videos');
if (! Storage::disk('public')->put($relativePath, $binary)) {
throw new RuntimeException('Gagal menyimpan hasil video Freepik ke storage public.');
}
return $relativePath;
}
private function offloadPublicPath(string $relativePath): string
{
try {
return $this->hfMedia()->offloadPublicRelativePath($relativePath);
} catch (\Throwable) {
return Storage::url($relativePath);
}
}
private function hfMedia(): HuggingFaceMediaStorageService
{
if ($this->hfMediaStorage instanceof HuggingFaceMediaStorageService) {
return $this->hfMediaStorage;
}
$service = app(HuggingFaceMediaStorageService::class);
if (! $service instanceof HuggingFaceMediaStorageService) {
throw new RuntimeException('Service HuggingFaceMediaStorageService tidak tersedia.');
}
$this->hfMediaStorage = $service;
return $this->hfMediaStorage;
}
private function client(string $apiKey, int $timeout)
{
return Http::connectTimeout(20)
->timeout($timeout)
->acceptJson()
->withHeaders([
'x-freepik-api-key' => $apiKey,
]);
}
/**
* Build the Freepik video generation endpoint.
*
* The original implementation concatenated the configured `services.freepik.base_url`
* (which defaults to the *text‑to‑image* endpoint) with the video path, resulting in
* an invalid URL such as `https://api.freepik.com/v1/ai/text-to-image/v1/ai/text-to-video/...`.
* This caused the remote API to return a 500 error which propagated to the Space UI.
*
* The logic now:
* 1. If the user explicitly sets a `FREEPIK_VIDEO_BASE_URL` env variable (via the config
* key `services.freepik.video_base_url` – fallback to the generic `base_url`), use it
* directly.
* 2. If the configured base URL already contains the segment `text-to-video`, assume it
* is a proper video endpoint and return it unchanged.
* 3. Otherwise fall back to the official default video endpoint.
*/
private function videoEndpoint(): string
{
// Allow an explicit video base URL; otherwise reuse the generic base_url.
$configured = trim((string) config('services.freepik.video_base_url', config('services.freepik.base_url', '')));
if ($configured === '' || Str::contains($configured, 'text-to-video')) {
// Empty config or already a video endpoint – use the default video URL.
return $configured !== '' && Str::contains($configured, 'text-to-video')
? rtrim($configured, '/').'/runway-4-5'
: 'https://api.freepik.com/v1/ai/text-to-video/runway-4-5';
}
// If a non‑video base URL is provided (e.g., the default text‑to‑image URL), ignore it
// and return the canonical video endpoint.
return 'https://api.freepik.com/v1/ai/text-to-video/runway-4-5';
}
private function apiOrigin(string $url): string
{
$scheme = parse_url($url, PHP_URL_SCHEME) ?: 'https';
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
if (! is_string($host) || $host === '') {
return 'https://api.freepik.com';
}
return $scheme.'://'.$host.($port ? ':'.$port : '');
}
private function extractData(Response $response, string $prefix): array
{
if (! $response->successful()) {
throw new RuntimeException($prefix.': '.$response->status().' - '.$this->responseError($response));
}
$json = $response->json();
$data = data_get($json, 'data', $json);
if (! is_array($data)) {
throw new RuntimeException($prefix.': format response Freepik tidak dikenali.');
}
return $data;
}
private function responseError(Response $response): string
{
$message = data_get($response->json(), 'message')
?? data_get($response->json(), 'error.message')
?? data_get($response->json(), 'detail')
?? $response->body();
return Str::limit(trim((string) $message), 240);
}
/**
* Return a helpful message when the Freepik API key is missing.
*
* In a Hugging Face Space the environment variables are defined in the Space
* settings. If the key is absent we guide the user to add
* `FREEPIK_API_KEY` there. When the space runs in HF‑only mode we explain
* that the video feature is disabled.
*/
private function missingApiKeyMessage(string $feature): string
{
$hfOnly = strtolower(trim((string) (
config('services.huggingface.hf_only')
?? $_ENV['TOOLS_HF_ONLY']
?? $_SERVER['TOOLS_HF_ONLY']
?? getenv('TOOLS_HF_ONLY')
?? 'false'
)));
if (in_array($hfOnly, ['1', 'true', 'yes', 'on'], true)) {
return $feature.' belum tersedia di mode Hugging Face‑only.';
}
return 'FREEPIK_API_KEY belum diisi di file .env. Tambahkan variabel `FREEPIK_API_KEY` di Settings → Secrets of the Hugging Face Space dan redeploy.';
}
private function extractGeneratedUrl(array $data): ?string
{
$candidates = data_get($data, 'generated', []);
if (! is_array($candidates) || $candidates === []) {
return null;
}
foreach ($candidates as $candidate) {
if (is_string($candidate) && Str::startsWith($candidate, ['http://', 'https://'])) {
return trim($candidate);
}
if (is_array($candidate)) {
$url = data_get($candidate, 'url', data_get($candidate, 'video_url'));
if (is_string($url) && trim($url) !== '') {
return trim($url);
}
}
}
return null;
}
}
|