diff --git a/upload.php b/upload.php index 459603a..f0da8bd 100644 --- a/upload.php +++ b/upload.php @@ -53,21 +53,37 @@ if (move_uploaded_file($file['tmp_name'], $target_path)) { $source_lang = $_POST['source_lang'] ?? 'auto-detect'; $target_lang = $_POST['target_lang'] ?? 'English'; - $file_path_for_ai = 'uploads/' . $file_name; // The path relative to the project root + $file_path_for_ai = 'uploads/' . $file_name; - $prompt = "You are an expert document translator. Please perform the following tasks:\n" - . "1. **OCR Extraction:** Analyze the document located at the following path: `{$file_path_for_ai}`. Extract all visible text from it.\n" - . "2. **Translation:** Translate the extracted text from `{$source_lang}` to `{$target_lang}`.\n" - . "3. **Output:** Return ONLY the translated text as a single block of plain text. Do not include any explanations, apologies, or introductory phrases. Just the translation."; + // Read the file content and base64 encode it + $file_content = file_get_contents($target_path); + $base64_image = base64_encode($file_content); + $mime_type = mime_content_type($target_path); - $resp = LocalAIApi::createResponse( - [ - 'input' => [ - ['role' => 'system', 'content' => 'You are a document translation service.'], - ['role' => 'user', 'content' => $prompt], + $resp = LocalAIApi::createResponse([ + 'input' => [ + [ + 'role' => 'user', + 'content' => [ + [ + 'type' => 'text', + 'text' => "You are an expert document translator. Please perform the following tasks:\n" \ + . "1. **OCR Extraction:** Extract all visible text from the attached image.\n" \ + . "2. **Translation:** Translate the extracted text from `{$source_lang}` to `{$target_lang}`.\n" \ + . "3. **Output:** Return ONLY the translated text as a single block of plain text. Do not include any explanations, apologies, or introductory phrases. Just the translation." + ], + [ + 'type' => 'image', + 'source' => [ + 'type' => 'base64', + 'media_type' => $mime_type, + 'data' => $base64_image, + ], + ], + ], ], - ] - ); + ], + ]); if (!empty($resp['success'])) { $translated_text = LocalAIApi::extractText($resp); diff --git a/uploads/20251120_150540.jpg b/uploads/20251120_150540.jpg new file mode 100644 index 0000000..1bbab03 Binary files /dev/null and b/uploads/20251120_150540.jpg differ