File size: 4,622 Bytes
9a88d9c
9acb9c3
9a88d9c
9acb9c3
 
9a88d9c
 
9acb9c3
9a88d9c
9acb9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a88d9c
9acb9c3
 
9a88d9c
9acb9c3
 
9a88d9c
9acb9c3
9a88d9c
 
9acb9c3
 
9a88d9c
 
9acb9c3
 
9a88d9c
 
 
9acb9c3
 
9a88d9c
 
 
9acb9c3
9a88d9c
 
 
 
 
 
 
 
9acb9c3
9a88d9c
 
9acb9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a88d9c
 
 
 
9acb9c3
9a88d9c
 
9acb9c3
9a88d9c
 
9acb9c3
9a88d9c
9acb9c3
 
9a88d9c
 
9acb9c3
 
9a88d9c
 
9acb9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import requests
import json

def test_kokoro_tts_api():
    """Example of how to use the Kokoro TTS API"""
    
    # API endpoint
    url = "http://localhost:7860/tts"
    
    # Text to convert to speech (using the example from the user's request)
    text = """
[Kokoro](/kˈOkəɹO/) is an open-weight TTS model with 82 million parameters. Despite its lightweight architecture, it delivers comparable quality to larger models while being significantly faster and more cost-efficient. With Apache-licensed weights, [Kokoro](/kˈOkəɹO/) can be deployed anywhere from production environments to personal projects.
"""
    
    # Prepare the request data
    data = {
        "text": text,
        "voice": "af_heart",  # Available voices: af_heart, af_sky, af_bella, etc.
        "lang_code": "a"      # 'a' for auto-detect
    }
    
    try:
        print("Sending request to Kokoro TTS API...")
        response = requests.post(url, data=data)
        
        if response.status_code == 200:
            # Save the generated audio
            output_filename = "kokoro_generated_speech.wav"
            with open(output_filename, "wb") as f:
                f.write(response.content)
            print(f"Success! Generated speech saved as {output_filename}")
        else:
            print(f"Error: {response.status_code}")
            print(response.text)
            
    except requests.exceptions.ConnectionError:
        print("Error: Could not connect to the API. Make sure the server is running on http://localhost:7860")
    except Exception as e:
        print(f"Error: {e}")

def test_kokoro_tts_json_api():
    """Example of using the JSON endpoint"""
    
    # API endpoint
    url = "http://localhost:7860/tts-json"
    
    # Text to convert to speech
    text = "Hello, this is a test of the Kokoro TTS system using the JSON API endpoint."
    
    # Prepare the JSON request
    data = {
        "text": text,
        "voice": "af_bella",
        "lang_code": "a"
    }
    
    headers = {
        "Content-Type": "application/json"
    }
    
    try:
        print("Sending JSON request to Kokoro TTS API...")
        response = requests.post(url, json=data, headers=headers)
        
        if response.status_code == 200:
            # Save the generated audio
            output_filename = "kokoro_json_speech.wav"
            with open(output_filename, "wb") as f:
                f.write(response.content)
            print(f"Success! Generated speech saved as {output_filename}")
        else:
            print(f"Error: {response.status_code}")
            print(response.text)
            
    except requests.exceptions.ConnectionError:
        print("Error: Could not connect to the API. Make sure the server is running on http://localhost:7860")
    except Exception as e:
        print(f"Error: {e}")

def get_available_voices():
    """Get list of available voices"""
    try:
        response = requests.get("http://localhost:7860/voices")
        if response.status_code == 200:
            voices = response.json()
            print("Available voices:", voices["voices"])
            return voices["voices"]
        else:
            print("Failed to get voices:", response.status_code)
            return []
    except requests.exceptions.ConnectionError:
        print("API is not running. Start it with: uvicorn app:app --host 0.0.0.0 --port 7860")
        return []

def check_api_health():
    """Check if the API is running"""
    try:
        response = requests.get("http://localhost:7860/health")
        if response.status_code == 200:
            print("API is healthy:", response.json())
            return True
        else:
            print("API health check failed:", response.status_code)
            return False
    except requests.exceptions.ConnectionError:
        print("API is not running. Start it with: uvicorn app:app --host 0.0.0.0 --port 7860")
        return False

if __name__ == "__main__":
    print("Kokoro TTS API Client Example")
    print("=" * 35)
    
    # First check if API is running
    if check_api_health():
        print()
        
        # Get available voices
        voices = get_available_voices()
        print()
        
        # Test the TTS functionality with form data
        print("Testing form-data endpoint...")
        test_kokoro_tts_api()
        print()
        
        # Test the TTS functionality with JSON
        print("Testing JSON endpoint...")
        test_kokoro_tts_json_api()
    else:
        print("\nPlease start the API server first:")
        print("uvicorn app:app --host 0.0.0.0 --port 7860")