Here's with this code i want to generate description for the provided image with the help of Chat GPT API using "gpt-4-vision-preview" model. But the cide is not working. Please help.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\JsonResponse;
use GuzzleHttp\Client;
class ImageDescController extends Controller
{
public function describeImageWithText(Request $request)
{
$api_key = "**********************";
$image_path = 'my_image_path';
$base64_image = base64_encode($image_path);
$client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$api_key}"
]
]);
$payload = [
'model' => 'gpt-4-vision-preview',
'messages' => [
[
'role' => 'user',
'content' => [
[
'type' => 'text',
'text' => "What is in the image?"
],
[
'type' => 'image_url',
'image_url' => [
'url' => "data:image/jpeg;base64,{$base64_image}"
]
]
]
]
],
'max_tokens' => 300
];
$response = $client->post('https://api.openai.com/v1/chat/completions', [
'json' => $payload
]);
dd($response);
return response()->json(json_decode($response->getBody(), true));
}
}
Tried uploading an image and want description as an output. Please provide a better solution. Also want to know if it is possible or not?