first
This commit is contained in:
76
app/Jobs/OllamaPostProcessJob.php
Normal file
76
app/Jobs/OllamaPostProcessJob.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\TranscriptVersion;
|
||||
use App\Models\TranscriptionProject;
|
||||
use App\Services\OllamaService;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class OllamaPostProcessJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $timeout = 180;
|
||||
public int $tries = 2;
|
||||
public int $backoff = 15;
|
||||
|
||||
public function __construct(private readonly TranscriptionProject $project) {}
|
||||
|
||||
public function handle(OllamaService $ollama): void
|
||||
{
|
||||
$project = $this->project;
|
||||
|
||||
// Reload fresh — status may have changed between dispatch and execution
|
||||
$project->refresh();
|
||||
|
||||
if (! $project->isCompleted() || ! $project->transcript_text) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $ollama->isAvailable()) {
|
||||
Log::info("OllamaPostProcessJob: Ollama tidak tersedia, skip projek #{$project->id}");
|
||||
return;
|
||||
}
|
||||
|
||||
$cleaned = $ollama->cleanTranscript($project->transcript_text, $project->language);
|
||||
|
||||
if ($cleaned === null) {
|
||||
Log::warning("OllamaPostProcessJob: hasil null untuk projek #{$project->id}");
|
||||
return;
|
||||
}
|
||||
|
||||
$oldText = $project->transcript_text;
|
||||
$nextVersion = ($project->transcriptVersions()->max('version_number') ?? 0) + 1;
|
||||
|
||||
TranscriptVersion::create([
|
||||
'project_id' => $project->id,
|
||||
'edited_by' => $project->owner_user_id,
|
||||
'version_number' => $nextVersion,
|
||||
'old_text' => $oldText,
|
||||
'new_text' => $cleaned,
|
||||
'change_summary' => 'Dibersihkan oleh Ollama',
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$project->update(['transcript_text' => $cleaned]);
|
||||
|
||||
AuditLog::create([
|
||||
'actor_user_id' => null,
|
||||
'actor_role' => 'system',
|
||||
'action' => 'transcript_postprocessed',
|
||||
'project_id' => $project->id,
|
||||
'new_values' => [
|
||||
'project_uuid' => $project->uuid,
|
||||
'model' => config('speech2text.ollama.model'),
|
||||
],
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
120
app/Jobs/TranscribeAudioJob.php
Normal file
120
app/Jobs/TranscribeAudioJob.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\TranscriptionProject;
|
||||
use App\Services\StorageService;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TranscribeAudioJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $timeout = 600;
|
||||
public int $tries = 3;
|
||||
public int $backoff = 30;
|
||||
|
||||
public function __construct(private readonly TranscriptionProject $project) {}
|
||||
|
||||
public function handle(StorageService $storage): void
|
||||
{
|
||||
$project = $this->project;
|
||||
|
||||
$project->update(['transcription_status' => 'processing']);
|
||||
$this->auditLog('transcription_started');
|
||||
|
||||
$tmpPath = null;
|
||||
|
||||
try {
|
||||
$path = $project->stored_audio_path;
|
||||
|
||||
abort_unless($storage->exists($path), 500, 'Fail audio tidak dijumpai.');
|
||||
|
||||
// Stream storage → temp file to avoid loading large files into memory
|
||||
$tmpPath = tempnam(sys_get_temp_dir(), 'whisper_');
|
||||
$src = $storage->readStream($path);
|
||||
$dst = fopen($tmpPath, 'wb');
|
||||
|
||||
stream_copy_to_stream($src, $dst);
|
||||
|
||||
fclose($src);
|
||||
fclose($dst);
|
||||
|
||||
$workerUrl = rtrim(config('speech2text.transcription.worker_url'), '/');
|
||||
|
||||
$response = Http::timeout(540)
|
||||
->attach(
|
||||
'audio',
|
||||
fopen($tmpPath, 'rb'),
|
||||
basename($path),
|
||||
['Content-Type' => $project->mime_type]
|
||||
)
|
||||
->post("{$workerUrl}/transcribe");
|
||||
|
||||
if (! $response->successful()) {
|
||||
throw new \RuntimeException("Transcription worker mengembalikan HTTP {$response->status()}");
|
||||
}
|
||||
|
||||
$result = $response->json();
|
||||
|
||||
if (! ($result['success'] ?? false)) {
|
||||
throw new \RuntimeException($result['error'] ?? 'Transcription gagal tanpa mesej ralat.');
|
||||
}
|
||||
|
||||
$project->update([
|
||||
'transcription_status' => 'completed',
|
||||
'transcript_text' => $result['transcript'] ?? '',
|
||||
'transcript_confidence' => $result['confidence'] ?? null,
|
||||
'duration_seconds' => $result['duration_seconds'] ?? null,
|
||||
'error_message' => null,
|
||||
'processed_at' => now(),
|
||||
]);
|
||||
|
||||
$this->auditLog('transcription_completed', [
|
||||
'duration_seconds' => $result['duration_seconds'] ?? null,
|
||||
'engine' => config('speech2text.transcription.engine'),
|
||||
]);
|
||||
|
||||
// Optional Ollama post-processing
|
||||
if (config('speech2text.ollama.enabled') && $project->transcript_text) {
|
||||
OllamaPostProcessJob::dispatch($project);
|
||||
}
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
Log::error("TranscribeAudioJob gagal untuk project #{$project->id}: {$e->getMessage()}");
|
||||
|
||||
$project->update([
|
||||
'transcription_status' => 'failed',
|
||||
'error_message' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
$this->auditLog('transcription_failed', ['error' => $e->getMessage()]);
|
||||
|
||||
$this->fail($e);
|
||||
|
||||
} finally {
|
||||
if ($tmpPath && file_exists($tmpPath)) {
|
||||
@unlink($tmpPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function auditLog(string $action, array $extra = []): void
|
||||
{
|
||||
AuditLog::create([
|
||||
'actor_user_id' => null,
|
||||
'actor_role' => 'system',
|
||||
'action' => $action,
|
||||
'project_id' => $this->project->id,
|
||||
'new_values' => array_merge(['project_uuid' => $this->project->uuid], $extra),
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user