tts / text.py
IAMCB's picture
update the ui and api
74ba223
#!/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()