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

38 lines
1.1 KiB
PHP
Executable File

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('X-Accel-Buffering: no');
ob_implicit_flush(true);
while (ob_get_level()) ob_end_flush();
$message = $_POST['message'] ?? $_GET['message'] ?? '';
$provider = $_POST['provider'] ?? $_GET['provider'] ?? 'mistral';
// Appeler l'API
$ctx = stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode(['message' => $message, 'provider' => $provider]),
'timeout' => 60
],
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false]
]);
$response = @file_get_contents("https://127.0.0.1:8443/hamid-api.php", false, $ctx);
$data = json_decode($response, true);
$text = $data['response'] ?? 'Erreur de réponse';
// Streamer mot par mot
$words = preg_split('/(\s+)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($words as $word) {
echo "data: " . json_encode(['text' => $word]) . "\n\n";
flush();
usleep(25000); // 25ms
}
echo "data: [DONE]\n\n";