OXIESEC PANEL
- Current Dir:
/
/
var
/
www
/
Deeplit transcription
Server IP: 139.59.38.164
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
05/09/2025 11:18:49 AM
rwxr-xr-x
📄
index.html
13.72 KB
05/09/2025 11:19:22 AM
rw-r--r--
📄
process_image.php
5.76 KB
05/09/2025 11:19:22 AM
rw-r--r--
Editing: process_image.php
Close
<?php header('Content-Type: application/json'); // Error handling function returnError($message) { echo json_encode(['error' => $message]); exit; } // Check if request method is POST if ($_SERVER['REQUEST_METHOD'] !== 'POST') { returnError('Invalid request method. Please use POST.'); } // Configure your OpenAI API key and URL here $api_key = "sk-proj-FrlWqCTIyid7DZGorv0uT3BlbkFJzqrUB0km57kpp4aFPNV7"; // Replace with your actual API key $url = "https://api.openai.com/v1/chat/completions"; $model = "gpt-4o"; // Using the current recommended model with vision capabilities // Validate image data if (!isset($_POST['image_data']) || empty($_POST['image_data'])) { returnError('Image data is required.'); } // Get strategy $strategy = isset($_POST['strategy']) ? $_POST['strategy'] : 'default'; $custom_instructions = isset($_POST['custom_instructions']) ? $_POST['custom_instructions'] : ''; // Process image data $image_data = $_POST['image_data']; // Extract base64 encoded image data if (preg_match('/^data:image\/(\w+);base64,(.+)$/', $image_data, $matches)) { $image_type = $matches[1]; $encoded_image_data = $matches[2]; // Decode base64 data $decoded_image = base64_decode($encoded_image_data); if ($decoded_image === false) { returnError('Failed to decode image data.'); } // Create temporary file to save the image $temp_file = tempnam(sys_get_temp_dir(), 'img_'); if ($temp_file === false) { returnError('Failed to create temporary file.'); } // Add appropriate extension $temp_file_with_ext = $temp_file . '.' . $image_type; rename($temp_file, $temp_file_with_ext); $temp_file = $temp_file_with_ext; // Save decoded image to temporary file if (file_put_contents($temp_file, $decoded_image) === false) { returnError('Failed to save image to temporary file.'); } } else { returnError('Invalid image data format.'); } // Prepare system message based on strategy $system_message = "You are an expert at analyzing images and providing accurate transcriptions of any text found in them. "; switch ($strategy) { case 'detailed': $system_message .= "Provide a detailed transcription that includes all text elements in the image, maintaining the original formatting as much as possible. Note any headers, sections, or special formatting. "; break; case 'summary': $system_message .= "Extract all text from the image and then summarize the key points. "; break; case 'structured': $system_message .= "Organize the transcription into clearly defined sections based on the visual structure of the document. Use headings, bullet points, or other appropriate formatting. "; break; case 'custom': if (!empty($custom_instructions)) { $system_message .= $custom_instructions . " "; } else { $system_message .= "Provide a complete and accurate transcription of all text visible in the image. "; } break; default: // default strategy $system_message .= "Provide a complete and accurate transcription of all text visible in the image. "; } // Append common instruction to always include a summary as well $system_message .= "Then, provide a concise summary highlighting the key points from the transcribed content."; // Read the image file $image_base64 = base64_encode(file_get_contents($temp_file)); if ($image_base64 === false) { unlink($temp_file); // Clean up returnError('Failed to read image file.'); } // Clean up temporary file unlink($temp_file); // Set up data for GPT-4o Vision API $payload = [ "model" => $model, "messages" => [ [ "role" => "system", "content" => $system_message ], [ "role" => "user", "content" => [ [ "type" => "text", "text" => "Please provide a transcript of all text in this image." ], [ "type" => "image_url", "image_url" => [ "url" => "data:image/" . $image_type . ";base64," . $image_base64 ] ] ] ] ], "max_tokens" => 1000 ]; // Set up cURL request to OpenAI API $ch = curl_init($url); 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, [ "Authorization: Bearer " . $api_key, "Content-Type: application/json", "Accept: application/json" ]); // Execute the request $response = curl_exec($ch); $err = curl_error($ch); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // Handle response if ($err) { returnError('cURL Error: ' . $err); } if ($status_code != 200) { $response_data = json_decode($response, true); $error_message = isset($response_data['error']['message']) ? $response_data['error']['message'] : 'API request failed with status code: ' . $status_code; returnError($error_message); } // Process successful response $response_data = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { returnError('Failed to parse API response: ' . json_last_error_msg()); } // Extract transcript from the response if (isset($response_data['choices'][0]['message']['content'])) { $transcript = $response_data['choices'][0]['message']['content']; echo json_encode([ 'success' => true, 'transcript' => $transcript ]); } else { returnError('Unexpected API response format.'); } ?>