AliJabbarpour commited on
Commit
3609d3d
·
verified ·
1 Parent(s): 5189c5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -2
app.py CHANGED
@@ -1,5 +1,9 @@
1
- from fastapi import FastAPI
 
 
 
2
  from fastapi.middleware.cors import CORSMiddleware
 
3
 
4
  app = FastAPI()
5
 
@@ -10,6 +14,79 @@ app.add_middleware(
10
  allow_headers=["*"],
11
  )
12
 
 
 
 
 
 
 
13
  @app.get("/health")
14
  async def health():
15
- return {"status": "ok", "message": "SubFlow backend is running"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
+ import asyncio
4
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
5
  from fastapi.middleware.cors import CORSMiddleware
6
+ from faster_whisper import WhisperModel
7
 
8
  app = FastAPI()
9
 
 
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, info = model.transcribe(
47
+ tmp_path,
48
+ language="en",
49
+ word_timestamps=True,
50
+ vad_filter=True,
51
+ )
52
+
53
+ # مرحله ۴ — ساخت SRT
54
+ await websocket.send_json({"status": "processing"})
55
+
56
+ srt_entries = []
57
+ index = 1
58
+
59
+ for segment in segments:
60
+ words = list(segment.words)
61
+ if not words:
62
+ continue
63
+
64
+ start = words[0].start
65
+ end = words[-1].end
66
+ text = segment.text.strip()
67
+
68
+ srt_entries.append({
69
+ "index": index,
70
+ "start": start,
71
+ "end": end,
72
+ "text": text,
73
+ "words": [{"word": w.word, "start": w.start, "end": w.end} for w in words]
74
+ })
75
+ index += 1
76
+
77
+ # مرحله ۵ — ارسال نتیجه
78
+ await websocket.send_json({
79
+ "status": "done",
80
+ "segments": srt_entries,
81
+ "language": info.language,
82
+ "duration": info.duration,
83
+ })
84
+
85
+ except WebSocketDisconnect:
86
+ pass
87
+ except Exception as e:
88
+ await websocket.send_json({"status": "error", "message": str(e)})
89
+ finally:
90
+ if tmp_path and os.path.exists(tmp_path):
91
+ os.remove(tmp_path)
92
+ await websocket.close()