AliJabbarpour commited on
Commit
26b0d5c
·
verified ·
1 Parent(s): c9bdefb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -32
app.py CHANGED
@@ -25,34 +25,6 @@ print("Loading DeepFilterNet model...", flush=True)
25
  df_model, df_state, _ = init_df()
26
  print("DeepFilterNet model ready!")
27
 
28
-
29
- def compress_dynamic_range(audio, sr, threshold_db=-28, ratio=4.0, makeup_gain_db=8, block_ms=20):
30
- """
31
- صدای آروم رو نسبت به صدای بلند بلندتر می‌کنه (یکسان‌سازی حجم صدا)،
32
- تا صدای یه نفر که از میکروفون دوره به صدای کسی که میکروفون دستشه نزدیک‌تر بشه.
33
- """
34
- audio = audio.astype(np.float32)
35
- block_size = max(1, int(sr * block_ms / 1000))
36
- n_blocks = int(np.ceil(len(audio) / block_size))
37
- pad_len = n_blocks * block_size - len(audio)
38
- padded = np.pad(audio, (0, pad_len))
39
- blocks = padded.reshape(n_blocks, block_size)
40
-
41
- rms = np.sqrt(np.mean(blocks ** 2, axis=1) + 1e-10)
42
- rms_db = 20 * np.log10(rms + 1e-10)
43
-
44
- target_db = np.where(rms_db > threshold_db, threshold_db + (rms_db - threshold_db) / ratio, rms_db)
45
- gain_db = target_db - rms_db + makeup_gain_db
46
- gain = 10 ** (gain_db / 20)
47
-
48
- kernel = np.ones(5, dtype=np.float32) / 5
49
- gain_smooth = np.convolve(gain, kernel, mode="same")
50
-
51
- gain_per_sample = np.repeat(gain_smooth, block_size)[:len(audio)].astype(np.float32)
52
- compressed = audio * gain_per_sample
53
- return np.clip(compressed, -1.0, 1.0).astype(np.float32)
54
-
55
-
56
  @app.get("/health")
57
  async def health():
58
  return {"status": "ok", "message": "SubFlow backend is running"}
@@ -76,12 +48,9 @@ async def transcribe(websocket: WebSocket):
76
  print(f"Step 3: saved to {tmp_path}")
77
 
78
  await websocket.send_json({"status": "cleaning"})
79
- print("Step 3.5: leveling volume + cleaning audio with DeepFilterNet")
80
 
81
  audio_data, _ = load_audio(tmp_path, sr=df_state.sr())
82
- audio_np = compress_dynamic_range(audio_data.numpy()[0], df_state.sr())
83
- audio_data = torch.from_numpy(audio_np).unsqueeze(0)
84
-
85
  enhanced_audio = enhance(df_model, df_state, audio_data, atten_lim_db=20)
86
  enhanced_path = tmp_path.replace(".wav", "_enhanced.wav")
87
  save_audio(enhanced_path, enhanced_audio, df_state.sr())
@@ -98,6 +67,7 @@ async def transcribe(websocket: WebSocket):
98
  vad_parameters=dict(
99
  min_silence_duration_ms=500,
100
  speech_pad_ms=200,
 
101
  ),
102
  )
103
 
 
25
  df_model, df_state, _ = init_df()
26
  print("DeepFilterNet model ready!")
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  @app.get("/health")
29
  async def health():
30
  return {"status": "ok", "message": "SubFlow backend is running"}
 
48
  print(f"Step 3: saved to {tmp_path}")
49
 
50
  await websocket.send_json({"status": "cleaning"})
51
+ print("Step 3.5: cleaning audio with DeepFilterNet")
52
 
53
  audio_data, _ = load_audio(tmp_path, sr=df_state.sr())
 
 
 
54
  enhanced_audio = enhance(df_model, df_state, audio_data, atten_lim_db=20)
55
  enhanced_path = tmp_path.replace(".wav", "_enhanced.wav")
56
  save_audio(enhanced_path, enhanced_audio, df_state.sr())
 
67
  vad_parameters=dict(
68
  min_silence_duration_ms=500,
69
  speech_pad_ms=200,
70
+ threshold=0.3,
71
  ),
72
  )
73