From c010161bd8d4f2d72ef4817fd36cf0777b564440 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Wed, 29 Oct 2025 13:52:10 +0000 Subject: [PATCH] AI --- .gitignore | 1 + api/defects.php | 107 ++++++++++++++++++++++++++++++++++++ index.php | 143 +++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 238 insertions(+), 13 deletions(-) create mode 100644 api/defects.php diff --git a/.gitignore b/.gitignore index e427ff3..36727fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ */node_modules/ */build/ +config.php \ No newline at end of file diff --git a/api/defects.php b/api/defects.php new file mode 100644 index 0000000..eb3c70e --- /dev/null +++ b/api/defects.php @@ -0,0 +1,107 @@ + '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]); +} +?> \ No newline at end of file diff --git a/index.php b/index.php index 580a662..51d74fd 100644 --- a/index.php +++ b/index.php @@ -18,6 +18,7 @@ --accent-blue-darker: #0056b3; --border-color: #dee2e6; --border-dashed: #ced4da; + --error-red: #dc3545; } body { @@ -148,7 +149,9 @@ } .upload-button { - display: block; + display: flex; + align-items: center; + justify-content: center; width: 100%; padding: 0.875rem; margin-top: 1.5rem; @@ -163,9 +166,29 @@ text-align: center; } - .upload-button:hover { + .upload-button:hover:not(:disabled) { background-color: var(--accent-blue-darker); } + + .upload-button:disabled { + background-color: #a1c9ff; + cursor: not-allowed; + } + + .spinner { + display: none; + width: 1.2em; + height: 1.2em; + border: 2px solid rgba(255, 255, 255, 0.3); + border-radius: 50%; + border-top-color: #fff; + animation: spin 1s ease-in-out infinite; + margin-right: 0.5rem; + } + + @keyframes spin { + to { transform: rotate(360deg); } + } /* Results Table */ .results-container { @@ -207,7 +230,12 @@ text-align: center; color: var(--text-secondary); padding: 3rem 1rem; - font-style: italic; + } + + .placeholder.error { + color: var(--error-red); + font-style: normal; + font-weight: 500; } footer { @@ -230,9 +258,9 @@

Fahrzeugbild hochladen

Foto zur Analyse von Schäden hochladen.

-
+
- +

Analyseergebnisse

-

Erkannte Schäden und geschätzte Kosten.

+

Erkannte Schäden.

- - + - +
# Erkannter SchadenGeschätzte Kosten
Laden Sie ein Bild hoch, um die Analyse zu sehen.Laden Sie ein Bild hoch, um die Analyse zu sehen.
@@ -277,10 +307,15 @@ - + \ No newline at end of file