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:
@@ -14,7 +14,7 @@ class TranscribeController extends Controller
|
||||
*/
|
||||
public function __invoke(TranscribeRecordingRequest $request, Recording $recording): RedirectResponse
|
||||
{
|
||||
if (in_array($recording->transcription_status, ['processing'], true)) {
|
||||
if ($recording->transcription_status === 'processing') {
|
||||
return back()->with('error', 'Transcription is already in progress.');
|
||||
}
|
||||
|
||||
@@ -24,12 +24,16 @@ class TranscribeController extends Controller
|
||||
'transcription_driver' => $driver,
|
||||
'ollama_url' => $driver === 'ollama' ? rtrim($request->validated('ollama_url'), '/') : null,
|
||||
'transcription_status' => 'pending',
|
||||
'transcription_progress' => 'Queued — waiting to start…',
|
||||
'transcription_percent' => 5,
|
||||
'transcription_started_at' => now(),
|
||||
'transcription_error' => null,
|
||||
'transcript' => null,
|
||||
'transcribed_at' => null,
|
||||
]);
|
||||
|
||||
TranscribeRecording::dispatch($recording->fresh());
|
||||
|
||||
return back()->with('success', 'Transcription started. Refresh in a moment to see the result.');
|
||||
return back()->with('success', 'Transcription started. Progress updates below.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Recording;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class TranscriptionStatusController extends Controller
|
||||
{
|
||||
/**
|
||||
* Live transcription progress for polling.
|
||||
*/
|
||||
public function __invoke(Recording $recording): JsonResponse
|
||||
{
|
||||
return response()->json($recording->fresh()->transcriptionStatusPayload());
|
||||
}
|
||||
}
|
||||
@@ -31,18 +31,32 @@ class TranscribeRecording implements ShouldQueue
|
||||
*/
|
||||
public function handle(TranscriptionService $transcription): void
|
||||
{
|
||||
$this->recording->update([
|
||||
$this->recording->refresh();
|
||||
|
||||
$this->recording->forceFill([
|
||||
'transcription_status' => 'processing',
|
||||
]);
|
||||
'transcription_started_at' => $this->recording->transcription_started_at ?? now(),
|
||||
'transcription_error' => null,
|
||||
])->save();
|
||||
|
||||
$this->recording->reportProgress('Preparing audio file…', 15);
|
||||
|
||||
try {
|
||||
$text = $transcription->transcribe($this->recording);
|
||||
$text = $transcription->transcribe(
|
||||
$this->recording,
|
||||
fn (string $message, int $percent) => $this->recording->reportProgress($message, $percent),
|
||||
);
|
||||
|
||||
$this->recording->update([
|
||||
$this->recording->reportProgress('Saving transcript…', 90);
|
||||
|
||||
$this->recording->forceFill([
|
||||
'transcript' => $text,
|
||||
'transcription_status' => 'done',
|
||||
'transcription_progress' => 'Transcription complete',
|
||||
'transcription_percent' => 100,
|
||||
'transcription_error' => null,
|
||||
'transcribed_at' => now(),
|
||||
]);
|
||||
])->save();
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Transcription failed', [
|
||||
'recording_id' => $this->recording->id,
|
||||
@@ -50,11 +64,26 @@ class TranscribeRecording implements ShouldQueue
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
$this->recording->update([
|
||||
$this->recording->forceFill([
|
||||
'transcription_status' => 'failed',
|
||||
]);
|
||||
'transcription_progress' => 'Transcription failed',
|
||||
'transcription_percent' => $this->recording->transcription_percent ?: 0,
|
||||
'transcription_error' => $e->getMessage(),
|
||||
])->save();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a job failure (timeouts, worker kill, etc.).
|
||||
*/
|
||||
public function failed(?Throwable $e): void
|
||||
{
|
||||
$this->recording->forceFill([
|
||||
'transcription_status' => 'failed',
|
||||
'transcription_progress' => 'Transcription failed',
|
||||
'transcription_error' => $e?->getMessage() ?: 'Transcription stopped unexpectedly.',
|
||||
])->save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,10 @@ class Recording extends Model
|
||||
'file_size_bytes',
|
||||
'transcript',
|
||||
'transcription_status',
|
||||
'transcription_progress',
|
||||
'transcription_percent',
|
||||
'transcription_started_at',
|
||||
'transcription_error',
|
||||
'transcription_driver',
|
||||
'ollama_url',
|
||||
'transcribed_at',
|
||||
@@ -35,8 +39,10 @@ class Recording extends Model
|
||||
return [
|
||||
'recorded_at' => 'datetime',
|
||||
'transcribed_at' => 'datetime',
|
||||
'transcription_started_at' => 'datetime',
|
||||
'duration_seconds' => 'integer',
|
||||
'file_size_bytes' => 'integer',
|
||||
'transcription_percent' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -57,6 +63,42 @@ class Recording extends Model
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Friendly label for the selected transcription engine.
|
||||
*/
|
||||
protected function transcriptionDriverLabel(): Attribute
|
||||
{
|
||||
return Attribute::get(function (): ?string {
|
||||
return match ($this->transcription_driver) {
|
||||
'cloud' => 'Cloud (OpenAI Whisper)',
|
||||
'local' => 'Local (faster-whisper)',
|
||||
'ollama' => 'Ollama host',
|
||||
default => $this->transcription_driver,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether transcription is actively running or queued.
|
||||
*/
|
||||
public function isTranscribing(): bool
|
||||
{
|
||||
return in_array($this->transcription_status, ['pending', 'processing'], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the live progress fields shown in the UI.
|
||||
*/
|
||||
public function reportProgress(string $message, int $percent, string $status = 'processing'): void
|
||||
{
|
||||
$this->forceFill([
|
||||
'transcription_status' => $status,
|
||||
'transcription_progress' => $message,
|
||||
'transcription_percent' => max(0, min(100, $percent)),
|
||||
'transcription_error' => null,
|
||||
])->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Absolute filesystem path for the stored audio file.
|
||||
*/
|
||||
@@ -74,4 +116,44 @@ class Recording extends Model
|
||||
Storage::disk('local')->delete($this->file_path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Payload for the live status endpoint / Alpine poller.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function transcriptionStatusPayload(): array
|
||||
{
|
||||
$startedAt = $this->transcription_started_at;
|
||||
$elapsed = $startedAt ? $startedAt->diffInSeconds(now()) : null;
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'status' => $this->transcription_status,
|
||||
'progress' => $this->transcription_progress,
|
||||
'percent' => $this->transcription_percent,
|
||||
'driver' => $this->transcription_driver,
|
||||
'driver_label' => $this->transcription_driver_label,
|
||||
'error' => $this->transcription_error,
|
||||
'started_at' => $startedAt?->toIso8601String(),
|
||||
'elapsed_seconds' => $elapsed,
|
||||
'elapsed_human' => $elapsed === null ? null : $this->formatElapsed($elapsed),
|
||||
'duration_seconds' => $this->duration_seconds,
|
||||
'is_active' => $this->isTranscribing(),
|
||||
'has_transcript' => filled($this->transcript),
|
||||
'transcribed_at' => $this->transcribed_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
|
||||
private function formatElapsed(int $seconds): string
|
||||
{
|
||||
$minutes = intdiv($seconds, 60);
|
||||
$remain = $seconds % 60;
|
||||
|
||||
if ($minutes === 0) {
|
||||
return sprintf('%ds', $remain);
|
||||
}
|
||||
|
||||
return sprintf('%dm %02ds', $minutes, $remain);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user