Files
wevads-platform/scripts/hamid-voice.php
2026-02-26 04:53:11 +01:00

133 lines
4.8 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? $_GET['action'] ?? 'tts';
// Config
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
switch ($action) {
case 'tts': // Text to Speech
$text = $input['text'] ?? '';
$voice = $input['voice'] ?? 'alloy';
$provider = $input['provider'] ?? 'browser'; // browser = Web Speech API côté client
if (empty($text)) {
echo json_encode(['error' => 'Texte requis']);
exit;
}
if ($provider === 'openai') {
// OpenAI TTS (payant)
$stmt = $pdo->query("SELECT config_value FROM admin.hamid_config WHERE config_key = 'openai_api_key'");
$apiKey = $stmt->fetchColumn();
if (empty($apiKey)) {
echo json_encode(['error' => 'Clé OpenAI non configurée', 'fallback' => 'browser']);
exit;
}
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.openai.com/v1/audio/speech',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
],
CURLOPT_POSTFIELDS => json_encode([
'model' => 'tts-1',
'input' => substr($text, 0, 4096),
'voice' => $voice // alloy, echo, fable, onyx, nova, shimmer
]),
CURLOPT_TIMEOUT => 30
]);
$audioData = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
$filename = "tts_" . date("Ymd_His") . ".mp3";
$filepath = "/opt/wevads/public/hamid-files/" . $filename;
file_put_contents($filepath, $audioData);
echo json_encode([
'success' => true,
'provider' => 'openai',
'url' => '/hamid-files/' . $filename,
'voice' => $voice
]);
} else {
echo json_encode(['error' => "Erreur TTS (HTTP $httpCode)", 'fallback' => 'browser']);
}
} else {
// Utiliser Web Speech API côté client (gratuit)
echo json_encode([
'success' => true,
'provider' => 'browser',
'text' => $text,
'instruction' => 'Utilisez speechSynthesis.speak() côté client'
]);
}
break;
case 'stt': // Speech to Text
// Le STT se fait côté client avec Web Speech API (gratuit)
// Ou via upload audio + Whisper API
if (isset($_FILES['audio'])) {
$stmt = $pdo->query("SELECT config_value FROM admin.hamid_config WHERE config_key = 'openai_api_key'");
$apiKey = $stmt->fetchColumn();
if (empty($apiKey)) {
echo json_encode(['error' => 'Clé OpenAI non configurée pour Whisper']);
exit;
}
$audioFile = $_FILES['audio']['tmp_name'];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.openai.com/v1/audio/transcriptions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey
],
CURLOPT_POSTFIELDS => [
'file' => new CURLFile($audioFile, 'audio/webm', 'audio.webm'),
'model' => 'whisper-1',
'language' => 'fr'
],
CURLOPT_TIMEOUT => 30
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
echo json_encode([
'success' => true,
'text' => $data['text'] ?? '',
'provider' => 'whisper'
]);
} else {
echo json_encode([
'success' => true,
'provider' => 'browser',
'instruction' => 'Utilisez webkitSpeechRecognition côté client'
]);
}
break;
case 'voices':
// Liste des voix disponibles
echo json_encode([
'openai' => ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'],
'browser' => 'speechSynthesis.getVoices()'
]);
break;
}