Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

feat: add async job polling for transcription to prevent timeouts

Augustus Otu44f4d94b8191
diff --git a/infra/transcribe-gpu/main.py b/infra/transcribe-gpu/main.py
index 095ba61e..8e38f1ed 100644
--- a/infra/transcribe-gpu/main.py
+++ b/infra/transcribe-gpu/main.py
@@ -1,6 +1,10 @@
import os
import tempfile
+import asyncio
+import time
+import uuid
from typing import Optional
+from dataclasses import dataclass, field
from fastapi import FastAPI, File, Form, HTTPException, Request, UploadFile
from fastapi.middleware.cors import CORSMiddleware
@@ -35,6 +39,30 @@ MODEL_SIZE = os.environ.get("WHISPER_MODEL", "large-v3-turbo")
DEVICE = os.environ.get("WHISPER_DEVICE", "cuda")
COMPUTE_TYPE = os.environ.get("WHISPER_COMPUTE_TYPE", "float16")
+JOB_TTL_SECONDS = 600
+
+
+@dataclass
+class TranscriptionJob:
+ id: str
+ status: str = "processing"
+ progress: float = 0
+ result: Optional[dict] = None
+ error: Optional[str] = None
+ created_at: float = field(default_factory=time.time)
+
+
+jobs: dict[str, TranscriptionJob] = {}
+
+
+def cleanup_expired_jobs():
+ now = time.time()
+ expired = [
+ jid for jid, job in jobs.items() if now - job.created_at > JOB_TTL_SECONDS
+ ]
+ for jid in expired:
+ del jobs[jid]
+
def get_model() -> WhisperModel:
global whisper_model
@@ -52,23 +80,16 @@ async def startup():
get_model()
[email protected]("/transcribe")
-async def transcribe(
- request: Request,
- audio: UploadFile = File(...),
- language: Optional[str] = Form(None),
- target_language: Optional[str] = Form(None),
+async def process_transcription(
+ job_id: str,
+ tmp_path: str,
+ language: Optional[str],
+ target_language: Optional[str],
):
- if not audio.filename:
- raise HTTPException(status_code=400, detail="No audio file provided")
-
- suffix = os.path.splitext(audio.filename)[1] or ".wav"
- with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
- file_content = await audio.read()
- tmp.write(file_content)
- tmp_path = tmp.name
-
+ job = jobs[job_id]
try:
+ job.progress = 10
+
model = get_model()
transcribe_kwargs = {
@@ -86,11 +107,15 @@ async def transcribe(
if use_whisper_translate:
transcribe_kwargs["task"] = "translate"
+ job.progress = 20
+
segments, info = model.transcribe(tmp_path, **transcribe_kwargs)
words = []
full_text = []
+ job.progress = 50
+
for segment in segments:
full_text.append(segment.text.strip())
if segment.words:
@@ -103,6 +128,8 @@ async def transcribe(
}
)
+ job.progress = 80
+
text = " ".join(full_text)
detected_language = info.language
@@ -126,7 +153,9 @@ async def transcribe(
except Exception as e:
print(f"Translation failed: {e}")
- return {
+ job.progress = 100
+ job.status = "completed"
+ job.result = {
"text": text,
"word_count": len(words),
"words": words,
@@ -134,8 +163,65 @@ async def transcribe(
"target_language": target_language,
"duration": info.duration,
}
+ except Exception as e:
+ job.status = "failed"
+ job.error = str(e)
finally:
- os.unlink(tmp_path)
+ try:
+ os.unlink(tmp_path)
+ except OSError:
+ pass
+
+
[email protected]("/transcribe")
+async def transcribe(
+ request: Request,
+ audio: UploadFile = File(...),
+ language: Optional[str] = Form(None),
+ target_language: Optional[str] = Form(None),
+):
+ if not audio.filename:
+ raise HTTPException(status_code=400, detail="No audio file provided")
+
+ cleanup_expired_jobs()
+
+ suffix = os.path.splitext(audio.filename)[1] or ".wav"
+ with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
+ file_content = await audio.read()
+ tmp.write(file_content)
+ tmp_path = tmp.name
+
+ job_id = str(uuid.uuid4())
+ jobs[job_id] = TranscriptionJob(id=job_id)
+
+ asyncio.get_event_loop().run_in_executor(
+ None,
+ lambda: asyncio.run(
+ process_transcription(job_id, tmp_path, language, target_language)
+ ),
+ )
+
+ return {"jobId": job_id, "status": "processing"}
+
+
[email protected]("/jobs/{job_id}")
+async def get_job(job_id: str):
+ job = jobs.get(job_id)
+ if not job:
+ raise HTTPException(status_code=404, detail="Job not found")
+
+ response = {
+ "jobId": job.id,
+ "status": job.status,
+ "progress": job.progress,
+ }
+
+ if job.status == "completed":
+ response["result"] = job.result
+ elif job.status == "failed":
+ response["error"] = job.error
+
+ return response
@app.post("/")
@@ -168,6 +254,7 @@ async def health():
"gpu": gpu_name,
"gpu_available": gpu_available,
"ready": whisper_model is not None,
+ "active_jobs": len(jobs),
}
diff --git a/packages/core/src/text/transcription-service.ts b/packages/core/src/text/transcription-service.ts
index 88d242e0..9ee74302 100644
--- a/packages/core/src/text/transcription-service.ts
+++ b/packages/core/src/text/transcription-service.ts
@@ -220,10 +220,8 @@ export class TranscriptionService {
onProgress?.({
phase: "transcribing",
- progress: 50,
- message: this.config.targetLanguage
- ? `Transcribing and translating to ${this.config.targetLanguage}...`
- : "Transcribing audio...",
+ progress: 30,
+ message: "Uploading audio...",
});
const response = await fetch(this.config.apiEndpoint, {
@@ -243,7 +241,60 @@ export class TranscriptionService {
);
}
- return response.json();
+ const submitResult = await response.json();
+
+ if (!submitResult.jobId) {
+ return submitResult as CloudflareWhisperResponse;
+ }
+
+ const baseUrl = this.config.apiEndpoint.replace(/\/transcribe$/, "").replace(/\/$/, "");
+ const pollUrl = `${baseUrl}/jobs/${submitResult.jobId}`;
+
+ return this.pollForResult(pollUrl, onProgress);
+ }
+
+ private async pollForResult(
+ pollUrl: string,
+ onProgress?: (progress: WhisperTranscriptionProgress) => void,
+ ): Promise<CloudflareWhisperResponse> {
+ const maxAttempts = 120;
+ const pollInterval = 3000;
+
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
+ await new Promise((resolve) => setTimeout(resolve, pollInterval));
+
+ const response = await fetch(pollUrl);
+ if (!response.ok) {
+ if (response.status === 404) {
+ throw new Error("Transcription job not found");
+ }
+ continue;
+ }
+
+ const job = await response.json();
+
+ if (job.status === "processing") {
+ const progress = 30 + Math.round((job.progress || 0) * 0.6);
+ onProgress?.({
+ phase: "transcribing",
+ progress,
+ message: this.config.targetLanguage
+ ? `Transcribing and translating to ${this.config.targetLanguage}...`
+ : "Transcribing audio...",
+ });
+ continue;
+ }
+
+ if (job.status === "completed" && job.result) {
+ return job.result as CloudflareWhisperResponse;
+ }
+
+ if (job.status === "failed") {
+ throw new Error(job.error || "Transcription failed on server");
+ }
+ }
+
+ throw new Error("Transcription timed out after 6 minutes");
}
private convertToSubtitles(