Back to Repositories

Testing Edge TTS Audio Processing in gpt_academic

This test suite validates the Edge TTS integration in gpt_academic, focusing on text-to-speech conversion and audio file processing. It ensures proper handling of TTS requests, audio format conversion, and error management.

Test Coverage Overview

The test coverage encompasses Edge TTS communication, audio file generation, and format conversion functionality.

  • Tests Edge TTS voice communication setup and execution
  • Validates MP3 to WAV conversion using pydub
  • Handles temporary file management and cleanup
  • Verifies error handling for network requests and ffmpeg dependencies

Implementation Analysis

The testing approach implements async/await patterns for HTTP communication and file operations.

Key implementation features include:
  • Asynchronous HTTP client usage with httpx
  • Temporary file handling with uuid generation
  • Audio processing with pydub library
  • Exception handling for network and dependency issues

Technical Details

Testing tools and dependencies:

  • edge_tts for TTS communication
  • httpx for async HTTP requests
  • pydub for audio processing
  • tempfile for temporary storage
  • uuid for unique file naming
  • ffmpeg as external dependency

Best Practices Demonstrated

The test demonstrates robust error handling and resource management practices.

  • Proper async context management
  • Comprehensive error handling with specific exceptions
  • Clean temporary file management
  • Dependency validation checks
  • Clear error messages with installation instructions

binary-husky/gpt_academic

tests/test_tts.py

            
import edge_tts
import os
import httpx
from toolbox import get_conf


async def test_tts():    
    async with httpx.AsyncClient() as client:
        try:
            # Forward the request to the target service
            import tempfile
            import edge_tts
            import wave
            import uuid
            from pydub import AudioSegment
            voice = get_conf("EDGE_TTS_VOICE")
            tts = edge_tts.Communicate(text="测试", voice=voice)
            temp_folder = tempfile.gettempdir()
            temp_file_name = str(uuid.uuid4().hex)
            temp_file = os.path.join(temp_folder, f'{temp_file_name}.mp3')
            await tts.save(temp_file)
            try:
                mp3_audio = AudioSegment.from_file(temp_file, format="mp3")
                mp3_audio.export(temp_file, format="wav")
                with open(temp_file, 'rb') as wav_file: t = wav_file.read()
            except:
                raise RuntimeError("ffmpeg未安装,无法处理EdgeTTS音频。安装方法见`https://github.com/jiaaro/pydub#getting-ffmpeg-set-up`")
        except httpx.RequestError as e:
            raise RuntimeError(f"请求失败: {e}")
        
if __name__ == "__main__":
    import asyncio
    asyncio.run(test_tts())