contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
190 lines (189 loc) โข 8.12 kB
JavaScript
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);