mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add OpenAI TTS provider
This commit is contained in:
@@ -63,6 +63,45 @@ function registerEndpoints(app, jsonParser) {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/openai/generate-voice', jsonParser, async (request, response) => {
|
||||
try {
|
||||
const key = readSecret(SECRET_KEYS.OPENAI);
|
||||
|
||||
if (!key) {
|
||||
console.log('No OpenAI key found');
|
||||
return response.sendStatus(401);
|
||||
}
|
||||
|
||||
const result = await fetch('https://api.openai.com/v1/audio/speech', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${key}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
input: request.body.text,
|
||||
response_format: 'mp3',
|
||||
voice: request.body.voice ?? 'alloy',
|
||||
speed: request.body.speed ?? 1,
|
||||
model: request.body.model ?? 'tts-1',
|
||||
}),
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const text = await result.text();
|
||||
console.log('OpenAI request failed', result.statusText, text);
|
||||
return response.status(500).send(text);
|
||||
}
|
||||
|
||||
const buffer = await result.arrayBuffer();
|
||||
response.setHeader('Content-Type', 'audio/mpeg');
|
||||
return response.send(Buffer.from(buffer));
|
||||
} catch (error) {
|
||||
console.error('OpenAI TTS generation failed', error);
|
||||
response.status(500).send('Internal server error');
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/openai/generate-image', jsonParser, async (request, response) => {
|
||||
try {
|
||||
const key = readSecret(SECRET_KEYS.OPENAI);
|
||||
|
Reference in New Issue
Block a user