example_optimized.py 6.19 KB
# 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")