AliJabbarpour commited on
Commit
2032ce4
·
verified ·
1 Parent(s): 6f1dfe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -1
app.py CHANGED
@@ -3,6 +3,7 @@ import tempfile
3
  from fastapi import FastAPI, WebSocket, WebSocketDisconnect
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from faster_whisper import WhisperModel
 
6
  from pipeline.postprocess import process
7
 
8
  app = FastAPI()
@@ -19,6 +20,10 @@ print("Loading Whisper model...", flush=True)
19
  model = WhisperModel("large-v3", device="cpu", compute_type="int8")
20
  print("Whisper model ready!")
21
 
 
 
 
 
22
  @app.get("/health")
23
  async def health():
24
  return {"status": "ok", "message": "SubFlow backend is running"}
@@ -27,6 +32,7 @@ async def health():
27
  async def transcribe(websocket: WebSocket):
28
  await websocket.accept()
29
  tmp_path = None
 
30
 
31
  try:
32
  print("Step 1: waiting for bytes")
@@ -39,11 +45,20 @@ async def transcribe(websocket: WebSocket):
39
  tmp_path = tmp.name
40
  print(f"Step 3: saved to {tmp_path}")
41
 
 
 
 
 
 
 
 
 
 
42
  await websocket.send_json({"status": "transcribing"})
43
  print("Step 4: starting transcription")
44
 
45
  segments_raw, info = model.transcribe(
46
- tmp_path,
47
  language="en",
48
  word_timestamps=True,
49
  vad_filter=True,
@@ -97,6 +112,8 @@ async def transcribe(websocket: WebSocket):
97
  finally:
98
  if tmp_path and os.path.exists(tmp_path):
99
  os.remove(tmp_path)
 
 
100
  try:
101
  await websocket.close()
102
  except Exception:
 
3
  from fastapi import FastAPI, WebSocket, WebSocketDisconnect
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from faster_whisper import WhisperModel
6
+ from df.enhance import enhance, init_df, load_audio, save_audio
7
  from pipeline.postprocess import process
8
 
9
  app = FastAPI()
 
20
  model = WhisperModel("large-v3", device="cpu", compute_type="int8")
21
  print("Whisper model ready!")
22
 
23
+ print("Loading DeepFilterNet model...", flush=True)
24
+ df_model, df_state, _ = init_df()
25
+ print("DeepFilterNet model ready!")
26
+
27
  @app.get("/health")
28
  async def health():
29
  return {"status": "ok", "message": "SubFlow backend is running"}
 
32
  async def transcribe(websocket: WebSocket):
33
  await websocket.accept()
34
  tmp_path = None
35
+ enhanced_path = None
36
 
37
  try:
38
  print("Step 1: waiting for bytes")
 
45
  tmp_path = tmp.name
46
  print(f"Step 3: saved to {tmp_path}")
47
 
48
+ await websocket.send_json({"status": "cleaning"})
49
+ print("Step 3.5: cleaning audio with DeepFilterNet")
50
+
51
+ audio_data, _ = load_audio(tmp_path, sr=df_state.sr())
52
+ enhanced_audio = enhance(df_model, df_state, audio_data)
53
+ enhanced_path = tmp_path.replace(".wav", "_enhanced.wav")
54
+ save_audio(enhanced_path, enhanced_audio, df_state.sr())
55
+ print(f"Step 3.6: cleaned audio saved to {enhanced_path}")
56
+
57
  await websocket.send_json({"status": "transcribing"})
58
  print("Step 4: starting transcription")
59
 
60
  segments_raw, info = model.transcribe(
61
+ enhanced_path,
62
  language="en",
63
  word_timestamps=True,
64
  vad_filter=True,
 
112
  finally:
113
  if tmp_path and os.path.exists(tmp_path):
114
  os.remove(tmp_path)
115
+ if enhanced_path and os.path.exists(enhanced_path):
116
+ os.remove(enhanced_path)
117
  try:
118
  await websocket.close()
119
  except Exception: