Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,11 @@ from funasr import AutoModel
|
|
| 7 |
|
| 8 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
model = AutoModel(
|
| 10 |
-
model="
|
| 11 |
-
vad_model="
|
| 12 |
vad_kwargs={"max_single_segment_time": 30000},
|
|
|
|
|
|
|
| 13 |
trust_remote_code=True,
|
| 14 |
device=device,
|
| 15 |
)
|
|
@@ -76,18 +78,10 @@ emo_set = {"😊", "😔", "😡", "😰", "🤢", "😮"}
|
|
| 76 |
event_set = {"🎼", "👏", "😀", "😭", "🤧", "😷"}
|
| 77 |
|
| 78 |
|
| 79 |
-
def clean_text_postprocess(raw_text):
|
| 80 |
-
"""彻底剥离所有模型内部标签,输出绝对纯净的文字(绝不含任何 Emoji)"""
|
| 81 |
-
return re.sub(r"<\|[^>]*\|?>", "", raw_text).strip()
|
| 82 |
-
|
| 83 |
-
|
| 84 |
def format_to_emoji(raw_text):
|
| 85 |
-
"""将模型标签转换为 Emoji 表情格式"""
|
| 86 |
-
|
| 87 |
def format_part(s):
|
| 88 |
-
sptk_dict = {}
|
| 89 |
for sptk in emoji_dict:
|
| 90 |
-
sptk_dict[sptk] = s.count(sptk)
|
| 91 |
s = s.replace(sptk, "")
|
| 92 |
emo = "<|NEUTRAL|>"
|
| 93 |
for e in emo_dict:
|
|
@@ -143,7 +137,7 @@ def generate_srt(sentence_info, clean=True):
|
|
| 143 |
end = ms_to_srt_time(seg.get("end", 0))
|
| 144 |
text = seg.get("text", "")
|
| 145 |
if clean:
|
| 146 |
-
text =
|
| 147 |
text = text.strip()
|
| 148 |
if text:
|
| 149 |
srt_lines.append(f"{i}\n{start} --> {end}\n{text}\n")
|
|
@@ -165,7 +159,6 @@ def model_inference(
|
|
| 165 |
if fs != 16000:
|
| 166 |
resampler = torchaudio.transforms.Resample(orig_freq=fs, new_freq=16000)
|
| 167 |
input_wav = resampler(torch.from_numpy(input_wav).to(torch.float32)).numpy()
|
| 168 |
-
|
| 169 |
res = model.generate(
|
| 170 |
input=input_wav,
|
| 171 |
cache={},
|
|
@@ -177,21 +170,23 @@ def model_inference(
|
|
| 177 |
ban_emo_unk=ban_emo_unk,
|
| 178 |
sentence_timestamp=True,
|
| 179 |
)
|
| 180 |
-
|
|
|
|
| 181 |
return "未能识别出文本。"
|
| 182 |
|
| 183 |
-
raw_text = res[0].get("text", "")
|
| 184 |
sentence_info = res[0].get("sentence_info", [])
|
| 185 |
|
| 186 |
if output_format == "纯净文本":
|
| 187 |
-
return
|
| 188 |
elif output_format == "原始富文本":
|
| 189 |
return raw_text
|
| 190 |
elif output_format == "Emoji 格式":
|
| 191 |
return format_to_emoji(raw_text)
|
| 192 |
elif output_format == "SRT 字幕":
|
| 193 |
srt_result = generate_srt(sentence_info, clean=True)
|
| 194 |
-
return
|
|
|
|
|
|
|
| 195 |
elif output_format == "MASTER_DATA":
|
| 196 |
srt_raw = generate_srt(sentence_info, clean=False)
|
| 197 |
return srt_raw if srt_raw else raw_text
|
|
|
|
| 7 |
|
| 8 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
model = AutoModel(
|
| 10 |
+
model="FunAudioLLM/SenseVoiceSmall",
|
| 11 |
+
vad_model="fsmn-vad",
|
| 12 |
vad_kwargs={"max_single_segment_time": 30000},
|
| 13 |
+
hub="hf",
|
| 14 |
+
disable_update=True,
|
| 15 |
trust_remote_code=True,
|
| 16 |
device=device,
|
| 17 |
)
|
|
|
|
| 78 |
event_set = {"🎼", "👏", "😀", "😭", "🤧", "😷"}
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def format_to_emoji(raw_text):
|
|
|
|
|
|
|
| 82 |
def format_part(s):
|
| 83 |
+
sptk_dict = {sptk: s.count(sptk) for sptk in emoji_dict}
|
| 84 |
for sptk in emoji_dict:
|
|
|
|
| 85 |
s = s.replace(sptk, "")
|
| 86 |
emo = "<|NEUTRAL|>"
|
| 87 |
for e in emo_dict:
|
|
|
|
| 137 |
end = ms_to_srt_time(seg.get("end", 0))
|
| 138 |
text = seg.get("text", "")
|
| 139 |
if clean:
|
| 140 |
+
text = re.sub(r"<\|[^>]*\|?>", "", text)
|
| 141 |
text = text.strip()
|
| 142 |
if text:
|
| 143 |
srt_lines.append(f"{i}\n{start} --> {end}\n{text}\n")
|
|
|
|
| 159 |
if fs != 16000:
|
| 160 |
resampler = torchaudio.transforms.Resample(orig_freq=fs, new_freq=16000)
|
| 161 |
input_wav = resampler(torch.from_numpy(input_wav).to(torch.float32)).numpy()
|
|
|
|
| 162 |
res = model.generate(
|
| 163 |
input=input_wav,
|
| 164 |
cache={},
|
|
|
|
| 170 |
ban_emo_unk=ban_emo_unk,
|
| 171 |
sentence_timestamp=True,
|
| 172 |
)
|
| 173 |
+
raw_text = res[0].get("text", "")
|
| 174 |
+
if not raw_text:
|
| 175 |
return "未能识别出文本。"
|
| 176 |
|
|
|
|
| 177 |
sentence_info = res[0].get("sentence_info", [])
|
| 178 |
|
| 179 |
if output_format == "纯净文本":
|
| 180 |
+
return re.sub(r"<\|[^>]*\|?>", "", raw_text).strip()
|
| 181 |
elif output_format == "原始富文本":
|
| 182 |
return raw_text
|
| 183 |
elif output_format == "Emoji 格式":
|
| 184 |
return format_to_emoji(raw_text)
|
| 185 |
elif output_format == "SRT 字幕":
|
| 186 |
srt_result = generate_srt(sentence_info, clean=True)
|
| 187 |
+
return (
|
| 188 |
+
srt_result if srt_result else re.sub(r"<\|[^>]*\|?>", "", raw_text).strip()
|
| 189 |
+
)
|
| 190 |
elif output_format == "MASTER_DATA":
|
| 191 |
srt_raw = generate_srt(sentence_info, clean=False)
|
| 192 |
return srt_raw if srt_raw else raw_text
|