Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ dashscope.base_http_api_url = "https://dashscope.aliyuncs.com/api/v1"
|
|
| 11 |
|
| 12 |
def asr_inference(audio_file, context, language, enable_itn):
|
| 13 |
if not audio_file:
|
| 14 |
-
return "
|
| 15 |
|
| 16 |
messages = [
|
| 17 |
{
|
|
@@ -69,49 +69,49 @@ def asr_inference(audio_file, context, language, enable_itn):
|
|
| 69 |
else:
|
| 70 |
result_lang = None
|
| 71 |
else:
|
| 72 |
-
result_text = "
|
| 73 |
result_lang = None
|
| 74 |
else:
|
| 75 |
-
result_text = "
|
| 76 |
result_lang = None
|
| 77 |
else:
|
| 78 |
-
result_text = "
|
| 79 |
result_lang = None
|
| 80 |
else:
|
| 81 |
-
status_code = getattr(response, 'status_code', '
|
| 82 |
-
error_msg = getattr(response, 'message', '
|
| 83 |
-
result_text = f"
|
| 84 |
result_lang = None
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
-
result_text = f"
|
| 88 |
result_lang = None
|
| 89 |
|
| 90 |
-
#
|
| 91 |
lang_display = {
|
| 92 |
-
"auto": "
|
| 93 |
-
"zh": "
|
| 94 |
-
"en": "
|
| 95 |
-
"ja": "
|
| 96 |
-
"ko": "
|
| 97 |
-
"es": "
|
| 98 |
-
"fr": "
|
| 99 |
-
"de": "
|
| 100 |
-
"ar": "
|
| 101 |
-
"it": "
|
| 102 |
-
"ru": "
|
| 103 |
-
"pt": "
|
| 104 |
}
|
| 105 |
if result_lang in lang_display:
|
| 106 |
result_lang = lang_display[result_lang]
|
| 107 |
elif result_lang is not None:
|
| 108 |
-
result_lang = f"
|
| 109 |
|
| 110 |
return result_text, result_lang
|
| 111 |
|
| 112 |
|
| 113 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="
|
| 114 |
-
# ========== LOGO
|
| 115 |
gr.Markdown("""
|
| 116 |
<div style="width: 100%; display: flex; justify-content: center; margin: 30px 0;">
|
| 117 |
<img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/00EE8C99-9C05-4236-A6D0-B58FF172D31B.png"
|
|
@@ -121,48 +121,47 @@ with gr.Blocks(theme=gr.themes.Soft(), title="语音识别工具") as demo:
|
|
| 121 |
</div>
|
| 122 |
""", sanitize_html=False)
|
| 123 |
|
| 124 |
-
# ========== API
|
| 125 |
gr.Markdown("""
|
| 126 |
<div style="text-align: center; margin: 10px 0; font-size: 14px; color: #555;">
|
| 127 |
🌐 <a href="https://help.aliyun.com/zh/dashscope/developer-reference/"
|
| 128 |
target="_blank"
|
| 129 |
style="color: #0066cc; text-decoration: none;">
|
| 130 |
-
|
| 131 |
</a>
|
| 132 |
</div>
|
| 133 |
""", sanitize_html=False)
|
| 134 |
|
| 135 |
-
gr.Markdown("
|
| 136 |
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column():
|
| 139 |
-
audio_input = gr.Audio(label="🎤
|
| 140 |
-
context_input = gr.Textbox(label="📝
|
| 141 |
language = gr.Dropdown(
|
| 142 |
-
label="🌍
|
| 143 |
choices=[
|
| 144 |
-
("
|
| 145 |
-
("
|
| 146 |
-
("
|
| 147 |
-
("
|
| 148 |
-
("
|
| 149 |
-
("
|
| 150 |
-
("
|
| 151 |
-
("
|
| 152 |
-
("
|
| 153 |
-
("
|
| 154 |
-
("
|
| 155 |
-
("
|
| 156 |
],
|
| 157 |
value="auto"
|
| 158 |
)
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
submit_btn = gr.Button("🚀 开始识别", variant="primary")
|
| 162 |
|
| 163 |
with gr.Column():
|
| 164 |
-
text_output = gr.Textbox(label="📝
|
| 165 |
-
lang_output = gr.Textbox(label="📝
|
| 166 |
|
| 167 |
submit_btn.click(
|
| 168 |
fn=asr_inference,
|
|
@@ -171,42 +170,39 @@ with gr.Blocks(theme=gr.themes.Soft(), title="语音识别工具") as demo:
|
|
| 171 |
)
|
| 172 |
|
| 173 |
gr.Markdown("---")
|
| 174 |
-
gr.Markdown("💡
|
| 175 |
-
gr.Markdown("-
|
| 176 |
-
gr.Markdown("-
|
| 177 |
-
gr.Markdown("-
|
| 178 |
-
gr.Markdown("-
|
| 179 |
-
|
| 180 |
-
#
|
| 181 |
-
gr.Markdown("### 💡
|
| 182 |
|
| 183 |
-
# 定义示例数据
|
| 184 |
examples_data = {
|
| 185 |
-
"Example 1 - CSGO
|
| 186 |
"audio": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-ASR/csgo.wav",
|
| 187 |
"context": "A csgo match between NAVI and FazeClan in Major Paris 2023. S1mple and B1t are in NAVI. Ropz, Rain, Karrigan and Twistzz are in Faze.",
|
| 188 |
-
"description": "
|
| 189 |
},
|
| 190 |
-
"Example 2 -
|
| 191 |
"audio": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-ASR/noise3.wav",
|
| 192 |
"context": "",
|
| 193 |
-
"description": "
|
| 194 |
},
|
| 195 |
-
"Example 3 -
|
| 196 |
"audio": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-ASR/noise1.wav",
|
| 197 |
"context": "",
|
| 198 |
-
"description": "
|
| 199 |
}
|
| 200 |
}
|
| 201 |
|
| 202 |
-
# 创建示例按钮
|
| 203 |
with gr.Row():
|
| 204 |
for title, data in examples_data.items():
|
| 205 |
with gr.Column():
|
| 206 |
example_btn = gr.Button(f"📎 {title}", variant="secondary", size="sm")
|
| 207 |
gr.Markdown(f"*{data['description']}*", elem_classes=["example-desc"])
|
| 208 |
|
| 209 |
-
# 为每个按钮创建点击事件
|
| 210 |
example_btn.click(
|
| 211 |
fn=lambda audio=data['audio'], context=data['context']: (audio, context),
|
| 212 |
outputs=[audio_input, context_input]
|
|
|
|
| 11 |
|
| 12 |
def asr_inference(audio_file, context, language, enable_itn):
|
| 13 |
if not audio_file:
|
| 14 |
+
return "Please upload an audio file"
|
| 15 |
|
| 16 |
messages = [
|
| 17 |
{
|
|
|
|
| 69 |
else:
|
| 70 |
result_lang = None
|
| 71 |
else:
|
| 72 |
+
result_text = "No text content found"
|
| 73 |
result_lang = None
|
| 74 |
else:
|
| 75 |
+
result_text = "Incomplete response structure"
|
| 76 |
result_lang = None
|
| 77 |
else:
|
| 78 |
+
result_text = "No recognition result found in response"
|
| 79 |
result_lang = None
|
| 80 |
else:
|
| 81 |
+
status_code = getattr(response, 'status_code', 'Unknown')
|
| 82 |
+
error_msg = getattr(response, 'message', 'Unknown error')
|
| 83 |
+
result_text = f"Request failed (Status: {status_code}): {error_msg}"
|
| 84 |
result_lang = None
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
+
result_text = f"Processing error: {str(e)}"
|
| 88 |
result_lang = None
|
| 89 |
|
| 90 |
+
# Map result_lang to display name
|
| 91 |
lang_display = {
|
| 92 |
+
"auto": "Auto Detect",
|
| 93 |
+
"zh": "Chinese",
|
| 94 |
+
"en": "English",
|
| 95 |
+
"ja": "Japanese",
|
| 96 |
+
"ko": "Korean",
|
| 97 |
+
"es": "Spanish",
|
| 98 |
+
"fr": "French",
|
| 99 |
+
"de": "German",
|
| 100 |
+
"ar": "Arabic",
|
| 101 |
+
"it": "Italian",
|
| 102 |
+
"ru": "Russian",
|
| 103 |
+
"pt": "Portuguese"
|
| 104 |
}
|
| 105 |
if result_lang in lang_display:
|
| 106 |
result_lang = lang_display[result_lang]
|
| 107 |
elif result_lang is not None:
|
| 108 |
+
result_lang = f"Unknown Language ({result_lang})"
|
| 109 |
|
| 110 |
return result_text, result_lang
|
| 111 |
|
| 112 |
|
| 113 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Speech Recognition Tool") as demo:
|
| 114 |
+
# ========== LOGO Area (Centered + Enlarged) ==========
|
| 115 |
gr.Markdown("""
|
| 116 |
<div style="width: 100%; display: flex; justify-content: center; margin: 30px 0;">
|
| 117 |
<img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/00EE8C99-9C05-4236-A6D0-B58FF172D31B.png"
|
|
|
|
| 121 |
</div>
|
| 122 |
""", sanitize_html=False)
|
| 123 |
|
| 124 |
+
# ========== API Documentation Link ==========
|
| 125 |
gr.Markdown("""
|
| 126 |
<div style="text-align: center; margin: 10px 0; font-size: 14px; color: #555;">
|
| 127 |
🌐 <a href="https://help.aliyun.com/zh/dashscope/developer-reference/"
|
| 128 |
target="_blank"
|
| 129 |
style="color: #0066cc; text-decoration: none;">
|
| 130 |
+
View DashScope API Documentation
|
| 131 |
</a>
|
| 132 |
</div>
|
| 133 |
""", sanitize_html=False)
|
| 134 |
|
| 135 |
+
gr.Markdown("Upload an audio file to get speech-to-text results.\nSupports custom context for tailored recognition. Supports language detection and inverse text normalization.")
|
| 136 |
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column():
|
| 139 |
+
audio_input = gr.Audio(label="🎤 Upload Audio", type="filepath")
|
| 140 |
+
context_input = gr.Textbox(label="📝 Context (Optional)", value="", interactive=True)
|
| 141 |
language = gr.Dropdown(
|
| 142 |
+
label="🌍 Language Setting",
|
| 143 |
choices=[
|
| 144 |
+
("Auto Detect", "auto"),
|
| 145 |
+
("Chinese", "zh"),
|
| 146 |
+
("English", "en"),
|
| 147 |
+
("Japanese", "ja"),
|
| 148 |
+
("Korean", "ko"),
|
| 149 |
+
("Spanish", "es"),
|
| 150 |
+
("French", "fr"),
|
| 151 |
+
("German", "de"),
|
| 152 |
+
("Arabic", "ar"),
|
| 153 |
+
("Italian", "it"),
|
| 154 |
+
("Russian", "ru"),
|
| 155 |
+
("Portuguese", "pt")
|
| 156 |
],
|
| 157 |
value="auto"
|
| 158 |
)
|
| 159 |
+
enable_itn = gr.Checkbox(label="🔄 Enable Inverse Text Normalization (ITN)", value=False)
|
| 160 |
+
submit_btn = gr.Button("🚀 Start Recognition", variant="primary")
|
|
|
|
| 161 |
|
| 162 |
with gr.Column():
|
| 163 |
+
text_output = gr.Textbox(label="📝 Recognition Result", interactive=False, lines=6, max_lines=12)
|
| 164 |
+
lang_output = gr.Textbox(label="📝 Detected Language (only in auto mode)", interactive=False, lines=1, max_lines=12)
|
| 165 |
|
| 166 |
submit_btn.click(
|
| 167 |
fn=asr_inference,
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
gr.Markdown("---")
|
| 173 |
+
gr.Markdown("💡 **Tips**:")
|
| 174 |
+
gr.Markdown("- Supports common audio formats: MP3, WAV, etc.")
|
| 175 |
+
gr.Markdown("- Enable LID to auto-detect spoken language")
|
| 176 |
+
gr.Markdown("- Use context to customize output, e.g., correct named entities")
|
| 177 |
+
gr.Markdown("- Enable ITN to convert numbers/dates to standardized text")
|
| 178 |
+
|
| 179 |
+
# Example Section
|
| 180 |
+
gr.Markdown("### 💡 Examples")
|
| 181 |
|
|
|
|
| 182 |
examples_data = {
|
| 183 |
+
"Example 1 - CSGO Match": {
|
| 184 |
"audio": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-ASR/csgo.wav",
|
| 185 |
"context": "A csgo match between NAVI and FazeClan in Major Paris 2023. S1mple and B1t are in NAVI. Ropz, Rain, Karrigan and Twistzz are in Faze.",
|
| 186 |
+
"description": "Game commentary (with professional terms)"
|
| 187 |
},
|
| 188 |
+
"Example 2 - Noisy Environment": {
|
| 189 |
"audio": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-ASR/noise3.wav",
|
| 190 |
"context": "",
|
| 191 |
+
"description": "Speech recognition in noisy environment"
|
| 192 |
},
|
| 193 |
+
"Example 3 - Complex Audio": {
|
| 194 |
"audio": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-ASR/noise1.wav",
|
| 195 |
"context": "",
|
| 196 |
+
"description": "Processing audio with complex background"
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
|
|
|
| 200 |
with gr.Row():
|
| 201 |
for title, data in examples_data.items():
|
| 202 |
with gr.Column():
|
| 203 |
example_btn = gr.Button(f"📎 {title}", variant="secondary", size="sm")
|
| 204 |
gr.Markdown(f"*{data['description']}*", elem_classes=["example-desc"])
|
| 205 |
|
|
|
|
| 206 |
example_btn.click(
|
| 207 |
fn=lambda audio=data['audio'], context=data['context']: (audio, context),
|
| 208 |
outputs=[audio_input, context_input]
|