Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,12 +12,67 @@ model = AutoModel(
|
|
| 12 |
trust_remote_code=True,
|
| 13 |
)
|
| 14 |
|
| 15 |
-
emo_dict = {
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
emo_set = {"😊", "😔", "😡", "😰", "🤢", "😮"}
|
| 20 |
-
event_set =
|
|
|
|
| 21 |
|
| 22 |
def format_str_v2(s):
|
| 23 |
sptk_dict = {}
|
|
@@ -26,43 +81,72 @@ def format_str_v2(s):
|
|
| 26 |
s = s.replace(sptk, "")
|
| 27 |
emo = "<|NEUTRAL|>"
|
| 28 |
for e in emo_dict:
|
| 29 |
-
if sptk_dict.get(e, 0) > sptk_dict.get(emo, 0):
|
|
|
|
| 30 |
for e in event_dict:
|
| 31 |
-
if sptk_dict.get(e, 0) > 0:
|
|
|
|
| 32 |
s = s + emo_dict.get(emo, "")
|
| 33 |
for emoji in emo_set.union(event_set):
|
| 34 |
s = s.replace(" " + emoji, emoji).replace(emoji + " ", emoji)
|
| 35 |
return s.strip()
|
| 36 |
|
|
|
|
| 37 |
def format_str_v3(s):
|
| 38 |
-
def get_emo(s):
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
s = s.replace("<|nospeech|><|Event_UNK|>", "❓")
|
| 41 |
-
for lang in lang_dict:
|
|
|
|
| 42 |
s_list = [format_str_v2(s_i).strip(" ") for s_i in s.split("<|lang|>")]
|
| 43 |
new_s = " " + (s_list[0] if s_list else "")
|
| 44 |
cur_ent_event = get_event(new_s)
|
| 45 |
for i in range(1, len(s_list)):
|
| 46 |
-
if not s_list[i]:
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
cur_ent_event = get_event(s_list[i])
|
| 49 |
-
if get_emo(s_list[i]) is not None and get_emo(s_list[i]) == get_emo(new_s):
|
|
|
|
| 50 |
new_s += s_list[i].strip().lstrip()
|
| 51 |
return new_s.strip()
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
fs, input_wav = audio_input
|
| 56 |
input_wav = input_wav.astype(np.float32) / np.iinfo(np.int16).max
|
| 57 |
-
if len(input_wav.shape) > 1:
|
|
|
|
| 58 |
if fs != 16000:
|
| 59 |
resampler = torchaudio.transforms.Resample(orig_freq=fs, new_freq=16000)
|
| 60 |
input_wav = resampler(torch.from_numpy(input_wav)).numpy()
|
| 61 |
-
res = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
raw_text = res[0].get("text", "未能识别出文本。")
|
| 63 |
-
if output_format == "原始富文本":
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
html_intro = """
|
| 68 |
<div>
|
|
@@ -80,8 +164,25 @@ SenseVoice-Small 是一种仅包含编码器的语音基础模型,专为快速
|
|
| 80 |
</div>
|
| 81 |
"""
|
| 82 |
|
| 83 |
-
lang_options = [
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 87 |
gr.HTML(html_intro)
|
|
@@ -89,16 +190,50 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 89 |
with gr.Column(scale=2):
|
| 90 |
audio_inputs = gr.Audio(label="上传音频或使用麦克风")
|
| 91 |
with gr.Accordion("高级设置", open=False):
|
| 92 |
-
output_format_dropdown = gr.Dropdown(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
fn_button = gr.Button("开始识别", variant="primary")
|
| 99 |
with gr.Column(scale=3):
|
| 100 |
text_outputs = gr.Textbox(label="识别结果", lines=20)
|
| 101 |
-
gr.Examples(
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
demo.launch()
|
|
|
|
| 12 |
trust_remote_code=True,
|
| 13 |
)
|
| 14 |
|
| 15 |
+
emo_dict = {
|
| 16 |
+
"<|HAPPY|>": "😊",
|
| 17 |
+
"<|SAD|>": "😔",
|
| 18 |
+
"<|ANGRY|>": "😡",
|
| 19 |
+
"<|NEUTRAL|>": "",
|
| 20 |
+
"<|FEARFUL|>": "😰",
|
| 21 |
+
"<|DISGUSTED|>": "🤢",
|
| 22 |
+
"<|SURPRISED|>": "😮",
|
| 23 |
+
}
|
| 24 |
+
event_dict = {
|
| 25 |
+
"<|BGM|>": "🎼",
|
| 26 |
+
"<|Speech|>": "",
|
| 27 |
+
"<|Applause|>": "👏",
|
| 28 |
+
"<|Laughter|>": "😀",
|
| 29 |
+
"<|Cry|>": "😭",
|
| 30 |
+
"<|Sneeze|>": "🤧",
|
| 31 |
+
"<|Breath|>": "",
|
| 32 |
+
"<|Cough|>": "🤧",
|
| 33 |
+
}
|
| 34 |
+
emoji_dict = {
|
| 35 |
+
"<|nospeech|><|Event_UNK|>": "❓",
|
| 36 |
+
"<|zh|>": "",
|
| 37 |
+
"<|en|>": "",
|
| 38 |
+
"<|yue|>": "",
|
| 39 |
+
"<|ja|>": "",
|
| 40 |
+
"<|ko|>": "",
|
| 41 |
+
"<|nospeech|>": "",
|
| 42 |
+
"<|HAPPY|>": "😊",
|
| 43 |
+
"<|SAD|>": "😔",
|
| 44 |
+
"<|ANGRY|>": "😡",
|
| 45 |
+
"<|NEUTRAL|>": "",
|
| 46 |
+
"<|BGM|>": "🎼",
|
| 47 |
+
"<|Speech|>": "",
|
| 48 |
+
"<|Applause|>": "👏",
|
| 49 |
+
"<|Laughter|>": "😀",
|
| 50 |
+
"<|FEARFUL|>": "😰",
|
| 51 |
+
"<|DISGUSTED|>": "🤢",
|
| 52 |
+
"<|SURPRISED|>": "😮",
|
| 53 |
+
"<|Cry|>": "😭",
|
| 54 |
+
"<|EMO_UNKNOWN|>": "",
|
| 55 |
+
"<|Sneeze|>": "🤧",
|
| 56 |
+
"<|Breath|>": "",
|
| 57 |
+
"<|Cough|>": "😷",
|
| 58 |
+
"<|Sing|>": "",
|
| 59 |
+
"<|Speech_Noise|>": "",
|
| 60 |
+
"<|withitn|>": "",
|
| 61 |
+
"<|woitn|>": "",
|
| 62 |
+
"<|GBG|>": "",
|
| 63 |
+
"<|Event_UNK|>": "",
|
| 64 |
+
}
|
| 65 |
+
lang_dict = {
|
| 66 |
+
"<|zh|>": "<|lang|>",
|
| 67 |
+
"<|en|>": "<|lang|>",
|
| 68 |
+
"<|yue|>": "<|lang|>",
|
| 69 |
+
"<|ja|>": "<|lang|>",
|
| 70 |
+
"<|ko|>": "<|lang|>",
|
| 71 |
+
"<|nospeech|>": "<|lang|>",
|
| 72 |
+
}
|
| 73 |
emo_set = {"😊", "😔", "😡", "😰", "🤢", "😮"}
|
| 74 |
+
event_set = {"🎼", "👏", "😀", "😭", "🤧", "😷"}
|
| 75 |
+
|
| 76 |
|
| 77 |
def format_str_v2(s):
|
| 78 |
sptk_dict = {}
|
|
|
|
| 81 |
s = s.replace(sptk, "")
|
| 82 |
emo = "<|NEUTRAL|>"
|
| 83 |
for e in emo_dict:
|
| 84 |
+
if sptk_dict.get(e, 0) > sptk_dict.get(emo, 0):
|
| 85 |
+
emo = e
|
| 86 |
for e in event_dict:
|
| 87 |
+
if sptk_dict.get(e, 0) > 0:
|
| 88 |
+
s = event_dict[e] + s
|
| 89 |
s = s + emo_dict.get(emo, "")
|
| 90 |
for emoji in emo_set.union(event_set):
|
| 91 |
s = s.replace(" " + emoji, emoji).replace(emoji + " ", emoji)
|
| 92 |
return s.strip()
|
| 93 |
|
| 94 |
+
|
| 95 |
def format_str_v3(s):
|
| 96 |
+
def get_emo(s):
|
| 97 |
+
return s[-1] if s and s[-1] in emo_set else None
|
| 98 |
+
|
| 99 |
+
def get_event(s):
|
| 100 |
+
return s[0] if s and s[0] in event_set else None
|
| 101 |
+
|
| 102 |
s = s.replace("<|nospeech|><|Event_UNK|>", "❓")
|
| 103 |
+
for lang in lang_dict:
|
| 104 |
+
s = s.replace(lang, "<|lang|>")
|
| 105 |
s_list = [format_str_v2(s_i).strip(" ") for s_i in s.split("<|lang|>")]
|
| 106 |
new_s = " " + (s_list[0] if s_list else "")
|
| 107 |
cur_ent_event = get_event(new_s)
|
| 108 |
for i in range(1, len(s_list)):
|
| 109 |
+
if not s_list[i]:
|
| 110 |
+
continue
|
| 111 |
+
if get_event(s_list[i]) == cur_ent_event and cur_ent_event is not None:
|
| 112 |
+
s_list[i] = s_list[i][1:]
|
| 113 |
cur_ent_event = get_event(s_list[i])
|
| 114 |
+
if get_emo(s_list[i]) is not None and get_emo(s_list[i]) == get_emo(new_s):
|
| 115 |
+
new_s = new_s[:-1]
|
| 116 |
new_s += s_list[i].strip().lstrip()
|
| 117 |
return new_s.strip()
|
| 118 |
|
| 119 |
+
|
| 120 |
+
def model_inference(
|
| 121 |
+
audio_input, language, output_format, use_itn, merge_vad, merge_length, ban_emo_unk
|
| 122 |
+
):
|
| 123 |
+
if audio_input is None:
|
| 124 |
+
return "错误:请上传或录制音频。"
|
| 125 |
fs, input_wav = audio_input
|
| 126 |
input_wav = input_wav.astype(np.float32) / np.iinfo(np.int16).max
|
| 127 |
+
if len(input_wav.shape) > 1:
|
| 128 |
+
input_wav = input_wav.mean(-1)
|
| 129 |
if fs != 16000:
|
| 130 |
resampler = torchaudio.transforms.Resample(orig_freq=fs, new_freq=16000)
|
| 131 |
input_wav = resampler(torch.from_numpy(input_wav)).numpy()
|
| 132 |
+
res = model.generate(
|
| 133 |
+
input=input_wav,
|
| 134 |
+
cache={},
|
| 135 |
+
language=language,
|
| 136 |
+
use_itn=use_itn,
|
| 137 |
+
batch_size_s=60,
|
| 138 |
+
merge_vad=merge_vad,
|
| 139 |
+
merge_length_s=merge_length,
|
| 140 |
+
ban_emo_unk=ban_emo_unk,
|
| 141 |
+
)
|
| 142 |
raw_text = res[0].get("text", "未能识别出文本。")
|
| 143 |
+
if output_format == "原始富文本":
|
| 144 |
+
return raw_text
|
| 145 |
+
elif output_format == "纯净文本":
|
| 146 |
+
return " ".join(re.sub(r"<\|[^>]*>", "", raw_text).split())
|
| 147 |
+
else:
|
| 148 |
+
return format_str_v3(raw_text)
|
| 149 |
+
|
| 150 |
|
| 151 |
html_intro = """
|
| 152 |
<div>
|
|
|
|
| 164 |
</div>
|
| 165 |
"""
|
| 166 |
|
| 167 |
+
lang_options = [
|
| 168 |
+
("自动检测", "auto"),
|
| 169 |
+
("中文", "zh"),
|
| 170 |
+
("English", "en"),
|
| 171 |
+
("粤语", "yue"),
|
| 172 |
+
("日本語", "ja"),
|
| 173 |
+
("한국어", "ko"),
|
| 174 |
+
("无语音", "nospeech"),
|
| 175 |
+
]
|
| 176 |
+
example_list = [
|
| 177 |
+
[f"{model.model_path}/example/zh.mp3", "auto"],
|
| 178 |
+
[f"{model.model_path}/example/en.mp3", "auto"],
|
| 179 |
+
[f"{model.model_path}/example/yue.mp3", "auto"],
|
| 180 |
+
[f"{model.model_path}/example/ja.mp3", "auto"],
|
| 181 |
+
[f"{model.model_path}/example/ko.mp3", "auto"],
|
| 182 |
+
[f"{model.model_path}/example/rich_1.wav", "auto"],
|
| 183 |
+
[f"{model.model_path}/example/rich_2.wav", "auto"],
|
| 184 |
+
[f"{model.model_path}/example/longwav_1.wav", "auto"],
|
| 185 |
+
]
|
| 186 |
|
| 187 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 188 |
gr.HTML(html_intro)
|
|
|
|
| 190 |
with gr.Column(scale=2):
|
| 191 |
audio_inputs = gr.Audio(label="上传音频或使用麦克风")
|
| 192 |
with gr.Accordion("高级设置", open=False):
|
| 193 |
+
output_format_dropdown = gr.Dropdown(
|
| 194 |
+
choices=["Emoji格式化", "纯净文本", "原始富文本"],
|
| 195 |
+
value="Emoji格式化",
|
| 196 |
+
label="选择输出格式",
|
| 197 |
+
)
|
| 198 |
+
language_inputs = gr.Dropdown(
|
| 199 |
+
choices=lang_options, value="auto", label="识别语言"
|
| 200 |
+
)
|
| 201 |
+
use_itn_checkbox = gr.Checkbox(
|
| 202 |
+
value=True, label="启用标点和逆文本正规化"
|
| 203 |
+
)
|
| 204 |
+
merge_vad_checkbox = gr.Checkbox(
|
| 205 |
+
value=True, label="合并VAD切分的音频片段"
|
| 206 |
+
)
|
| 207 |
+
merge_length_slider = gr.Slider(
|
| 208 |
+
minimum=5,
|
| 209 |
+
maximum=30,
|
| 210 |
+
value=15,
|
| 211 |
+
step=1,
|
| 212 |
+
label="合并后片段最大长度(秒)",
|
| 213 |
+
)
|
| 214 |
+
ban_emo_unk_checkbox = gr.Checkbox(
|
| 215 |
+
value=False, label="禁用未知情感标签"
|
| 216 |
+
)
|
| 217 |
fn_button = gr.Button("开始识别", variant="primary")
|
| 218 |
with gr.Column(scale=3):
|
| 219 |
text_outputs = gr.Textbox(label="识别结果", lines=20)
|
| 220 |
+
gr.Examples(
|
| 221 |
+
examples=example_list,
|
| 222 |
+
inputs=[audio_inputs, language_inputs],
|
| 223 |
+
label="示例音频 (点击即可试用)",
|
| 224 |
+
)
|
| 225 |
+
fn_button.click(
|
| 226 |
+
fn=model_inference,
|
| 227 |
+
inputs=[
|
| 228 |
+
audio_inputs,
|
| 229 |
+
language_inputs,
|
| 230 |
+
output_format_dropdown,
|
| 231 |
+
use_itn_checkbox,
|
| 232 |
+
merge_vad_checkbox,
|
| 233 |
+
merge_length_slider,
|
| 234 |
+
ban_emo_unk_checkbox,
|
| 235 |
+
],
|
| 236 |
+
outputs=text_outputs,
|
| 237 |
+
)
|
| 238 |
|
| 239 |
+
demo.launch()
|