Update app.py
Browse files
app.py
CHANGED
|
@@ -14,34 +14,34 @@ app.add_middleware(
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# ─── Model Load ───────────────────────────────────────────────────────────────
|
| 18 |
print("Loading Whisper model...")
|
| 19 |
model = WhisperModel("large-v3", device="cpu", compute_type="int8")
|
| 20 |
print("Whisper model ready!")
|
| 21 |
|
| 22 |
-
# ─── Health ───────────────────────────────────────────────────────────────────
|
| 23 |
@app.get("/health")
|
| 24 |
async def health():
|
| 25 |
return {"status": "ok", "message": "SubFlow backend is running"}
|
| 26 |
|
| 27 |
-
# ─── WebSocket Transcribe ─────────────────────────────────────────────────────
|
| 28 |
@app.websocket("/transcribe")
|
| 29 |
async def transcribe(websocket: WebSocket):
|
| 30 |
await websocket.accept()
|
| 31 |
tmp_path = None
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
await websocket.send_json({"status": "receiving"})
|
|
|
|
|
|
|
| 36 |
audio_bytes = await websocket.receive_bytes()
|
|
|
|
| 37 |
|
| 38 |
-
# مرحله ۲ — ذخیره موقت روی دیسک
|
| 39 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 40 |
tmp.write(audio_bytes)
|
| 41 |
tmp_path = tmp.name
|
|
|
|
| 42 |
|
| 43 |
-
# مرحله ۳ — transcription
|
| 44 |
await websocket.send_json({"status": "transcribing"})
|
|
|
|
| 45 |
|
| 46 |
segments_raw, info = model.transcribe(
|
| 47 |
tmp_path,
|
|
@@ -49,8 +49,8 @@ async def transcribe(websocket: WebSocket):
|
|
| 49 |
word_timestamps=True,
|
| 50 |
vad_filter=True,
|
| 51 |
)
|
|
|
|
| 52 |
|
| 53 |
-
# مرحله ۴ — تبدیل به لیست قابل پردازش
|
| 54 |
await websocket.send_json({"status": "processing"})
|
| 55 |
|
| 56 |
segments = []
|
|
@@ -67,22 +67,29 @@ async def transcribe(websocket: WebSocket):
|
|
| 67 |
for w in words
|
| 68 |
],
|
| 69 |
})
|
|
|
|
| 70 |
|
| 71 |
-
# مرحله ۵ — Post-Processing (اون ۲۰٪ طلایی)
|
| 72 |
srt_content = process(segments)
|
|
|
|
| 73 |
|
| 74 |
-
# مرحله ۶ — ارسال نتیجه
|
| 75 |
await websocket.send_json({
|
| 76 |
"status": "done",
|
| 77 |
"srt": srt_content,
|
| 78 |
"language": info.language,
|
| 79 |
"duration": info.duration,
|
| 80 |
})
|
|
|
|
| 81 |
|
| 82 |
except WebSocketDisconnect:
|
| 83 |
-
|
| 84 |
except Exception as e:
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
finally:
|
| 87 |
if tmp_path and os.path.exists(tmp_path):
|
| 88 |
os.remove(tmp_path)
|
|
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
|
|
|
| 17 |
print("Loading Whisper model...")
|
| 18 |
model = WhisperModel("large-v3", device="cpu", compute_type="int8")
|
| 19 |
print("Whisper model ready!")
|
| 20 |
|
|
|
|
| 21 |
@app.get("/health")
|
| 22 |
async def health():
|
| 23 |
return {"status": "ok", "message": "SubFlow backend is running"}
|
| 24 |
|
|
|
|
| 25 |
@app.websocket("/transcribe")
|
| 26 |
async def transcribe(websocket: WebSocket):
|
| 27 |
await websocket.accept()
|
| 28 |
tmp_path = None
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
print("Step 1: sending receiving status")
|
| 32 |
await websocket.send_json({"status": "receiving"})
|
| 33 |
+
|
| 34 |
+
print("Step 2: waiting for bytes")
|
| 35 |
audio_bytes = await websocket.receive_bytes()
|
| 36 |
+
print(f"Step 3: received {len(audio_bytes)} bytes")
|
| 37 |
|
|
|
|
| 38 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 39 |
tmp.write(audio_bytes)
|
| 40 |
tmp_path = tmp.name
|
| 41 |
+
print(f"Step 4: saved to {tmp_path}")
|
| 42 |
|
|
|
|
| 43 |
await websocket.send_json({"status": "transcribing"})
|
| 44 |
+
print("Step 5: starting transcription")
|
| 45 |
|
| 46 |
segments_raw, info = model.transcribe(
|
| 47 |
tmp_path,
|
|
|
|
| 49 |
word_timestamps=True,
|
| 50 |
vad_filter=True,
|
| 51 |
)
|
| 52 |
+
print("Step 6: transcription generator ready")
|
| 53 |
|
|
|
|
| 54 |
await websocket.send_json({"status": "processing"})
|
| 55 |
|
| 56 |
segments = []
|
|
|
|
| 67 |
for w in words
|
| 68 |
],
|
| 69 |
})
|
| 70 |
+
print(f"Step 7: got {len(segments)} segments")
|
| 71 |
|
|
|
|
| 72 |
srt_content = process(segments)
|
| 73 |
+
print(f"Step 8: SRT built, length={len(srt_content)}")
|
| 74 |
|
|
|
|
| 75 |
await websocket.send_json({
|
| 76 |
"status": "done",
|
| 77 |
"srt": srt_content,
|
| 78 |
"language": info.language,
|
| 79 |
"duration": info.duration,
|
| 80 |
})
|
| 81 |
+
print("Step 9: done sent!")
|
| 82 |
|
| 83 |
except WebSocketDisconnect:
|
| 84 |
+
print("WebSocket disconnected by client")
|
| 85 |
except Exception as e:
|
| 86 |
+
print(f"ERROR: {type(e).__name__}: {e}")
|
| 87 |
+
import traceback
|
| 88 |
+
traceback.print_exc()
|
| 89 |
+
try:
|
| 90 |
+
await websocket.send_json({"status": "error", "message": str(e)})
|
| 91 |
+
except Exception:
|
| 92 |
+
pass
|
| 93 |
finally:
|
| 94 |
if tmp_path and os.path.exists(tmp_path):
|
| 95 |
os.remove(tmp_path)
|