Add live transcription progress and Docker Whisper.

Surface stage, percent, and elapsed time while jobs run, and ship Compose so local confidential transcription can use faster-whisper on port 8090.
This commit is contained in:
ben
2026-08-12 14:32:56 +02:00
parent 67c1941833
commit e0400aadef
14 changed files with 695 additions and 177 deletions
+35 -7
View File
@@ -3,6 +3,7 @@
namespace App\Services;
use App\Models\Recording;
use Closure;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Laravel\Ai\Transcription;
@@ -12,41 +13,63 @@ class TranscriptionService
{
/**
* Run transcription for a recording using the selected driver.
*
* @param (Closure(string, int): void)|null $onProgress
*/
public function transcribe(Recording $recording): string
public function transcribe(Recording $recording, ?Closure $onProgress = null): string
{
$report = $onProgress ?? static fn (string $message, int $percent) => null;
return match ($recording->transcription_driver) {
'cloud' => $this->viaCloud($recording),
'local' => $this->viaLocal($recording),
'ollama' => $this->viaRemoteCompatible($recording),
'cloud' => $this->viaCloud($recording, $report),
'local' => $this->viaLocal($recording, $report),
'ollama' => $this->viaRemoteCompatible($recording, $report),
default => throw new RuntimeException('Unknown transcription driver: '.$recording->transcription_driver),
};
}
private function viaCloud(Recording $recording): string
/**
* @param Closure(string, int): void $report
*/
private function viaCloud(Recording $recording, Closure $report): string
{
$report('Sending audio to OpenAI Whisper…', 35);
$report('Waiting for cloud transcript (this can take a while for long recordings)…', 55);
$transcript = Transcription::fromStorage($recording->file_path)
->timeout((int) config('ai.transcription_timeout', 600))
->generate('openai', 'whisper-1');
$report('Received transcript from OpenAI…', 85);
return (string) $transcript;
}
private function viaLocal(Recording $recording): string
/**
* @param Closure(string, int): void $report
*/
private function viaLocal(Recording $recording, Closure $report): string
{
$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);
$transcript = Transcription::fromStorage($recording->file_path)
->timeout((int) config('ai.transcription_timeout', 600))
->generate('local-whisper', $model);
$report('Received transcript from local Whisper…', 85);
return (string) $transcript;
}
/**
* Call an OpenAI-compatible /v1/audio/transcriptions endpoint at a user-supplied host URL.
*
* @param Closure(string, int): void $report
*/
private function viaRemoteCompatible(Recording $recording): string
private function viaRemoteCompatible(Recording $recording, Closure $report): string
{
if (! filled($recording->ollama_url)) {
throw new RuntimeException('Ollama host URL is required for remote transcription.');
@@ -60,6 +83,9 @@ class TranscriptionService
throw new RuntimeException('Recording audio file is not readable.');
}
$report('Connecting to remote host '.$recording->ollama_url.'…', 30);
$report("Uploading audio and waiting for transcript ({$model})…", 50);
$response = Http::timeout((int) config('ai.transcription_timeout', 600))
->attach(
'file',
@@ -83,6 +109,8 @@ class TranscriptionService
throw new RuntimeException('Remote transcription returned an empty transcript. Ensure the host exposes OpenAI-compatible /v1/audio/transcriptions.');
}
$report('Received transcript from remote host…', 85);
return $text;
}