UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

190 lines (189 loc) โ€ข 8.12 kB
#!/usr/bin/env node import { SpeechToTextTool } from '../services/tools/SpeechToTextTool.js'; import path from 'path'; import fs from 'fs/promises'; async function testSpeechToTextTool() { console.log('๐ŸŽค Testing Speech-to-Text Tool...\n'); const baseDir = process.cwd(); const tool = new SpeechToTextTool(baseDir); // Test 1: Tool Information console.log('๐Ÿ“‹ Tool Information:'); console.log(`Name: ${tool.getName()}`); console.log(`Description: ${tool.getDescription()}`); console.log('\n๐Ÿ“ Parameters:'); tool.getParameters().forEach(param => { console.log(` - ${param.name} (${param.type}): ${param.description}`); }); console.log('\n๐ŸŽฏ Agent Guidance:'); console.log(tool.getAgentGuidance()); // Test 2: Create a test audio file using FFmpeg (if available) console.log('\n๐ŸŽต Creating test audio file...'); try { const { exec } = await import('child_process'); const { promisify } = await import('util'); const execAsync = promisify(exec); // Create a simple test audio file with speech synthesis const testAudioPath = path.join(baseDir, 'test_speech.wav'); // Try to create a test audio file with a simple tone (fallback if no TTS available) try { await execAsync(`ffmpeg -f lavfi -i "sine=frequency=440:duration=3" -ar 16000 -ac 1 "${testAudioPath}" -y`); console.log('โœ… Test audio file created (tone)'); } catch (error) { console.log('โš ๏ธ Could not create test audio file with FFmpeg'); console.log(' This is expected if FFmpeg is not installed'); return; } // Test 3: Provider Detection console.log('\n๐Ÿ” Testing provider detection...'); // Test with auto provider selection console.log('\n๐Ÿ“ฑ Testing auto provider selection:'); const autoResult = await tool.execute({ input_file: 'test_speech.wav', provider: 'auto', format: 'text' }); if (autoResult.success) { console.log('โœ… Auto provider selection successful'); console.log(` Provider used: ${autoResult.data?.provider}`); console.log(` Transcription: "${autoResult.data?.transcription}"`); } else { console.log('โŒ Auto provider selection failed'); console.log(` Error: ${autoResult.error}`); } // Test 4: Local Whisper (if available) console.log('\n๐Ÿ  Testing local Whisper provider:'); const localResult = await tool.execute({ input_file: 'test_speech.wav', provider: 'whisper_local', model: 'tiny', format: 'text' }); if (localResult.success) { console.log('โœ… Local Whisper successful'); console.log(` Transcription: "${localResult.data?.transcription}"`); console.log(` Model: ${localResult.data?.model}`); } else { console.log('โŒ Local Whisper failed (expected if not installed)'); console.log(` Error: ${localResult.error}`); } // Test 5: OpenAI Whisper (if API key available) console.log('\nโ˜๏ธ Testing OpenAI Whisper provider:'); const openaiResult = await tool.execute({ input_file: 'test_speech.wav', provider: 'openai_whisper', model: 'whisper-1', format: 'json', timestamps: true }); if (openaiResult.success) { console.log('โœ… OpenAI Whisper successful'); console.log(` Transcription: "${openaiResult.data?.transcription}"`); console.log(` Language: ${openaiResult.data?.language}`); console.log(` Confidence: ${openaiResult.data?.confidence}`); } else { console.log('โŒ OpenAI Whisper failed (expected if no API key)'); console.log(` Error: ${openaiResult.error}`); } // Test 6: Gemini (if API key available) console.log('\n๐Ÿค– Testing Gemini provider:'); const geminiResult = await tool.execute({ input_file: 'test_speech.wav', provider: 'gemini', language: 'en', format: 'text' }); if (geminiResult.success) { console.log('โœ… Gemini successful'); console.log(` Transcription: "${geminiResult.data?.transcription}"`); console.log(` Language: ${geminiResult.data?.language}`); } else { console.log('โŒ Gemini failed (expected if no API key)'); console.log(` Error: ${geminiResult.error}`); } // Test 7: Different Output Formats console.log('\n๐Ÿ“„ Testing different output formats...'); const formats = ['text', 'srt', 'vtt', 'json']; for (const format of formats) { console.log(`\n Testing ${format} format:`); const formatResult = await tool.execute({ input_file: 'test_speech.wav', provider: 'auto', format: format, timestamps: format !== 'text' }); if (formatResult.success) { console.log(` โœ… ${format} format successful`); if (format === 'json') { console.log(` Word count: ${formatResult.data?.word_count}`); } } else { console.log(` โŒ ${format} format failed: ${formatResult.error}`); } } // Test 8: File Output console.log('\n๐Ÿ’พ Testing file output...'); const fileOutputResult = await tool.execute({ input_file: 'test_speech.wav', output_file: 'transcription_output.txt', provider: 'auto', format: 'text' }); if (fileOutputResult.success) { console.log('โœ… File output successful'); console.log(` Output file: ${fileOutputResult.data?.output_file}`); // Check if file was created try { const outputContent = await fs.readFile('transcription_output.txt', 'utf8'); console.log(` File content: "${outputContent.trim()}"`); } catch (error) { console.log(' โš ๏ธ Could not read output file'); } } else { console.log('โŒ File output failed'); console.log(` Error: ${fileOutputResult.error}`); } // Test 9: Error Handling console.log('\n๐Ÿšจ Testing error handling...'); // Test with non-existent file const errorResult = await tool.execute({ input_file: 'non_existent_file.wav', provider: 'auto' }); if (!errorResult.success) { console.log('โœ… Error handling works correctly'); console.log(` Expected error: ${errorResult.error}`); } else { console.log('โŒ Error handling failed - should have failed with non-existent file'); } // Cleanup console.log('\n๐Ÿงน Cleaning up test files...'); try { await fs.unlink('test_speech.wav'); await fs.unlink('transcription_output.txt'); console.log('โœ… Cleanup completed'); } catch (error) { console.log('โš ๏ธ Some test files could not be cleaned up'); } } catch (error) { console.error('โŒ Test failed:', error); } console.log('\n๐ŸŽ‰ Speech-to-Text Tool test completed!'); console.log('\n๐Ÿ’ก Usage Tips:'); console.log(' - Install Whisper locally for privacy: pip install openai-whisper'); console.log(' - Set OPENAI_API_KEY for cloud transcription'); console.log(' - Set GEMINI_API_KEY for Google AI transcription'); console.log(' - Use "auto" provider for smart fallback selection'); } // Run the test testSpeechToTextTool().catch(console.error);