import sys
import os
from pathlib import Path
try:
from llmkit import (
LLMKitClient,
SynthesisRequest,
SynthesizeOptions,
VoiceSettings,
LatencyMode,
)
except ImportError:
print("Error: llmkit package not found. Please install it first.")
sys.exit(1)
def synthesize_speech(client: LLMKitClient, text: str, output_file: Path) -> None:
print("\n" + "=" * 70)
print("TEXT-TO-SPEECH SYNTHESIS")
print("=" * 70)
print(f"\nInput text: {text}")
request = SynthesisRequest(text)
request = request.with_voice("21m00Tcm4TlvDq8ikWAM")
options = SynthesizeOptions()
options = options.with_latency_mode(LatencyMode.Balanced)
options = options.with_output_format("mp3_44100_64")
voice_settings = VoiceSettings(stability=0.5, similarity_boost=0.75)
options = options.with_voice_settings(voice_settings)
try:
print("\nSynthesizing speech...")
response = client.synthesize_speech(request)
print(f"\nSynthesis complete!")
print(f" Format: {response.format}")
print(f" Size: {response.size} bytes")
if response.duration:
print(f" Duration: {response.duration:.2f}s")
print(f"\nSaving audio to: {output_file}")
with open(output_file, "wb") as f:
f.write(response.audio_bytes)
print(f"✓ Audio file saved successfully!")
print(f" File size: {output_file.stat().st_size / 1024:.1f} KB")
except Exception as e:
print(f"Error during synthesis: {e}")
def main() -> None:
if len(sys.argv) < 2:
print("Usage: python python_audio_synthesis.py 'Your text here' [output_file]")
print("\nExamples:")
print(" python python_audio_synthesis.py 'Hello, world!'")
print(" python python_audio_synthesis.py 'Tell me a joke' output.mp3")
sys.exit(1)
text = sys.argv[1]
output_file = Path(sys.argv[2]) if len(sys.argv) > 2 else Path("synthesized.mp3")
if output_file.exists():
response = input(f"File {output_file} already exists. Overwrite? [y/N]: ")
if response.lower() != "y":
print("Cancelled.")
sys.exit(0)
print("Initializing LLMKit client...")
try:
client = LLMKitClient.from_env()
except Exception as e:
print(f"Error initializing client: {e}")
print("\nMake sure you have set the required environment variable:")
print(" - ELEVENLABS_API_KEY")
sys.exit(1)
synthesize_speech(client, text, output_file)
print("\n" + "=" * 70)
print("Synthesis example complete!")
print("=" * 70)
if __name__ == "__main__":
main()