Spaces:
Running
Running
File size: 7,759 Bytes
5cb9c90 27c943a 5cb9c90 b03989f 5cb9c90 27c943a 5cb9c90 5613fc5 5cb9c90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# coding=utf-8
import os
import librosa
import base64
import io
import gradio as gr
import re
import numpy as np
import torch
import torchaudio
import spaces
from funasr import AutoModel
model = "FunAudioLLM/SenseVoiceSmall"
model = AutoModel(model=model,
vad_model="iic/speech_fsmn_vad_zh-cn-16k-common-pytorch",
vad_kwargs={"max_single_segment_time": 30000},
hub="hf",
device="cuda"
)
import re
emo_dict = {
"<|HAPPY|>": "๐",
"<|SAD|>": "๐",
"<|ANGRY|>": "๐ก",
"<|NEUTRAL|>": "",
"<|FEARFUL|>": "๐ฐ",
"<|DISGUSTED|>": "๐คข",
"<|SURPRISED|>": "๐ฎ",
}
event_dict = {
"<|BGM|>": "๐ผ",
"<|Speech|>": "",
"<|Applause|>": "๐",
"<|Laughter|>": "๐",
"<|Cry|>": "๐ญ",
"<|Sneeze|>": "๐คง",
"<|Breath|>": "",
"<|Cough|>": "๐คง",
}
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|>": "",
}
lang_dict = {
"<|zh|>": "<|lang|>",
"<|en|>": "<|lang|>",
"<|yue|>": "<|lang|>",
"<|ja|>": "<|lang|>",
"<|ko|>": "<|lang|>",
"<|nospeech|>": "<|lang|>",
}
emo_set = {"๐", "๐", "๐ก", "๐ฐ", "๐คข", "๐ฎ"}
event_set = {"๐ผ", "๐", "๐", "๐ญ", "๐คง", "๐ท",}
def format_str(s):
for sptk in emoji_dict:
s = s.replace(sptk, emoji_dict[sptk])
return s
def format_str_v2(s):
sptk_dict = {}
for sptk in emoji_dict:
sptk_dict[sptk] = s.count(sptk)
s = s.replace(sptk, "")
emo = "<|NEUTRAL|>"
for e in emo_dict:
if sptk_dict[e] > sptk_dict[emo]:
emo = e
for e in event_dict:
if sptk_dict[e] > 0:
s = event_dict[e] + s
s = s + emo_dict[emo]
for emoji in emo_set.union(event_set):
s = s.replace(" " + emoji, emoji)
s = s.replace(emoji + " ", emoji)
return s.strip()
def format_str_v3(s):
def get_emo(s):
return s[-1] if s[-1] in emo_set else None
def get_event(s):
return s[0] if s[0] in event_set else None
s = s.replace("<|nospeech|><|Event_UNK|>", "โ")
for lang in lang_dict:
s = s.replace(lang, "<|lang|>")
s_list = [format_str_v2(s_i).strip(" ") for s_i in s.split("<|lang|>")]
new_s = " " + s_list[0]
cur_ent_event = get_event(new_s)
for i in range(1, len(s_list)):
if len(s_list[i]) == 0:
continue
if get_event(s_list[i]) == cur_ent_event and get_event(s_list[i]) != None:
s_list[i] = s_list[i][1:]
#else:
cur_ent_event = get_event(s_list[i])
if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s):
new_s = new_s[:-1]
new_s += s_list[i].strip().lstrip()
new_s = new_s.replace("The.", " ")
return new_s.strip()
@spaces.GPU
def model_inference(input_wav, language, fs=16000):
# task_abbr = {"Speech Recognition": "ASR", "Rich Text Transcription": ("ASR", "AED", "SER")}
language_abbr = {"auto": "auto", "zh": "zh", "en": "en", "yue": "yue", "ja": "ja", "ko": "ko",
"nospeech": "nospeech"}
# task = "Speech Recognition" if task is None else task
language = "auto" if len(language) < 1 else language
selected_language = language_abbr[language]
# selected_task = task_abbr.get(task)
# print(f"input_wav: {type(input_wav)}, {input_wav[1].shape}, {input_wav}")
if isinstance(input_wav, tuple):
fs, input_wav = input_wav
input_wav = input_wav.astype(np.float32) / np.iinfo(np.int16).max
if len(input_wav.shape) > 1:
input_wav = input_wav.mean(-1)
if fs != 16000:
print(f"audio_fs: {fs}")
resampler = torchaudio.transforms.Resample(fs, 16000)
input_wav_t = torch.from_numpy(input_wav).to(torch.float32)
input_wav = resampler(input_wav_t[None, :])[0, :].numpy()
merge_vad = True #False if selected_task == "ASR" else True
print(f"language: {language}, merge_vad: {merge_vad}")
text = model.generate(input=input_wav,
cache={},
language=language,
use_itn=True,
batch_size_s=500, merge_vad=merge_vad)
print(text)
text = text[0]["text"]
text = format_str_v3(text)
print(text)
return text
audio_examples = [
["example/zh.mp3", "zh"],
["example/yue.mp3", "yue"],
["example/en.mp3", "en"],
["example/ja.mp3", "ja"],
["example/ko.mp3", "ko"],
["example/emo_1.wav", "auto"],
["example/emo_2.wav", "auto"],
["example/emo_3.wav", "auto"],
["example/rich_1.wav", "auto"],
["example/rich_2.wav", "auto"],
["example/longwav_1.wav", "auto"],
["example/longwav_2.wav", "auto"],
["example/longwav_3.wav", "auto"],
]
html_content = """
<div>
<h2 style="font-size: 22px;margin-left: 0px;">Voice Understanding Model: SenseVoice-Small</h2>
<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>
<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>
<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>
<h2 style="font-size: 22px;margin-left: 0px;">Repo</h2>
<p style="font-size: 18px;margin-left: 20px;"><a href="https://github.com/FunAudioLLM/SenseVoice" target="_blank">SenseVoice</a>: multilingual speech understanding model</p>
<p style="font-size: 18px;margin-left: 20px;"><a href="https://github.com/modelscope/FunASR" target="_blank">FunASR</a>: fundamental speech recognition toolkit</p>
<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>
</div>
"""
def launch():
with gr.Blocks(theme=gr.themes.Soft()) as demo:
# gr.Markdown(description)
gr.HTML(html_content)
with gr.Row():
with gr.Column():
audio_inputs = gr.Audio(label="Upload audio or use the microphone")
with gr.Accordion("Configuration"):
language_inputs = gr.Dropdown(choices=["auto", "zh", "en", "yue", "ja", "ko", "nospeech"],
value="auto",
label="Language")
fn_button = gr.Button("Start", variant="primary")
text_outputs = gr.Textbox(label="Results")
gr.Examples(examples=audio_examples, inputs=[audio_inputs, language_inputs], examples_per_page=20)
fn_button.click(model_inference, inputs=[audio_inputs, language_inputs], outputs=text_outputs)
demo.launch(auth=[("admin", "pass1234"),("a", "1")])
if __name__ == "__main__":
# iface.launch()
launch()
|