Hi everyone,
I’m using the Gemini API to moderate various types of content: video, audio, image, PDF, and chat. It works perfectly for chat and image content, but I’m encountering a 404 error when trying to moderate video, audio, and PDF files after the upload.
Here’s the process I’m using to upload the files to the Gemini API:
$pdfPath = Storage::path('uploaded_images/sample.mp4');
$mimeType = Storage::mimeType('uploaded_images/sample.mp4');
$numBytes = filesize($pdfPath);
$googleApiKey = config('services.gemini.api_key');
$displayName = 'sample.mp4';
// Initial resumable upload request
$response = Http::timeout(600)->withHeaders([
'X-Goog-Upload-Protocol' => 'resumable',
'X-Goog-Upload-Command' => 'start',
'X-Goog-Upload-Header-Content-Length' => $numBytes,
'X-Goog-Upload-Header-Content-Type' => $mimeType,
'Content-Type' => 'application/json',
])->post("https://generativelanguage.googleapis.com/upload/v1beta/files?key=$googleApiKey", [
'file' => [
'display_name' => $displayName,
],
]);
if (!$response->successful()) {
dd('Failed to start resumable upload');
}
// Extract the upload URL from the headers
$uploadUrl = $response->header('X-Goog-Upload-URL');
// Upload the actual file bytes
$res = Http::timeout(600)->withHeaders([
'Content-Length' => $numBytes,
'X-Goog-Upload-Offset' => 0,
'X-Goog-Upload-Command' => 'upload, finalize',
])->withBody(
file_get_contents($pdfPath), $mimeType
)->post($uploadUrl);
if (!$res->successful()) {
dd('Failed to finish upload');
}
// Get response and send a moderation check request
$x = $res->json();
// When I make the following request, I get a 404 error
$check = Http::post('https://generativelanguage.googleapis.com/v1beta/'. $x['file']['name']);
Problem: I get a 404 error when attempting to moderate video, audio, and PDF files using the API after successfully uploading them. The error happens at the moderation check step.
Things I’ve Tried:
I’ve confirmed that the upload process completes for chat and image files, but for video, audio, and PDFs, the issue persists.
The file paths, MIME types, and sizes are all being correctly handled during upload.
Has anyone encountered a similar issue, or does anyone know if there’s a limitation with the Gemini API for these file types? Any guidance or suggestions would be much appreciated!
Thanks in advance!