Auto commit: 2025-11-20T12:20:29.851Z
This commit is contained in:
parent
28bed0ead6
commit
bb853fb0c8
38
upload.php
38
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(
|
||||
[
|
||||
$resp = LocalAIApi::createResponse([
|
||||
'input' => [
|
||||
['role' => 'system', 'content' => 'You are a document translation service.'],
|
||||
['role' => 'user', 'content' => $prompt],
|
||||
[
|
||||
'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);
|
||||
|
||||
BIN
uploads/20251120_150540.jpg
Normal file
BIN
uploads/20251120_150540.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 355 KiB |
Loading…
x
Reference in New Issue
Block a user