Spaces:
Sleeping
Sleeping
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() |