Spaces:
Running
Running
File size: 2,322 Bytes
9a88d9c |
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 |
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() |