Spaces:
Running
Running
File size: 5,278 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 |
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() |