Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
|
4 |
import torchaudio
|
5 |
import os
|
6 |
import re
|
|
|
7 |
|
8 |
# Device setup
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -21,7 +22,7 @@ def transcribe_audio(audio_path):
|
|
21 |
if duration > 60:
|
22 |
results = []
|
23 |
for start in range(0, int(duration), 50):
|
24 |
-
end = min(start + 60, int(duration))
|
25 |
chunk = waveform[:, start * sample_rate:end * sample_rate]
|
26 |
temp_filename = f"temp_chunk_{start}.wav"
|
27 |
torchaudio.save(temp_filename, chunk, sample_rate)
|
@@ -48,14 +49,35 @@ def translate(text):
|
|
48 |
translations.append(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
49 |
return " ".join(translations)
|
50 |
|
51 |
-
# Load
|
52 |
rating_pipe = pipeline("text-classification", model="Leo0129/CustomModel_dianping-chinese")
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
def rate_quality(text):
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# Streamlit UI
|
61 |
st.title("Cantonese Audio Analysis")
|
|
|
4 |
import torchaudio
|
5 |
import os
|
6 |
import re
|
7 |
+
import jieba
|
8 |
|
9 |
# Device setup
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
22 |
if duration > 60:
|
23 |
results = []
|
24 |
for start in range(0, int(duration), 50):
|
25 |
+
end = min(start + 60, int(duration))
|
26 |
chunk = waveform[:, start * sample_rate:end * sample_rate]
|
27 |
temp_filename = f"temp_chunk_{start}.wav"
|
28 |
torchaudio.save(temp_filename, chunk, sample_rate)
|
|
|
49 |
translations.append(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
50 |
return " ".join(translations)
|
51 |
|
52 |
+
# Load quality rating model
|
53 |
rating_pipe = pipeline("text-classification", model="Leo0129/CustomModel_dianping-chinese")
|
54 |
|
55 |
+
def split_text(text, max_length=512):
|
56 |
+
words = list(jieba.cut(text))
|
57 |
+
chunks, current_chunk = [], ""
|
58 |
+
|
59 |
+
for word in words:
|
60 |
+
if len(current_chunk) + len(word) < max_length:
|
61 |
+
current_chunk += word
|
62 |
+
else:
|
63 |
+
chunks.append(current_chunk)
|
64 |
+
current_chunk = word
|
65 |
+
|
66 |
+
if current_chunk:
|
67 |
+
chunks.append(current_chunk)
|
68 |
+
|
69 |
+
return chunks
|
70 |
+
|
71 |
def rate_quality(text):
|
72 |
+
chunks = split_text(text)
|
73 |
+
results = []
|
74 |
+
|
75 |
+
for chunk in chunks:
|
76 |
+
result = rating_pipe(chunk)[0]
|
77 |
+
label_map = {"LABEL_0": "Poor", "LABEL_1": "Neutral", "LABEL_2": "Good"}
|
78 |
+
results.append(label_map.get(result["label"], "Unknown"))
|
79 |
+
|
80 |
+
return max(set(results), key=results.count) # Return most frequent rating
|
81 |
|
82 |
# Streamlit UI
|
83 |
st.title("Cantonese Audio Analysis")
|