tts-api / client_example.py
Avinyaa
new
9acb9c3
raw
history blame
4.62 kB
import requests
import json
def test_kokoro_tts_api():
"""Example of how to use the Kokoro TTS API"""
# API endpoint
url = "http://localhost:7860/tts"
# Text to convert to speech (using the example from the user's request)
text = """
[Kokoro](/kˈOkəɹO/) is an open-weight TTS model with 82 million parameters. Despite its lightweight architecture, it delivers comparable quality to larger models while being significantly faster and more cost-efficient. With Apache-licensed weights, [Kokoro](/kˈOkəɹO/) can be deployed anywhere from production environments to personal projects.
"""
# Prepare the request data
data = {
"text": text,
"voice": "af_heart", # Available voices: af_heart, af_sky, af_bella, etc.
"lang_code": "a" # 'a' for auto-detect
}
try:
print("Sending request to Kokoro TTS API...")
response = requests.post(url, data=data)
if response.status_code == 200:
# Save the generated audio
output_filename = "kokoro_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:7860")
except Exception as e:
print(f"Error: {e}")
def test_kokoro_tts_json_api():
"""Example of using the JSON endpoint"""
# API endpoint
url = "http://localhost:7860/tts-json"
# Text to convert to speech
text = "Hello, this is a test of the Kokoro TTS system using the JSON API endpoint."
# Prepare the JSON request
data = {
"text": text,
"voice": "af_bella",
"lang_code": "a"
}
headers = {
"Content-Type": "application/json"
}
try:
print("Sending JSON request to Kokoro TTS API...")
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
# Save the generated audio
output_filename = "kokoro_json_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:7860")
except Exception as e:
print(f"Error: {e}")
def get_available_voices():
"""Get list of available voices"""
try:
response = requests.get("http://localhost:7860/voices")
if response.status_code == 200:
voices = response.json()
print("Available voices:", voices["voices"])
return voices["voices"]
else:
print("Failed to get voices:", response.status_code)
return []
except requests.exceptions.ConnectionError:
print("API is not running. Start it with: uvicorn app:app --host 0.0.0.0 --port 7860")
return []
def check_api_health():
"""Check if the API is running"""
try:
response = requests.get("http://localhost:7860/health")
if response.status_code == 200:
print("API is healthy:", response.json())
return True
else:
print("API health check failed:", response.status_code)
return False
except requests.exceptions.ConnectionError:
print("API is not running. Start it with: uvicorn app:app --host 0.0.0.0 --port 7860")
return False
if __name__ == "__main__":
print("Kokoro TTS API Client Example")
print("=" * 35)
# First check if API is running
if check_api_health():
print()
# Get available voices
voices = get_available_voices()
print()
# Test the TTS functionality with form data
print("Testing form-data endpoint...")
test_kokoro_tts_api()
print()
# Test the TTS functionality with JSON
print("Testing JSON endpoint...")
test_kokoro_tts_json_api()
else:
print("\nPlease start the API server first:")
print("uvicorn app:app --host 0.0.0.0 --port 7860")