Auto commit: 2025-11-20T13:22:26.468Z
@ -3,9 +3,22 @@ ini_set('display_errors', 0);
|
||||
ini_set('log_errors', 1);
|
||||
ini_set('error_log', __DIR__ . '/error_log.txt');
|
||||
|
||||
// --- Debug Logging ---
|
||||
$log_file = __DIR__ . '/translate_log.txt';
|
||||
file_put_contents($log_file, "--- New Translation Request ---
|
||||
", FILE_APPEND);
|
||||
|
||||
function write_log($message) {
|
||||
global $log_file;
|
||||
$timestamp = date('Y-m-d H:i:s');
|
||||
file_put_contents($log_file, "[{$timestamp}] " . print_r($message, true) . "\n", FILE_APPEND);
|
||||
}
|
||||
// --- End Debug Logging ---
|
||||
|
||||
require_once __DIR__ . '/ai/LocalAIApi.php';
|
||||
|
||||
function json_response($status, $message, $data = null) {
|
||||
write_log("Sending JSON response: Status: {$status}, Message: {$message}");
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode([
|
||||
'status' => $status,
|
||||
@ -15,78 +28,82 @@ function json_response($status, $message, $data = null) {
|
||||
exit;
|
||||
}
|
||||
|
||||
write_log("Request method: " . $_SERVER['REQUEST_METHOD']);
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
json_response('error', 'Invalid request method.');
|
||||
}
|
||||
|
||||
if (!isset($_POST['file_path']) || !isset($_POST['source_lang']) || !isset($_POST['target_lang'])) {
|
||||
write_log("POST data: " . json_encode($_POST));
|
||||
if (!isset($_POST['file_path']) || !isset($_POST['source-lang']) || !isset($_POST['target-lang'])) {
|
||||
json_response('error', 'Missing required parameters.');
|
||||
}
|
||||
|
||||
$file_path = $_POST['file_path'];
|
||||
$source_lang = $_POST['source_lang'];
|
||||
$target_lang = $_POST['target_lang'];
|
||||
$source_lang = $_POST['source-lang'];
|
||||
$target_lang = $_POST['target-lang'];
|
||||
|
||||
$full_path = __DIR__ . '/' . $file_path;
|
||||
$url_path = parse_url($file_path, PHP_URL_PATH);
|
||||
$full_path = __DIR__ . $url_path;
|
||||
write_log("Full file path: {$full_path}");
|
||||
|
||||
if (!file_exists($full_path)) {
|
||||
json_response('error', 'File not found.');
|
||||
}
|
||||
|
||||
try {
|
||||
$file_content = file_get_contents($full_path);
|
||||
if ($file_content === false) {
|
||||
json_response('error', 'Failed to read file content.');
|
||||
}
|
||||
|
||||
$base64_image = base64_encode($file_content);
|
||||
// Step 1: Perform OCR on the image
|
||||
write_log("Performing OCR on file: {$full_path}");
|
||||
// Use escapeshellarg to prevent command injection vulnerabilities
|
||||
$escaped_path = escapeshellarg($full_path);
|
||||
$ocr_command = "tesseract {$escaped_path} stdout";
|
||||
write_log("Executing OCR command: {$ocr_command}");
|
||||
|
||||
if (function_exists('mime_content_type')) {
|
||||
$mime_type = mime_content_type($full_path);
|
||||
} else {
|
||||
$mime_type = 'application/octet-stream';
|
||||
}
|
||||
// Execute the command and capture the output
|
||||
$extracted_text = shell_exec($ocr_command);
|
||||
write_log("OCR raw output: " . $extracted_text);
|
||||
|
||||
$resp = LocalAIApi::createResponse([
|
||||
if ($extracted_text === null || trim($extracted_text) === '') {
|
||||
json_response('error', 'OCR failed or no text was found in the image.');
|
||||
}
|
||||
|
||||
$extracted_text = trim($extracted_text);
|
||||
write_log("Extracted text (trimmed): " . $extracted_text);
|
||||
|
||||
// Step 2: Translate the extracted text
|
||||
write_log("Sending extracted text to AI for translation...");
|
||||
|
||||
$prompt = "Translate the following text from {$source_lang} to {$target_lang}:\n\n{$extracted_text}";
|
||||
|
||||
$payload = [
|
||||
'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,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
'content' => $prompt
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
write_log("AI request payload: " . json_encode($payload, JSON_PRETTY_PRINT));
|
||||
$resp = LocalAIApi::createResponse($payload);
|
||||
write_log("Received response from AI API: " . json_encode($resp, JSON_PRETTY_PRINT));
|
||||
|
||||
if (!empty($resp['success'])) {
|
||||
$translated_text = LocalAIApi::extractText($resp);
|
||||
if ($translated_text === '') {
|
||||
$decoded = LocalAIApi::decodeJsonFromResponse($resp);
|
||||
$error_details = $decoded ? json_encode($decoded, JSON_PRETTY_PRINT) : 'AI returned an empty or invalid response.';
|
||||
json_response('error', 'AI processing failed. Details: ' . $error_details);
|
||||
json_response('error', 'AI translation failed. Details: ' . $error_details);
|
||||
} else {
|
||||
json_response('success', 'File translated successfully.', ['translatedText' => $translated_text]);
|
||||
json_response('success', 'Translation successful!', ['translatedText' => $translated_text, 'extractedText' => $extracted_text]);
|
||||
}
|
||||
} else {
|
||||
$error_message = $resp['error'] ?? 'Unknown AI error.';
|
||||
json_response('error', 'Failed to get response from AI service: ' . $error_message);
|
||||
json_response('error', 'AI translation failed: ' . $error_message);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
json_response('error', 'An exception occurred: ' . $e->getMessage());
|
||||
write_log("An exception occurred: " . $e->getMessage());
|
||||
write_log("Stack trace: " . $e->getTraceAsString());
|
||||
json_response('error', 'An unexpected error occurred: ' . $e->getMessage());
|
||||
}
|
||||
?>
|
||||
?>
|
||||
482
translate_log.txt
Normal file
BIN
uploads/doc_691f0fc897201.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f109003148.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f10e1495ec.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f11431672c.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f11e4b0ca0.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f1291a7b29.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f13131bb30.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f13af407b9.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f144e9064d.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f14bb0f188.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f1543b7b27.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
uploads/doc_691f15ab4855e.jpg
Normal file
|
After Width: | Height: | Size: 355 KiB |