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
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
import torchaudio
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
import jieba
|
| 8 |
+
|
| 9 |
+
# Device setup: 自动选择使用 CUDA 或 CPU
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
|
| 12 |
+
# 加载 Whisper 模型,用于音频转录(粤语版)
|
| 13 |
+
MODEL_NAME = "alvanlii/whisper-small-cantonese"
|
| 14 |
+
language = "zh"
|
| 15 |
+
pipe = pipeline(task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=60, device=device)
|
| 16 |
+
pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=language, task="transcribe")
|
| 17 |
+
|
| 18 |
+
def transcribe_audio(audio_path):
|
| 19 |
+
"""
|
| 20 |
+
对音频文件进行转录,支持大于60秒的音频分段处理
|
| 21 |
+
"""
|
| 22 |
+
waveform, sample_rate = torchaudio.load(audio_path)
|
| 23 |
+
duration = waveform.shape[1] / sample_rate
|
| 24 |
+
if duration > 60:
|
| 25 |
+
results = []
|
| 26 |
+
for start in range(0, int(duration), 50):
|
| 27 |
+
end = min(start + 60, int(duration))
|
| 28 |
+
chunk = waveform[:, start * sample_rate:end * sample_rate]
|
| 29 |
+
temp_filename = f"temp_chunk_{start}.wav"
|
| 30 |
+
torchaudio.save(temp_filename, chunk, sample_rate)
|
| 31 |
+
result = pipe(temp_filename)["text"]
|
| 32 |
+
results.append(result)
|
| 33 |
+
os.remove(temp_filename)
|
| 34 |
+
return " ".join(results)
|
| 35 |
+
return pipe(audio_path)["text"]
|
| 36 |
+
|
| 37 |
+
# 加载翻译模型(粤语到中文)
|
| 38 |
+
tokenizer = AutoTokenizer.from_pretrained("botisan-ai/mt5-translate-yue-zh")
|
| 39 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("botisan-ai/mt5-translate-yue-zh").to(device)
|
| 40 |
+
|
| 41 |
+
def split_sentences(text):
|
| 42 |
+
"""根据中文标点分割句子"""
|
| 43 |
+
return [s for s in re.split(r'(?<=[。!?])', text) if s]
|
| 44 |
+
|
| 45 |
+
def translate(text):
|
| 46 |
+
"""
|
| 47 |
+
将转录文本翻译为中文,逐句翻译后拼接输出
|
| 48 |
+
"""
|
| 49 |
+
sentences = split_sentences(text)
|
| 50 |
+
translations = []
|
| 51 |
+
for sentence in sentences:
|
| 52 |
+
inputs = tokenizer(sentence, return_tensors="pt").to(device)
|
| 53 |
+
outputs = model.generate(inputs["input_ids"], max_length=1000, num_beams=5)
|
| 54 |
+
translations.append(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 55 |
+
return " ".join(translations)
|
| 56 |
+
|
| 57 |
+
# 加载质量评分模型,用于评价对话质量
|
| 58 |
+
rating_pipe = pipeline("text-classification", model="Leo0129/CustomModel_dianping-chinese")
|
| 59 |
+
|
| 60 |
+
def split_text(text, max_length=512):
|
| 61 |
+
"""
|
| 62 |
+
将文本按照最大长度拆分成多个片段,使用 jieba 分词
|
| 63 |
+
"""
|
| 64 |
+
words = list(jieba.cut(text))
|
| 65 |
+
chunks, current_chunk = [], ""
|
| 66 |
+
for word in words:
|
| 67 |
+
if len(current_chunk) + len(word) < max_length:
|
| 68 |
+
current_chunk += word
|
| 69 |
+
else:
|
| 70 |
+
chunks.append(current_chunk)
|
| 71 |
+
current_chunk = word
|
| 72 |
+
if current_chunk:
|
| 73 |
+
chunks.append(current_chunk)
|
| 74 |
+
return chunks
|
| 75 |
+
|
| 76 |
+
def rate_quality(text):
|
| 77 |
+
"""
|
| 78 |
+
对翻译后的文本进行质量评价,返回最频繁的评分结果
|
| 79 |
+
"""
|
| 80 |
+
chunks = split_text(text)
|
| 81 |
+
results = []
|
| 82 |
+
for chunk in chunks:
|
| 83 |
+
result = rating_pipe(chunk)[0]
|
| 84 |
+
label_map = {"LABEL_0": "Poor", "LABEL_1": "Neutral", "LABEL_2": "Good"}
|
| 85 |
+
results.append(label_map.get(result["label"], "Unknown"))
|
| 86 |
+
return max(set(results), key=results.count)
|
| 87 |
+
|
| 88 |
+
def main():
|
| 89 |
+
# 设置页面配置和图标,吸引用户注意
|
| 90 |
+
st.set_page_config(page_title="Cantonese Audio Analyzer", page_icon="🎙️")
|
| 91 |
+
|
| 92 |
+
# 自定义 CSS 样式(引用 Comic Neue 字体,并设置背景渐变、边框圆角等效果)
|
| 93 |
+
st.markdown("""
|
| 94 |
+
<style>
|
| 95 |
+
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&display=swap');
|
| 96 |
+
.header {
|
| 97 |
+
background: linear-gradient(45deg, #FF9A6C, #FF6B6B);
|
| 98 |
+
border-radius: 15px;
|
| 99 |
+
padding: 2rem;
|
| 100 |
+
text-align: center;
|
| 101 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 102 |
+
margin-bottom: 2rem;
|
| 103 |
+
}
|
| 104 |
+
.subtitle {
|
| 105 |
+
font-family: 'Comic Neue', cursive;
|
| 106 |
+
color: #4B4B4B;
|
| 107 |
+
font-size: 1.2rem;
|
| 108 |
+
margin: 1rem 0;
|
| 109 |
+
padding: 1rem;
|
| 110 |
+
background: rgba(255,255,255,0.9);
|
| 111 |
+
border-radius: 10px;
|
| 112 |
+
border-left: 5px solid #FF6B6B;
|
| 113 |
+
}
|
| 114 |
+
</style>
|
| 115 |
+
""", unsafe_allow_html=True)
|
| 116 |
+
|
| 117 |
+
# 页面头部展示
|
| 118 |
+
st.markdown("""
|
| 119 |
+
<div class="header">
|
| 120 |
+
<h1 style='margin:0;'>🎙️ Cantonese Audio Analyzer</h1>
|
| 121 |
+
<p style='color: white; font-size: 1.2rem;'>Transcribe, translate, and evaluate your audio magic!</p>
|
| 122 |
+
</div>
|
| 123 |
+
""", unsafe_allow_html=True)
|
| 124 |
+
|
| 125 |
+
# 上传音频文件(支持 wav、mp3、flac 格式)
|
| 126 |
+
uploaded_file = st.file_uploader("👉🏻 Upload your Cantonese audio file here...", type=["wav", "mp3", "flac"])
|
| 127 |
+
|
| 128 |
+
if uploaded_file is not None:
|
| 129 |
+
# 直接播放上传的音频
|
| 130 |
+
st.audio(uploaded_file, format="audio/wav")
|
| 131 |
+
# 将上传的文件保存为临时文件
|
| 132 |
+
temp_audio_path = "uploaded_audio.wav"
|
| 133 |
+
with open(temp_audio_path, "wb") as f:
|
| 134 |
+
f.write(uploaded_file.getbuffer())
|
| 135 |
+
|
| 136 |
+
# 初始化进度条和状态提示区域
|
| 137 |
+
progress_bar = st.progress(0)
|
| 138 |
+
status_container = st.empty()
|
| 139 |
+
|
| 140 |
+
# Step 1: 音频转录
|
| 141 |
+
status_container.info("🔮 **Step 1/3**: Transcribing audio...")
|
| 142 |
+
transcript = transcribe_audio(temp_audio_path)
|
| 143 |
+
progress_bar.progress(33)
|
| 144 |
+
st.write("**Transcript:**", transcript)
|
| 145 |
+
|
| 146 |
+
# Step 2: 翻译转录内容
|
| 147 |
+
status_container.info("📚 **Step 2/3**: Translating transcript...")
|
| 148 |
+
translated_text = translate(transcript)
|
| 149 |
+
progress_bar.progress(66)
|
| 150 |
+
st.write("**Translation:**", translated_text)
|
| 151 |
+
|
| 152 |
+
# Step 3: 音频质量评分
|
| 153 |
+
status_container.info("🎵 **Step 3/3**: Evaluating audio quality...")
|
| 154 |
+
quality_rating = rate_quality(translated_text)
|
| 155 |
+
progress_bar.progress(100)
|
| 156 |
+
st.write("**Quality Rating:**", quality_rating)
|
| 157 |
+
|
| 158 |
+
# 处理完成后删除临时文件
|
| 159 |
+
os.remove(temp_audio_path)
|
| 160 |
+
|
| 161 |
+
if __name__ == "__main__":
|
| 162 |
+
main()
|