%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/komfo908/.trash/
Upload File :
Create Path :
Current File : /home/komfo908/.trash/chat_audio.php

<?php
// Substitua pela sua chave da API OpenAI
$apiKey = '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';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['audio'])) {
    $audioPath = $_FILES['audio']['tmp_name'];

    // Passo 1: Transcrição com Whisper
    $whisperCurl = curl_init();
    curl_setopt_array($whisperCurl, [
        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($audioPath),
            'model' => 'whisper-1'
        ]
    ]);
    $whisperResponse = curl_exec($whisperCurl);
    curl_close($whisperCurl);

    $transcription = json_decode($whisperResponse, true);
    $texto = $transcription['text'] ?? '';

    // Passo 2: Enviar texto para ChatGPT
    $chatData = [
        'model' => 'gpt-4',
        'messages' => [
            ['role' => 'user', 'content' => $texto]
        ]
    ];

    $chatCurl = curl_init('https://api.openai.com/v1/chat/completions');
    curl_setopt_array($chatCurl, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => [
            'Content-Type: application/json',
            "Authorization: Bearer $apiKey"
        ],
        CURLOPT_POSTFIELDS => json_encode($chatData)
    ]);
    $chatResponse = curl_exec($chatCurl);
    curl_close($chatCurl);

    $chatResult = json_decode($chatResponse, true);
    $resposta = $chatResult['choices'][0]['message']['content'] ?? 'Erro ao gerar resposta.';

    echo json_encode([
        'transcricao' => $texto,
        'resposta' => $resposta
    ]);
} else {
    echo json_encode(['erro' => 'Envie um arquivo de áudio via POST.']);
}
?>

Zerion Mini Shell 1.0