Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,8 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
| 4 |
import torchaudio
|
|
|
|
| 5 |
from funasr import AutoModel
|
| 6 |
-
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
| 7 |
|
| 8 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
model = AutoModel(
|
|
@@ -76,7 +76,14 @@ emo_set = {"😊", "😔", "😡", "😰", "🤢", "😮"}
|
|
| 76 |
event_set = {"🎼", "👏", "😀", "😭", "🤧", "😷"}
|
| 77 |
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def format_to_emoji(raw_text):
|
|
|
|
|
|
|
| 80 |
def format_part(s):
|
| 81 |
sptk_dict = {}
|
| 82 |
for sptk in emoji_dict:
|
|
@@ -136,7 +143,7 @@ def generate_srt(sentence_info, clean=True):
|
|
| 136 |
end = ms_to_srt_time(seg.get("end", 0))
|
| 137 |
text = seg.get("text", "")
|
| 138 |
if clean:
|
| 139 |
-
text =
|
| 140 |
text = text.strip()
|
| 141 |
if text:
|
| 142 |
srt_lines.append(f"{i}\n{start} --> {end}\n{text}\n")
|
|
@@ -177,14 +184,14 @@ def model_inference(
|
|
| 177 |
sentence_info = res[0].get("sentence_info", [])
|
| 178 |
|
| 179 |
if output_format == "纯净文本":
|
| 180 |
-
return
|
| 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 srt_result if srt_result else
|
| 188 |
elif output_format == "MASTER_DATA":
|
| 189 |
srt_raw = generate_srt(sentence_info, clean=False)
|
| 190 |
return srt_raw if srt_raw else raw_text
|
|
@@ -196,19 +203,24 @@ custom_css = """
|
|
| 196 |
body, .gradio-container {
|
| 197 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
| 198 |
}
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
| 201 |
cursor: pointer !important;
|
| 202 |
}
|
| 203 |
-
|
| 204 |
-
.dropdown-arrow
|
| 205 |
-
|
| 206 |
-
|
|
|
|
| 207 |
}
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
| 212 |
transform: rotate(180deg) !important;
|
| 213 |
}
|
| 214 |
"""
|
|
@@ -228,12 +240,16 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 228 |
with gr.Column(scale=2):
|
| 229 |
audio_inputs = gr.Audio(label="上传/录制音频")
|
| 230 |
language_inputs = gr.Dropdown(
|
| 231 |
-
choices=lang_options,
|
|
|
|
|
|
|
|
|
|
| 232 |
)
|
| 233 |
output_format_dropdown = gr.Dropdown(
|
| 234 |
choices=["纯净文本", "原始富文本", "Emoji 格式", "SRT 字幕"],
|
| 235 |
value="纯净文本",
|
| 236 |
label="输出格式",
|
|
|
|
| 237 |
)
|
| 238 |
with gr.Accordion("高级设置", open=False):
|
| 239 |
use_itn_checkbox = gr.Checkbox(value=True, label="自动添加标点与格式化")
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
| 4 |
import torchaudio
|
| 5 |
+
import re
|
| 6 |
from funasr import AutoModel
|
|
|
|
| 7 |
|
| 8 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
model = AutoModel(
|
|
|
|
| 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:
|
|
|
|
| 143 |
end = ms_to_srt_time(seg.get("end", 0))
|
| 144 |
text = seg.get("text", "")
|
| 145 |
if clean:
|
| 146 |
+
text = clean_text_postprocess(text)
|
| 147 |
text = text.strip()
|
| 148 |
if text:
|
| 149 |
srt_lines.append(f"{i}\n{start} --> {end}\n{text}\n")
|
|
|
|
| 184 |
sentence_info = res[0].get("sentence_info", [])
|
| 185 |
|
| 186 |
if output_format == "纯净文本":
|
| 187 |
+
return clean_text_postprocess(raw_text)
|
| 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 srt_result if srt_result else clean_text_postprocess(raw_text)
|
| 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
|
|
|
|
| 203 |
body, .gradio-container {
|
| 204 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
| 205 |
}
|
| 206 |
+
.custom-dropdown,
|
| 207 |
+
.custom-dropdown *,
|
| 208 |
+
.custom-dropdown input,
|
| 209 |
+
.custom-dropdown .wrap,
|
| 210 |
+
.custom-dropdown .wrap-inner {
|
| 211 |
cursor: pointer !important;
|
| 212 |
}
|
| 213 |
+
.custom-dropdown svg,
|
| 214 |
+
.custom-dropdown .dropdown-arrow,
|
| 215 |
+
.custom-dropdown .secondary-wrap svg {
|
| 216 |
+
transition: transform 0.25s ease-in-out !important;
|
| 217 |
+
transform-origin: center center !important;
|
| 218 |
}
|
| 219 |
+
.custom-dropdown:focus-within svg,
|
| 220 |
+
.custom-dropdown:has(input:focus) svg,
|
| 221 |
+
.custom-dropdown:has(.options) svg,
|
| 222 |
+
.custom-dropdown:has(ul) svg,
|
| 223 |
+
.custom-dropdown input:focus ~ .secondary-wrap svg {
|
| 224 |
transform: rotate(180deg) !important;
|
| 225 |
}
|
| 226 |
"""
|
|
|
|
| 240 |
with gr.Column(scale=2):
|
| 241 |
audio_inputs = gr.Audio(label="上传/录制音频")
|
| 242 |
language_inputs = gr.Dropdown(
|
| 243 |
+
choices=lang_options,
|
| 244 |
+
value="auto",
|
| 245 |
+
label="源语言",
|
| 246 |
+
elem_classes=["custom-dropdown"],
|
| 247 |
)
|
| 248 |
output_format_dropdown = gr.Dropdown(
|
| 249 |
choices=["纯净文本", "原始富文本", "Emoji 格式", "SRT 字幕"],
|
| 250 |
value="纯净文本",
|
| 251 |
label="输出格式",
|
| 252 |
+
elem_classes=["custom-dropdown"],
|
| 253 |
)
|
| 254 |
with gr.Accordion("高级设置", open=False):
|
| 255 |
use_itn_checkbox = gr.Checkbox(value=True, label="自动添加标点与格式化")
|