Update app.py
Browse files
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"正在加载
|
| 11 |
-
|
| 12 |
-
|
| 13 |
pipe = pipeline(
|
| 14 |
task="automatic-speech-recognition",
|
| 15 |
-
model=
|
| 16 |
device=device
|
| 17 |
)
|
| 18 |
|
| 19 |
-
def
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
"""
|
| 23 |
-
if
|
| 24 |
-
return
|
| 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 |
-
|
| 46 |
generate_kwargs={
|
| 47 |
-
"language": "zh",
|
| 48 |
"task": "transcribe",
|
| 49 |
-
"max_new_tokens":
|
| 50 |
-
"repetition_penalty": 1.
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
return state, state
|
| 69 |
-
|
| 70 |
-
with gr.Blocks(title="语音字幕转写系统") as demo:
|
| 71 |
-
gr.Markdown("# 🎙️ 语音字幕转写系统(轻量响应版)")
|
| 72 |
-
gr.Markdown("点击 **`录制`** 后说话,说话停顿后会自动出字。")
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column():
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
label="
|
| 83 |
)
|
| 84 |
-
|
| 85 |
|
| 86 |
with gr.Column():
|
| 87 |
text_output = gr.Textbox(
|
| 88 |
label="转写结果",
|
| 89 |
-
placeholder="
|
| 90 |
-
lines=8
|
| 91 |
-
autoscroll=True
|
| 92 |
)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 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__":
|