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()