tts-api / test_coqui_api.py
Divax
test
71905d8
raw
history blame
5.28 kB
import requests
import os
import time
# API base URL (update this to your deployed Hugging Face Space URL)
BASE_URL = "http://localhost:7860" # Change to your HF Space URL when deployed
def test_health():
"""Test the health endpoint"""
print("πŸ” Testing health endpoint...")
try:
response = requests.get(f"{BASE_URL}/health")
if response.status_code == 200:
print("βœ… Health check passed!")
print(f"Response: {response.json()}")
else:
print(f"❌ Health check failed: {response.status_code}")
print(f"Response: {response.text}")
except Exception as e:
print(f"❌ Health check error: {e}")
def test_list_models():
"""Test the models endpoint"""
print("\nπŸ” Testing models endpoint...")
try:
response = requests.get(f"{BASE_URL}/models")
if response.status_code == 200:
models = response.json()
print("βœ… Models endpoint working!")
print(f"Found {len(models.get('models', []))} models")
# Show first 5 models
for i, model in enumerate(models.get('models', [])[:5]):
print(f" {i+1}. {model}")
else:
print(f"❌ Models endpoint failed: {response.status_code}")
except Exception as e:
print(f"❌ Models endpoint error: {e}")
def test_simple_tts():
"""Test simple text-to-speech without voice cloning"""
print("\nπŸ” Testing simple TTS...")
try:
data = {
"text": "Hello world! This is a test of Coqui TTS.",
"language": "en"
}
response = requests.post(f"{BASE_URL}/tts", data=data)
if response.status_code == 200:
# Save the audio file
output_file = "simple_tts_output.wav"
with open(output_file, "wb") as f:
f.write(response.content)
print(f"βœ… Simple TTS successful! Audio saved to: {output_file}")
print(f"File size: {len(response.content)} bytes")
else:
print(f"❌ Simple TTS failed: {response.status_code}")
print(f"Response: {response.text}")
except Exception as e:
print(f"❌ Simple TTS error: {e}")
def test_voice_cloning(speaker_file_path=None):
"""Test voice cloning with uploaded speaker file"""
if not speaker_file_path or not os.path.exists(speaker_file_path):
print("\n⚠️ Skipping voice cloning test - no speaker file provided")
print(" To test voice cloning, provide a .wav file path")
return
print(f"\nπŸ” Testing voice cloning with: {speaker_file_path}")
try:
data = {
"text": "This is voice cloning using Coqui TTS. The voice should match the reference audio.",
"language": "en"
}
with open(speaker_file_path, "rb") as f:
files = {"speaker_file": f}
response = requests.post(f"{BASE_URL}/tts", data=data, files=files)
if response.status_code == 200:
# Save the cloned audio
output_file = "voice_cloned_output.wav"
with open(output_file, "wb") as f:
f.write(response.content)
print(f"βœ… Voice cloning successful! Audio saved to: {output_file}")
print(f"File size: {len(response.content)} bytes")
else:
print(f"❌ Voice cloning failed: {response.status_code}")
print(f"Response: {response.text}")
except Exception as e:
print(f"❌ Voice cloning error: {e}")
def test_json_tts():
"""Test JSON endpoint"""
print("\nπŸ” Testing JSON TTS endpoint...")
try:
import json
data = {
"text": "This is a JSON request test for Coqui TTS API.",
"language": "en"
}
response = requests.post(
f"{BASE_URL}/tts-json",
headers={"Content-Type": "application/json"},
data=json.dumps(data)
)
if response.status_code == 200:
output_file = "json_tts_output.wav"
with open(output_file, "wb") as f:
f.write(response.content)
print(f"βœ… JSON TTS successful! Audio saved to: {output_file}")
print(f"File size: {len(response.content)} bytes")
else:
print(f"❌ JSON TTS failed: {response.status_code}")
print(f"Response: {response.text}")
except Exception as e:
print(f"❌ JSON TTS error: {e}")
def main():
print("🐸 Testing Coqui TTS API")
print("=" * 50)
# Test all endpoints
test_health()
test_list_models()
test_simple_tts()
test_json_tts()
# Test voice cloning if speaker file is available
# You can specify a speaker file path here
speaker_file = None # Change to your speaker file path
test_voice_cloning(speaker_file)
print("\nπŸŽ‰ API testing completed!")
print("\nTo test voice cloning:")
print("1. Record a short audio sample (5-10 seconds)")
print("2. Save it as a .wav file")
print("3. Update speaker_file variable with the file path")
print("4. Run the test again")
if __name__ == "__main__":
main()