Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,61 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import torch
|
| 3 |
import torchaudio
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
# Device setup
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
| 10 |
-
# Load Whisper model for
|
| 11 |
MODEL_NAME = "alvanlii/whisper-small-cantonese"
|
| 12 |
language = "zh"
|
| 13 |
-
|
| 14 |
task="automatic-speech-recognition",
|
| 15 |
model=MODEL_NAME,
|
| 16 |
chunk_length_s=60,
|
| 17 |
device=device
|
| 18 |
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Load Summarization model
|
| 22 |
-
summarizer = pipeline("summarization", model="Ayaka/bart-base-cantonese")
|
| 23 |
|
| 24 |
# Load quality rating model
|
| 25 |
rating_pipe = pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
st.title("π Customer Service Conversation Quality Analyzer")
|
| 30 |
-
st.write("Upload a Cantonese audio file to transcribe, summarize, and evaluate its quality.")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
def transcribe_audio(audio_path):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def summarize_text(text):
|
| 39 |
-
return summarizer(text, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
|
| 40 |
|
| 41 |
def rate_quality(text):
|
| 42 |
-
result = rating_pipe(text
|
| 43 |
-
label_map = {"Very Negative": "Very Poor", "Negative": "Poor", "Neutral": "Neutral", "Positive": "Good", "Very Positive": "Very Good"}
|
| 44 |
return label_map.get(result["label"], "Unknown")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
transcript = transcribe_audio(temp_audio_path)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
st.subheader("π Summary")
|
| 60 |
-
st.write(summary)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
st.subheader("π Quality Rating")
|
| 65 |
-
st.write(f"**{quality_rating}**")
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torchaudio
|
| 3 |
import os
|
| 4 |
+
import re
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from difflib import SequenceMatcher
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
# Device setup
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
+
# Load Whisper model for transcription
|
| 13 |
MODEL_NAME = "alvanlii/whisper-small-cantonese"
|
| 14 |
language = "zh"
|
| 15 |
+
pipe = pipeline(
|
| 16 |
task="automatic-speech-recognition",
|
| 17 |
model=MODEL_NAME,
|
| 18 |
chunk_length_s=60,
|
| 19 |
device=device
|
| 20 |
)
|
| 21 |
+
pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=language, task="transcribe")
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Load quality rating model
|
| 24 |
rating_pipe = pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
|
| 25 |
|
| 26 |
+
# Sentiment label mapping
|
| 27 |
+
label_map = {"Negative": "Very Poor", "Neutral": "Neutral", "Positive": "Very Good"}
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def remove_punctuation(text):
|
| 30 |
+
return re.sub(r'[^\w\s]', '', text)
|
| 31 |
|
| 32 |
def transcribe_audio(audio_path):
|
| 33 |
+
transcript = pipe(audio_path)["text"]
|
| 34 |
+
return remove_punctuation(transcript)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def rate_quality(text):
|
| 37 |
+
result = rating_pipe(text)[0]
|
|
|
|
| 38 |
return label_map.get(result["label"], "Unknown")
|
| 39 |
|
| 40 |
+
# Streamlit UI
|
| 41 |
+
st.set_page_config(page_title="Cantonese Audio Transcription & Analysis", layout="centered")
|
| 42 |
+
st.title("π£οΈ Cantonese Audio Transcriber & Sentiment Analyzer")
|
| 43 |
+
st.markdown("Upload your Cantonese audio file, and we will transcribe and analyze its sentiment.")
|
| 44 |
+
|
| 45 |
+
uploaded_file = st.file_uploader("Upload an audio file (WAV, MP3, etc.)", type=["wav", "mp3", "m4a"])
|
| 46 |
+
if uploaded_file is not None:
|
| 47 |
+
with st.spinner("Processing audio..."):
|
| 48 |
+
temp_audio_path = "temp_audio.wav"
|
| 49 |
+
with open(temp_audio_path, "wb") as f:
|
| 50 |
+
f.write(uploaded_file.getbuffer())
|
| 51 |
transcript = transcribe_audio(temp_audio_path)
|
| 52 |
+
sentiment = rate_quality(transcript)
|
| 53 |
+
os.remove(temp_audio_path)
|
| 54 |
|
| 55 |
+
st.subheader("Transcription")
|
| 56 |
+
st.text_area("", transcript, height=150)
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
st.subheader("Sentiment Analysis")
|
| 59 |
+
st.markdown(f"### π Sentiment: **{sentiment}**")
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
st.success("Processing complete! π")
|