-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage_examples.js
More file actions
328 lines (285 loc) · 9.25 KB
/
Copy pathusage_examples.js
File metadata and controls
328 lines (285 loc) · 9.25 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Kitten TTS API - JavaScript Usage Examples
*
* Run with: node usage_examples.js
*/
const fs = require('fs');
const path = require('path');
// Configuration
const BASE_URL = process.env.KITTEN_BASE_URL || 'http://localhost:8000';
const API_KEY = process.env.KITTEN_API_KEY || '';
/**
* Get headers with optional API key
*/
function getHeaders() {
const headers = { 'Content-Type': 'application/json' };
if (API_KEY) {
headers['Authorization'] = `Bearer ${API_KEY}`;
}
return headers;
}
/**
* Example 1: Basic Text-to-Speech
*/
async function exampleBasicTTS() {
console.log('Example 1: Basic TTS');
console.log('-'.repeat(40));
try {
const response = await fetch(`${BASE_URL}/v1/audio/speech`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({
model: 'kitten-tts-mini-0.8',
input: 'Hello! This is a basic text-to-speech example.',
voice: 'Jasper',
response_format: 'mp3'
})
});
if (response.ok) {
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync('output_basic.mp3', buffer);
console.log('✓ Audio saved to output_basic.mp3');
} else {
const error = await response.json();
console.log(`✗ Error: ${response.status} - ${error.detail}`);
}
} catch (error) {
console.log(`✗ Error: ${error.message}`);
}
console.log();
}
/**
* Example 2: Different Voices
*/
async function exampleDifferentVoices() {
console.log('Example 2: Different Voices');
console.log('-'.repeat(40));
const voices = ['Bella', 'Jasper', 'Luna', 'Bruno', 'Rosie', 'Hugo', 'Kiki', 'Leo'];
const text = 'This is a voice demonstration.';
for (const voice of voices) {
console.log(`Generating with voice: ${voice}`);
try {
const response = await fetch(`${BASE_URL}/v1/audio/speech`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({
model: 'kitten-tts-mini-0.8',
input: text,
voice: voice,
response_format: 'mp3'
})
});
if (response.ok) {
const buffer = Buffer.from(await response.arrayBuffer());
const filename = `output_voice_${voice.toLowerCase()}.mp3`;
fs.writeFileSync(filename, buffer);
console.log(` ✓ Saved: ${filename}`);
} else {
console.log(` ✗ Error: ${response.status}`);
}
} catch (error) {
console.log(` ✗ Error: ${error.message}`);
}
}
console.log();
}
/**
* Example 3: List Available Voices
*/
async function exampleListVoices() {
console.log('Example 3: List Available Voices');
console.log('-'.repeat(40));
try {
const response = await fetch(`${BASE_URL}/v1/audio/voices`, {
headers: getHeaders()
});
if (response.ok) {
const data = await response.json();
console.log(`Available voices (${data.data.length}):`);
data.data.forEach(voice => {
console.log(` - ${voice.name} (ID: ${voice.id})`);
});
} else {
console.log(`✗ Error: ${response.status}`);
}
} catch (error) {
console.log(`✗ Error: ${error.message}`);
}
console.log();
}
/**
* Example 4: OpenAI-Compatible Usage
*/
async function exampleOpenAICompatibility() {
console.log('Example 4: OpenAI Voice Compatibility');
console.log('-'.repeat(40));
const openai_voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'];
const text = 'This uses OpenAI-compatible voice names.';
for (const openai_voice of openai_voices) {
console.log(`Using OpenAI voice: ${openai_voice}`);
try {
const response = await fetch(`${BASE_URL}/v1/audio/speech`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({
model: 'kitten-tts-mini-0.8',
input: text,
voice: openai_voice,
response_format: 'mp3'
})
});
if (response.ok) {
const buffer = Buffer.from(await response.arrayBuffer());
const filename = `output_openai_${openai_voice}.mp3`;
fs.writeFileSync(filename, buffer);
console.log(` ✓ Saved: ${filename}`);
} else {
console.log(` ✗ Error: ${response.status}`);
}
} catch (error) {
console.log(` ✗ Error: ${error.message}`);
}
}
console.log();
}
/**
* Example 5: Play Audio in Browser (Frontend Example)
*/
function exampleFrontendUsage() {
console.log('Example 5: Frontend/Browser Usage');
console.log('-'.repeat(40));
console.log(`
HTML Example:
-------------
<button onclick="generateSpeech()">Generate Speech</button>
<audio id="audioPlayer" controls></audio>
<script>
async function generateSpeech() {
const response = await fetch('http://localhost:8000/v1/audio/speech', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// 'Authorization': 'Bearer YOUR_API_KEY' // if enabled
},
body: JSON.stringify({
model: 'kitten-tts-mini-0.8',
input: 'Hello from the browser!',
voice: 'Jasper',
response_format: 'mp3'
})
});
const blob = await response.blob();
const url = URL.createObjectURL(blob);
document.getElementById('audioPlayer').src = url;
document.getElementById('audioPlayer').play();
}
<\/script>
`);
console.log();
}
/**
* Example 6: Health Check
*/
async function exampleHealthCheck() {
console.log('Example 6: Health Check');
console.log('-'.repeat(40));
try {
const response = await fetch(`${BASE_URL}/health`);
if (response.ok) {
const data = await response.json();
console.log(`Status: ${data.status}`);
console.log(`Model loaded: ${data.model_loaded}`);
} else {
console.log(`✗ Error: ${response.status}`);
}
} catch (error) {
console.log(`✗ Error: ${error.message}`);
}
console.log();
}
/**
* Example 7: Batch Processing
*/
async function exampleBatchProcessing() {
console.log('Example 7: Batch Processing');
console.log('-'.repeat(40));
const texts = [
'Welcome to our application!',
'Your order has been confirmed.',
'Thank you for using our service.',
'Have a great day!'
];
console.log(`Processing ${texts.length} texts...`);
for (let i = 0; i < texts.length; i++) {
const text = texts[i];
console.log(` [${i + 1}/${texts.length}] Processing: '${text.substring(0, 30)}...'`);
try {
const response = await fetch(`${BASE_URL}/v1/audio/speech`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({
model: 'kitten-tts-mini-0.8',
input: text,
voice: 'Bella',
response_format: 'mp3'
})
});
if (response.ok) {
const buffer = Buffer.from(await response.arrayBuffer());
const filename = `output_batch_${(i + 1).toString().padStart(2, '0')}.mp3`;
fs.writeFileSync(filename, buffer);
console.log(` ✓ Saved: ${filename}`);
} else {
console.log(` ✗ Error: ${response.status}`);
}
} catch (error) {
console.log(` ✗ Error: ${error.message}`);
}
}
console.log();
}
/**
* Main - Run All Examples
*/
async function main() {
console.log('='.repeat(60));
console.log('Kitten TTS API - JavaScript Usage Examples');
console.log('='.repeat(60));
console.log(`Base URL: ${BASE_URL}`);
console.log(`API Key: ${API_KEY ? 'Set' : 'Not set'}`);
console.log('='.repeat(60));
console.log();
const examples = [
exampleHealthCheck,
exampleListVoices,
exampleBasicTTS,
exampleDifferentVoices,
exampleOpenAICompatibility,
exampleFrontendUsage,
exampleBatchProcessing
];
for (const example of examples) {
try {
await example();
} catch (error) {
console.log(`✗ Example failed: ${error.message}`);
console.log();
}
}
console.log('='.repeat(60));
console.log('All examples completed!');
console.log('='.repeat(60));
}
// Run if executed directly
if (require.main === module) {
main().catch(console.error);
}
module.exports = {
getHeaders,
exampleBasicTTS,
exampleDifferentVoices,
exampleListVoices,
exampleOpenAICompatibility,
exampleHealthCheck,
exampleBatchProcessing
};