Spaces:
Sleeping
Sleeping
| import requests | |
| import os | |
| def test_tts_api(): | |
| """Example of how to use the TTS API""" | |
| # API endpoint | |
| url = "http://localhost:8000/tts" | |
| # Text to convert to speech | |
| text = "It took me quite a long time to develop a voice, and now that I have it I'm not going to be silent." | |
| # Path to your speaker reference audio file | |
| speaker_file_path = "/path/to/target/speaker.wav" # Update this path | |
| # Check if speaker file exists | |
| if not os.path.exists(speaker_file_path): | |
| print(f"Error: Speaker file not found at {speaker_file_path}") | |
| print("Please update the speaker_file_path variable with a valid audio file path") | |
| return | |
| # Prepare the request | |
| data = { | |
| "text": text, | |
| "language": "en" | |
| } | |
| files = { | |
| "speaker_file": open(speaker_file_path, "rb") | |
| } | |
| try: | |
| print("Sending request to TTS API...") | |
| response = requests.post(url, data=data, files=files) | |
| if response.status_code == 200: | |
| # Save the generated audio | |
| output_filename = "generated_speech.wav" | |
| with open(output_filename, "wb") as f: | |
| f.write(response.content) | |
| print(f"Success! Generated speech saved as {output_filename}") | |
| else: | |
| print(f"Error: {response.status_code}") | |
| print(response.text) | |
| except requests.exceptions.ConnectionError: | |
| print("Error: Could not connect to the API. Make sure the server is running on http://localhost:8000") | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| finally: | |
| files["speaker_file"].close() | |
| def check_api_health(): | |
| """Check if the API is running""" | |
| try: | |
| response = requests.get("http://localhost:8000/health") | |
| if response.status_code == 200: | |
| print("API is healthy:", response.json()) | |
| else: | |
| print("API health check failed:", response.status_code) | |
| except requests.exceptions.ConnectionError: | |
| print("API is not running. Start it with: python app.py") | |
| if __name__ == "__main__": | |
| print("TTS API Client Example") | |
| print("=" * 30) | |
| # First check if API is running | |
| check_api_health() | |
| print() | |
| # Test the TTS functionality | |
| test_tts_api() |