Leo Liu commited on
Commit
795e7cf
·
verified ·
1 Parent(s): c875150

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -66
app.py CHANGED
@@ -1,24 +1,21 @@
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:
@@ -34,33 +31,11 @@ def transcribe_audio(audio_path):
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:
@@ -73,23 +48,21 @@ def split_text(text, max_length=512):
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="Customer Service Quality 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');
@@ -113,49 +86,39 @@ def main():
113
  }
114
  </style>
115
  """, unsafe_allow_html=True)
116
-
117
- # 页面头部展示
118
  st.markdown("""
119
  <div class="header">
120
  <h1 style='margin:0;'>🎙️ Customer Service Quality Analyzer</h1>
121
  <p style='color: white; font-size: 1.2rem;'>Evaluate the service quality with simple-uploading!</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__":
 
1
  import streamlit as st
2
  import torch
3
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
4
  import torchaudio
5
  import os
 
6
  import jieba
7
 
8
+ # Device setup: automatically selects CUDA or CPU
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
+ # Load Whisper model for Cantonese audio transcription
12
  MODEL_NAME = "alvanlii/whisper-small-cantonese"
13
  language = "zh"
14
  pipe = pipeline(task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=60, device=device)
15
  pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=language, task="transcribe")
16
 
17
+ # Transcription function (supports long audio)
18
  def transcribe_audio(audio_path):
 
 
 
19
  waveform, sample_rate = torchaudio.load(audio_path)
20
  duration = waveform.shape[1] / sample_rate
21
  if duration > 60:
 
31
  return " ".join(results)
32
  return pipe(audio_path)["text"]
33
 
34
+ # Load sentiment analysis model (Custom multilingual sentiment analysis)
35
+ sentiment_pipe = pipeline("text-classification", model="CustomModel-multilingual-sentiment-analysis", device=device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ # Text splitting function (using jieba for Chinese text)
38
  def split_text(text, max_length=512):
 
 
 
39
  words = list(jieba.cut(text))
40
  chunks, current_chunk = [], ""
41
  for word in words:
 
48
  chunks.append(current_chunk)
49
  return chunks
50
 
51
+ # Function to rate sentiment quality based on most frequent result
52
  def rate_quality(text):
 
 
 
53
  chunks = split_text(text)
54
  results = []
55
  for chunk in chunks:
56
+ result = sentiment_pipe(chunk)[0]
57
+ label_map = {"Very Negative": "Very Poor", "Negative": "Poor", "Neutral": "Neutral", "Positive": "Good", "Very Positive": "Very Good"}
58
  results.append(label_map.get(result["label"], "Unknown"))
59
  return max(set(results), key=results.count)
60
 
61
+ # Streamlit main interface
62
  def main():
 
63
  st.set_page_config(page_title="Customer Service Quality Analyzer", page_icon="🎙️")
64
+
65
+ # Custom CSS styling
66
  st.markdown("""
67
  <style>
68
  @import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&display=swap');
 
86
  }
87
  </style>
88
  """, unsafe_allow_html=True)
89
+
90
+ # Header
91
  st.markdown("""
92
  <div class="header">
93
  <h1 style='margin:0;'>🎙️ Customer Service Quality Analyzer</h1>
94
  <p style='color: white; font-size: 1.2rem;'>Evaluate the service quality with simple-uploading!</p>
95
  </div>
96
  """, unsafe_allow_html=True)
97
+
98
+ # Audio file uploader
99
  uploaded_file = st.file_uploader("👉🏻 Upload your Cantonese audio file here...", type=["wav", "mp3", "flac"])
100
+
101
  if uploaded_file is not None:
 
102
  st.audio(uploaded_file, format="audio/wav")
 
103
  temp_audio_path = "uploaded_audio.wav"
104
  with open(temp_audio_path, "wb") as f:
105
  f.write(uploaded_file.getbuffer())
106
+
 
107
  progress_bar = st.progress(0)
108
  status_container = st.empty()
109
+
110
+ # Step 1: Audio transcription
111
+ status_container.info("📝 **Step 1/2**: Transcribing audio...")
112
  transcript = transcribe_audio(temp_audio_path)
113
+ progress_bar.progress(50)
114
  st.write("**Transcript:**", transcript)
115
+
116
+ # Step 2: Sentiment Analysis
117
+ status_container.info("🧑‍⚖️ **Step 2/2**: Evaluating sentiment quality...")
118
+ quality_rating = rate_quality(transcript)
 
 
 
 
 
 
119
  progress_bar.progress(100)
120
+ st.write("**Sentiment Rating:**", quality_rating)
121
+
 
122
  os.remove(temp_audio_path)
123
 
124
  if __name__ == "__main__":