35328-vm/api/defects.php
Flatlogic Bot c010161bd8 AI
2025-10-29 13:52:10 +00:00

107 lines
3.5 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../config.php';
if (!defined('OPENAI_API_KEY')) {
http_response_code(500);
echo json_encode(['error' => 'API key not configured.']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_FILES['vehicleImage'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid request.']);
exit;
}
$file = $_FILES['vehicleImage'];
if ($file['error'] !== UPLOAD_ERR_OK) {
http_response_code(500);
echo json_encode(['error' => 'Error uploading file.']);
exit;
}
// Check file type
$mime_type = mime_content_type($file['tmp_name']);
if (strpos($mime_type, 'image') !== 0) {
http_response_code(400);
echo json_encode(['error' => 'Invalid file type. Please upload an image.']);
exit;
}
$imageData = file_get_contents($file['tmp_name']);
$base64Image = base64_encode($imageData);
$payload = [
'model' => 'gpt-4o',
'messages' => [
[
'role' => 'system',
'content' => 'You are an expert in vehicle damage assessment. Analyze the provided image and identify all damages. Respond with a JSON object containing a single key "defects". The value of "defects" should be an array of strings, where each string is a description of a single detected defect.'
],
[
'role' => 'user',
'content' => [
[
'type' => 'text',
'text' => 'Please analyze the attached image for vehicle defects and list them.'
],
[
'type' => 'image_url',
'image_url' => [
'url' => "data:{$mime_type};base64,{$base64Image}"
]
]
]
]
],
'response_format' => ['type' => 'json_object'],
'max_tokens' => 1000
];
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . OPENAI_API_KEY
]);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode >= 400) {
http_response_code($httpcode);
// Forward the error from OpenAI if possible
$error_response = json_decode($response, true);
if (isset($error_response['error']['message'])) {
echo json_encode(['error' => 'OpenAI API Error: ' . $error_response['error']['message']]);
} else {
echo json_encode(['error' => 'An error occurred with the OpenAI API.']);
}
exit;
}
$result = json_decode($response, true);
if (isset($result['choices'][0]['message']['content'])) {
// The response from OpenAI is a JSON string, so we need to decode it again
$defects_json = $result['choices'][0]['message']['content'];
$defects_data = json_decode($defects_json, true);
if (json_last_error() === JSON_ERROR_NONE && isset($defects_data['defects'])) {
echo json_encode($defects_data);
} else {
// Handle cases where the response is not the expected JSON
http_response_code(500);
echo json_encode(['error' => 'Failed to parse defects from API response.', 'raw_response' => $defects_json]);
}
} else {
http_response_code(500);
echo json_encode(['error' => 'No content received from API.', 'raw_response' => $response]);
}
?>