Spaces:
Running
Running
File size: 7,112 Bytes
71905d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
#!/usr/bin/env python3
"""
Test script for C-3PO TTS model integration
"""
import os
import requests
import json
import tempfile
from pathlib import Path
# Test configuration
API_BASE_URL = "http://localhost:7860"
TEST_TEXTS = [
"I am C-3PO, human-cyborg relations.",
"The odds of successfully navigating an asteroid field are approximately 3,720 to 1.",
"R2-D2, you know better than to trust a strange computer!",
"Oh my! How interesting!"
]
def test_health_check():
"""Test the health check endpoint"""
print("π Testing health check...")
try:
response = requests.get(f"{API_BASE_URL}/health")
if response.status_code == 200:
data = response.json()
print(f"β
Health check passed")
print(f" Model: {data.get('model', 'Unknown')}")
print(f" Device: {data.get('device', 'Unknown')}")
print(f" C-3PO voice available: {data.get('c3po_voice_available', False)}")
print(f" Model path: {data.get('model_path', 'Not specified')}")
return True
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_c3po_endpoint():
"""Test the dedicated C-3PO endpoint"""
print("\nπ Testing C-3PO endpoint...")
test_text = "I am C-3PO, human-cyborg relations."
try:
data = {
'text': test_text,
'language': 'en'
}
response = requests.post(f"{API_BASE_URL}/tts-c3po", data=data)
if response.status_code == 200:
# Save the audio file
output_path = Path(tempfile.gettempdir()) / "c3po_test_output.wav"
with open(output_path, 'wb') as f:
f.write(response.content)
print(f"β
C-3PO endpoint test passed")
print(f" Audio saved to: {output_path}")
print(f" File size: {os.path.getsize(output_path)} bytes")
return True
else:
print(f"β C-3PO endpoint failed: {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print(f"β C-3PO endpoint error: {e}")
return False
def test_general_tts_with_c3po():
"""Test the general TTS endpoint with C-3PO voice enabled"""
print("\nπ€ Testing general TTS with C-3PO voice...")
test_text = "The odds of successfully navigating an asteroid field are approximately 3,720 to 1."
try:
data = {
'text': test_text,
'language': 'en',
'use_c3po_voice': 'true'
}
response = requests.post(f"{API_BASE_URL}/tts", data=data)
if response.status_code == 200:
# Save the audio file
output_path = Path(tempfile.gettempdir()) / "general_c3po_test_output.wav"
with open(output_path, 'wb') as f:
f.write(response.content)
print(f"β
General TTS with C-3PO test passed")
print(f" Audio saved to: {output_path}")
print(f" File size: {os.path.getsize(output_path)} bytes")
return True
else:
print(f"β General TTS with C-3PO failed: {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print(f"β General TTS with C-3PO error: {e}")
return False
def test_json_endpoint():
"""Test the JSON endpoint"""
print("\nπ Testing JSON endpoint...")
test_text = "R2-D2, you know better than to trust a strange computer!"
try:
data = {
'text': test_text,
'language': 'en'
}
headers = {'Content-Type': 'application/json'}
response = requests.post(f"{API_BASE_URL}/tts-json", json=data, headers=headers)
if response.status_code == 200:
# Save the audio file
output_path = Path(tempfile.gettempdir()) / "json_c3po_test_output.wav"
with open(output_path, 'wb') as f:
f.write(response.content)
print(f"β
JSON endpoint test passed")
print(f" Audio saved to: {output_path}")
print(f" File size: {os.path.getsize(output_path)} bytes")
return True
else:
print(f"β JSON endpoint failed: {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print(f"β JSON endpoint error: {e}")
return False
def test_multilingual_support():
"""Test multilingual support with C-3PO voice"""
print("\nπ Testing multilingual support...")
test_cases = [
("Hello, I am C-3PO", "en"),
("Hola, soy C-3PO", "es"),
("Bonjour, je suis C-3PO", "fr"),
("Guten Tag, ich bin C-3PO", "de")
]
success_count = 0
for text, language in test_cases:
try:
data = {
'text': text,
'language': language
}
response = requests.post(f"{API_BASE_URL}/tts-c3po", data=data)
if response.status_code == 200:
output_path = Path(tempfile.gettempdir()) / f"c3po_test_{language}.wav"
with open(output_path, 'wb') as f:
f.write(response.content)
print(f" β
{language}: {text} -> {output_path}")
success_count += 1
else:
print(f" β {language}: Failed ({response.status_code})")
except Exception as e:
print(f" β {language}: Error - {e}")
print(f"\n Multilingual test: {success_count}/{len(test_cases)} languages successful")
return success_count == len(test_cases)
def main():
"""Run all tests"""
print("π Starting C-3PO TTS Model Tests")
print("=" * 50)
tests = [
test_health_check,
test_c3po_endpoint,
test_general_tts_with_c3po,
test_json_endpoint,
test_multilingual_support
]
passed = 0
total = len(tests)
for test in tests:
if test():
passed += 1
print("\n" + "=" * 50)
print(f"π― Test Results: {passed}/{total} tests passed")
if passed == total:
print("π All tests passed! C-3PO model integration is working correctly.")
else:
print("β οΈ Some tests failed. Check the API logs for more details.")
print("\nπ‘ Tips:")
print(" - Make sure the API server is running on http://localhost:7860")
print(" - Check that the C-3PO model downloaded successfully")
print(" - Generated audio files are saved in the system temp directory")
if __name__ == "__main__":
main() |