ๆธธ้› commited on
Commit
5cb9c90
โ€ข
1 Parent(s): c88b5fa
Files changed (3) hide show
  1. .DS_Store +0 -0
  2. app.py +239 -0
  3. requirements.txt +8 -0
.DS_Store ADDED
Binary file (8.2 kB). View file
 
app.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ import os
4
+ import librosa
5
+ import base64
6
+ import io
7
+ import gradio as gr
8
+ import re
9
+
10
+ import numpy as np
11
+ import torch
12
+ import torchaudio
13
+
14
+
15
+ from funasr import AutoModel
16
+
17
+ model = "FunAudioLLM/SenseVoiceSmall"
18
+ model = AutoModel(model=model,
19
+ vad_model="iic/speech_fsmn_vad_zh-cn-16k-common-pytorch",
20
+ vad_kwargs={"max_single_segment_time": 30000},
21
+ hub="hf",
22
+ )
23
+
24
+ import re
25
+
26
+ emo_dict = {
27
+ "<|HAPPY|>": "๐Ÿ˜Š",
28
+ "<|SAD|>": "๐Ÿ˜”",
29
+ "<|ANGRY|>": "๐Ÿ˜ก",
30
+ "<|NEUTRAL|>": "",
31
+ "<|FEARFUL|>": "๐Ÿ˜ฐ",
32
+ "<|DISGUSTED|>": "๐Ÿคข",
33
+ "<|SURPRISED|>": "๐Ÿ˜ฎ",
34
+ }
35
+
36
+ event_dict = {
37
+ "<|BGM|>": "๐ŸŽผ",
38
+ "<|Speech|>": "",
39
+ "<|Applause|>": "๐Ÿ‘",
40
+ "<|Laughter|>": "๐Ÿ˜€",
41
+ "<|Cry|>": "๐Ÿ˜ญ",
42
+ "<|Sneeze|>": "๐Ÿคง",
43
+ "<|Breath|>": "",
44
+ "<|Cough|>": "๐Ÿคง",
45
+ }
46
+
47
+ emoji_dict = {
48
+ "<|nospeech|><|Event_UNK|>": "โ“",
49
+ "<|zh|>": "",
50
+ "<|en|>": "",
51
+ "<|yue|>": "",
52
+ "<|ja|>": "",
53
+ "<|ko|>": "",
54
+ "<|nospeech|>": "",
55
+ "<|HAPPY|>": "๐Ÿ˜Š",
56
+ "<|SAD|>": "๐Ÿ˜”",
57
+ "<|ANGRY|>": "๐Ÿ˜ก",
58
+ "<|NEUTRAL|>": "",
59
+ "<|BGM|>": "๐ŸŽผ",
60
+ "<|Speech|>": "",
61
+ "<|Applause|>": "๐Ÿ‘",
62
+ "<|Laughter|>": "๐Ÿ˜€",
63
+ "<|FEARFUL|>": "๐Ÿ˜ฐ",
64
+ "<|DISGUSTED|>": "๐Ÿคข",
65
+ "<|SURPRISED|>": "๐Ÿ˜ฎ",
66
+ "<|Cry|>": "๐Ÿ˜ญ",
67
+ "<|EMO_UNKNOWN|>": "",
68
+ "<|Sneeze|>": "๐Ÿคง",
69
+ "<|Breath|>": "",
70
+ "<|Cough|>": "๐Ÿ˜ท",
71
+ "<|Sing|>": "",
72
+ "<|Speech_Noise|>": "",
73
+ "<|withitn|>": "",
74
+ "<|woitn|>": "",
75
+ "<|GBG|>": "",
76
+ "<|Event_UNK|>": "",
77
+ }
78
+
79
+ lang_dict = {
80
+ "<|zh|>": "<|lang|>",
81
+ "<|en|>": "<|lang|>",
82
+ "<|yue|>": "<|lang|>",
83
+ "<|ja|>": "<|lang|>",
84
+ "<|ko|>": "<|lang|>",
85
+ "<|nospeech|>": "<|lang|>",
86
+ }
87
+
88
+ emo_set = {"๐Ÿ˜Š", "๐Ÿ˜”", "๐Ÿ˜ก", "๐Ÿ˜ฐ", "๐Ÿคข", "๐Ÿ˜ฎ"}
89
+ event_set = {"๐ŸŽผ", "๐Ÿ‘", "๐Ÿ˜€", "๐Ÿ˜ญ", "๐Ÿคง", "๐Ÿ˜ท",}
90
+
91
+ def format_str(s):
92
+ for sptk in emoji_dict:
93
+ s = s.replace(sptk, emoji_dict[sptk])
94
+ return s
95
+
96
+
97
+ def format_str_v2(s):
98
+ sptk_dict = {}
99
+ for sptk in emoji_dict:
100
+ sptk_dict[sptk] = s.count(sptk)
101
+ s = s.replace(sptk, "")
102
+ emo = "<|NEUTRAL|>"
103
+ for e in emo_dict:
104
+ if sptk_dict[e] > sptk_dict[emo]:
105
+ emo = e
106
+ for e in event_dict:
107
+ if sptk_dict[e] > 0:
108
+ s = event_dict[e] + s
109
+ s = s + emo_dict[emo]
110
+
111
+ for emoji in emo_set.union(event_set):
112
+ s = s.replace(" " + emoji, emoji)
113
+ s = s.replace(emoji + " ", emoji)
114
+ return s.strip()
115
+
116
+ def format_str_v3(s):
117
+ def get_emo(s):
118
+ return s[-1] if s[-1] in emo_set else None
119
+ def get_event(s):
120
+ return s[0] if s[0] in event_set else None
121
+
122
+ s = s.replace("<|nospeech|><|Event_UNK|>", "โ“")
123
+ for lang in lang_dict:
124
+ s = s.replace(lang, "<|lang|>")
125
+ s_list = [format_str_v2(s_i).strip(" ") for s_i in s.split("<|lang|>")]
126
+ new_s = " " + s_list[0]
127
+ cur_ent_event = get_event(new_s)
128
+ for i in range(1, len(s_list)):
129
+ if len(s_list[i]) == 0:
130
+ continue
131
+ if get_event(s_list[i]) == cur_ent_event and get_event(s_list[i]) != None:
132
+ s_list[i] = s_list[i][1:]
133
+ #else:
134
+ cur_ent_event = get_event(s_list[i])
135
+ if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s):
136
+ new_s = new_s[:-1]
137
+ new_s += s_list[i].strip().lstrip()
138
+ new_s = new_s.replace("The.", " ")
139
+ return new_s.strip()
140
+
141
+ def model_inference(input_wav, language, fs=16000):
142
+ # task_abbr = {"Speech Recognition": "ASR", "Rich Text Transcription": ("ASR", "AED", "SER")}
143
+ language_abbr = {"auto": "auto", "zh": "zh", "en": "en", "yue": "yue", "ja": "ja", "ko": "ko",
144
+ "nospeech": "nospeech"}
145
+
146
+ # task = "Speech Recognition" if task is None else task
147
+ language = "auto" if len(language) < 1 else language
148
+ selected_language = language_abbr[language]
149
+ # selected_task = task_abbr.get(task)
150
+
151
+ # print(f"input_wav: {type(input_wav)}, {input_wav[1].shape}, {input_wav}")
152
+
153
+ if isinstance(input_wav, tuple):
154
+ fs, input_wav = input_wav
155
+ input_wav = input_wav.astype(np.float32) / np.iinfo(np.int16).max
156
+ if len(input_wav.shape) > 1:
157
+ input_wav = input_wav.mean(-1)
158
+ if fs != 16000:
159
+ print(f"audio_fs: {fs}")
160
+ resampler = torchaudio.transforms.Resample(fs, 16000)
161
+ input_wav_t = torch.from_numpy(input_wav).to(torch.float32)
162
+ input_wav = resampler(input_wav_t[None, :])[0, :].numpy()
163
+
164
+
165
+ merge_vad = True #False if selected_task == "ASR" else True
166
+ print(f"language: {language}, merge_vad: {merge_vad}")
167
+ text = model.generate(input=input_wav,
168
+ cache={},
169
+ language=language,
170
+ use_itn=True,
171
+ batch_size_s=500, merge_vad=merge_vad)
172
+
173
+ print(text)
174
+ text = text[0]["text"]
175
+ text = format_str_v3(text)
176
+
177
+ print(text)
178
+
179
+ return text
180
+
181
+
182
+ audio_examples = [
183
+ ["example/zh.mp3", "zh"],
184
+ ["example/yue.mp3", "yue"],
185
+ ["example/en.mp3", "en"],
186
+ ["example/ja.mp3", "ja"],
187
+ ["example/ko.mp3", "ko"],
188
+ ["example/emo_1.wav", "auto"],
189
+ ["example/emo_2.wav", "auto"],
190
+ ["example/emo_3.wav", "auto"],
191
+ ["example/rich_1.wav", "auto"],
192
+ ["example/rich_2.wav", "auto"],
193
+ ["example/longwav_1.wav", "auto"],
194
+ ["example/longwav_2.wav", "auto"],
195
+ ["example/longwav_3.wav", "auto"],
196
+ ]
197
+
198
+
199
+
200
+ html_content = """
201
+ <div>
202
+ <h2 style="font-size: 22px;margin-left: 0px;">Voice Understanding Model: SenseVoice-Small</h2>
203
+ <p style="font-size: 18px;margin-left: 20px;">SenseVoice-Small is an encoder-only speech foundation model designed for rapid voice understanding. It encompasses a variety of features including automatic speech recognition (ASR), spoken language identification (LID), speech emotion recognition (SER), and acoustic event detection (AED). SenseVoice-Small supports multilingual recognition for Chinese, English, Cantonese, Japanese, and Korean. Additionally, it offers exceptionally low inference latency, performing 7 times faster than Whisper-small and 17 times faster than Whisper-large.</p>
204
+ <h2 style="font-size: 22px;margin-left: 0px;">Usage</h2> <p style="font-size: 18px;margin-left: 20px;">Upload an audio file or input through a microphone, then select the task and language. the audio is transcribed into corresponding text along with associated emotions (๐Ÿ˜Š happy, ๐Ÿ˜ก angry/exicting, ๐Ÿ˜” sad) and types of sound events (๐Ÿ˜€ laughter, ๐ŸŽผ music, ๐Ÿ‘ applause, ๐Ÿคง cough&sneeze, ๐Ÿ˜ญ cry). The event labels are placed in the front of the text and the emotion are in the back of the text.</p>
205
+ <p style="font-size: 18px;margin-left: 20px;">Recommended audio input duration is below 30 seconds. For audio longer than 30 seconds, local deployment is recommended.</p>
206
+ <h2 style="font-size: 22px;margin-left: 0px;">Repo</h2>
207
+ <p style="font-size: 18px;margin-left: 20px;"><a href="https://github.com/FunAudioLLM/SenseVoice" target="_blank">SenseVoice</a>: multilingual speech understanding model</p>
208
+ <p style="font-size: 18px;margin-left: 20px;"><a href="https://github.com/modelscope/FunASR" target="_blank">FunASR</a>: fundamental speech recognition toolkit</p>
209
+ <p style="font-size: 18px;margin-left: 20px;"><a href="https://github.com/FunAudioLLM/CosyVoice" target="_blank">CosyVoice</a>: high-quality multilingual TTS model</p>
210
+ </div>
211
+ """
212
+
213
+
214
+ def launch():
215
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
216
+ # gr.Markdown(description)
217
+ gr.HTML(html_content)
218
+ with gr.Row():
219
+ with gr.Column():
220
+ audio_inputs = gr.Audio(label="Upload audio or use the microphone")
221
+
222
+ with gr.Accordion("Configuration"):
223
+ language_inputs = gr.Dropdown(choices=["auto", "zh", "en", "yue", "ja", "ko", "nospeech"],
224
+ value="auto",
225
+ label="Language")
226
+ fn_button = gr.Button("Start", variant="primary")
227
+ text_outputs = gr.Textbox(label="Results")
228
+ gr.Examples(examples=audio_examples, inputs=[audio_inputs, language_inputs], examples_per_page=20)
229
+
230
+ fn_button.click(model_inference, inputs=[audio_inputs, language_inputs], outputs=text_outputs)
231
+
232
+ demo.launch()
233
+
234
+
235
+ if __name__ == "__main__":
236
+ # iface.launch()
237
+ launch()
238
+
239
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch>=1.13
2
+ torchaudio
3
+ modelscope
4
+ huggingface
5
+ huggingface_hub
6
+ funasr>=1.1.2
7
+ numpy<=1.26.4
8
+ gradio