Leo Liu
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassifica
|
|
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"
|
@@ -31,7 +32,7 @@ def transcribe_audio(audio_path):
|
|
31 |
return " ".join(results)
|
32 |
return pipe(audio_path)["text"]
|
33 |
|
34 |
-
# Load sentiment analysis model
|
35 |
sentiment_pipe = pipeline("text-classification", model="Leo0129/CustomModel-multilingual-sentiment-analysis", device=device)
|
36 |
|
37 |
# Text splitting function (using jieba for Chinese text)
|
@@ -91,14 +92,22 @@ def main():
|
|
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
|
95 |
</div>
|
96 |
""", unsafe_allow_html=True)
|
97 |
|
|
|
|
|
|
|
98 |
# Audio file uploader
|
99 |
-
uploaded_file = st.file_uploader("ππ» Upload your
|
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:
|
@@ -108,16 +117,24 @@ def main():
|
|
108 |
status_container = st.empty()
|
109 |
|
110 |
# Step 1: Audio transcription
|
111 |
-
status_container.info("π **Step 1
|
112 |
-
|
|
|
113 |
progress_bar.progress(50)
|
114 |
st.write("**Transcript:**", transcript)
|
115 |
|
116 |
# Step 2: Sentiment Analysis
|
117 |
-
status_container.info("π§ββοΈ **Step 2
|
118 |
quality_rating = rate_quality(transcript)
|
119 |
progress_bar.progress(100)
|
120 |
-
st.write("**Sentiment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
os.remove(temp_audio_path)
|
123 |
|
|
|
4 |
import torchaudio
|
5 |
import os
|
6 |
import jieba
|
7 |
+
import magic
|
8 |
|
9 |
# Device setup: automatically selects CUDA or CPU
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
32 |
return " ".join(results)
|
33 |
return pipe(audio_path)["text"]
|
34 |
|
35 |
+
# Load sentiment analysis model
|
36 |
sentiment_pipe = pipeline("text-classification", model="Leo0129/CustomModel-multilingual-sentiment-analysis", device=device)
|
37 |
|
38 |
# Text splitting function (using jieba for Chinese text)
|
|
|
92 |
st.markdown("""
|
93 |
<div class="header">
|
94 |
<h1 style='margin:0;'>ποΈ Customer Service Quality Analyzer</h1>
|
95 |
+
<p style='color: white; font-size: 1.2rem;'>Evaluate the service quality with simple uploading!</p>
|
96 |
</div>
|
97 |
""", unsafe_allow_html=True)
|
98 |
|
99 |
+
# Step-by-step instructions
|
100 |
+
st.markdown("π€ **Step 1:** Please upload your Cantonese customer service audio file")
|
101 |
+
|
102 |
# Audio file uploader
|
103 |
+
uploaded_file = st.file_uploader("ππ» Upload your audio file here...", type=["wav", "mp3", "flac"])
|
104 |
|
105 |
if uploaded_file is not None:
|
106 |
+
file_type = magic.from_buffer(uploaded_file.getbuffer(), mime=True)
|
107 |
+
if not file_type.startswith("audio/"):
|
108 |
+
st.error("β οΈ Sorry, the uploaded file format is not supported. Please upload an audio file in .wav, .mp3, or .flac format.")
|
109 |
+
return
|
110 |
+
|
111 |
st.audio(uploaded_file, format="audio/wav")
|
112 |
temp_audio_path = "uploaded_audio.wav"
|
113 |
with open(temp_audio_path, "wb") as f:
|
|
|
117 |
status_container = st.empty()
|
118 |
|
119 |
# Step 1: Audio transcription
|
120 |
+
status_container.info("π **Step 1:** Transcribing audio, please wait...")
|
121 |
+
with st.spinner('π Transcribing, please wait...'):
|
122 |
+
transcript = transcribe_audio(temp_audio_path)
|
123 |
progress_bar.progress(50)
|
124 |
st.write("**Transcript:**", transcript)
|
125 |
|
126 |
# Step 2: Sentiment Analysis
|
127 |
+
status_container.info("π§ββοΈ **Step 2:** Analyzing sentiment, please wait...")
|
128 |
quality_rating = rate_quality(transcript)
|
129 |
progress_bar.progress(100)
|
130 |
+
st.write("**Sentiment Analysis Result:**", quality_rating)
|
131 |
+
|
132 |
+
# Download analysis results
|
133 |
+
result_text = f"Transcript:\n{transcript}\n\nSentiment Analysis Result: {quality_rating}"
|
134 |
+
st.download_button(label="π₯ Download Analysis Report", data=result_text, file_name="analysis_report.txt")
|
135 |
+
|
136 |
+
# Customer support info
|
137 |
+
st.markdown("βIf you encounter any issues, please contact customer support: π§ **support@hellotoby.com**")
|
138 |
|
139 |
os.remove(temp_audio_path)
|
140 |
|