File size: 9,765 Bytes
9a88d9c
a7aae29
9a88d9c
a7aae29
 
9a88d9c
a7aae29
 
9a88d9c
a7aae29
 
9acb9c3
 
 
 
a7aae29
 
9acb9c3
 
 
a7aae29
 
 
9acb9c3
 
 
 
a7aae29
9acb9c3
 
a7aae29
9acb9c3
 
 
 
 
 
 
 
 
a7aae29
 
9a88d9c
9acb9c3
a7aae29
9a88d9c
9acb9c3
a7aae29
 
 
 
 
 
 
 
 
 
 
 
 
9a88d9c
a7aae29
9a88d9c
 
a7aae29
 
 
 
 
 
 
9a88d9c
 
a7aae29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a88d9c
 
 
a7aae29
 
 
 
9a88d9c
 
 
a7aae29
9a88d9c
 
a7aae29
9a88d9c
 
 
 
 
9acb9c3
9a88d9c
 
9acb9c3
a7aae29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9acb9c3
a7aae29
9acb9c3
a7aae29
 
 
9acb9c3
a7aae29
9acb9c3
 
 
 
9a88d9c
 
 
 
9acb9c3
9a88d9c
a7aae29
 
 
 
 
 
 
9acb9c3
9a88d9c
 
9acb9c3
9a88d9c
9acb9c3
 
9a88d9c
a7aae29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a88d9c
a7aae29
 
9a88d9c
 
9acb9c3
 
 
a7aae29
 
 
 
 
 
 
9acb9c3
 
a7aae29
 
 
9acb9c3
 
a7aae29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import requests
import os

def test_c3po_voice():
    """Test the C3PO voice without uploading any files"""
    
    # API endpoint for C3PO voice only
    url = "http://localhost:7860/tts-c3po"
    
    # Text to convert to speech
    text = "Hello there! I am C-3PO, human-cyborg relations. How may I assist you today?"
    
    # Prepare the request data
    data = {
        "text": text,
        "language": "en",
        "no_lang_auto_detect": False
    }
    
    try:
        print("Testing C3PO voice...")
        print(f"Text: {text}")
        
        response = requests.post(url, data=data)
        
        if response.status_code == 200:
            # Save the generated audio
            output_filename = "c3po_voice_sample.wav"
            with open(output_filename, "wb") as f:
                f.write(response.content)
            print(f"Success! C3PO voice sample 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_xtts_with_custom_voice():
    """Example of using XTTS with custom voice upload"""
    
    # API endpoint
    url = "http://localhost:7860/tts"
    
    # Text to convert to speech
    text = "This is a test of XTTS voice cloning with a custom reference voice."
    
    # Path to your speaker reference audio file
    speaker_file_path = "reference.wav"  # Update this path to your reference audio
    
    # Check if speaker file exists
    if not os.path.exists(speaker_file_path):
        print(f"Custom voice test skipped: Speaker file not found at {speaker_file_path}")
        print("To test custom voice cloning:")
        print("1. Record 3-10 seconds of clear speech")
        print("2. Save as 'reference.wav' in this directory")
        print("3. Run this test again")
        return
    
    # Prepare the request data
    data = {
        "text": text,
        "language": "en",
        "voice_cleanup": False,
        "no_lang_auto_detect": False
    }
    
    files = {
        "speaker_file": open(speaker_file_path, "rb")
    }
    
    try:
        print("Testing XTTS with custom voice...")
        print(f"Text: {text}")
        print(f"Speaker file: {speaker_file_path}")
        
        response = requests.post(url, data=data, files=files)
        
        if response.status_code == 200:
            # Save the generated audio
            output_filename = "custom_voice_clone.wav"
            with open(output_filename, "wb") as f:
                f.write(response.content)
            print(f"Success! Custom voice clone 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}")
    finally:
        files["speaker_file"].close()

def test_xtts_fallback_to_c3po():
    """Test XTTS endpoint without speaker file (should use C3PO voice)"""
    
    # API endpoint
    url = "http://localhost:7860/tts"
    
    # Text to convert to speech
    text = "When no custom voice is provided, I will speak in the C3PO voice by default."
    
    # Prepare the request data (no speaker file)
    data = {
        "text": text,
        "language": "en",
        "voice_cleanup": False,
        "no_lang_auto_detect": False
    }
    
    try:
        print("Testing XTTS fallback to C3PO voice...")
        print(f"Text: {text}")
        
        response = requests.post(url, data=data)
        
        if response.status_code == 200:
            # Save the generated audio
            output_filename = "xtts_c3po_fallback.wav"
            with open(output_filename, "wb") as f:
                f.write(response.content)
            print(f"Success! XTTS with C3PO fallback 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_multilingual_c3po():
    """Test C3PO voice in different languages"""
    
    # API endpoint for C3PO voice only
    url = "http://localhost:7860/tts-c3po"
    
    # Test different languages
    test_cases = [
        ("en", "Hello, I am C-3PO. I am fluent in over six million forms of communication."),
        ("es", "Hola, soy C-3PO. Domino más de seis millones de formas de comunicación."),
        ("fr", "Bonjour, je suis C-3PO. Je maîtrise plus de six millions de formes de communication."),
        ("de", "Hallo, ich bin C-3PO. Ich beherrsche über sechs Millionen Kommunikationsformen."),
    ]
    
    for language, text in test_cases:
        data = {
            "text": text,
            "language": language,
            "no_lang_auto_detect": True  # Force the specified language
        }
        
        try:
            print(f"Testing C3PO voice in {language.upper()}...")
            print(f"Text: {text}")
            
            response = requests.post(url, data=data)
            
            if response.status_code == 200:
                # Save the generated audio
                output_filename = f"c3po_voice_{language}.wav"
                with open(output_filename, "wb") as f:
                    f.write(response.content)
                print(f"Success! C3PO {language} voice 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}")
        
        print()  # Add spacing between tests

def get_supported_languages():
    """Get list of supported languages"""
    try:
        response = requests.get("http://localhost:7860/languages")
        if response.status_code == 200:
            languages = response.json()
            print("Supported languages:", languages["languages"])
            return languages["languages"]
        else:
            print("Failed to get languages:", 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:
            health_info = response.json()
            print("API Health Check:")
            print(f"  Status: {health_info['status']}")
            print(f"  Device: {health_info['device']}")
            print(f"  Model: {health_info['model']}")
            print(f"  Default Voice: {health_info['default_voice']}")
            print(f"  Languages: {len(health_info['supported_languages'])} supported")
            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

def create_sample_reference():
    """Instructions for creating a reference audio file"""
    print("\n" + "="*50)
    print("REFERENCE AUDIO SETUP")
    print("="*50)
    print("To use XTTS voice cloning, you need a reference audio file:")
    print("1. Record 3-10 seconds of clear speech")
    print("2. Save as WAV format (recommended)")
    print("3. Ensure good audio quality (no background noise)")
    print("4. Place the file in the same directory as this script")
    print("5. Update the 'speaker_file_path' variable in the functions above")
    print("\nExample recording text:")
    print("'Hello, this is my voice. I'm recording this sample for voice cloning.'")
    print("="*50)

if __name__ == "__main__":
    print("XTTS C3PO API Client Example")
    print("=" * 40)
    
    # First check if API is running
    if check_api_health():
        print()
        
        # Get supported languages
        languages = get_supported_languages()
        print()
        
        # Test C3PO voice (no file upload needed)
        print("1. Testing C3PO voice (no upload required)...")
        test_c3po_voice()
        print()
        
        # Test XTTS fallback to C3PO
        print("2. Testing XTTS endpoint without speaker file (C3PO fallback)...")
        test_xtts_fallback_to_c3po()
        print()
        
        # Test custom voice if reference file exists
        print("3. Testing custom voice cloning...")
        test_xtts_with_custom_voice()
        print()
        
        # Test multilingual C3PO
        print("4. Testing multilingual C3PO voice...")
        test_multilingual_c3po()
        
        print("All tests completed!")
        print("\nGenerated files:")
        for file in os.listdir("."):
            if file.endswith(".wav") and ("c3po" in file or "custom" in file or "xtts" in file):
                print(f"  - {file}")
                
    else:
        print("\nPlease start the API server first:")
        print("uvicorn app:app --host 0.0.0.0 --port 7860")