Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Voice Bot ASR Comparison Test | |
| Demonstrates the superior accuracy of Groq ASR vs Whisper for voice transcription | |
| Usage: | |
| 1. Set your GROQ_API_KEY environment variable | |
| 2. Run: python test_groq_asr.py | |
| 3. Record some audio to test transcription quality | |
| This shows why your friend's bot works better - Groq ASR is significantly more accurate! | |
| """ | |
| import asyncio | |
| import os | |
| import logging | |
| import tempfile | |
| import wave | |
| import pyaudio | |
| from pathlib import Path | |
| # Configure minimal logging | |
| logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') | |
| logger = logging.getLogger(__name__) | |
| async def test_groq_asr(): | |
| """Test Groq ASR with sample audio""" | |
| # Check for API key | |
| groq_api_key = os.environ.get("GROQ_API_KEY") | |
| if not groq_api_key: | |
| print("β Please set GROQ_API_KEY environment variable") | |
| print(" export GROQ_API_KEY=your_api_key_here") | |
| return | |
| try: | |
| from groq_voice_service import groq_voice_service | |
| print("β Groq Voice Service loaded successfully") | |
| # Check service status | |
| status = groq_voice_service.get_voice_status() | |
| print(f"π€ Voice Service Status:") | |
| print(f" - Voice Enabled: {status['voice_enabled']}") | |
| print(f" - ASR Available: {status['asr_available']}") | |
| print(f" - ASR Provider: {status['asr_provider']}") | |
| print(f" - Groq Available: {status['groq_available']}") | |
| if not status['asr_available']: | |
| print("β Groq ASR not available - check API key") | |
| return | |
| print("\nπ― Ready to test Groq ASR!") | |
| print("π Example test phrases that often fail with Whisper:") | |
| print(" - 'I want to know about pension rules'") | |
| print(" - 'Tell me about provident fund benefits'") | |
| print(" - 'What are the retirement policies?'") | |
| print(" - 'How do I apply for gratuity?'") | |
| # Test with sample audio if available | |
| sample_audio_path = Path("sample_audio.wav") | |
| if sample_audio_path.exists(): | |
| print(f"\nπ΅ Testing with sample audio: {sample_audio_path}") | |
| try: | |
| with open(sample_audio_path, 'rb') as audio_file: | |
| audio_bytes = audio_file.read() | |
| print("π€ Processing with Groq ASR...") | |
| transcription = await groq_voice_service.groq_asr_bytes(audio_bytes) | |
| if transcription: | |
| print(f"β Groq ASR Result: '{transcription}'") | |
| print("π― Notice how clear and accurate the transcription is!") | |
| else: | |
| print("β Transcription failed") | |
| except Exception as e: | |
| print(f"β Error processing audio: {e}") | |
| else: | |
| print(f"\nπ‘ To test with your own audio:") | |
| print(f" 1. Record a WAV file and save as 'sample_audio.wav'") | |
| print(f" 2. Run this script again") | |
| print(f" 3. Compare the results with your current Whisper setup") | |
| print(f"\nπ₯ Key Advantages of Groq ASR:") | |
| print(f" β Much higher accuracy (vs your current 0.24 quality)") | |
| print(f" β Better handling of technical terms (pension, provident, etc.)") | |
| print(f" β Faster processing with cloud infrastructure") | |
| print(f" β More robust against background noise") | |
| print(f" β Consistent performance across different accents") | |
| except ImportError as e: | |
| print(f"β Import error: {e}") | |
| print("π‘ Make sure you have installed: pip install groq") | |
| except Exception as e: | |
| print(f"β Error: {e}") | |
| def record_sample_audio(): | |
| """Record a sample audio for testing (requires pyaudio)""" | |
| try: | |
| import pyaudio | |
| import wave | |
| # Audio parameters | |
| CHUNK = 1024 | |
| FORMAT = pyaudio.paInt16 | |
| CHANNELS = 1 | |
| RATE = 16000 | |
| RECORD_SECONDS = 5 | |
| print("π€ Recording 5 seconds of audio...") | |
| print("π’ Say: 'I want to know about pension rules'") | |
| p = pyaudio.PyAudio() | |
| stream = p.open(format=FORMAT, | |
| channels=CHANNELS, | |
| rate=RATE, | |
| input=True, | |
| frames_per_buffer=CHUNK) | |
| frames = [] | |
| for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): | |
| data = stream.read(CHUNK) | |
| frames.append(data) | |
| stream.stop_stream() | |
| stream.close() | |
| p.terminate() | |
| # Save audio | |
| wf = wave.open("sample_audio.wav", 'wb') | |
| wf.setnchannels(CHANNELS) | |
| wf.setsampwidth(p.get_sample_size(FORMAT)) | |
| wf.setframerate(RATE) | |
| wf.writeframes(b''.join(frames)) | |
| wf.close() | |
| print("β Audio recorded as sample_audio.wav") | |
| return True | |
| except ImportError: | |
| print("β PyAudio not installed. Install with: pip install pyaudio") | |
| return False | |
| except Exception as e: | |
| print(f"β Recording error: {e}") | |
| return False | |
| if __name__ == "__main__": | |
| print("π― Voice Bot ASR Comparison Test") | |
| print("=" * 50) | |
| # Check if we should record audio first | |
| if not Path("sample_audio.wav").exists(): | |
| choice = input("πΌ Record sample audio for testing? (y/n): ").lower().strip() | |
| if choice == 'y': | |
| if record_sample_audio(): | |
| print("\n" + "=" * 50) | |
| # Run the ASR test | |
| asyncio.run(test_groq_asr()) |