fdbw commited on
Commit
d2b821e
·
verified ·
1 Parent(s): 362de9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -70
app.py CHANGED
@@ -1,109 +1,77 @@
1
  import os
2
  import torch
3
- import numpy as np
4
  import gradio as gr
5
  from transformers import pipeline
6
 
7
  # 自动检测设备
8
  device = 0 if torch.cuda.is_available() else -1
9
 
10
- print(f"正在加载轻量级 Whisper 模型,使用设备: {'GPU' if device == 0 else 'CPU'}...")
11
- MODEL_NAME = "openai/whisper-tiny" # CPU环境必须用 tiny 才能保证速度
12
-
13
  pipe = pipeline(
14
  task="automatic-speech-recognition",
15
- model=MODEL_NAME,
16
  device=device
17
  )
18
 
19
- def transcribe_stream(audio_chunk, state=""):
20
  """
21
- 流式音频处理:CPU 极致优化版
22
  """
23
- if audio_chunk is None:
24
- return state, state
25
-
26
- sample_rate, audio_data = audio_chunk
27
 
28
- # 转换为 float32 并且转为单声道
29
- audio_data = audio_data.astype("float32")
30
- if audio_data.ndim > 1:
31
- audio_data = audio_data.mean(axis=1)
32
- audio_data = audio_data / 32768.0
33
-
34
- # 1. 过滤环境噪音和静音:音量过小直接忽略,大幅减少 CPU 计算量与幻觉
35
- if np.max(np.abs(audio_data)) < 0.05:
36
- return state, state
37
-
38
- # 2. 限制单次音频块的最大长度,避免卡死
39
- if len(audio_data) > sample_rate * 5: # 最多处理5秒
40
- audio_data = audio_data[-sample_rate * 5:]
41
-
42
  try:
43
- # 执行推理
44
  result = pipe(
45
- {"sampling_rate": sample_rate, "raw": audio_data},
46
  generate_kwargs={
47
- "language": "zh",
48
  "task": "transcribe",
49
- "max_new_tokens": 64,
50
- "repetition_penalty": 1.5, # 强力惩罚重复文本,杜绝“这就是普通话”无限循环
51
- "no_speech_threshold": 0.6 # 静音检测门槛,杜绝“打赏订阅”幻觉
52
  }
53
  )
 
 
54
 
55
- new_text = result.get("text", "").strip()
56
-
57
- # 过滤Whisper常见的通用幻觉套话
58
- bad_phrases = ["点赞", "订阅", "转发", "打赏", "明镜", "字幕", "普通话"]
59
- if any(phrase in new_text for phrase in bad_phrases) and len(new_text) < 15:
60
- return state, state
61
-
62
- if new_text:
63
- state = (state + " " + new_text).strip()
64
-
65
  except Exception as e:
66
- print(f"识别出错: {e}")
67
-
68
- return state, state
69
-
70
- with gr.Blocks(title="语音字幕转写系统") as demo:
71
- gr.Markdown("# 🎙️ 语音字幕转写系统(轻量响应版)")
72
- gr.Markdown("点击 **`录制`** 后说话,说话停顿后会自动出字。")
73
 
74
- state_store = gr.State(value="")
 
 
 
75
 
76
  with gr.Row():
77
  with gr.Column():
78
- audio_stream = gr.Audio(
79
- sources=["microphone"],
80
- type="numpy",
81
- streaming=True,
82
- label="麦克风实时输入"
83
  )
84
- clear_btn = gr.Button("清空字幕", variant="secondary")
85
 
86
  with gr.Column():
87
  text_output = gr.Textbox(
88
  label="转写结果",
89
- placeholder="说话内容将显示在这里...",
90
- lines=8,
91
- autoscroll=True
92
  )
93
 
94
- audio_stream.stream(
95
- fn=transcribe_stream,
96
- inputs=[audio_stream, state_store],
97
- outputs=[text_output, state_store]
 
98
  )
99
-
100
- def reset_state():
101
- return "", ""
102
-
103
- clear_btn.click(
104
- fn=reset_state,
105
- inputs=None,
106
- outputs=[text_output, state_store]
107
  )
108
 
109
  if __name__ == "__main__":
 
1
  import os
2
  import torch
 
3
  import gradio as gr
4
  from transformers import pipeline
5
 
6
  # 自动检测设备
7
  device = 0 if torch.cuda.is_available() else -1
8
 
9
+ print(f"正在加载 Whisper 模型,使用设备: {'GPU' if device == 0 else 'CPU'}...")
10
+ # 使用 small 模型,保证中文及方言(如四川话/西南口音)的超高准确率
 
11
  pipe = pipeline(
12
  task="automatic-speech-recognition",
13
+ model="openai/whisper-small",
14
  device=device
15
  )
16
 
17
+ def transcribe_audio(audio_path):
18
  """
19
+ 接收完整的音频文件/录音,进行高精度中文/方言转写
20
  """
21
+ if audio_path is None:
22
+ return "请先录音或上传音频文件。"
 
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  try:
25
+ # 执行稳定转写
26
  result = pipe(
27
+ audio_path,
28
  generate_kwargs={
29
+ "language": "zh", # 强行锁定中文,绝不吐英文
30
  "task": "transcribe",
31
+ "max_new_tokens": 256,
32
+ "repetition_penalty": 1.2 # 抑制字词重复
 
33
  }
34
  )
35
+ text = result.get("text", "").strip()
36
+ return text if text else "未能识别出有效语音,请重新录制。"
37
 
 
 
 
 
 
 
 
 
 
 
38
  except Exception as e:
39
+ return f"识别过程出错: {str(e)}"
 
 
 
 
 
 
40
 
41
+ # 构建简单直观的 Gradio 界面
42
+ with gr.Blocks(title="Whisper 语音精准转写系统") as demo:
43
+ gr.Markdown("# 🎙️ 语音精准转写系统(基础稳定版)")
44
+ gr.Markdown("点击麦克风图标**开始录音**,说完后点击**停止**,系统将自动进行高精度转写。")
45
 
46
  with gr.Row():
47
  with gr.Column():
48
+ # 标准音频组件(支持手机麦克风录音与文件上传)
49
+ audio_input = gr.Audio(
50
+ sources=["microphone", "upload"],
51
+ type="filepath",
52
+ label="录音或上传音频"
53
  )
54
+ submit_btn = gr.Button("开始转写文本", variant="primary")
55
 
56
  with gr.Column():
57
  text_output = gr.Textbox(
58
  label="转写结果",
59
+ placeholder="转写后的文字将显示在这里...",
60
+ lines=8
 
61
  )
62
 
63
+ # 点击按钮触发转写
64
+ submit_btn.click(
65
+ fn=transcribe_audio,
66
+ inputs=audio_input,
67
+ outputs=text_output
68
  )
69
+
70
+ # 录音/上传完成后,也自动触发一次转写,提升体验
71
+ audio_input.change(
72
+ fn=transcribe_audio,
73
+ inputs=audio_input,
74
+ outputs=text_output
 
 
75
  )
76
 
77
  if __name__ == "__main__":