example_optimized.py
6.19 KB
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
# AIfeng/2025-07-17 13:58:00
"""
豆包ASR优化使用示例
演示如何使用结果处理器来优化ASR输出,只获取文本内容
"""
import asyncio
import logging
from pathlib import Path
from typing import Dict, Any
from .service_factory import create_asr_service
from .result_processor import DoubaoResultProcessor, create_text_only_callback, extract_text_only
# 设置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def example_optimized_streaming():
"""优化的流式识别示例 - 只输出文本内容"""
logger.info("=== 优化流式识别示例 ===")
# 创建结果处理器
processor = DoubaoResultProcessor(
text_only=True,
enable_streaming_log=False # 关闭流式日志,减少输出
)
# 用户自定义文本处理函数
def on_text_result(text: str):
"""只处理文本内容的回调函数"""
print(f"识别文本: {text}")
# 这里可以添加你的业务逻辑
# 比如:发送到WebSocket、保存到数据库等
# 创建优化的回调函数
optimized_callback = processor.create_optimized_callback(on_text_result)
# 创建ASR服务
service = create_asr_service(
app_key="your_app_key",
access_key="your_access_key",
streaming=True,
debug=False # 关闭调试模式,减少日志输出
)
try:
# 识别音频文件
audio_path = "path/to/your/audio.wav"
result = await service.recognize_file(
audio_path,
result_callback=optimized_callback
)
# 获取最终状态
final_status = processor.get_current_result()
logger.info(f"识别完成: {final_status}")
finally:
await service.close()
async def example_simple_text_extraction():
"""简单文本提取示例"""
logger.info("=== 简单文本提取示例 ===")
# 使用便捷函数创建只处理文本的回调
def print_text(text: str):
print(f">> {text}")
text_callback = create_text_only_callback(
user_callback=print_text,
enable_streaming_log=False
)
service = create_asr_service(
app_key="your_app_key",
access_key="your_access_key"
)
try:
audio_path = "path/to/your/audio.wav"
await service.recognize_file(
audio_path,
result_callback=text_callback
)
finally:
await service.close()
async def example_manual_text_extraction():
"""手动文本提取示例"""
logger.info("=== 手动文本提取示例 ===")
def manual_callback(result: Dict[str, Any]):
"""手动提取文本的回调函数"""
# 使用便捷函数提取文本
text = extract_text_only(result)
if text:
print(f"提取的文本: {text}")
# 检查是否为最终结果
is_final = result.get('is_last_package', False)
if is_final:
print(f"[最终结果] {text}")
service = create_asr_service(
app_key="your_app_key",
access_key="your_access_key"
)
try:
audio_path = "path/to/your/audio.wav"
await service.recognize_file(
audio_path,
result_callback=manual_callback
)
finally:
await service.close()
async def example_streaming_with_websocket():
"""流式识别结合WebSocket示例"""
logger.info("=== 流式识别WebSocket示例 ===")
# 模拟WebSocket连接
class MockWebSocket:
def __init__(self):
self.messages = []
async def send_text(self, text: str):
self.messages.append(text)
print(f"WebSocket发送: {text}")
websocket = MockWebSocket()
# 创建处理器
processor = DoubaoResultProcessor(
text_only=True,
enable_streaming_log=False
)
async def websocket_handler(text: str):
"""WebSocket文本处理器"""
await websocket.send_text(text)
# 创建回调
callback = processor.create_optimized_callback(
lambda text: asyncio.create_task(websocket_handler(text))
)
service = create_asr_service(
app_key="your_app_key",
access_key="your_access_key"
)
try:
audio_path = "path/to/your/audio.wav"
await service.recognize_file(
audio_path,
result_callback=callback
)
print(f"WebSocket消息历史: {websocket.messages}")
finally:
await service.close()
async def example_comparison():
"""对比示例:原始输出 vs 优化输出"""
logger.info("=== 对比示例 ===")
print("\n1. 原始完整输出:")
def original_callback(result: Dict[str, Any]):
print(f"完整结果: {result}")
print("\n2. 优化文本输出:")
def optimized_callback(text: str):
print(f"文本: {text}")
# 这里只是演示,实际使用时选择其中一种即可
text_callback = create_text_only_callback(optimized_callback)
service = create_asr_service(
app_key="your_app_key",
access_key="your_access_key"
)
try:
audio_path = "path/to/your/audio.wav"
# 使用优化回调
await service.recognize_file(
audio_path,
result_callback=text_callback
)
finally:
await service.close()
def run_example(example_name: str = "optimized"):
"""运行指定示例"""
examples = {
"optimized": example_optimized_streaming,
"simple": example_simple_text_extraction,
"manual": example_manual_text_extraction,
"websocket": example_streaming_with_websocket,
"comparison": example_comparison
}
if example_name not in examples:
print(f"可用示例: {list(examples.keys())}")
return
asyncio.run(examples[example_name]())
if __name__ == "__main__":
# 运行优化示例
run_example("optimized")
# 或者运行其他示例
# run_example("simple")
# run_example("manual")
# run_example("websocket")
# run_example("comparison")