DeepLearning101 commited on
Commit
333984b
·
verified ·
1 Parent(s): a5fab61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -19,12 +19,19 @@ def transcribe(file_upload, microphone):
19
  model.load_state_dict(state_dict)
20
 
21
  # 載入音訊並強制轉單聲道
22
- x, sr = torchaudio.load(file, channels_first=True) # 確保通道優先格式
 
 
 
 
 
 
 
23
  if x.shape[0] > 1:
24
- x = torch.mean(x, dim=0, keepdim=True) # 平均所有通道轉單聲道
25
 
26
  # 執行降噪
27
- out = model(x[None])[0] # 增加batch維度
28
 
29
  # 後處理
30
  out = out / max(out.abs().max().item(), 1)
 
19
  model.load_state_dict(state_dict)
20
 
21
  # 載入音訊並強制轉單聲道
22
+ x, sr = torchaudio.load(file, channels_first=True) # 載入音訊
23
+
24
+ # 新增:音訊長度檢查(插入在此處)
25
+ MAX_AUDIO_SECONDS = 600 # 10分鐘限制
26
+ if x.shape[1] / sr > MAX_AUDIO_SECONDS:
27
+ raise ValueError(f"音訊長度不可超過 {MAX_AUDIO_SECONDS} 秒,當前音訊長度:{x.shape[1]/sr:.1f} 秒")
28
+
29
+ # 單聲道轉換
30
  if x.shape[0] > 1:
31
+ x = torch.mean(x, dim=0, keepdim=True)
32
 
33
  # 執行降噪
34
+ out = model(x[None])[0]
35
 
36
  # 後處理
37
  out = out / max(out.abs().max().item(), 1)