Choultion-Rudas commited on
Commit
66f9ef8
·
verified ·
1 Parent(s): cd66ae0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -31
app.py CHANGED
@@ -12,12 +12,67 @@ model = AutoModel(
12
  trust_remote_code=True,
13
  )
14
 
15
- emo_dict = {"<|HAPPY|>":"😊", "<|SAD|>":"😔", "<|ANGRY|>":"😡", "<|NEUTRAL|>":"", "<|FEARFUL|>":"😰", "<|DISGUSTED|>":"🤢", "<|SURPRISED|>":"😮"}
16
- event_dict = {"<|BGM|>":"🎼", "<|Speech|>":"", "<|Applause|>":"👏", "<|Laughter|>":"😀", "<|Cry|>":"😭", "<|Sneeze|>":"🤧", "<|Breath|>":"", "<|Cough|>":"🤧"}
17
- emoji_dict = {"<|nospeech|><|Event_UNK|>":"❓", "<|zh|>":"", "<|en|>":"", "<|yue|>":"", "<|ja|>":"", "<|ko|>":"", "<|nospeech|>":"", "<|HAPPY|>":"😊", "<|SAD|>":"😔", "<|ANGRY|>":"😡", "<|NEUTRAL|>":"", "<|BGM|>":"🎼", "<|Speech|>":"", "<|Applause|>":"👏", "<|Laughter|>":"😀", "<|FEARFUL|>":"😰", "<|DISGUSTED|>":"🤢", "<|SURPRISED|>":"😮", "<|Cry|>":"😭", "<|EMO_UNKNOWN|>":"", "<|Sneeze|>":"🤧", "<|Breath|>":"", "<|Cough|>":"😷", "<|Sing|>":"", "<|Speech_Noise|>":"", "<|withitn|>":"", "<|woitn|>":"", "<|GBG|>":"", "<|Event_UNK|>":""}
18
- lang_dict = {"<|zh|>":"<|lang|>", "<|en|>":"<|lang|>", "<|yue|>":"<|lang|>", "<|ja|>":"<|lang|>", "<|ko|>":"<|lang|>", "<|nospeech|>":"<|lang|>"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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): emo = e
 
30
  for e in event_dict:
31
- if sptk_dict.get(e, 0) > 0: s = event_dict[e] + s
 
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): return s[-1] if s and s[-1] in emo_set else None
39
- def get_event(s): return s[0] if s and s[0] in event_set else None
 
 
 
 
40
  s = s.replace("<|nospeech|><|Event_UNK|>", "❓")
41
- for lang in lang_dict: s = s.replace(lang, "<|lang|>")
 
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]: continue
47
- if get_event(s_list[i]) == cur_ent_event and cur_ent_event is not None: s_list[i] = s_list[i][1:]
 
 
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): new_s = new_s[:-1]
 
50
  new_s += s_list[i].strip().lstrip()
51
  return new_s.strip()
52
 
53
- def model_inference(audio_input, language, output_format, use_itn, merge_vad, merge_length, ban_emo_unk):
54
- if audio_input is None: return "错误:请上传或录制音频。"
 
 
 
 
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: input_wav = input_wav.mean(-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(input=input_wav, cache={}, language=language, use_itn=use_itn, batch_size_s=60, merge_vad=merge_vad, merge_length_s=merge_length, ban_emo_unk=ban_emo_unk)
 
 
 
 
 
 
 
 
 
62
  raw_text = res[0].get("text", "未能识别出文本。")
63
- if output_format == "原始富文本": return raw_text
64
- elif output_format == "纯净文本": return ' '.join(re.sub(r'<\|[^>]*>', '', raw_text).split())
65
- else: return format_str_v3(raw_text)
 
 
 
 
66
 
67
  html_intro = """
68
  <div>
@@ -80,8 +164,25 @@ SenseVoice-Small 是一种仅包含编码器的语音基础模型,专为快速
80
  </div>
81
  """
82
 
83
- lang_options = [("自动检测", "auto"), ("中文", "zh"), ("English", "en"), ("粤语", "yue"), ("日本語", "ja"), ("한국어", "ko"), ("无语音", "nospeech")]
84
- example_list = [[f"{model.model_path}/example/zh.mp3", "auto"], [f"{model.model_path}/example/en.mp3", "auto"], [f"{model.model_path}/example/yue.mp3", "auto"], [f"{model.model_path}/example/ja.mp3", "auto"], [f"{model.model_path}/example/ko.mp3", "auto"], [f"{model.model_path}/example/rich_1.wav", "auto"], [f"{model.model_path}/example/rich_2.wav", "auto"], [f"{model.model_path}/example/longwav_1.wav", "auto"]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(choices=["Emoji格式化", "纯净文本", "原始富文本"], value="Emoji格式化", label="选择输出格式")
93
- language_inputs = gr.Dropdown(choices=lang_options, value="auto", label="识别语言")
94
- use_itn_checkbox = gr.Checkbox(value=True, label="启用标点和逆文本正规化")
95
- merge_vad_checkbox = gr.Checkbox(value=True, label="合并VAD切分的音频片段")
96
- merge_length_slider = gr.Slider(minimum=5, maximum=30, value=15, step=1, label="合并后片段最大长度(秒)")
97
- ban_emo_unk_checkbox = gr.Checkbox(value=False, label="禁用未知情感标签")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  fn_button = gr.Button("开始识别", variant="primary")
99
  with gr.Column(scale=3):
100
  text_outputs = gr.Textbox(label="识别结果", lines=20)
101
- gr.Examples(examples=example_list, inputs=[audio_inputs, language_inputs], label="示例音频 (点击即可试用)")
102
- fn_button.click(fn=model_inference, inputs=[audio_inputs, language_inputs, output_format_dropdown, use_itn_checkbox, merge_vad_checkbox, merge_length_slider, ban_emo_unk_checkbox], outputs=text_outputs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()