File size: 2,081 Bytes
a17f08d
c7438ae
a17f08d
c7438ae
0f6a7a4
 
 
 
a17f08d
 
 
 
 
0f6a7a4
a17f08d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36fb9e5
a17f08d
36fb9e5
c7438ae
a17f08d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from pydantic import BaseModel
from talk import say

app = FastAPI()


class TTSRequest(BaseModel):
    text: str
    voice: str = "af_heart"


@app.get("/")
def root():
    return {
        "message": "Kokoro TTS API",
        "endpoints": {
            "/tts": "POST - Generate speech from text",
            "/voices": "GET - List available voices"
        }
    }


@app.post("/tts")
def generate_speech(request: TTSRequest):
    """Generate speech from text using specified voice."""
    if not request.text or not request.text.strip():
        raise HTTPException(status_code=400, detail="Text cannot be empty")

    audio_file = say(request.text, voice=request.voice, play=False)

    if audio_file:
        return FileResponse(
            audio_file,
            media_type="audio/wav",
            filename=f"speech_{request.voice}.wav"
        )

    raise HTTPException(status_code=500, detail="Failed to generate audio")


@app.get("/voices")
def list_voices():
    """List available voices."""
    return {
        "voices": [
            {"id": "af_heart", "name": "Female Heart", "language": "English"},
            {"id": "af_sky", "name": "Female Sky", "language": "English"},
            {"id": "af_bella", "name": "Female Bella", "language": "English"},
            {"id": "af_nicole", "name": "Female Nicole", "language": "English"},
            {"id": "af_sarah", "name": "Female Sarah", "language": "English"},
            {"id": "am_adam", "name": "Male Adam", "language": "English"},
            {"id": "am_michael", "name": "Male Michael", "language": "English"},
            {"id": "bf_emma", "name": "British Female Emma", "language": "English (British)"},
            {"id": "bf_isabella", "name": "British Female Isabella", "language": "English (British)"},
            {"id": "bm_george", "name": "British Male George", "language": "English (British)"},
            {"id": "bm_lewis", "name": "British Male Lewis", "language": "English (British)"}
        ]
    }