File size: 3,727 Bytes
74ba223 |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
#!/usr/bin/env python3
import sys
import requests
# Base URL of your running server. Adjust the port if needed.
BASE_URL = "http://172.21.207.14:5000/"
def check_api_health():
"""Call the /api/health endpoint and display the result."""
try:
response = requests.get(f"{BASE_URL}/api/health")
if response.status_code == 200:
data = response.json()
print("API Health:", data)
else:
print("Error:", response.text)
except Exception as e:
print("Error connecting to API Health endpoint:", e)
def check_voices():
"""Call the /api/voices endpoint and list available voices."""
try:
response = requests.get(f"{BASE_URL}/api/voices")
if response.status_code == 200:
data = response.json()
voices = data.get("voices", [])
default_voice = data.get("default", "None")
print("\nDefault Voice:", default_voice)
print("Available Voices:")
for voice in voices:
print(" -", voice)
else:
print("Error:", response.text)
except Exception as e:
print("Error connecting to API Voices endpoint:", e)
def generate_speech():
"""Prompt for text and parameters, call the /api/tts endpoint, and save the generated audio."""
text = input("Enter text to convert to speech: ").strip()
if not text:
print("Error: Text cannot be empty.")
return
voice = input("Enter voice to use (leave blank for default): ").strip()
if voice == "":
# Get the default voice from the voices endpoint
try:
response = requests.get(f"{BASE_URL}/api/voices")
if response.status_code == 200:
voice = response.json().get("default", "")
print(f"Using default voice: {voice}")
else:
print("Unable to get default voice. Using empty voice name.")
except Exception as e:
print("Error getting default voice:", e)
output_format = input("Enter output format (wav/mp3/aac, default: wav): ").strip().lower() or "wav"
try:
speed = float(input("Enter speech speed (default: 1.0): ").strip() or 1.0)
except ValueError:
print("Invalid speed input. Using default speed 1.0.")
speed = 1.0
payload = {
"text": text,
"voice": voice,
"format": output_format,
"speed": speed
}
print("\nGenerating speech...")
try:
response = requests.post(f"{BASE_URL}/api/tts", json=payload)
if response.status_code == 200:
content_type = response.headers.get("Content-Type", "")
filename = f"output.{output_format}"
with open(filename, "wb") as f:
f.write(response.content)
print(f"Audio generated and saved as '{filename}' (Content-Type: {content_type})")
else:
print("Error generating speech:", response.text)
except Exception as e:
print("Error connecting to API TTS endpoint:", e)
def main():
while True:
print("\n==== Kokoro-TTS CLI Test ====")
print("1. Check API Health")
print("2. Check Voices")
print("3. Generate Speech")
print("4. Quit")
choice = input("Select an option (1-4): ").strip()
if choice == "1":
check_api_health()
elif choice == "2":
check_voices()
elif choice == "3":
generate_speech()
elif choice == "4":
print("Exiting.")
sys.exit(0)
else:
print("Invalid option. Please choose a number between 1 and 4.")
if __name__ == "__main__":
main()
|