|
|
|
|
|
""" |
|
|
Test script for the optimized server to verify model loading and functionality. |
|
|
""" |
|
|
|
|
|
import requests |
|
|
import json |
|
|
import numpy as np |
|
|
import base64 |
|
|
import io |
|
|
import soundfile as sf |
|
|
import tempfile |
|
|
import os |
|
|
|
|
|
def create_test_audio(duration=2.0, sample_rate=16000): |
|
|
"""Create a simple test audio signal.""" |
|
|
t = np.linspace(0, duration, int(sample_rate * duration), False) |
|
|
|
|
|
frequency = 440 |
|
|
audio = 0.3 * np.sin(2 * np.pi * frequency * t) |
|
|
return audio.astype(np.float32) |
|
|
|
|
|
def audio_to_base64(audio, sample_rate): |
|
|
"""Convert audio array to base64 string.""" |
|
|
buf = io.BytesIO() |
|
|
np.save(buf, audio.astype(np.float32)) |
|
|
return base64.b64encode(buf.getvalue()).decode() |
|
|
|
|
|
def test_health_check(): |
|
|
"""Test the health check endpoint.""" |
|
|
try: |
|
|
response = requests.get("http://localhost:8000/api/v1/health") |
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"โ Health check passed: {data}") |
|
|
|
|
|
|
|
|
if "language_model_device" in data: |
|
|
print(f" ๐ฑ Language Model Device: {data['language_model_device']}") |
|
|
print(f" ๐ข Model Dtype: {data['language_model_dtype']}") |
|
|
if data.get("cuda_available"): |
|
|
print(f" ๐ฎ CUDA Device: {data.get('cuda_device_name', 'Unknown')}") |
|
|
print(f" ๐พ Memory Allocated: {data.get('cuda_memory_allocated', 'Unknown')}") |
|
|
print(f" ๐พ Memory Reserved: {data.get('cuda_memory_reserved', 'Unknown')}") |
|
|
else: |
|
|
print(" โ CUDA not available - running on CPU") |
|
|
|
|
|
return data.get("model_loaded", False) |
|
|
else: |
|
|
print(f"โ Health check failed: {response.status_code}") |
|
|
return False |
|
|
except Exception as e: |
|
|
print(f"โ Health check error: {e}") |
|
|
return False |
|
|
|
|
|
def test_v2t_endpoint(): |
|
|
"""Test the voice-to-text endpoint.""" |
|
|
try: |
|
|
|
|
|
audio = create_test_audio() |
|
|
audio_b64 = audio_to_base64(audio, 16000) |
|
|
|
|
|
payload = { |
|
|
"audio_data": audio_b64, |
|
|
"sample_rate": 16000 |
|
|
} |
|
|
|
|
|
response = requests.post( |
|
|
"http://localhost:8000/api/v1/v2t", |
|
|
json=payload, |
|
|
headers={"Content-Type": "application/json"} |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"โ V2T endpoint working: {data.get('text', 'No text')[:100]}...") |
|
|
return True |
|
|
else: |
|
|
print(f"โ V2T endpoint failed: {response.status_code} - {response.text}") |
|
|
return False |
|
|
|
|
|
except Exception as e: |
|
|
print(f"โ V2T endpoint error: {e}") |
|
|
return False |
|
|
|
|
|
def test_error_scenarios(): |
|
|
"""Test error scenarios to ensure proper responses.""" |
|
|
print("\n4. Testing error scenarios...") |
|
|
|
|
|
|
|
|
try: |
|
|
payload = { |
|
|
"audio_data": "invalid_base64_data", |
|
|
"sample_rate": 16000 |
|
|
} |
|
|
|
|
|
response = requests.post( |
|
|
"http://localhost:8000/api/v1/v2t", |
|
|
json=payload, |
|
|
headers={"Content-Type": "application/json"} |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"โ Error handling working: {data.get('text', 'No text')[:100]}...") |
|
|
else: |
|
|
print(f"โ Error handling failed: {response.status_code} - {response.text}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"โ Error scenario test failed: {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
payload = { |
|
|
"audio_data": "", |
|
|
"sample_rate": 16000 |
|
|
} |
|
|
|
|
|
response = requests.post( |
|
|
"http://localhost:8000/api/v1/v2t", |
|
|
json=payload, |
|
|
headers={"Content-Type": "application/json"} |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"โ Empty input handling working: {data.get('text', 'No text')[:100]}...") |
|
|
else: |
|
|
print(f"โ Empty input handling failed: {response.status_code} - {response.text}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"โ Empty input test failed: {e}") |
|
|
|
|
|
return True |
|
|
|
|
|
def test_authentication(): |
|
|
"""Test authentication functionality.""" |
|
|
print("\n5. Testing authentication...") |
|
|
|
|
|
|
|
|
try: |
|
|
audio = create_test_audio() |
|
|
audio_b64 = audio_to_base64(audio, 16000) |
|
|
|
|
|
payload = { |
|
|
"audio_data": audio_b64, |
|
|
"sample_rate": 16000 |
|
|
} |
|
|
|
|
|
response = requests.post( |
|
|
"http://localhost:8000/api/v1/v2t", |
|
|
json=payload, |
|
|
headers={"Content-Type": "application/json"} |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
text = data.get('text', '') |
|
|
if "Authentication failed" in text: |
|
|
print(f"โ Authentication check working: {text}") |
|
|
else: |
|
|
print(f"โ Authentication passed: {text[:100]}...") |
|
|
return True |
|
|
else: |
|
|
print(f"โ Authentication test failed: {response.status_code} - {response.text}") |
|
|
return False |
|
|
|
|
|
except Exception as e: |
|
|
print(f"โ Authentication test error: {e}") |
|
|
return False |
|
|
|
|
|
def test_inference_endpoint(): |
|
|
"""Test the inference endpoint (if INTERFACE is available).""" |
|
|
try: |
|
|
|
|
|
audio = create_test_audio() |
|
|
audio_b64 = audio_to_base64(audio, 16000) |
|
|
|
|
|
payload = { |
|
|
"audio_data": audio_b64, |
|
|
"sample_rate": 16000 |
|
|
} |
|
|
|
|
|
response = requests.post( |
|
|
"http://localhost:8000/api/v1/inference", |
|
|
json=payload, |
|
|
headers={"Content-Type": "application/json"} |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"โ Inference endpoint working: Audio data length {len(data.get('audio_data', ''))}") |
|
|
return True |
|
|
elif response.status_code == 503: |
|
|
print(f"โ Inference endpoint not available (expected if outetts models not loaded): {response.text}") |
|
|
return True |
|
|
else: |
|
|
print(f"โ Inference endpoint failed: {response.status_code} - {response.text}") |
|
|
return False |
|
|
|
|
|
except Exception as e: |
|
|
print(f"โ Inference endpoint error: {e}") |
|
|
return False |
|
|
|
|
|
def main(): |
|
|
"""Run all tests.""" |
|
|
print("Testing optimized server...") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
print("\n1. Testing health check...") |
|
|
models_loaded = test_health_check() |
|
|
|
|
|
if not models_loaded: |
|
|
print("โ Models not loaded. Some tests may fail.") |
|
|
|
|
|
|
|
|
print("\n2. Testing voice-to-text endpoint...") |
|
|
v2t_success = test_v2t_endpoint() |
|
|
|
|
|
|
|
|
print("\n3. Testing inference endpoint...") |
|
|
inference_success = test_inference_endpoint() |
|
|
|
|
|
|
|
|
error_success = test_error_scenarios() |
|
|
|
|
|
|
|
|
auth_success = test_authentication() |
|
|
|
|
|
|
|
|
print("\n" + "=" * 50) |
|
|
print("Test Summary:") |
|
|
print(f"Health Check: {'โ' if models_loaded else 'โ'}") |
|
|
print(f"V2T Endpoint: {'โ' if v2t_success else 'โ'}") |
|
|
print(f"Inference Endpoint: {'โ' if inference_success else 'โ'}") |
|
|
print(f"Error Handling: {'โ' if error_success else 'โ'}") |
|
|
print(f"Authentication: {'โ' if auth_success else 'โ'}") |
|
|
|
|
|
if models_loaded and v2t_success and error_success and auth_success: |
|
|
print("\n๐ Server is working correctly with authentication and error handling!") |
|
|
else: |
|
|
print("\nโ Some issues detected. Check the logs above.") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|