Python SDK Detaylı İnceleme: İleri TTS Entegrasyonu
Python SDK temel istekleri 3 satırda karşılar. Gerçek üretim kodu daha fazlasını gerektirir.
Kurulum
pip install speekoTemel Kullanım
from speeko import Client
client = Client(api_key="sk-...")
audio = client.tts(text="Merhaba dünya", voice="tr_female_aylin")
with open("output.mp3", "wb") as f:
f.write(audio)Async Client
Yüksek verimli iş yükleri için async kullanın:
from speeko import AsyncClient
import asyncio
async def main():
async with AsyncClient(api_key="sk-...") as client:
tasks = [
client.tts(text=t, voice="tr_female_aylin")
for t in TEXTS
]
results = await asyncio.gather(*tasks)
asyncio.run(main())Toplu İşleme
10K öğeyi API'yi aşırı yüklemeden işleyin:
from asyncio import Semaphore
async def batch_process(texts, concurrency=10):
sem = Semaphore(concurrency)
async def bounded(text):
async with sem:
return await client.tts(text=text, voice="tr_female_aylin")
return await asyncio.gather(*[bounded(t) for t in texts])Streaming
Gerçek zamanlı uygulamalar için:
async def stream_tts(text):
async for chunk in client.tts_stream(text=text, voice="tr_female_aylin"):
play_audio_chunk(chunk)Hata Yönetimi
from speeko.exceptions import (
AuthError,
RateLimitError,
InsufficientCreditsError,
ServerError
)
try:
audio = client.tts(text="Merhaba", voice="tr_female_aylin")
except RateLimitError as e:
await asyncio.sleep(e.retry_after)
except InsufficientCreditsError:
notify_billing_team()
except AuthError:
rotate_api_key()
except ServerError:
passYeniden Deneme Mantığı
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=2, max=60)
)
async def robust_tts(text):
return await client.tts(text=text, voice="tr_female_aylin")Context Manager Deseni
from contextlib import asynccontextmanager
@asynccontextmanager
async def speeko_session():
async with AsyncClient(api_key=os.getenv("SPEEKO_KEY")) as client:
yield clientGözlemlenebilirlik
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger("speeko").setLevel(logging.DEBUG)Loglar request ID'leri içerir — destek taleplerinde bunları paylaşın.
Test
Unit testlerde client'ı mock'layın:
from unittest.mock import AsyncMock
client = AsyncMock()
client.tts.return_value = b"sahte ses byte'lari"