43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../ai/LocalAIApi.php';
|
|
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['success' => false, 'error' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
$text = $_POST['text'] ?? '';
|
|
$direction = $_POST['direction'] ?? 'en-ar'; // en-ar or ar-en
|
|
|
|
if (empty($text)) {
|
|
echo json_encode(['success' => false, 'error' => 'Text is empty']);
|
|
exit;
|
|
}
|
|
|
|
$target_lang = $direction === 'en-ar' ? 'Arabic' : 'English';
|
|
$source_lang = $direction === 'en-ar' ? 'English' : 'Arabic';
|
|
|
|
try {
|
|
$prompt = "Translate the following text from $source_lang to $target_lang. Provide ONLY the translated text, no explanation or extra characters.\n\nText: $text";
|
|
|
|
$response = LocalAIApi::createResponse([
|
|
'input' => [
|
|
['role' => 'system', 'content' => 'You are a professional translator specializing in business and laundry service terminology.'],
|
|
['role' => 'user', 'content' => $prompt],
|
|
],
|
|
]);
|
|
|
|
if (!empty($response['success'])) {
|
|
$translatedText = LocalAIApi::extractText($response);
|
|
echo json_encode(['success' => true, 'translation' => trim($translatedText)]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'error' => $response['error'] ?? 'AI translation failed']);
|
|
}
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|
|
|