voice2text/process.php
DJP 846693b097 Initial commit: Voice to Text with Whisper & DeepL Translation
Features:
- OpenAI Whisper for audio transcription
- DeepL API for translation (30+ languages)
- Multiple output formats: TXT, VTT, SRT
- Flask Python API backend
- PHP frontend with black/gold theme
- Support for 350MB files
- Generates both original and translated files

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 11:54:39 -04:00

86 lines
No EOL
3.4 KiB
PHP

<?php
session_start();
require_once 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['voiceFile'])) {
$file = $_FILES['voiceFile'];
$outputFormat = isset($_POST['outputFormat']) ? $_POST['outputFormat'] : 'txt';
$enableTranslation = isset($_POST['enableTranslation']) ? $_POST['enableTranslation'] : '0';
$targetLanguage = isset($_POST['targetLanguage']) ? $_POST['targetLanguage'] : 'EN-US';
// Check file size
if ($file['size'] > 350 * 1024 * 1024) { // 350 MB limit
echo json_encode(['success' => false, 'error' => "File is too large. Maximum size is 350 MB."]);
exit;
}
// Prepare the file for sending to Python API
$formData = [
'audio' => new CURLFile($file['tmp_name'], $file['type'], $file['name']),
'format' => $outputFormat,
'translate' => $enableTranslation,
'target_lang' => $targetLanguage
];
// Send request to Python API
$ch = curl_init(PYTHON_API_URL . '/transcribe');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 5 minutes timeout for large files
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo json_encode(['success' => false, 'error' => "Error processing file: " . curl_error($ch)]);
} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode === 200) {
$data = json_decode($response, true);
if (isset($data['success']) && $data['success']) {
// For text format, return the text directly
if ($outputFormat === 'txt' && isset($data['text'])) {
$response = [
'success' => true,
'response' => nl2br(htmlspecialchars($data['text'])),
'format' => $outputFormat
];
// Add translated text if available
if (isset($data['translated_text'])) {
$response['translatedResponse'] = nl2br(htmlspecialchars($data['translated_text']));
}
echo json_encode($response);
} else {
// For VTT/SRT, provide download links
$downloadUrl = 'download.php?file=' . urlencode($data['filename']);
$response = [
'success' => true,
'fileUrl' => $downloadUrl,
'filename' => $data['filename'],
'format' => $outputFormat
];
// Add translated file download link if available
if (isset($data['translated_filename'])) {
$response['translatedFileUrl'] = 'download.php?file=' . urlencode($data['translated_filename']);
$response['translatedFilename'] = $data['translated_filename'];
}
echo json_encode($response);
}
} else {
echo json_encode(['success' => false, 'error' => $data['error'] ?? "Unknown error occurred"]);
}
} else {
echo json_encode(['success' => false, 'error' => "Server error: HTTP $httpCode"]);
}
}
curl_close($ch);
} else {
echo json_encode(['success' => false, 'error' => "Invalid request."]);
}