Stream Whisper verbose metadata live without overflowing Reverb.
Persist accumulated verbose_json on the recording and broadcast only transcript/whisper deltas so the show page can render language, segments, word timestamps, and confidence while a run is in progress.
This commit is contained in:
@@ -7,6 +7,7 @@ use Closure;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laravel\Ai\Transcription;
|
||||
use RuntimeException;
|
||||
|
||||
@@ -17,16 +18,15 @@ class TranscriptionService
|
||||
/**
|
||||
* Transcribe a recording with the local faster-whisper server.
|
||||
*
|
||||
* @param (Closure(string, int, ?string): void)|null $onProgress
|
||||
* @param (Closure(string, int, ?string, ?array): void)|null $onProgress
|
||||
* @param (Closure(): bool)|null $shouldContinue
|
||||
*/
|
||||
public function transcribe(Recording $recording, ?Closure $onProgress = null, ?Closure $shouldContinue = null): string
|
||||
{
|
||||
$report = $onProgress ?? static fn (string $message, int $percent, ?string $partial = null) => null;
|
||||
$report = $onProgress ?? static fn (string $message, int $percent, ?string $partial = null, ?array $whisper = null) => null;
|
||||
$model = config('ai.local_whisper_model', 'Systran/faster-whisper-base');
|
||||
|
||||
$report('Connecting to local faster-whisper server…', 30);
|
||||
$report("Transcribing locally with {$model} (audio stays on this machine)…", 50);
|
||||
$report("Transcribing locally with {$model} (audio stays on this machine)…", 0);
|
||||
|
||||
if (Transcription::isFaked()) {
|
||||
$transcript = Transcription::fromStorage($recording->file_path)
|
||||
@@ -36,15 +36,13 @@ class TranscriptionService
|
||||
$transcript = $this->transcribeViaLocalWhisper($recording, $report, $model, $shouldContinue);
|
||||
}
|
||||
|
||||
$report('Received transcript from local Whisper…', 85);
|
||||
|
||||
return (string) $transcript;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call local Whisper, streaming SSE when the server supports it.
|
||||
*
|
||||
* @param Closure(string, int, ?string): void $report
|
||||
* @param Closure(string, int, ?string, ?array): void $report
|
||||
* @param (Closure(): bool)|null $shouldContinue
|
||||
*/
|
||||
private function transcribeViaLocalWhisper(Recording $recording, Closure $report, string $model, ?Closure $shouldContinue): string
|
||||
@@ -62,9 +60,10 @@ class TranscriptionService
|
||||
->withOptions(['stream' => true])
|
||||
->post('audio/transcriptions', [
|
||||
'model' => $model,
|
||||
'response_format' => 'json',
|
||||
'response_format' => 'verbose_json',
|
||||
'stream' => 'true',
|
||||
'without_timestamps' => 'false',
|
||||
'timestamp_granularities[]' => 'word',
|
||||
]);
|
||||
|
||||
if (! $response->successful()) {
|
||||
@@ -76,30 +75,33 @@ class TranscriptionService
|
||||
$contentType = strtolower((string) $response->header('Content-Type'));
|
||||
|
||||
if (! str_contains($contentType, 'text/event-stream')) {
|
||||
return $this->extractTranscriptText($response);
|
||||
Log::warning('Whisper did not stream SSE; falling back to a single JSON body', [
|
||||
'recording_id' => $recording->id,
|
||||
'content_type' => $contentType,
|
||||
]);
|
||||
|
||||
return $this->extractTranscriptText($response, $report, $message);
|
||||
}
|
||||
|
||||
return $this->consumeWhisperStream($response, $report, $message, $recording->duration_seconds, $shouldContinue);
|
||||
return $this->consumeWhisperStream($response, $report, $message, $recording, $shouldContinue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure(string, int, ?string): void $report
|
||||
* @param Closure(string, int, ?string, ?array): void $report
|
||||
* @param (Closure(): bool)|null $shouldContinue
|
||||
*/
|
||||
private function consumeWhisperStream(
|
||||
Response $response,
|
||||
Closure $report,
|
||||
string $message,
|
||||
?int $durationSeconds,
|
||||
Recording $recording,
|
||||
?Closure $shouldContinue,
|
||||
): string {
|
||||
$body = $response->toPsrResponse()->getBody();
|
||||
$buffer = '';
|
||||
$accumulated = '';
|
||||
$lastPercent = 50;
|
||||
$eventsWithoutTimestamp = 0;
|
||||
$lastFlushAt = 0.0;
|
||||
$pending = false;
|
||||
$lastPercent = 0;
|
||||
$idleReads = 0;
|
||||
|
||||
try {
|
||||
while (! $body->eof()) {
|
||||
@@ -112,43 +114,42 @@ class TranscriptionService
|
||||
$chunk = $body->read(8192);
|
||||
|
||||
if ($chunk === '') {
|
||||
break;
|
||||
$idleReads++;
|
||||
|
||||
if ($idleReads >= 40) {
|
||||
break;
|
||||
}
|
||||
|
||||
usleep(50_000);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$idleReads = 0;
|
||||
$buffer .= $chunk;
|
||||
|
||||
foreach ($this->stream->extractPayloads($buffer) as $payload) {
|
||||
[$accumulated, $lastPercent, $eventsWithoutTimestamp, $pending] = $this->ingestEvent(
|
||||
[$accumulated, $lastPercent] = $this->ingestEvent(
|
||||
$payload,
|
||||
$accumulated,
|
||||
$lastPercent,
|
||||
$eventsWithoutTimestamp,
|
||||
$durationSeconds,
|
||||
$recording,
|
||||
$report,
|
||||
$message,
|
||||
$lastFlushAt,
|
||||
$pending,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->stream->flushBuffer($buffer) as $payload) {
|
||||
[$accumulated, $lastPercent, $eventsWithoutTimestamp, $pending] = $this->ingestEvent(
|
||||
[$accumulated, $lastPercent] = $this->ingestEvent(
|
||||
$payload,
|
||||
$accumulated,
|
||||
$lastPercent,
|
||||
$eventsWithoutTimestamp,
|
||||
$durationSeconds,
|
||||
$recording,
|
||||
$report,
|
||||
$message,
|
||||
$lastFlushAt,
|
||||
$pending,
|
||||
);
|
||||
}
|
||||
|
||||
if ($pending && $accumulated !== '') {
|
||||
$report($message, $lastPercent, $accumulated);
|
||||
}
|
||||
} finally {
|
||||
$response->close();
|
||||
}
|
||||
@@ -161,61 +162,55 @@ class TranscriptionService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure(string, int, ?string): void $report
|
||||
* @return array{0: string, 1: int, 2: int, 3: bool}
|
||||
* @param Closure(string, int, ?string, ?array): void $report
|
||||
* @return array{0: string, 1: int}
|
||||
*/
|
||||
private function ingestEvent(
|
||||
string $payload,
|
||||
string $accumulated,
|
||||
int $lastPercent,
|
||||
int $eventsWithoutTimestamp,
|
||||
?int $durationSeconds,
|
||||
Recording $recording,
|
||||
Closure $report,
|
||||
string $message,
|
||||
float &$lastFlushAt,
|
||||
bool $pending,
|
||||
): array {
|
||||
$event = $this->stream->parseEvent($payload);
|
||||
|
||||
if ($event === null) {
|
||||
return [$accumulated, $lastPercent, $eventsWithoutTimestamp, $pending];
|
||||
return [$accumulated, $lastPercent];
|
||||
}
|
||||
|
||||
$wasEmpty = $accumulated === '';
|
||||
$accumulated = $this->stream->applyEvent($accumulated, $event);
|
||||
$whisper = $event['whisper'] ?? [];
|
||||
|
||||
if ($accumulated === '') {
|
||||
return [$accumulated, $lastPercent, $eventsWithoutTimestamp, $pending];
|
||||
if ($accumulated === '' && $whisper === []) {
|
||||
return [$accumulated, $lastPercent];
|
||||
}
|
||||
|
||||
$percent = $this->percentForEvent($event, $durationSeconds, $lastPercent, $eventsWithoutTimestamp);
|
||||
$lastPercent = max($lastPercent, $percent);
|
||||
$lastPercent = $this->percentForEvent($event, $recording, $lastPercent);
|
||||
|
||||
$now = microtime(true);
|
||||
$shouldFlush = $wasEmpty || $event['done'] || ($now - $lastFlushAt) >= 1.0;
|
||||
$report(
|
||||
$message,
|
||||
$lastPercent,
|
||||
$accumulated === '' ? null : $accumulated,
|
||||
$whisper === [] ? null : $whisper,
|
||||
);
|
||||
|
||||
if ($shouldFlush) {
|
||||
$report($message, $lastPercent, $accumulated);
|
||||
$lastFlushAt = $now;
|
||||
|
||||
return [$accumulated, $lastPercent, $eventsWithoutTimestamp, false];
|
||||
}
|
||||
|
||||
return [$accumulated, $lastPercent, $eventsWithoutTimestamp, true];
|
||||
return [$accumulated, $lastPercent];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{append: ?string, replace: ?string, end: ?float, done: bool, legacy: bool} $event
|
||||
* @param array{append: ?string, replace: ?string, end: ?float, done: bool, legacy: bool, whisper?: array<string, mixed>} $event
|
||||
*/
|
||||
private function percentForEvent(array $event, ?int $durationSeconds, int $lastPercent, int &$eventsWithoutTimestamp): int
|
||||
private function percentForEvent(array $event, Recording $recording, int $lastPercent): int
|
||||
{
|
||||
if ($event['end'] !== null && $durationSeconds !== null && $durationSeconds > 0) {
|
||||
return (int) min(99, max(50, round(100 * $event['end'] / $durationSeconds)));
|
||||
$duration = $recording->duration_seconds;
|
||||
$end = $event['end'] ?? null;
|
||||
|
||||
if ($end === null || $duration === null || $duration <= 0) {
|
||||
return $lastPercent;
|
||||
}
|
||||
|
||||
$eventsWithoutTimestamp++;
|
||||
|
||||
return min(84, max($lastPercent, 50 + $eventsWithoutTimestamp));
|
||||
return (int) min(99, max($lastPercent, round(100 * $end / $duration)));
|
||||
}
|
||||
|
||||
private function localWhisperRequest(string $filename, string $path): PendingRequest
|
||||
@@ -231,14 +226,29 @@ class TranscriptionService
|
||||
->attach('file', fopen($path, 'r'), $filename);
|
||||
}
|
||||
|
||||
private function extractTranscriptText(Response $response): string
|
||||
/**
|
||||
* @param Closure(string, int, ?string, ?array): void $report
|
||||
*/
|
||||
private function extractTranscriptText(Response $response, ?Closure $report = null, string $message = ''): string
|
||||
{
|
||||
$text = $response->json('text');
|
||||
$json = $response->json();
|
||||
|
||||
if (! is_array($json)) {
|
||||
throw new RuntimeException('Local transcription returned an empty transcript.');
|
||||
}
|
||||
|
||||
$text = $json['text'] ?? null;
|
||||
|
||||
if (! is_string($text) || $text === '') {
|
||||
throw new RuntimeException('Local transcription returned an empty transcript.');
|
||||
}
|
||||
|
||||
$whisper = $this->stream->extractWhisperMeta($json);
|
||||
|
||||
if ($report !== null && $whisper !== []) {
|
||||
$report($message, 99, $text, $whisper);
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user